Browse Source

Add Buffer::fill method to do memset

Fixes #477.
v0.7.4-release
Konstantin Käfer 14 years ago
committed by Ryan Dahl
parent
commit
5e1b7cadb4
  1. 33
      lib/buffer.js
  2. 22
      src/node_buffer.cc
  3. 1
      src/node_buffer.h
  4. 17
      test/simple/test-buffer.js

33
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;

22
src/node_buffer.cc

@ -376,6 +376,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;
@ -734,6 +755,7 @@ void Buffer::Initialize(Handle<Object> 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(),

1
src/node_buffer.h

@ -117,6 +117,7 @@ class Buffer : public ObjectWrap {
static v8::Handle<v8::Value> Ucs2Write(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);

17
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]);

Loading…
Cancel
Save