diff --git a/lib/buffer.js b/lib/buffer.js index ab51ddfb720..aa57943bad1 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -280,6 +280,39 @@ Buffer.prototype.toString = function (encoding, start, end) { Buffer.byteLength = SlowBuffer.byteLength; +// fill(value, start=0, end=buffer.length) +Buffer.prototype.fill = function fill (value, start, end) { + value || (value = 0); + start || (start = 0); + end || (end = this.length); + + if (typeof value === "string") { + value = value.charCodeAt(0); + } + if (!(typeof value === "number") || isNaN(value)) { + throw new Error("value is not a number"); + } + + if (end < start) throw new Error("end < start"); + + // Fill 0 bytes; we're done + if (end === start) return 0; + if (this.length == 0) return 0; + + if (start < 0 || start >= this.length) { + throw new Error("start out of bounds"); + } + + if (end < 0 || end > this.length) { + throw new Error("end out of bounds"); + } + + return this.parent.fill(value, + start + this.offset, + end + this.offset); +}; + + // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) Buffer.prototype.copy = function copy (target, target_start, start, end) { var source = this; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index ef74b5a4071..1dd52aa71b0 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -333,6 +333,27 @@ Handle Buffer::Base64Slice(const Arguments &args) { } +// buffer.fill(value, start, end); +Handle Buffer::Fill(const Arguments &args) { + HandleScope scope; + + if (!args[0]->IsInt32()) { + return ThrowException(Exception::Error(String::New( + "value is not a number"))); + } + int value = (char)args[0]->Int32Value(); + + Buffer *parent = ObjectWrap::Unwrap(args.This()); + SLICE_ARGS(args[1], args[2]) + + memset( (void*)(parent->data_ + start), + value, + end - start); + + return Undefined(); +} + + // var bytesCopied = buffer.copy(target, targetStart, sourceStart, sourceEnd); Handle Buffer::Copy(const Arguments &args) { HandleScope scope; @@ -652,6 +673,7 @@ void Buffer::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite); NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite); NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Write", Buffer::Base64Write); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "fill", Buffer::Fill); NODE_SET_PROTOTYPE_METHOD(constructor_template, "copy", Buffer::Copy); NODE_SET_METHOD(constructor_template->GetFunction(), diff --git a/src/node_buffer.h b/src/node_buffer.h index 79fa34d84b4..20ca9c5f700 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -56,6 +56,7 @@ class Buffer : public ObjectWrap { static v8::Handle Utf8Write(const v8::Arguments &args); static v8::Handle ByteLength(const v8::Arguments &args); static v8::Handle MakeFastBuffer(const v8::Arguments &args); + static v8::Handle Fill(const v8::Arguments &args); static v8::Handle Copy(const v8::Arguments &args); Buffer(v8::Handle wrapper, size_t length); diff --git a/test/simple/test-buffer.js b/test/simple/test-buffer.js index d98da04ed90..b05e8d5b39a 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -357,3 +357,19 @@ assert.equal(12, Buffer.byteLength("Il était tué", "binary")); // slice(0,0).length === 0 assert.equal(0, Buffer('hello').slice(0, 0).length) + +b = new Buffer(50); +b.fill("h"); +for (var i = 0; i < b.length; i++) { + assert.equal("h".charCodeAt(0), b[i]); +} + +b.fill(0); +for (var i = 0; i < b.length; i++) { + assert.equal(0, b[i]); +} + +b.fill(1, 16, 32); +for (var i = 0; i < 16; i++) assert.equal(0, b[i]); +for (; i < 32; i++) assert.equal(1, b[i]); +for (; i < b.length; i++) assert.equal(0, b[i]);