GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • src
    • node.h
    • node_buffer.cc
    • string_bytes.cc
    • string_bytes.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node.h‎

Copy file name to clipboardExpand all lines: src/node.h
+1Lines 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);
Collapse file

‎src/node_buffer.cc‎

Copy file name to clipboardExpand all lines: src/node_buffer.cc
+5-1Lines 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().
Collapse file

‎src/string_bytes.cc‎

Copy file name to clipboardExpand all lines: src/string_bytes.cc
-13Lines 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,
Collapse file

‎src/string_bytes.h‎

Copy file name to clipboardExpand all lines: src/string_bytes.h
+1Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.