Browse Source

Use new method of getting chars written for UTF8

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
5281f29012
  1. 5
      lib/net.js
  2. 9
      src/node_buffer.cc

5
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;

9
src/node_buffer.cc

@ -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…
Cancel
Save