GeezerVison is showing github.com — buffer: write strings directly from call · tcper/node@f5e13ae
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_buffer.cc +7-6Lines changed: 7 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -113,15 +113,16 @@ size_t Length(Handle<Object> obj) { |
113 | 113 | } |
114 | 114 |
|
115 | 115 |
|
116 | | -// TODO(trevnorris): would be more efficient to use StringBytes to calculate the |
117 | | -// length and write out the data beforehand then do the same as New(). |
118 | | -Local<Object> New(Handle<String> string) { |
| 116 | +Local<Object> New(Handle<String> string, enum encoding enc) { |
119 | 117 | HandleScope scope(node_isolate); |
120 | 118 |
|
121 | | - Handle<Value> argv[1] = { string }; |
122 | | - Local<Object> obj = p_buffer_fn->NewInstance(1, argv); |
| 119 | + size_t length = StringBytes::Size(string, enc); |
123 | 120 |
|
124 | | - return scope.Close(obj); |
| 121 | + Local<Object> buf = New(length); |
| 122 | + char* data = Buffer::Data(buf); |
| 123 | + StringBytes::Write(data, length, string, enc); |
| 124 | + |
| 125 | + return scope.Close(buf); |
125 | 126 | } |
126 | 127 |
|
127 | 128 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: src/node_buffer.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle<v8::Object> val); |
42 | 42 | // public constructor |
43 | 43 | NODE_EXTERN v8::Local<v8::Object> New(size_t length); |
44 | 44 | // public constructor from string |
45 | | -NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string); |
| 45 | +NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string, |
| 46 | + enum encoding enc = UTF8); |
46 | 47 | // public constructor - data is copied |
47 | 48 | // TODO(trevnorris): should be something like Copy() |
48 | 49 | NODE_EXTERN v8::Local<v8::Object> New(const char* data, size_t len); |
|