3 ms·
function readUtf8String(memory, pointer) { let s = ""; for (i = pointer; memory[i]!==0; i++) { s += String.fromCharCode(memory[i]);
by dchest 9y ago
function readUtf8String(memory, pointer) {
let s = "";
for (i = pointer; memory[i]!==0; i++) {
s += String.fromCharCode(memory[i]);
}
return s;
}
This doesn't read UTF-8, this reads bytes and converts them to UTF-16 char codes, so it works only with ASCII or some other 0-255 encoding. To read UTF-8 into a JavaScript string (which is UTF-16), bytes should be properly decoded from UTF-8 (e.g. with TextDecoder).
- arnoooooo 9y agoThanks ! I've renamed the function accordingly.
- chrismorgan 9y agoASCII only covers values 0–127; this function allows 128–255 as well, which makes it ISO-8859-1 (latin1), the first 256 code points of Unicode being the entirety of ISO-8859-1.