Default value for second arg of Buffer#slice

This commit is contained in:
Ryan Dahl 2010-09-14 15:39:27 -07:00
parent 4fe3007a1a
commit 77fc61d539
3 changed files with 10 additions and 7 deletions

View File

@ -175,7 +175,7 @@ into `buf2`, starting at the 8th byte in `buf2`.
// !!!!!!!!qrst!!!!!!!!!!!!!
### buffer.slice(start, end)
### buffer.slice(start, end=buffer.length)
Returns a new buffer which references the
same memory as the old, but offset and cropped by the `start` and `end`

View File

@ -322,12 +322,9 @@ Buffer.prototype.copy = function copy (target, target_start, start, end) {
// slice(start, end)
Buffer.prototype.slice = function (start, end) {
if (end > this.length) {
throw new Error("oob");
}
if (start > end) {
throw new Error("oob");
}
if (!end) end = this.length;
if (end > this.length) throw new Error("oob");
if (start > end) throw new Error("oob");
return new Buffer(this.parent, end - start, +start + this.offset);
};

View File

@ -312,3 +312,9 @@ for (i = 0; i < l; i++) {
sb = b.toString();
assert.equal(sb.length, s.length);
assert.equal(sb, s);
// Single argument slice
b = new Buffer("abcde");
assert.equal("bcde", b.slice(1).toString());