Files
maple/maple-ts/src/pdb/strings.ts
T
2026-05-26 16:27:50 +10:00

34 lines
1.0 KiB
TypeScript

const EMPTY = new Uint8Array([0x03]);
function isAscii(s: string): boolean {
for (let i = 0; i < s.length; i++) if (s.charCodeAt(i) > 0x7f) return false;
return true;
}
export function encodeDevicesql(s: string): Uint8Array {
if (!s) return EMPTY;
if (isAscii(s) && s.length < 127) {
const ascii = new TextEncoder().encode(s);
const length = ascii.length + 1;
const lengthAndKind = ((length << 1) | 1) & 0xff;
const out = new Uint8Array(1 + ascii.length);
out[0] = lengthAndKind;
out.set(ascii, 1);
return out;
}
const utf16 = new Uint8Array(s.length * 2);
for (let i = 0; i < s.length; i++) {
const c = s.charCodeAt(i);
utf16[i * 2] = c & 0xff;
utf16[i * 2 + 1] = (c >> 8) & 0xff;
}
const totalLength = utf16.length + 4;
const out = new Uint8Array(4 + utf16.length);
out[0] = 0x90;
out[1] = totalLength & 0xff;
out[2] = (totalLength >> 8) & 0xff;
out[3] = 0x00;
out.set(utf16, 4);
return out;
}