Use new method of getting chars written for UTF8
This commit is contained in:
parent
d2e1b0855f
commit
5281f29012
@ -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;
|
||||
|
@ -38,6 +38,7 @@ using namespace v8;
|
||||
|
||||
|
||||
static Persistent<String> length_symbol;
|
||||
static Persistent<String> chars_written_sym;
|
||||
Persistent<FunctionTemplate> Buffer::constructor_template;
|
||||
|
||||
|
||||
@ -308,11 +309,16 @@ Handle<Value> 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<Object> target) {
|
||||
HandleScope scope;
|
||||
|
||||
length_symbol = Persistent<String>::New(String::NewSymbol("length"));
|
||||
chars_written_sym = Persistent<String>::New(String::NewSymbol("_charsWritten"));
|
||||
|
||||
Local<FunctionTemplate> t = FunctionTemplate::New(Buffer::New);
|
||||
constructor_template = Persistent<FunctionTemplate>::New(t);
|
||||
|
Loading…
x
Reference in New Issue
Block a user