Browse Source

Fix bug in buffer.utf8Write() which included \u0000

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
53dd9fe200
  1. 2
      lib/net.js
  2. 2
      src/node_buffer.cc
  3. 11
      test/simple/test-buffer.js

2
lib/net.js

@ -453,8 +453,6 @@ Stream.prototype._writeOut = function (data, encoding) {
} else {
// default to utf8
bytesWritten = pool.utf8Write(data, pool.used);
// Don't include the null
if (pool[pool.used + bytesWritten-1] == 0) bytesWritten--;
// XXX Hacky way to find out the number of characters written.
// Waiting for a more optimal way: http://codereview.chromium.org/1539013
var _s = pool.utf8Slice(pool.used, pool.used + bytesWritten);

2
src/node_buffer.cc

@ -310,6 +310,8 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
int written = s->WriteUtf8((char*)p, buffer->length_ - offset);
if (written > 0 && p[written-1] == '\0') written--;
return scope.Close(Integer::New(written));
}

11
test/simple/test-buffer.js

@ -84,3 +84,14 @@ b[6] = 0xBE;
b[7] = 0xEF;
assert.deepEqual([0xDEADBEEF], b.unpack('N', 4));
// Bug regression test
var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
var buffer = new Buffer(32);
var size = buffer.utf8Write(testValue, 0);
puts('bytes written to buffer: ' + size);
var slice = buffer.utf8Slice(0, size);
assert.equal(slice, testValue);

Loading…
Cancel
Save