(bytes: Uint8Array)
| 90 | } |
| 91 | |
| 92 | function 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 | |
| 115 | function checkMagic(bytes: Uint8Array): void { |
| 116 | if (bytes.length < 4) { |
no test coverage detected