MCPcopy Create free account
hub / github.com/p2r3/convert / decodeUTF16

Function decodeUTF16

src/handlers/textEncoding.ts:20–36  ·  view source on GitHub ↗
(bytes: Uint8Array, littleEndian: boolean)

Source from the content-addressed store, hash-verified

18}
19
20function decodeUTF16(bytes: Uint8Array, littleEndian: boolean) {
21 const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
22 let out = "";
23 for (let i = 0; i + 2 <= dv.byteLength; ) {
24 const w1 = dv.getUint16(i, littleEndian);
25 i += 2;
26 if (w1 >= 0xd800 && w1 <= 0xdbff && i + 2 <= dv.byteLength) {
27 const w2 = dv.getUint16(i, littleEndian);
28 i += 2;
29 const cp = (((w1 - 0xd800) << 10) | (w2 - 0xdc00)) + 0x10000;
30 out += String.fromCodePoint(cp);
31 } else {
32 out += String.fromCharCode(w1);
33 }
34 }
35 return out;
36}
37
38function encodeUTF16(str: string, littleEndian: boolean, addBOM = false) {
39 // count code units

Callers 1

doConvertMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…