Browse Source

Buffer.utf8ByteLength -> Buffer.byteLength

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
3a993d8897
  1. 4
      lib/net.js
  2. 17
      src/node_buffer.cc
  3. 2
      src/node_buffer.h

4
lib/net.js

@ -428,7 +428,7 @@ Stream.prototype._writeString = function (data, encoding) {
if (encoding.toLowerCase() == 'utf8') { if (encoding.toLowerCase() == 'utf8') {
charsWritten = buffer.utf8Write(data, buffer.used); charsWritten = buffer.utf8Write(data, buffer.used);
bytesWritten = Buffer.utf8ByteLength(data.slice(0, charsWritten)); bytesWritten = Buffer.byteLength(data.slice(0, charsWritten));
} else { } else {
// ascii // ascii
charsWritten = buffer.asciiWrite(data, buffer.used); charsWritten = buffer.asciiWrite(data, buffer.used);
@ -486,7 +486,7 @@ Stream.prototype.write = function (data, encoding) {
if (typeof data == 'string') { if (typeof data == 'string') {
encoding = (encoding || 'utf8').toLowerCase(); encoding = (encoding || 'utf8').toLowerCase();
var bytes = encoding == 'utf8' ? Buffer.utf8ByteLength(data) : data.length; var bytes = Buffer.byteLength(data, encoding);
//debug('write string :' + JSON.stringify(data)); //debug('write string :' + JSON.stringify(data));

17
src/node_buffer.cc

@ -331,15 +331,22 @@ Handle<Value> Buffer::Unpack(const Arguments &args) {
} }
// var nbytes = Buffer.utf8ByteLength("string") // var nbytes = Buffer.byteLength("string", "utf8")
Handle<Value> Buffer::Utf8ByteLength(const Arguments &args) { Handle<Value> Buffer::ByteLength(const Arguments &args) {
HandleScope scope; HandleScope scope;
if (!args[0]->IsString()) { if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(String::New( return ThrowException(Exception::TypeError(String::New(
"Argument must be a string"))); "Argument must be a string")));
} }
Local<String> s = args[0]->ToString(); Local<String> s = args[0]->ToString();
return scope.Close(Integer::New(s->Utf8Length())); enum encoding e = ParseEncoding(args[1], UTF8);
Local<Integer> length =
Integer::New(e == UTF8 ? s->Utf8Length() : s->Length());
return scope.Close(length);
} }
@ -365,8 +372,8 @@ void Buffer::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack); NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack);
NODE_SET_METHOD(constructor_template->GetFunction(), NODE_SET_METHOD(constructor_template->GetFunction(),
"utf8ByteLength", "byteLength",
Buffer::Utf8ByteLength); Buffer::ByteLength);
target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction()); target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction());
} }

2
src/node_buffer.h

@ -49,7 +49,7 @@ class Buffer : public ObjectWrap {
static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args); static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args); static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args); static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
static v8::Handle<v8::Value> Utf8ByteLength(const v8::Arguments &args); static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);
static v8::Handle<v8::Value> Unpack(const v8::Arguments &args); static v8::Handle<v8::Value> Unpack(const v8::Arguments &args);
int AsciiWrite(char *string, int offset, int length); int AsciiWrite(char *string, int offset, int length);

Loading…
Cancel
Save