476
477
export function decodeHex(hex: string): VSBuffer {
479
throw new SyntaxError('Hex string must have an even length');
480
}
481
>
const out = new Uint8Array(hex.length >> 1);
buffer.ts
482
>
for (let i = 0; i < hex.length;) {
483
>
out[i >> 1] = (decodeHexChar(hex, i++) << 4) | decodeHexChar(hex, i++);
484
>
}
485
return VSBuffer.wrap(out);
486
}
487
488
>
function decodeHexChar(str: string, position: number) {
buffer.ts
489
>
const s = str.charCodeAt(position);
490
>
if (s >= 48 && s <= 57) { // '0'-'9'
491
return s - 48;
492
>
} else if (s >= 97 && s <= 102) { // 'a'-'f'
buffer.ts
493
return s - 87;
494
>
} else if (s >= 65 && s <= 70) { // 'A'-'F'
buffer.ts
495
return s - 55;
496
} else {
497
throw new SyntaxError(`Invalid hex character at position ${position}`);
498
}