mirror of https://github.com/nodejs/node.git
parent
9812e31e8b
commit
5e1b7cadb4
|
@ -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;
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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…
Reference in New Issue