Skip to content

Instantly share code, notes, and snippets.

@cevek
Created October 27, 2016 14:07
Show Gist options
  • Save cevek/a3c3c1955511c6ffbf0b359f0a5336f8 to your computer and use it in GitHub Desktop.
Save cevek/a3c3c1955511c6ffbf0b359f0a5336f8 to your computer and use it in GitHub Desktop.
function Utf8BufferToUint16Array(buff) {
let c, char2, char3;
let len = buff.length;
let i = 0;
let ni = 0;
const newA = new Uint16Array(len);
while (i < len) {
c = buff[i++];
if (c < 128) {
newA[ni++] = c;
} else if (c <= 224) {
char2 = buff[i++];
newA[ni++] = (c & 0x1F) << 6 | char2 & 0x3F;
} else {
char2 = buff[i++];
char3 = buff[i++];
newA[ni++] = (c & 0x0F) << 12 | (char2 & 0x3F) << 6 | (char3 & 0x3F) << 0;
}
}
return newA;
}
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
// performance: 100x1mb ~ 260ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment