| 24 | * in a loop (which is a lot faster). |
| 25 | */ |
| 26 | export function utf32ToString(data: Uint32Array, start: number = 0, end: number = data.length): string { |
| 27 | let result = ''; |
| 28 | for (let i = start; i < end; ++i) { |
| 29 | let codepoint = data[i]; |
| 30 | if (codepoint > 0xFFFF) { |
| 31 | // JS strings are encoded as UTF16, thus a non BMP codepoint gets converted into a surrogate |
| 32 | // pair conversion rules: |
| 33 | // - subtract 0x10000 from code point, leaving a 20 bit number |
| 34 | // - add high 10 bits to 0xD800 --> first surrogate |
| 35 | // - add low 10 bits to 0xDC00 --> second surrogate |
| 36 | codepoint -= 0x10000; |
| 37 | result += String.fromCharCode((codepoint >> 10) + 0xD800) + String.fromCharCode((codepoint % 0x400) + 0xDC00); |
| 38 | } else { |
| 39 | result += String.fromCharCode(codepoint); |
| 40 | } |
| 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * StringToUtf32 - decodes UTF16 sequences into UTF32 codepoints. |