MCPcopy Create free account
hub / github.com/nodejs/node / encodeWtf8

Function encodeWtf8

deps/v8/test/mjsunit/wasm/stringrefs-exec-gc.js:12–36  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

10let kSig_w_ii = makeSig([kWasmI32, kWasmI32], [kWasmStringRef]);
11
12function encodeWtf8(str) {
13 // String iterator coalesces surrogate pairs.
14 let out = [];
15 for (let codepoint of str) {
16 codepoint = codepoint.codePointAt(0);
17 if (codepoint <= 0x7f) {
18 out.push(codepoint);
19 } else if (codepoint <= 0x7ff) {
20 out.push(0xc0 | (codepoint >> 6));
21 out.push(0x80 | (codepoint & 0x3f));
22 } else if (codepoint <= 0xffff) {
23 out.push(0xe0 | (codepoint >> 12));
24 out.push(0x80 | ((codepoint >> 6) & 0x3f));
25 out.push(0x80 | (codepoint & 0x3f));
26 } else if (codepoint <= 0x10ffff) {
27 out.push(0xf0 | (codepoint >> 18));
28 out.push(0x80 | ((codepoint >> 12) & 0x3f));
29 out.push(0x80 | ((codepoint >> 6) & 0x3f));
30 out.push(0x80 | (codepoint & 0x3f));
31 } else {
32 throw new Error("bad codepoint " + codepoint);
33 }
34 }
35 return out;
36}
37
38let externalString = "I'm an external string";
39externalizeString(externalString);

Callers 2

makeWtf8TestDataSegmentFunction · 0.70

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected