diff --git a/lib/net.js b/lib/net.js index 6080150724..e683101f5b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -432,10 +432,7 @@ Stream.prototype._writeOut = function (data, encoding) { if (encoding == 'utf8' || encoding == 'utf-8') { // default to utf8 bytesWritten = pool.write(data, 'utf8', pool.used); - // 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.toString('utf8', pool.used, pool.used + bytesWritten); - charsWritten = _s.length; + charsWritten = Buffer._charsWritten; } else { bytesWritten = pool.write(data, encoding, pool.used); charsWritten = bytesWritten; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 45586c38c3..62c667b40c 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -38,6 +38,7 @@ using namespace v8; static Persistent length_symbol; +static Persistent chars_written_sym; Persistent Buffer::constructor_template; @@ -308,11 +309,16 @@ Handle Buffer::Utf8Write(const Arguments &args) { const char *p = buffer->data() + offset; + int char_written; + int written = s->WriteUtf8((char*)p, buffer->length_ - offset, - NULL, + &char_written, String::HINT_MANY_WRITES_EXPECTED); + constructor_template->GetFunction()->Set(chars_written_sym, + Integer::New(char_written)); + if (written > 0 && p[written-1] == '\0') written--; return scope.Close(Integer::New(written)); @@ -463,6 +469,7 @@ void Buffer::Initialize(Handle target) { HandleScope scope; length_symbol = Persistent::New(String::NewSymbol("length")); + chars_written_sym = Persistent::New(String::NewSymbol("_charsWritten")); Local t = FunctionTemplate::New(Buffer::New); constructor_template = Persistent::New(t);