const bytesSet = Function.prototype.call.bind(Bytes.prototype.set)
const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill)
+const charCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt)
function get<T> (target: object, key: PropertyKey): (this: unknown) => T {
const found = Object.getOwnPropertyDescriptor(target, key)
}
/**
- * Decodes hex string to Bytes. Validates input directly instead of using a
- * patchable `RegExp.prototype.test()`.
+ * Decodes hex string to Bytes..
*/
function hexToBytes (hex: string): Bytes {
const length = hex.length
throw new TypeError(`Invalid hexadecimal length ${length}`)
}
const bytes = new Bytes(length >> 1)
+ let d = 0
for (let i = 0, b = 0; i < length; i += 2, b++) {
- const hi = hex[i]
- const lo = hex[i + 1]
- if (NIBBLE[hi] == null) {
- throw new TypeError(`Invalid hexadecimal character ${hi}`)
- }
- if (NIBBLE[lo] == null) {
- throw new TypeError(`Invalid hexadecimal character ${lo}`)
- }
+ const hi = charCodeAt(hex, i)
+ const lo = charCodeAt(hex, i + 1)
+ d |= hi | lo
bytes[b] = (NIBBLE[hi] << 4) | NIBBLE[lo]
}
+ if (d & 0x100) {
+ fill(bytes, 0)
+ throw new TypeError('Invalid hexadecimal character')
+ }
return bytes
}
/**