From 5e1b7cadb48aaf47978b7661a5ce58b0c4d21d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 30 Nov 2010 13:18:02 +0200 Subject: [PATCH] Add Buffer::fill method to do memset Fixes #477. --- lib/buffer.js | 33 +++++++++++++++++++++++++++++++++ src/node_buffer.cc | 22 ++++++++++++++++++++++ src/node_buffer.h | 1 + test/simple/test-buffer.js | 17 +++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/lib/buffer.js b/lib/buffer.js index d0a7535455..9fb5682106 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -373,6 +373,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(target, target_start, start, end) { var source = this; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 63db6107e0..f8d1a30879 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -376,6 +376,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; @@ -734,6 +755,7 @@ void Buffer::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite); NODE_SET_PROTOTYPE_METHOD(constructor_template, "base64Write", Buffer::Base64Write); NODE_SET_PROTOTYPE_METHOD(constructor_template, "ucs2Write", Buffer::Ucs2Write); + 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 91f93dc1d0..e6d47452e9 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -117,6 +117,7 @@ class Buffer : public ObjectWrap { static v8::Handle Ucs2Write(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 fb925323fb..ee57f6dbdf 100644 --- a/test/simple/test-buffer.js +++ b/test/simple/test-buffer.js @@ -540,3 +540,20 @@ console.log(z.length) assert.equal(2, z.length); assert.equal(0x66, z[0]); assert.equal(0x6f, z[1]); +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]);