]> git.codecow.com Git - libnemo.git/commitdiff
Simplify hex-to-byte converter.
authorChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 22:03:46 +0000 (15:03 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 22:03:46 +0000 (15:03 -0700)
src/lib/convert/hex.ts

index 92844adf807da73cb07d6991ee65e0d2a6174d21..fb4360ecef4e0844d6c01364748044eb57c16221 100644 (file)
@@ -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
        },