Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
buffer: write() should always set _charsWritten.
Refs #1633.
  • Loading branch information
koichik committed Sep 3, 2011
1 parent 96ede8c commit 3e853e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/node_buffer.cc
Expand Up @@ -555,6 +555,10 @@ Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
0,
max_length,
String::HINT_MANY_WRITES_EXPECTED);

constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(written));

return scope.Close(Integer::New(written));
}

Expand Down Expand Up @@ -642,6 +646,9 @@ Handle<Value> Buffer::Base64Write(const Arguments &args) {
*dst++ = ((c & 0x03) << 6) | (d & 0x3F);
}

constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(s.length()));

return scope.Close(Integer::New(dst - start));
}

Expand Down Expand Up @@ -672,6 +679,10 @@ Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
max_length = MIN(s->Length(), MIN(buffer->length_ - offset, max_length));

int written = DecodeWrite(p, max_length, s, BINARY);

constructor_template->GetFunction()->Set(chars_written_sym,
Integer::New(written));

return scope.Close(Integer::New(written));
}

Expand Down
13 changes: 13 additions & 0 deletions test/simple/test-buffer.js
Expand Up @@ -559,3 +559,16 @@ var sub = buf.slice(0, 4); // length: 4
written = sub.write('12345', 'binary');
assert.equal(written, 4);
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);

0 comments on commit 3e853e6

Please sign in to comment.