(utf32Bytes: Uint8Array, isLE: boolean = true)
| 10 | }; |
| 11 | |
| 12 | export const decodeUTF32 = (utf32Bytes: Uint8Array, isLE: boolean = true): string => { |
| 13 | if (!(utf32Bytes instanceof Uint8Array)) { |
| 14 | throw new TypeError("utf32Bytes must be a Uint8Array"); |
| 15 | } |
| 16 | const byteLen = utf32Bytes.byteLength; |
| 17 | if (byteLen % 4 !== 0) { |
| 18 | throw new RangeError("UTF-32 byte length must be a multiple of 4"); |
| 19 | } |
| 20 | const view = new DataView(utf32Bytes.buffer, utf32Bytes.byteOffset, byteLen); |
| 21 | let out = ""; |
| 22 | let chunk: number[] = []; |
| 23 | for (let i = 0; i < byteLen; i += 4) { |
| 24 | const codePoint = view.getUint32(i, isLE); |
| 25 | if (i === 0 && codePoint === 0x0000feff) continue; |
| 26 | chunk.push(codePoint); |
| 27 | if (chunk.length >= 16384) { |
| 28 | out += String.fromCodePoint(...chunk); |
| 29 | chunk = []; |
| 30 | } |
| 31 | } |
| 32 | if (chunk.length) out += String.fromCodePoint(...chunk); |
| 33 | return out; |
| 34 | }; |
| 35 | |
| 36 | export const bytesDecode = (charset: string, bytes: Uint8Array): string => { |
| 37 | const normalizedCharset = charset.toLowerCase(); |
no test coverage detected