| 64 | } |
| 65 | |
| 66 | function encodeUTF32(str: string, littleEndian: boolean, addBOM = false) { |
| 67 | const codepoints = Array.from(str, (ch) => ch.codePointAt(0) || 0); |
| 68 | const buf = new ArrayBuffer(codepoints.length * 4 + (addBOM ? 4 : 0)); |
| 69 | const dv = new DataView(buf); |
| 70 | let offset = 0; |
| 71 | if (addBOM) { |
| 72 | if (littleEndian) dv.setUint32(0, 0xFF_FE_00_00, true); |
| 73 | else dv.setUint32(0, 0x00_00_FE_FF, false); |
| 74 | offset += 4; |
| 75 | } |
| 76 | for (const cp of codepoints) { |
| 77 | dv.setUint32(offset, cp, littleEndian); |
| 78 | offset += 4; |
| 79 | } |
| 80 | return new Uint8Array(buf, 0, offset); |
| 81 | } |
| 82 | |
| 83 | function decodeUsingTextDecoder(bytes: Uint8Array, label: string) { |
| 84 | try { |