Browse Source

buffer.toString() shouldn't include null values

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
909a5b39d2
  1. 10
      src/node_buffer.cc
  2. 12
      test/simple/test-buffer.js

10
src/node_buffer.cc

@ -232,7 +232,8 @@ Handle<Value> Buffer::AsciiSlice(const Arguments &args) {
SLICE_ARGS(args[0], args[1])
char* data = parent->data_ + start;
Local<String> string = String::New(data, end - start);
size_t len = strnlen(data, end - start);
Local<String> string = String::New(data, len);
return scope.Close(string);
}
@ -242,11 +243,13 @@ Handle<Value> Buffer::Utf8Slice(const Arguments &args) {
HandleScope scope;
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
SLICE_ARGS(args[0], args[1])
char *data = parent->data_ + start;
Local<String> string = String::New(data, end - start);
char* data = parent->data_ + start;
size_t len = strnlen(data, end - start);
Local<String> string = String::New(data, len);
return scope.Close(string);
}
Handle<Value> Buffer::Ucs2Slice(const Arguments &args) {
HandleScope scope;
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
@ -256,6 +259,7 @@ Handle<Value> Buffer::Ucs2Slice(const Arguments &args) {
return scope.Close(string);
}
static const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

12
test/simple/test-buffer.js

@ -410,3 +410,15 @@ assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
// slice(0,0).length === 0
assert.equal(0, Buffer('hello').slice(0, 0).length);
// toString('utf8') should not include null values
var b = new Buffer(20);
for (var i = 0; i < b.length; i++) {
b[i] = 0;
}
b.write('hello');
assert.equal('hello', b.toString('utf8'));
assert.equal('hello', b.toString('ascii'));

Loading…
Cancel
Save