Skip to content

Commit

Permalink
fix(bytes): equals() works with subarray (#4630)
Browse files Browse the repository at this point in the history
fix(bytes): `equals()` works with `.subarray()`
  • Loading branch information
iuioiua authored Apr 23, 2024
1 parent d209aa3 commit 3149cdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bytes/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function equalsNaive(a: Uint8Array, b: Uint8Array): boolean {
function equals32Bit(a: Uint8Array, b: Uint8Array): boolean {
const len = a.length;
const compressible = Math.floor(len / 4);
const compressedA = new Uint32Array(a.buffer, 0, compressible);
const compressedB = new Uint32Array(b.buffer, 0, compressible);
const compressedA = new Uint32Array(a, 0, compressible);
const compressedB = new Uint32Array(b, 0, compressible);
for (let i = compressible * 4; i < len; i++) {
if (a[i] !== b[i]) return false;
}
Expand Down
18 changes: 17 additions & 1 deletion bytes/equals_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { equals } from "./equals.ts";
import { assert } from "../assert/mod.ts";
import { assert, assertEquals, assertNotEquals } from "../assert/mod.ts";

Deno.test("equals()", () => {
const v = equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
Expand Down Expand Up @@ -32,3 +32,19 @@ Deno.test("equals() handles randomized testing", () => {
assert(!equals(arr1, arr3));
}
});

// https://github.com/denoland/deno_std/issues/3603
Deno.test("equals() works with .subarray()", () => {
const a = new Uint8Array(1001).subarray(1);
const b = new Uint8Array(1000);
a[0] = 123;
b[0] = 123;
assertEquals(a, b);
assert(equals(a, b));

const c = new Uint8Array(1001).subarray(1);
const d = new Uint8Array(1000);
c[999] = 123;
assertNotEquals(c, d); // ok
assert(!equals(c, d));
});

0 comments on commit 3149cdb

Please # to comment.