Browse Source

buffer: write() should always set _charsWritten.

Refs #1633.
v0.7.4-release
koichik 14 years ago
parent
commit
3e853e627e
  1. 11
      src/node_buffer.cc
  2. 13
      test/simple/test-buffer.js

11
src/node_buffer.cc

@ -555,6 +555,10 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
0, 0,
max_length, max_length,
String::HINT_MANY_WRITES_EXPECTED); String::HINT_MANY_WRITES_EXPECTED);
constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(written));
return scope.Close(Integer::New(written)); return scope.Close(Integer::New(written));
} }
@ -642,6 +646,9 @@ Handle<Value> Buffer::Base64Write(const Arguments &args) {
*dst++ = ((c & 0x03) << 6) | (d & 0x3F); *dst++ = ((c & 0x03) << 6) | (d & 0x3F);
} }
constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(s.length()));
return scope.Close(Integer::New(dst - start)); return scope.Close(Integer::New(dst - start));
} }
@ -672,6 +679,10 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
max_length = MIN(s->Length(), MIN(buffer->length_ - offset, max_length)); max_length = MIN(s->Length(), MIN(buffer->length_ - offset, max_length));
int written = DecodeWrite(p, max_length, s, BINARY); int written = DecodeWrite(p, max_length, s, BINARY);
constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(written));
return scope.Close(Integer::New(written)); return scope.Close(Integer::New(written));
} }

13
test/simple/test-buffer.js

@ -559,3 +559,16 @@ var sub = buf.slice(0, 4); // length: 4
written = sub.write('12345', 'binary'); written = sub.write('12345', 'binary');
assert.equal(written, 4); assert.equal(written, 4);
assert.equal(buf[4], 0); assert.equal(buf[4], 0);
// test for _charsWritten
buf = new Buffer(9);
buf.write('あいうえ', 'utf8'); // 3bytes * 4
assert.equal(Buffer._charsWritten, 3);
buf.write('あいうえお', 'ucs2'); // 2bytes * 5
assert.equal(Buffer._charsWritten, 4);
buf.write('0123456789', 'ascii');
assert.equal(Buffer._charsWritten, 9);
buf.write('0123456789', 'binary');
assert.equal(Buffer._charsWritten, 9);
buf.write('123456', 'base64');
assert.equal(Buffer._charsWritten, 6);

Loading…
Cancel
Save