From 77fc61d5395a4cf0aa2c15e24d2a43d3708dbb58 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 14 Sep 2010 15:39:27 -0700 Subject: [PATCH] Default value for second arg of Buffer#slice --- doc/api.markdown | 2 +- lib/buffer.js | 9 +++------ test/simple/test-buffer.js | 6 ++++++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/api.markdown b/doc/api.markdown index e5109c296b..f2933ed3ac 100644 --- a/doc/api.markdown +++ b/doc/api.markdown @@ -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` diff --git a/lib/buffer.js b/lib/buffer.js index 3fb35f6a2c..ecb25b7e57 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -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); }; diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index 933b48ec46..abc234b835 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -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()); +