2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-04-16 06:59:52 +00:00
|
|
|
/**
|
|
|
|
* Check whether byte slices are equal to each other using 8-bit comparisons.
|
|
|
|
*
|
|
|
|
* @param a First array to check equality
|
|
|
|
* @param b Second array to check equality
|
|
|
|
* @returns `true` if the arrays are equal, `false` otherwise
|
|
|
|
*
|
2022-11-08 15:17:16 +00:00
|
|
|
* @private
|
2021-10-06 17:10:43 +00:00
|
|
|
*/
|
2022-11-29 06:01:21 +00:00
|
|
|
function equalsNaive(a: Uint8Array, b: Uint8Array): boolean {
|
2022-11-08 15:17:16 +00:00
|
|
|
for (let i = 0; i < b.length; i++) {
|
|
|
|
if (a[i] !== b[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:59:52 +00:00
|
|
|
/** Check whether byte slices are equal to each other using 32-bit comparisons.
|
|
|
|
*
|
|
|
|
* @param a First array to check equality.
|
|
|
|
* @param b Second array to check equality.
|
|
|
|
* @returns `true` if the arrays are equal, `false` otherwise.
|
|
|
|
*
|
2022-11-08 15:17:16 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2022-11-29 06:01:21 +00:00
|
|
|
function equals32Bit(a: Uint8Array, b: Uint8Array): boolean {
|
2021-10-06 17:10:43 +00:00
|
|
|
const len = a.length;
|
2024-04-24 00:59:32 +00:00
|
|
|
const compactOffset = 3 - ((a.byteOffset + 3) % 4);
|
|
|
|
const compactLen = Math.floor((len - compactOffset) / 4);
|
|
|
|
const compactA = new Uint32Array(
|
|
|
|
a.buffer,
|
|
|
|
a.byteOffset + compactOffset,
|
|
|
|
compactLen,
|
|
|
|
);
|
|
|
|
const compactB = new Uint32Array(
|
|
|
|
b.buffer,
|
|
|
|
b.byteOffset + compactOffset,
|
|
|
|
compactLen,
|
|
|
|
);
|
|
|
|
for (let i = 0; i < compactOffset; i++) {
|
2021-10-06 17:10:43 +00:00
|
|
|
if (a[i] !== b[i]) return false;
|
|
|
|
}
|
2024-04-24 00:59:32 +00:00
|
|
|
for (let i = 0; i < compactA.length; i++) {
|
|
|
|
if (compactA[i] !== compactB[i]) return false;
|
|
|
|
}
|
|
|
|
for (let i = compactOffset + compactLen * 4; i < len; i++) {
|
|
|
|
if (a[i] !== b[i]) return false;
|
2021-10-06 17:10:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-08 15:17:16 +00:00
|
|
|
|
2024-04-24 00:59:32 +00:00
|
|
|
/**
|
|
|
|
* Byte length threshold for when to use 32-bit comparisons, based on
|
|
|
|
* benchmarks.
|
|
|
|
*
|
|
|
|
* @see {@link https://github.com/denoland/deno_std/pull/4635}
|
|
|
|
*/
|
|
|
|
const THRESHOLD_32_BIT = 160;
|
|
|
|
|
2024-04-16 06:59:52 +00:00
|
|
|
/**
|
|
|
|
* Check whether byte slices are equal to each other.
|
|
|
|
*
|
|
|
|
* @param a First array to check equality.
|
|
|
|
* @param b Second array to check equality.
|
|
|
|
* @returns `true` if the arrays are equal, `false` otherwise.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { equals } from "@std/bytes/equals";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-04-16 06:59:52 +00:00
|
|
|
*
|
|
|
|
* const a = new Uint8Array([1, 2, 3]);
|
|
|
|
* const b = new Uint8Array([1, 2, 3]);
|
|
|
|
* const c = new Uint8Array([4, 5, 6]);
|
|
|
|
*
|
2024-05-08 06:18:26 +00:00
|
|
|
* assertEquals(equals(a, b), true);
|
|
|
|
* assertEquals(equals(a, c), false);
|
2024-04-16 06:59:52 +00:00
|
|
|
* ```
|
2022-11-08 15:17:16 +00:00
|
|
|
*/
|
|
|
|
export function equals(a: Uint8Array, b: Uint8Array): boolean {
|
2022-12-03 12:27:32 +00:00
|
|
|
if (a.length !== b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-04-24 00:59:32 +00:00
|
|
|
return a.length >= THRESHOLD_32_BIT &&
|
|
|
|
(a.byteOffset % 4) === (b.byteOffset % 4)
|
|
|
|
? equals32Bit(a, b)
|
|
|
|
: equalsNaive(a, b);
|
2022-11-08 15:17:16 +00:00
|
|
|
}
|