diff --git a/packages/util/src/array/flatten.ts b/packages/util/src/array/flatten.ts index 0479cb996..ba2431880 100644 --- a/packages/util/src/array/flatten.ts +++ b/packages/util/src/array/flatten.ts @@ -22,14 +22,16 @@ export function arrayFlatten (arrays: T[][]): T[] { // pre-allocate based on the combined size let size = 0; - for (const a of arrays) { - size += a.length; + for (let i = 0; i < arrays.length; i++) { + size += arrays[i].length; } const output = new Array(size); let i = -1; - for (const a of arrays) { + for (let j = 0; j < arrays.length; j++) { + const a = arrays[j]; + // instead of pushing, we just set the entries for (let e = 0; e < a.length; e++) { output[++i] = a[e]; diff --git a/packages/util/src/hex/fixLength.spec.ts b/packages/util/src/hex/fixLength.spec.ts index 45ebccd09..6b0466772 100644 --- a/packages/util/src/hex/fixLength.spec.ts +++ b/packages/util/src/hex/fixLength.spec.ts @@ -10,12 +10,6 @@ describe('hexFixLength', (): void => { ).toEqual('0x12345678'); }); - it('returns bitLength === -1 as-is (adding missing 0)', (): void => { - expect( - hexFixLength('0x1234567') - ).toEqual('0x01234567'); - }); - it('does not change when bitlength === length', (): void => { expect( hexFixLength('0x12345678', 32) diff --git a/packages/util/src/hex/hasPrefix.spec.ts b/packages/util/src/hex/hasPrefix.spec.ts index 7b79ad09f..0de10c10f 100644 --- a/packages/util/src/hex/hasPrefix.spec.ts +++ b/packages/util/src/hex/hasPrefix.spec.ts @@ -6,7 +6,7 @@ import { hexHasPrefix } from '.'; describe('hexHasPrefix', (): void => { it('returns true when hex prefix is found', (): void => { expect( - hexHasPrefix('0x123') + hexHasPrefix('0x1234') ).toEqual(true); }); @@ -24,8 +24,7 @@ describe('hexHasPrefix', (): void => { it('returns false when non-string value supplied', (): void => { expect( - // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument - hexHasPrefix(false as any) + hexHasPrefix(false as unknown as string) ).toEqual(false); }); }); diff --git a/packages/util/src/hex/hasPrefix.ts b/packages/util/src/hex/hasPrefix.ts index 185cf724c..99c9ac93b 100644 --- a/packages/util/src/hex/hasPrefix.ts +++ b/packages/util/src/hex/hasPrefix.ts @@ -20,5 +20,5 @@ import { isHex } from '../is/hex'; * ``` */ export function hexHasPrefix (value?: string | null): value is HexString { - return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x'); + return !!value && isHex(value, -1); } diff --git a/packages/util/src/hex/stripPrefix.spec.ts b/packages/util/src/hex/stripPrefix.spec.ts index 8efc2fd30..38386505f 100644 --- a/packages/util/src/hex/stripPrefix.spec.ts +++ b/packages/util/src/hex/stripPrefix.spec.ts @@ -10,6 +10,12 @@ describe('hexStripPrefix', (): void => { ).toEqual(''); }); + it('returns an empty string when 0x value supplied', (): void => { + expect( + hexStripPrefix('0x') + ).toEqual(''); + }); + it('strips the prefix from hex strings', (): void => { expect( hexStripPrefix('0x1223') @@ -24,7 +30,7 @@ describe('hexStripPrefix', (): void => { it('throws when invalid hex', (): void => { expect( - (): string => hexStripPrefix('0x0x01ab') - ).toThrow(/Invalid hex/); + () => hexStripPrefix('0x0x01ab') + ).toThrow(/Expected hex value to convert/); }); }); diff --git a/packages/util/src/hex/stripPrefix.ts b/packages/util/src/hex/stripPrefix.ts index 4abfff920..c58af8163 100644 --- a/packages/util/src/hex/stripPrefix.ts +++ b/packages/util/src/hex/stripPrefix.ts @@ -1,9 +1,7 @@ // Copyright 2017-2021 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { hexHasPrefix } from './hasPrefix'; - -const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/; +import { REGEX_HEX_NOPREFIX, REGEX_HEX_PREFIXED } from '../is/hex'; /** * @name hexStripPrefix @@ -20,13 +18,13 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/; * ``` */ export function hexStripPrefix (value?: string | null): string { - if (!value) { + if (!value || value === '0x') { return ''; - } else if (hexHasPrefix(value)) { + } else if (REGEX_HEX_PREFIXED.test(value)) { return value.substr(2); - } else if (UNPREFIX_HEX_REGEX.test(value)) { + } else if (REGEX_HEX_NOPREFIX.test(value)) { return value; } - throw new Error(`Invalid hex ${value} passed to hexStripPrefix`); + throw new Error(`Expected hex value to convert, found '${value}'`); } diff --git a/packages/util/src/hex/toU8a.spec.ts b/packages/util/src/hex/toU8a.spec.ts index d7eedf565..03a0b8b3c 100644 --- a/packages/util/src/hex/toU8a.spec.ts +++ b/packages/util/src/hex/toU8a.spec.ts @@ -21,9 +21,9 @@ describe('hexToU8a', (): void => { it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => { expect( - hexToU8a('0x80000A', 32) + hexToU8a('0x80000A', 64) ).toEqual( - new Uint8Array([0, 128, 0, 10]) + new Uint8Array([0, 0, 0, 0, 0, 128, 0, 10]) ); }); @@ -37,20 +37,20 @@ describe('hexToU8a', (): void => { it('fails when non-hex value provided', (): void => { expect( - (): Uint8Array => hexToU8a('notahex') + () => hexToU8a('notahex') ).toThrow(/hex value to convert/); }); it.skip('performance', (): void => { let a = '0x'; - for (let i = 0; i < 8192; i++) { - a += U16_TO_HEX[i]; + for (let i = 0; i < 640000; i++) { + a += U16_TO_HEX[i % (256 * 256)]; } console.time('hexToU8a:performance'); - for (let i = 0; i < 8192; i++) { + for (let i = 0; i < 64; i++) { hexToU8a(a); } diff --git a/packages/util/src/hex/toU8a.ts b/packages/util/src/hex/toU8a.ts index 3613f6a5b..5ee3305ab 100644 --- a/packages/util/src/hex/toU8a.ts +++ b/packages/util/src/hex/toU8a.ts @@ -3,9 +3,7 @@ import type { HexString } from '../types'; -import { assert } from '../assert'; -import { isHex } from '../is/hex'; -import { HEX_TO_U8 } from './alphabet'; +import { HEX_TO_U8, HEX_TO_U16 } from './alphabet'; import { hexStripPrefix } from './stripPrefix'; /** @@ -28,8 +26,6 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U return new Uint8Array(); } - assert(isHex(_value), () => `Expected hex value to convert, found '${_value}'`); - const value = hexStripPrefix(_value).toLowerCase(); const valLength = value.length / 2; const resultLength = Math.ceil( @@ -38,12 +34,31 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U : bitLength / 8 ); const result = new Uint8Array(resultLength); - const offset = Math.max(0, resultLength - valLength); + const offset = resultLength > valLength + ? resultLength - valLength + : 0; - for (let i = 0; i < resultLength; i++) { - // TODO With some magic we can use the HEX_TO_U16 lookups here as well - // (and then finally HEX_TO_U8 for the overflows) - result[i + offset] = HEX_TO_U8[value.substr(i * 2, 2)]; + // With offsets, it becomes slightly trickier since we need to ensure the U16 + // is at the correct alignment as well. Due to this, we just focus on the simple + // case where we do no bitlength conversions (which is the most-taken path) + if (offset) { + for (let i = 0; i < resultLength; i++) { + result[i + offset] = HEX_TO_U8[value.substr(i * 2, 2)]; + } + } else { + // NOTE: It is only _slightly_ more optimal to use HEX_TO_U16 here as + // well. The overhead does not seem to be in the lookup map iteration, + // even with 640k inputs + const mod = resultLength % 2; + const u16 = new Uint16Array(result.buffer, 0, (resultLength - mod) / 2); + + for (let i = 0; i < u16.length; i++) { + u16[i] = HEX_TO_U16[value.substr(i * 4, 4)]; + } + + if (mod) { + result[resultLength - 1] = HEX_TO_U8[value.substr(value.length - 2, 2)]; + } } return result; diff --git a/packages/util/src/hex/toU8aBuffer.spec.ts b/packages/util/src/hex/toU8aBuffer.spec.ts index 1af52ab2d..712e28471 100644 --- a/packages/util/src/hex/toU8aBuffer.spec.ts +++ b/packages/util/src/hex/toU8aBuffer.spec.ts @@ -21,9 +21,9 @@ describe('hexToU8a', (): void => { it('returns a Uint8Array with the correct values (bitLength > provided)', (): void => { expect( - hexToU8a('0x80000A', 32) + hexToU8a('0x80000A', 64) ).toEqual( - new Uint8Array([0, 128, 0, 10]) + new Uint8Array([0, 0, 0, 0, 0, 128, 0, 10]) ); }); @@ -37,20 +37,20 @@ describe('hexToU8a', (): void => { it('fails when non-hex value provided', (): void => { expect( - (): Uint8Array => hexToU8a('notahex') + () => hexToU8a('notahex') ).toThrow(/hex value to convert/); }); it.skip('performance', (): void => { let a = '0x'; - for (let i = 0; i < 8192; i++) { - a += U16_TO_HEX[i]; + for (let i = 0; i < 640000; i++) { + a += U16_TO_HEX[i % (256 * 256)]; } console.time('hexToU8a:performance'); - for (let i = 0; i < 8192; i++) { + for (let i = 0; i < 64; i++) { hexToU8a(a); } diff --git a/packages/util/src/hex/toU8aBuffer.ts b/packages/util/src/hex/toU8aBuffer.ts index 14fc5c841..f72fa3845 100644 --- a/packages/util/src/hex/toU8aBuffer.ts +++ b/packages/util/src/hex/toU8aBuffer.ts @@ -3,8 +3,6 @@ import type { HexString } from '../types'; -import { assert } from '../assert'; -import { isHex } from '../is/hex'; import { hexStripPrefix } from './stripPrefix'; /** @@ -27,8 +25,6 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U return new Uint8Array(); } - assert(isHex(_value), () => `Expected hex value to convert, found '${_value}'`); - const value = hexStripPrefix(_value); const buf = Buffer.from(value, 'hex'); const valLength = value.length / 2; @@ -42,7 +38,9 @@ export function hexToU8a (_value?: HexString | string | null, bitLength = -1): U return Uint8Array.from(buf); } - const offset = Math.max(0, resultLength - valLength); + const offset = resultLength > valLength + ? resultLength - valLength + : 0; if (offset) { const u8a = new Uint8Array(resultLength); diff --git a/packages/util/src/is/hex.ts b/packages/util/src/is/hex.ts index 719a66a59..10dabdb99 100644 --- a/packages/util/src/is/hex.ts +++ b/packages/util/src/is/hex.ts @@ -3,7 +3,9 @@ import type { HexString } from '../types'; -const HEX_REGEX = /^0x[a-fA-F0-9]+$/; +export const REGEX_HEX_PREFIXED = /^0x([\da-fA-F]{2})+$/; + +export const REGEX_HEX_NOPREFIX = /^([\da-fA-F]{2})+$/; /** * @name isHex @@ -20,10 +22,10 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/; * isHex('0x1234', 8); // => false * ``` */ -export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is HexString { - return (typeof value === 'string' && (value === '0x' || HEX_REGEX.test(value))) +export function isHex (value: unknown, bitLength = -1): value is HexString { + return (typeof value === 'string' && (value === '0x' || REGEX_HEX_PREFIXED.test(value))) ? bitLength === -1 - ? ((value.length % 2 === 0) || ignoreLength) + ? true : (value.length === (2 + Math.ceil(bitLength / 4))) : false; } diff --git a/packages/util/src/number/toHex.ts b/packages/util/src/number/toHex.ts index 43125b464..9a8867c51 100644 --- a/packages/util/src/number/toHex.ts +++ b/packages/util/src/number/toHex.ts @@ -27,5 +27,7 @@ export function numberToHex (value?: number | null, bitLength = -1): HexString { return '0x'; } - return hexFixLength(value.toString(16), bitLength, true); + const hex = value.toString(16); + + return hexFixLength(hex.length % 2 ? `0${hex}` : hex, bitLength, true); }