From 3a993d889704314329686695a01b58d60f0d4b85 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 19 Mar 2010 12:02:59 -0700 Subject: [PATCH] Buffer.utf8ByteLength -> Buffer.byteLength --- lib/net.js | 4 ++-- src/node_buffer.cc | 17 ++++++++++++----- src/node_buffer.h | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/net.js b/lib/net.js index 09d88da43d1..fdbe7f51f34 100644 --- a/lib/net.js +++ b/lib/net.js @@ -428,7 +428,7 @@ Stream.prototype._writeString = function (data, encoding) { if (encoding.toLowerCase() == 'utf8') { charsWritten = buffer.utf8Write(data, buffer.used); - bytesWritten = Buffer.utf8ByteLength(data.slice(0, charsWritten)); + bytesWritten = Buffer.byteLength(data.slice(0, charsWritten)); } else { // ascii charsWritten = buffer.asciiWrite(data, buffer.used); @@ -486,7 +486,7 @@ Stream.prototype.write = function (data, encoding) { if (typeof data == 'string') { 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)); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index a2402939f35..b0efba427fb 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -331,15 +331,22 @@ Handle Buffer::Unpack(const Arguments &args) { } -// var nbytes = Buffer.utf8ByteLength("string") -Handle Buffer::Utf8ByteLength(const Arguments &args) { +// var nbytes = Buffer.byteLength("string", "utf8") +Handle Buffer::ByteLength(const Arguments &args) { HandleScope scope; + if (!args[0]->IsString()) { return ThrowException(Exception::TypeError(String::New( "Argument must be a string"))); } + Local s = args[0]->ToString(); - return scope.Close(Integer::New(s->Utf8Length())); + enum encoding e = ParseEncoding(args[1], UTF8); + + Local length = + Integer::New(e == UTF8 ? s->Utf8Length() : s->Length()); + + return scope.Close(length); } @@ -365,8 +372,8 @@ void Buffer::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack); NODE_SET_METHOD(constructor_template->GetFunction(), - "utf8ByteLength", - Buffer::Utf8ByteLength); + "byteLength", + Buffer::ByteLength); target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction()); } diff --git a/src/node_buffer.h b/src/node_buffer.h index 78059be7efe..569943eb26e 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -49,7 +49,7 @@ class Buffer : public ObjectWrap { static v8::Handle Utf8Slice(const v8::Arguments &args); static v8::Handle AsciiWrite(const v8::Arguments &args); static v8::Handle Utf8Write(const v8::Arguments &args); - static v8::Handle Utf8ByteLength(const v8::Arguments &args); + static v8::Handle ByteLength(const v8::Arguments &args); static v8::Handle Unpack(const v8::Arguments &args); int AsciiWrite(char *string, int offset, int length);