Skip to content

Commit 52fc406

Browse files
committed
src: move BE/LE buffer conversion to StringSlice()
Move the big endian to little endian conversion logic for UCS2 input from src/string_bytes.cc to src/node_buffer.cc; StringSlice() is the only function that actually needs it and with this commit, a second copy is avoided on big endian architectures.
1 parent 56fde66 commit 52fc406

File tree

4 files changed

+7
-14
lines changed

4 files changed

+7
-14
lines changed

src/node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
274274
size_t len,
275275
enum encoding encoding = BINARY);
276276

277+
// The input buffer should be in host endianness.
277278
NODE_EXTERN v8::Local<v8::Value> Encode(v8::Isolate* isolate,
278279
const uint16_t* buf,
279280
size_t len);

src/node_buffer.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) {
276276
const uint16_t* buf;
277277
bool release = false;
278278

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) {
280284
buf = reinterpret_cast<const uint16_t*>(data);
281285
} else {
282286
// Make a copy to avoid unaligned accesses in v8::String::NewFromTwoByte().

src/string_bytes.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -777,19 +777,6 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
777777
size_t buflen) {
778778
const uint16_t* src = buf;
779779

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-
793780
Local<String> val;
794781
if (buflen < EXTERN_APEX) {
795782
val = String::NewFromTwoByte(isolate,

src/string_bytes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class StringBytes {
7777
size_t buflen,
7878
enum encoding encoding);
7979

80+
// The input buffer should be in host endianness.
8081
static v8::Local<v8::Value> Encode(v8::Isolate* isolate,
8182
const uint16_t* buf,
8283
size_t buflen);

0 commit comments

Comments
 (0)