GeezerVison is showing github.com — src: move BE/LE buffer conversion to StringSlice() · littledan/node@52fc406
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.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -274,6 +274,7 @@ NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate, |
274 | 274 | size_t len, |
275 | 275 | enum encoding encoding = BINARY); |
276 | 276 |
|
| 277 | +// The input buffer should be in host endianness. |
277 | 278 | NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate, |
278 | 279 | const uint16_t* buf, |
279 | 280 | size_t len); |
|
Collapse file
Copy file name to clipboardExpand all lines: src/node_buffer.cc +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -276,7 +276,11 @@ void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) { |
276 | 276 | const uint16_t* buf; |
277 | 277 | bool release = false; |
278 | 278 |
|
279 | | - if (reinterpret_cast<uintptr_t>(data) % sizeof(*buf) == 0) { |
| 279 | + // Node's "ucs2" encoding expects LE character data inside a Buffer, so we |
| 280 | + // need to reorder on BE platforms. See http://nodejs.org/api/buffer.html |
| 281 | + // regarding Node's "ucs2" encoding specification. |
| 282 | + const bool aligned = (reinterpret_cast<uintptr_t>(data) % sizeof(*buf) == 0); |
| 283 | + if (IsLittleEndian() && aligned) { |
280 | 284 | buf = reinterpret_cast<const uint16_t*>(data); |
281 | 285 | } else { |
282 | 286 | // Make a copy to avoid unaligned accesses in v8::String::NewFromTwoByte(). |
|
Collapse file
Copy file name to clipboardExpand all lines: src/string_bytes.cc -13Lines changed: 0 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -777,19 +777,6 @@ Local<Value> StringBytes::Encode(Isolate* isolate, |
777 | 777 | size_t buflen) { |
778 | 778 | const uint16_t* src = buf; |
779 | 779 |
|
780 | | - // Node's "ucs2" encoding expects LE character data inside a |
781 | | - // Buffer, so we need to reorder on BE platforms. See |
782 | | - // http://nodejs.org/api/buffer.html regarding Node's "ucs2" |
783 | | - // encoding specification. |
784 | | - if (IsBigEndian()) { |
785 | | - // Inefficient, see StringSlice<UCS2>() in src/node_buffer.cc; |
786 | | - // this is potentially the second copy of the actual input. |
787 | | - uint16_t* copy = new uint16_t[buflen]; |
788 | | - for (size_t i = 0; i < buflen; i += 1) |
789 | | - copy[i] = buf[i] << 8 | buf[i] >> 8; |
790 | | - src = copy; |
791 | | - } |
792 | | - |
793 | 780 | Local<String> val; |
794 | 781 | if (buflen < EXTERN_APEX) { |
795 | 782 | val = String::NewFromTwoByte(isolate, |
|
Collapse file
Copy file name to clipboardExpand all lines: src/string_bytes.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,7 @@ class StringBytes { |
77 | 77 | size_t buflen, |
78 | 78 | enum encoding encoding); |
79 | 79 |
|
| 80 | + // The input buffer should be in host endianness. |
80 | 81 | static v8::Local<v8::Value> Encode(v8::Isolate* isolate, |
81 | 82 | const uint16_t* buf, |
82 | 83 | size_t buflen); |
|