Add u8a-only strict variants (#1503)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { u8aConcat } from '../u8a';
|
||||
import { u8aConcatStrict } from '../u8a';
|
||||
import { compactToU8a } from './toU8a';
|
||||
|
||||
/**
|
||||
@@ -17,8 +17,8 @@ import { compactToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function compactAddLength (input: Uint8Array): Uint8Array {
|
||||
return u8aConcat(
|
||||
return u8aConcatStrict([
|
||||
compactToU8a(input.length),
|
||||
input
|
||||
);
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { BN } from '../bn/bn';
|
||||
import { hexToU8a } from '../hex/toU8a';
|
||||
import { performance } from '../test/performance';
|
||||
import { compactFromU8a } from '.';
|
||||
import { compactFromU8a, compactFromU8aStrict } from '.';
|
||||
|
||||
describe('compactFromU8a', (): void => {
|
||||
it('decoded u8 value', (): void => {
|
||||
@@ -76,6 +76,7 @@ describe('compactFromU8a', (): void => {
|
||||
performance('compactFromU8a (u8)', 1_000_000, [[new Uint8Array([63 << 2])]], compactFromU8a);
|
||||
performance('compactFromU8a (u16)', 1_000_000, [[new Uint8Array([0b11111101, 0b00000111])]], compactFromU8a);
|
||||
performance('compactFromU8a (u32)', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8a);
|
||||
performance('compactFromU8aStrict', 1_000_000, [[new Uint8Array([254, 255, 3, 0])]], compactFromU8aStrict);
|
||||
performance('compactFromU8a (u48)', 1_000_000, [[hexToU8a('0x0b00407a10f35a')]], compactFromU8a);
|
||||
performance('compactFromU8a (u96)', 250_000, [[new Uint8Array([23, 52, 0x40, 0x7a, 0x10, 0xf3, 0x5a, 0, 0, 18])]], compactFromU8a);
|
||||
});
|
||||
|
||||
@@ -21,8 +21,14 @@ import { u8aToBn, u8aToU8a } from '../u8a';
|
||||
* ```
|
||||
*/
|
||||
export function compactFromU8a (input: U8aLike): [number, BN] {
|
||||
const u8a = u8aToU8a(input);
|
||||
return compactFromU8aStrict(u8aToU8a(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @name compactFromU8aStrict
|
||||
* @description A strict version of [[compactFromU8a]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function compactFromU8aStrict (u8a: Uint8Array): [number, BN] {
|
||||
// The u8a is manually converted here for 1, 2 & 4 lengths, it is 2x faster
|
||||
// than doing an additional call to u8aToBn (as with variable length)
|
||||
switch (u8a[0] & 0b11) {
|
||||
|
||||
@@ -24,5 +24,5 @@
|
||||
|
||||
export { compactAddLength } from './addLength';
|
||||
export { compactStripLength } from './stripLength';
|
||||
export { compactFromU8a } from './fromU8a';
|
||||
export { compactFromU8a, compactFromU8aStrict } from './fromU8a';
|
||||
export { compactToU8a } from './toU8a';
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn';
|
||||
import { u8aConcat } from '../u8a';
|
||||
import { u8aConcatStrict } from '../u8a';
|
||||
|
||||
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
|
||||
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
|
||||
@@ -44,9 +44,9 @@ export function compactToU8a (value: BN | bigint | number): Uint8Array {
|
||||
|
||||
assert(length >= 4, 'Invalid length, previous checks match anything less than 2^30');
|
||||
|
||||
return u8aConcat(
|
||||
return u8aConcatStrict([
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
[((length - 4) << 2) + 0b11],
|
||||
new Uint8Array([((length - 4) << 2) + 0b11]),
|
||||
u8a.subarray(0, length)
|
||||
);
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -33,8 +33,16 @@ export function isAscii (value?: U8aLike | null): boolean {
|
||||
if (value) {
|
||||
return isStringIn && !isHex(value)
|
||||
? value.toString().split('').every(isAsciiChar)
|
||||
: u8aToU8a(value).every(isAsciiByte);
|
||||
: isAsciiStrict(u8aToU8a(value));
|
||||
}
|
||||
|
||||
return isStringIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name isAsciiStrict
|
||||
* @description A strict version of [[isAscii]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function isAsciiStrict (u8a: Uint8Array): boolean {
|
||||
return u8a.every(isAsciiByte);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
export function isError (value: unknown): value is Error {
|
||||
return (
|
||||
((value && (value as Record<string, unknown>).constructor) === Error) ||
|
||||
((value && (value as Error).constructor) === Error) ||
|
||||
value instanceof Error
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @summary Type checking utilities
|
||||
*/
|
||||
export { isArray } from './array';
|
||||
export { isAscii } from './ascii';
|
||||
export { isAscii, isAsciiStrict } from './ascii';
|
||||
export { isBigInt } from './bigInt';
|
||||
export { isBn } from './bn';
|
||||
export { isBuffer } from './buffer';
|
||||
@@ -30,5 +30,5 @@ export { isToBigInt } from './toBigInt';
|
||||
export { isToBn } from './toBn';
|
||||
export { isU8a } from './u8a';
|
||||
export { isUndefined } from './undefined';
|
||||
export { isUtf8 } from './utf8';
|
||||
export { isUtf8, isUtf8Strict } from './utf8';
|
||||
export { isWasm } from './wasm';
|
||||
|
||||
@@ -10,6 +10,16 @@ describe('isInstanceOf', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on real instances (inherited)', (): void => {
|
||||
class Test extends Array {
|
||||
// default
|
||||
}
|
||||
|
||||
expect(
|
||||
isInstanceOf(new Test(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', (): void => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function isInstanceOf (value: unknown, clazz: Function): boolean {
|
||||
export function isInstanceOf (value: unknown, Clazz: Function): boolean {
|
||||
return (
|
||||
((value && (value as Record<string, unknown>).constructor) === clazz) ||
|
||||
value instanceof Uint8Array
|
||||
((value && (value as Record<string, unknown>).constructor) === Clazz) ||
|
||||
value instanceof Clazz
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ export function isU8a (value?: unknown): value is Uint8Array {
|
||||
// here we defer the instanceof check which is actually slightly
|
||||
// slower than just checking the constrctor (direct instances)
|
||||
return (
|
||||
((value && (value as Record<string, unknown>).constructor) === Uint8Array) ||
|
||||
((value && (value as Uint8Array).constructor) === Uint8Array) ||
|
||||
value instanceof Uint8Array
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,14 @@ export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | str
|
||||
return isString(value);
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
return isUtf8Strict(u8aToU8a(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @name isUtf8Strict
|
||||
* @description A strict version of [[isUtf8]], acception only Uint8Array inpouts
|
||||
*/
|
||||
export function isUtf8Strict (u8a: Uint8Array): boolean {
|
||||
const len = u8a.length;
|
||||
let i = 0;
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ import { formatDecimal, formatNumber } from '..';
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ExecFn = (...params: any[]) => unknown;
|
||||
|
||||
const NUM_PAD = 16;
|
||||
const PRE_PAD = 32;
|
||||
|
||||
function loop (count: number, inputs: unknown[][], exec: ExecFn): [number, unknown[]] {
|
||||
const start = Date.now();
|
||||
const results = new Array<unknown>(inputs.length);
|
||||
@@ -32,8 +35,8 @@ export function formatOps (count: number, time: number): string {
|
||||
const ops = 1_000_000 / micro;
|
||||
|
||||
return `
|
||||
${formatFixed(ops).padStart(15 + 20)} ops/s
|
||||
${formatFixed(micro).padStart(15 + 20)} μs/op`;
|
||||
${formatFixed(ops).padStart(NUM_PAD + PRE_PAD + 1)} ops/s
|
||||
${formatFixed(micro).padStart(NUM_PAD + PRE_PAD + 1)} μs/op`;
|
||||
}
|
||||
|
||||
export function performance (name: string, count: number, inputs: unknown[][], exec: ExecFn): void {
|
||||
@@ -43,7 +46,7 @@ export function performance (name: string, count: number, inputs: unknown[][], e
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${name}:`.padStart(19)} ${time.toString().padStart(15)} ms${formatOps(count, time)}
|
||||
${`${name}:`.padStart(PRE_PAD)} ${time.toString().padStart(NUM_PAD)} ms${formatOps(count, time)}
|
||||
`);
|
||||
});
|
||||
}
|
||||
@@ -58,9 +61,9 @@ export function performanceCmp (name: string, [first, second]: [string, string],
|
||||
console.log(`
|
||||
performance run for ${name} completed with ${formatNumber(count)} iterations.
|
||||
|
||||
${`${first}:`.padStart(19)} ${ta.toString().padStart(15)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
|
||||
${`${first}:`.padStart(PRE_PAD)} ${ta.toString().padStart(NUM_PAD)} ms ${ta < tb ? '(fastest)' : `(slowest, ${(ta / tb).toFixed(2)}x)`}${formatOps(count, ta)}
|
||||
|
||||
${`${second}:`.padStart(19)} ${tb.toString().padStart(15)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
|
||||
${`${second}:`.padStart(PRE_PAD)} ${tb.toString().padStart(NUM_PAD)} ms ${ta > tb ? '(fastest)' : `(slowest, ${(tb / ta).toFixed(2)}x)`}${formatOps(count, tb)}
|
||||
`);
|
||||
|
||||
const unmatched = ra.filter((r, i) =>
|
||||
|
||||
@@ -22,8 +22,14 @@ import { u8aToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = u8aToU8a(b);
|
||||
return u8aCmpStrict(u8aToU8a(a), u8aToU8a(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aCmpStrict
|
||||
* @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function u8aCmpStrict (u8aa: Uint8Array, u8ab: Uint8Array): number {
|
||||
let i = 0;
|
||||
|
||||
while (true) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { getRandomValues } from '@polkadot/x-randomvalues';
|
||||
|
||||
import { arrayRange } from '../array';
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aConcat } from '.';
|
||||
import { u8aConcat, u8aConcatStrict } from '.';
|
||||
|
||||
const ptest = arrayRange(10).map(() => getRandomValues(new Uint8Array(32)));
|
||||
|
||||
@@ -34,5 +34,7 @@ describe('u8aConcat', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
performance('u8aConcat', 50000, [[ptest]], u8aConcat);
|
||||
performance('u8aConcat', 10000, [[ptest]], u8aConcat);
|
||||
performance('u8aConcatStrict', 10000, [[[ptest]]], u8aConcatStrict);
|
||||
performance('u8aConcatStrict (len)', 10000, [[[ptest], 320]], u8aConcatStrict);
|
||||
});
|
||||
|
||||
@@ -23,15 +23,30 @@ import { u8aToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function u8aConcat (...list: U8aLike[]): Uint8Array {
|
||||
let length = 0;
|
||||
let offset = 0;
|
||||
const u8as = new Array<Uint8Array>(list.length);
|
||||
let length = 0;
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
length += u8as[i].length;
|
||||
}
|
||||
|
||||
return u8aConcatStrict(u8as, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aConcatStrict
|
||||
* @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function u8aConcatStrict (u8as: Uint8Array[], length = 0): Uint8Array {
|
||||
let offset = 0;
|
||||
|
||||
if (!length) {
|
||||
for (let i = 0; i < u8as.length; i++) {
|
||||
length += u8as[i].length;
|
||||
}
|
||||
}
|
||||
|
||||
const result = new Uint8Array(length);
|
||||
|
||||
for (let i = 0; i < u8as.length; i++) {
|
||||
|
||||
@@ -20,9 +20,14 @@ import { u8aToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): boolean {
|
||||
const u8aa = u8aToU8a(a);
|
||||
const u8ab = u8aToU8a(b);
|
||||
return u8aEqStrict(u8aToU8a(a), u8aToU8a(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aEqStrict
|
||||
* @description A strict version of [[u8aCmp]], accepting only Uint8Array inputs
|
||||
*/
|
||||
export function u8aEqStrict (u8aa: Uint8Array, u8ab: Uint8Array): boolean {
|
||||
if (u8aa.length === u8ab.length) {
|
||||
const dvA = new DataView(u8aa.buffer, u8aa.byteOffset);
|
||||
const dvB = new DataView(u8ab.buffer, u8ab.byteOffset);
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
* @summary Utility methods to convert to and from `Uint8Array` objects
|
||||
*/
|
||||
|
||||
export { u8aCmp } from './cmp';
|
||||
export { u8aConcat } from './concat';
|
||||
export { u8aCmp, u8aCmpStrict } from './cmp';
|
||||
export { u8aConcat, u8aConcatStrict } from './concat';
|
||||
export { u8aEmpty } from './empty';
|
||||
export { u8aEq } from './eq';
|
||||
export { u8aEq, u8aEqStrict } from './eq';
|
||||
export { u8aFixLength } from './fixLength';
|
||||
export { u8aSorted } from './sorted';
|
||||
export { u8aToBigInt } from './toBigInt';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { performance } from '../test/performance';
|
||||
import { u8aToU8a } from '.';
|
||||
|
||||
describe('u8aToU8a', (): void => {
|
||||
@@ -68,4 +69,8 @@ describe('u8aToU8a', (): void => {
|
||||
u8aToU8a(123 as unknown as Uint8Array)
|
||||
).toEqual(new Uint8Array([49, 50, 51]));
|
||||
});
|
||||
|
||||
performance('u8aToU8a (hex)', 250_000, [['0x1234']], u8aToU8a);
|
||||
performance('u8aToU8a (str)', 250_000, [['test']], u8aToU8a);
|
||||
performance('u8aToU8a (u8a)', 1_000_000, [[new Uint8Array()]], u8aToU8a);
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { U8aLike } from '../types';
|
||||
|
||||
import { u8aConcat } from './concat';
|
||||
import { u8aConcatStrict } from './concat';
|
||||
import { u8aEq } from './eq';
|
||||
import { u8aToU8a } from './toU8a';
|
||||
|
||||
@@ -45,5 +45,5 @@ export function u8aWrapBytes (bytes: U8aLike): Uint8Array {
|
||||
// if Ethereum-wrapping, we don't add our wrapping bytes
|
||||
return u8aIsWrapped(u8a, true)
|
||||
? u8a
|
||||
: u8aConcat(U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX);
|
||||
: u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user