Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 3 commits into from

4 participants

Konstantin Käfer elarkin ry xk
Konstantin Käfer

Idea from xk@ec46774

Konstantin Käfer

still missing tests

xk

Beware : charCode might be bigger than a byte : 16 bits versus 8 bits !

Konstantin Käfer

Yeah, throw an exception in that case? Currently it just ignores the upper bits. E.g. a charcode of 256 would be 0 and 257 = 1.

Konstantin Käfer add test ba4dec3
Konstantin Käfer

I mostly added the start/end for API consistency, but if there's a consensus, I can also remove it.

elarkin

This conflicts with an existing buffer set method: See https://github.com/ry/node/blob/master/lib/buffer.js#L191

Konstantin Käfer

Thanks elarkin, I changed it to "fill" now as xk suggested in #472

ry ry closed this pull request from a commit
Konstantin Käfer Add Buffer::fill method to do memset
Fixes #477.
5e1b7ca
ry ry closed this in 5e1b7ca
ry
ry commented

thanks. sorry it took so long

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 30, 2010
  1. add Buffer::set method to do memset

    Konstantin Käfer authored
  2. add test

    Konstantin Käfer authored
Commits on Dec 4, 2010
  1. rename set to fill since set already was a function

    Konstantin Käfer authored
This page is out of date. Refresh to see the latest.
33 lib/buffer.js
View
@@ -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;
22 src/node_buffer.cc
View
@@ -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;
@@ -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(),
1  src/node_buffer.h
View
@@ -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);
16 test/simple/test-buffer.js
View
@@ -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]);
Something went wrong with that request. Please try again.