Add u8a-only strict variants (#1503)
This commit is contained in:
@@ -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