mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
Support arrays and strings in buffer constructor
This is be very useful for testing code that deals with buffers.
This commit is contained in:
parent
45948e054d
commit
0a539865dd
@ -68,6 +68,14 @@ strip the high bit if set.
|
||||
|
||||
Allocates a new buffer of `size` octets.
|
||||
|
||||
### new Buffer(array)
|
||||
|
||||
Allocates a new buffer using an `array` of octets.
|
||||
|
||||
### new Buffer(str, encoding = 'utf8')
|
||||
|
||||
Allocates a new buffer containing the given `str`.
|
||||
|
||||
### buffer.write(string, encoding, offset)
|
||||
|
||||
Writes `string` to the buffer at `offset` using the given encoding. Returns
|
||||
|
@ -39,6 +39,7 @@ using namespace v8;
|
||||
|
||||
static Persistent<String> length_symbol;
|
||||
static Persistent<String> chars_written_sym;
|
||||
static Persistent<String> write_sym;
|
||||
Persistent<FunctionTemplate> Buffer::constructor_template;
|
||||
|
||||
|
||||
@ -128,6 +129,18 @@ Handle<Value> Buffer::New(const Arguments &args) {
|
||||
size_t length = args[0]->Uint32Value();
|
||||
buffer = new Buffer(length);
|
||||
|
||||
} else if (args[0]->IsArray()) {
|
||||
Local<Array> a = Local<Array>::Cast(args[0]);
|
||||
buffer = new Buffer(a->Length());
|
||||
char *p = buffer->data();
|
||||
for (int i = 0; i < a->Length(); i++) {
|
||||
p[i] = a->Get(i)->Uint32Value();
|
||||
}
|
||||
} else if (args[0]->IsString()) {
|
||||
Local<String> s = args[0]->ToString();
|
||||
enum encoding e = ParseEncoding(args[1], UTF8);
|
||||
int length = e == UTF8 ? s->Utf8Length() : s->Length();
|
||||
buffer = new Buffer(length);
|
||||
} else if (Buffer::HasInstance(args[0]) && args.Length() > 2) {
|
||||
// var slice = new Buffer(buffer, 123, 130);
|
||||
// args: parent, start, end
|
||||
@ -143,6 +156,27 @@ Handle<Value> Buffer::New(const Arguments &args) {
|
||||
kExternalUnsignedByteArray,
|
||||
buffer->length());
|
||||
args.This()->Set(length_symbol, Integer::New(buffer->length_));
|
||||
|
||||
if (args[0]->IsString()) {
|
||||
if (write_sym.IsEmpty()) {
|
||||
write_sym = Persistent<String>::New(String::NewSymbol("write"));
|
||||
}
|
||||
|
||||
Local<Value> write_v = args.This()->Get(write_sym);
|
||||
assert(write_v->IsFunction());
|
||||
Local<Function> write = Local<Function>::Cast(write_v);
|
||||
|
||||
Local<Value> argv[2] = { args[0], args[1] };
|
||||
|
||||
TryCatch try_catch;
|
||||
|
||||
write->Call(args.This(), 2, argv);
|
||||
|
||||
if (try_catch.HasCaught()) {
|
||||
FatalException(try_catch);
|
||||
}
|
||||
}
|
||||
|
||||
return args.This();
|
||||
}
|
||||
|
||||
|
@ -106,3 +106,16 @@ assert.equal(7, b[3]);
|
||||
var c = b.slice(2 , 4);
|
||||
assert.equal(6, c[0]);
|
||||
assert.equal(7, c[1]);
|
||||
|
||||
|
||||
var d = new Buffer([23, 42, 255]);
|
||||
assert.equal(d.length, 3);
|
||||
assert.equal(d[0], 23);
|
||||
assert.equal(d[1], 42);
|
||||
assert.equal(d[2], 255);
|
||||
|
||||
var e = new Buffer('über');
|
||||
assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
|
||||
|
||||
var f = new Buffer('über', 'ascii');
|
||||
assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
|
Loading…
Reference in New Issue
Block a user