Skip to content

Commit 65d493c

Browse files
committed
Just use codepoints as it'll be copied into an Uint32Array anyway
1 parent 4ee74f7 commit 65d493c

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/utils.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,15 @@ export function getUID(): number {
5151
return ++uidCounter;
5252
}
5353

54-
export function utfDecodeString(array: Uint16Array): string {
55-
let string = '';
56-
const { length } = array;
57-
for (let i = 0; i < length; i++) {
58-
string += String.fromCharCode(array[i]);
59-
}
60-
return string;
54+
export function utfDecodeString(array: Uint32Array): string {
55+
return String.fromCodePoint(...array);
6156
}
6257

63-
export function utfEncodeString(string: string): Uint16Array {
64-
const array = new Uint16Array(string.length);
65-
const { length } = string;
66-
for (let i = 0; i < length; i++) {
67-
array[i] = string.charCodeAt(i);
68-
}
69-
return array;
58+
export function utfEncodeString(string: string): Uint32Array {
59+
// $FlowFixMe Flow's Uint32Array.from's type definition is wrong; first argument of mapFn will be string
60+
return Uint32Array.from(string, toCodePoint);
61+
}
62+
63+
function toCodePoint(string: string) {
64+
return string.codePointAt(0);
7065
}

0 commit comments

Comments
 (0)