Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Add Buffer::fill() method to do a memset #477

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ Handle<Value> Buffer::Base64Slice(const Arguments &args) {
}


// buffer.fill(value, start, end);
Handle<Value> 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<Buffer>(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<Value> Buffer::Copy(const Arguments &args) {
HandleScope scope;
Expand Down Expand Up @@ -652,6 +673,7 @@ void Buffer::Initialize(Handle<Object> 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(),
Expand Down
1 change: 1 addition & 0 deletions src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Buffer : public ObjectWrap {
static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);
static v8::Handle<v8::Value> MakeFastBuffer(const v8::Arguments &args);
static v8::Handle<v8::Value> Fill(const v8::Arguments &args);
static v8::Handle<v8::Value> Copy(const v8::Arguments &args);

Buffer(v8::Handle<v8::Object> wrapper, size_t length);
Expand Down
16 changes: 16 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);