File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -432,10 +432,7 @@ Stream.prototype._writeOut = function (data, encoding) {
432
432
if ( encoding == 'utf8' || encoding == 'utf-8' ) {
433
433
// default to utf8
434
434
bytesWritten = pool . write ( data , 'utf8' , pool . used ) ;
435
- // XXX Hacky way to find out the number of characters written.
436
- // Waiting for a more optimal way: http://codereview.chromium.org/1539013
437
- var _s = pool . toString ( 'utf8' , pool . used , pool . used + bytesWritten ) ;
438
- charsWritten = _s . length ;
435
+ charsWritten = Buffer . _charsWritten ;
439
436
} else {
440
437
bytesWritten = pool . write ( data , encoding , pool . used ) ;
441
438
charsWritten = bytesWritten ;
Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ using namespace v8;
38
38
39
39
40
40
static Persistent<String> length_symbol;
41
+ static Persistent<String> chars_written_sym;
41
42
Persistent<FunctionTemplate> Buffer::constructor_template;
42
43
43
44
@@ -308,11 +309,16 @@ Handle<Value> Buffer::Utf8Write(const Arguments &args) {
308
309
309
310
const char *p = buffer->data () + offset;
310
311
312
+ int char_written;
313
+
311
314
int written = s->WriteUtf8 ((char *)p,
312
315
buffer->length_ - offset,
313
- NULL ,
316
+ &char_written ,
314
317
String::HINT_MANY_WRITES_EXPECTED);
315
318
319
+ constructor_template->GetFunction ()->Set (chars_written_sym,
320
+ Integer::New (char_written));
321
+
316
322
if (written > 0 && p[written-1 ] == ' \0 ' ) written--;
317
323
318
324
return scope.Close (Integer::New (written));
@@ -463,6 +469,7 @@ void Buffer::Initialize(Handle<Object> target) {
463
469
HandleScope scope;
464
470
465
471
length_symbol = Persistent<String>::New (String::NewSymbol (" length" ));
472
+ chars_written_sym = Persistent<String>::New (String::NewSymbol (" _charsWritten" ));
466
473
467
474
Local<FunctionTemplate> t = FunctionTemplate::New (Buffer::New);
468
475
constructor_template = Persistent<FunctionTemplate>::New (t);
You can’t perform that action at this time.
0 commit comments