Permalink
Browse files

Buffer.utf8ByteLength -> Buffer.byteLength

  • Loading branch information...
1 parent 3e969f0 commit 3a993d889704314329686695a01b58d60f0d4b85 @ry ry committed Mar 19, 2010
Showing with 15 additions and 8 deletions.
  1. +2 −2 lib/net.js
  2. +12 −5 src/node_buffer.cc
  3. +1 −1 src/node_buffer.h
View
@@ -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));
View
@@ -331,15 +331,22 @@ Handle<Value> Buffer::Unpack(const Arguments &args) {
}
-// var nbytes = Buffer.utf8ByteLength("string")
-Handle<Value> Buffer::Utf8ByteLength(const Arguments &args) {
+// var nbytes = Buffer.byteLength("string", "utf8")
+Handle<Value> Buffer::ByteLength(const Arguments &args) {
HandleScope scope;
+
if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(String::New(
"Argument must be a string")));
}
+
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_METHOD(constructor_template->GetFunction(),
- "utf8ByteLength",
- Buffer::Utf8ByteLength);
+ "byteLength",
+ Buffer::ByteLength);
target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction());
}
View
@@ -49,7 +49,7 @@ class Buffer : public ObjectWrap {
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> 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);
int AsciiWrite(char *string, int offset, int length);

0 comments on commit 3a993d8

Please sign in to comment.