diff --git a/src/node_buffer.cc b/src/node_buffer.cc index d7f63b2ec2..d304063abc 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -113,15 +113,16 @@ size_t Length(Handle obj) { } -// TODO(trevnorris): would be more efficient to use StringBytes to calculate the -// length and write out the data beforehand then do the same as New(). -Local New(Handle string) { +Local New(Handle string, enum encoding enc) { HandleScope scope(node_isolate); - Handle argv[1] = { string }; - Local obj = p_buffer_fn->NewInstance(1, argv); + size_t length = StringBytes::Size(string, enc); - return scope.Close(obj); + Local buf = New(length); + char* data = Buffer::Data(buf); + StringBytes::Write(data, length, string, enc); + + return scope.Close(buf); } diff --git a/src/node_buffer.h b/src/node_buffer.h index fe2abb9e5e..37468f2d37 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle val); // public constructor NODE_EXTERN v8::Local New(size_t length); // public constructor from string -NODE_EXTERN v8::Local New(v8::Handle string); +NODE_EXTERN v8::Local New(v8::Handle string, + enum encoding enc = UTF8); // public constructor - data is copied // TODO(trevnorris): should be something like Copy() NODE_EXTERN v8::Local New(const char* data, size_t len);