Browse Source

Default value for second arg of Buffer#slice

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
77fc61d539
  1. 2
      doc/api.markdown
  2. 9
      lib/buffer.js
  3. 6
      test/simple/test-buffer.js

2
doc/api.markdown

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

9
lib/buffer.js

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

6
test/simple/test-buffer.js

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

Loading…
Cancel
Save