u8aEq w/ U32 and lower only (#1182)
* u8aEq w/ U32 and lower only * Adjust mod * Adjust signatures
This commit is contained in:
+10
-23
@@ -5,26 +5,14 @@ import type { HexString } from '../types';
|
||||
|
||||
import { u8aToU8a } from './toU8a';
|
||||
|
||||
interface Constructor<T extends BigUint64Array | Uint32Array | Uint16Array | Uint8Array> {
|
||||
interface Constructor<T extends Uint32Array | Uint16Array | Uint8Array> {
|
||||
BYTES_PER_ELEMENT: number;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
new(...args: any[]): T;
|
||||
new(...args: unknown[]): T;
|
||||
}
|
||||
|
||||
// React-native does not have BigUint64Array, check others as well
|
||||
const U64_BYTES = typeof BigUint64Array !== 'undefined'
|
||||
? BigUint64Array.BYTES_PER_ELEMENT
|
||||
: 0;
|
||||
const U32_BYTES = typeof Uint32Array !== 'undefined'
|
||||
? Uint32Array.BYTES_PER_ELEMENT
|
||||
: 0;
|
||||
const U16_BYTES = typeof Uint16Array !== 'undefined'
|
||||
? Uint16Array.BYTES_PER_ELEMENT
|
||||
: 0;
|
||||
|
||||
// Creates a Uint8Array, ensuring that the alignment is correct
|
||||
function createArray <T extends BigUint64Array | Uint32Array | Uint16Array | Uint8Array> (Clazz: Constructor<T>, value: Uint8Array): T {
|
||||
function createArray <T extends Uint32Array | Uint16Array | Uint8Array> (Clazz: Constructor<T>, value: Uint8Array): T {
|
||||
// The byteOffset needs to match the size of the data, i.e. for Uint32 it needs to be 4
|
||||
// NOTE: DataView doesn't have this limitation, but getters are slower
|
||||
const align = Clazz.BYTES_PER_ELEMENT;
|
||||
@@ -34,7 +22,7 @@ function createArray <T extends BigUint64Array | Uint32Array | Uint16Array | Uin
|
||||
: new Clazz(value.buffer, value.byteOffset, value.length / align);
|
||||
}
|
||||
|
||||
function equalsArray <T extends BigUint64Array | Uint32Array | Uint16Array | Uint8Array> (a: T, b: T): boolean {
|
||||
function equalsArray <T extends Uint32Array | Uint16Array | Uint8Array> (a: T, b: T): boolean {
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i] !== b[i]) {
|
||||
return false;
|
||||
@@ -46,13 +34,12 @@ function equalsArray <T extends BigUint64Array | Uint32Array | Uint16Array | Uin
|
||||
|
||||
function equals (a: Uint8Array, b: Uint8Array): boolean {
|
||||
if (a.length === b.length) {
|
||||
return U64_BYTES && (a.length % U64_BYTES)
|
||||
? U32_BYTES && (a.length % U32_BYTES)
|
||||
? U16_BYTES && (a.length % U16_BYTES)
|
||||
? equalsArray(a, b)
|
||||
: equalsArray(createArray(Uint16Array, a), createArray(Uint16Array, b))
|
||||
: equalsArray(createArray(Uint32Array, a), createArray(Uint32Array, b))
|
||||
: equalsArray(createArray(BigUint64Array, a), createArray(BigUint64Array, b));
|
||||
// NOTE: We don't do BigUint64Array, at this point it is significantly slower
|
||||
return a.length % Uint32Array.BYTES_PER_ELEMENT
|
||||
? a.length % Uint16Array.BYTES_PER_ELEMENT
|
||||
? equalsArray(a, b)
|
||||
: equalsArray(createArray(Uint16Array, a), createArray(Uint16Array, b))
|
||||
: equalsArray(createArray(Uint32Array, a), createArray(Uint32Array, b));
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user