MCPcopy
hub / github.com/codedogQBY/ReadAny / decodeUtf16LE

Function decodeUtf16LE

packages/core/src/utils/umd-parser.ts:92–113  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

90}
91
92function decodeUtf16LE(bytes: Uint8Array): string {
93 if (utf16leDecoder) {
94 return utf16leDecoder.decode(bytes);
95 }
96 // Manual UTF-16LE decode. JS strings are already UTF-16 internally, so each
97 // 16-bit code unit maps 1:1 to a string char (surrogate pairs pass through
98 // unchanged, same as TextDecoder would yield).
99 const len = bytes.length & ~1; // round down to even
100 const CHUNK = 8192; // bounded apply() arg count to avoid stack issues
101 const buf: number[] = new Array(CHUNK);
102 let out = "";
103 for (let start = 0; start < len; start += CHUNK * 2) {
104 const stop = Math.min(len, start + CHUNK * 2);
105 const n = (stop - start) >>> 1;
106 for (let i = 0; i < n; i++) {
107 const off = start + (i << 1);
108 buf[i] = bytes[off]! | (bytes[off + 1]! << 8);
109 }
110 out += String.fromCharCode.apply(null, n === CHUNK ? buf : buf.slice(0, n));
111 }
112 return out;
113}
114
115function checkMagic(bytes: Uint8Array): void {
116 if (bytes.length < 4) {

Callers 3

parseUmdFunction · 0.85
assembleFunction · 0.85
parseTitlesFunction · 0.85

Calls 2

sliceMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected