File size: 712 Bytes
391a73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { toArrayBuffer } from "../bytes/index.js";
export function encode(text, options = {}) {
    const result = new ArrayBuffer(text.length * 2);
    const view = new DataView(result);
    for (let i = 0; i < text.length; i++) {
        view.setUint16(i * 2, text.charCodeAt(i), options.littleEndian ?? false);
    }
    return new Uint8Array(result);
}
export function decode(data, options = {}) {
    const buffer = toArrayBuffer(data);
    const view = new DataView(buffer);
    let result = "";
    for (let i = 0; i < buffer.byteLength; i += 2) {
        result += String.fromCharCode(view.getUint16(i, options.littleEndian ?? false));
    }
    return result;
}
export const utf16 = { encode, decode };