| 1 | export function stringToCodepoints(string: string): number[] { |
| 2 | const result: number[] = []; |
| 3 | for (let i = 0; i < string.length; i++) { |
| 4 | let code = string.charCodeAt(i); |
| 5 | // if (code === 0x0d && string.charCodeAt(i + 1) === 0x0a) { |
| 6 | // code = 0x0a; |
| 7 | // i++; |
| 8 | // } |
| 9 | |
| 10 | // if (code === 0x0d || code === 0x0c) code = 0x0a; |
| 11 | // if (code === 0x00) code = 0xfffd; |
| 12 | |
| 13 | // Decode a surrogate pair into an astral codepoint. |
| 14 | if (code >= 0xd800 && code <= 0xdbff) { |
| 15 | const nextCode = string.charCodeAt(i + 1); |
| 16 | if (nextCode >= 0xdc00 && nextCode <= 0xdfff) { |
| 17 | const lead = code - 0xd800; |
| 18 | const trail = nextCode - 0xdc00; |
| 19 | code = 2 ** 16 + lead * 2 ** 10 + trail; |
| 20 | // N = ((H - 0xD800) * 0x400) + (L - 0xDC00) + 0x10000; |
| 21 | i++; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | result.push(code); |
| 26 | } |
| 27 | |
| 28 | return result; |
| 29 | } |
| 30 | |
| 31 | const ZWJ = 0x200d; // Zero-width joiner |
| 32 | |