-
Notifications
You must be signed in to change notification settings - Fork 3
/
stringBytes.ts
35 lines (32 loc) · 986 Bytes
/
stringBytes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export namespace StringBytes {
/**
* Convert a given string into a Uint8Array encoded as UTF-8.
* @param s data to encode
*/
export function stringToBytes(s: string): Uint8Array {
let len = String.UTF8.byteLength(s, true) - 1;
let bytes = new Uint8Array(len);
memory.copy(bytes.dataStart, toUTF8(s), len);
return bytes;
}
/**
* Decode an UTF-8 encoded Uint8Array into a string.
* @param bytes array to decode
*/
export function bytesToString(bytes: Uint8Array | null): string | null {
if (bytes == null) {
return null;
}
return String.UTF8.decode(uint8ArrayToBuffer(bytes), true);
}
// Private helpers
function toUTF8(str: string, nullTerminated: boolean = false): usize {
return changetype<usize>(String.UTF8.encode(str, nullTerminated));
}
function uint8ArrayToBuffer(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(
array.byteOffset,
array.byteLength + array.byteOffset
);
}
}