From 5281f2901249de173704234238a4cb872b4fb884 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 15 Apr 2010 02:01:49 -0700 Subject: [PATCH] Use new method of getting chars written for UTF8 --- lib/net.js | 5 +---- src/node_buffer.cc | 9 ++++++++- 2 files changed, 9 insertions(+), 5 deletions(-) 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);