From: Chris Duncan Date: Tue, 30 Jun 2026 22:03:46 +0000 (-0700) Subject: Simplify hex-to-byte converter. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=d76f42006031a7aba091c3fc29defae1799b4031;p=libnemo.git Simplify hex-to-byte converter. --- diff --git a/src/lib/convert/hex.ts b/src/lib/convert/hex.ts index 92844ad..fb4360e 100644 --- a/src/lib/convert/hex.ts +++ b/src/lib/convert/hex.ts @@ -30,14 +30,10 @@ export const hex = Object.freeze({ throw new TypeError('Invalid padding when converting hex to bytes', { cause: padding }) } if (hex.length & 1) hex = `0${hex}` - const hexArray = hex.match(/.{2}/g) - if (hexArray == null) { - throw new RangeError('Error converting hex string to bytes', { cause: hexArray }) - } - const diff = padding - hexArray.length + const byteLength = hex.length >> 1 + const diff = padding - byteLength const offset = diff & ~(diff >> 31) - const length = hexArray.length + offset - const bytes = new Uint8Array(length) + const bytes = new Uint8Array(byteLength + offset) bytes.set(hex.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [], offset) return bytes },