Skip to content

Instantly share code, notes, and snippets.

@mary-ext
Last active October 16, 2024 00:38
Show Gist options
  • Save mary-ext/2a07198dedd16f2fc13ba7220eeb6ff2 to your computer and use it in GitHub Desktop.
Save mary-ext/2a07198dedd16f2fc13ba7220eeb6ff2 to your computer and use it in GitHub Desktop.
utf8 to utf16 index map
// ty @retr0id
const getUtf8Size = (str: string, index: number): number => {
const code = str.codePointAt(index)!;
if (code <= 0x7f) {
return 1;
} else if (code <= 0x7ff) {
return 2;
} else if (code <= 0xffff) {
return 3;
} else {
return 4;
}
};
const getUtf8To16Mapping = (str: string): number[] => {
const map: number[] = [];
const len = str.length;
let u16pos = 0;
while (u16pos < len) {
const code = str.codePointAt(u16pos)!;
for (let i = getUtf8Size(str, u16pos); i--; ) {
map.push(u16pos);
}
u16pos += code > 0xffff ? 2 : 1;
}
return map;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment