diff --git a/bytes/equals.ts b/bytes/equals.ts index 7e431f957..ab5dcda70 100644 --- a/bytes/equals.ts +++ b/bytes/equals.ts @@ -7,7 +7,6 @@ * @param b second array to check equality */ function equalsNaive(a: Uint8Array, b: Uint8Array): boolean { - if (a.length !== b.length) return false; for (let i = 0; i < b.length; i++) { if (a[i] !== b[i]) return false; } @@ -20,7 +19,6 @@ function equalsNaive(a: Uint8Array, b: Uint8Array): boolean { * @param b second array to check equality */ function equals32Bit(a: Uint8Array, b: Uint8Array): boolean { - if (a.length !== b.length) return false; const len = a.length; const compressable = Math.floor(len / 4); const compressedA = new Uint32Array(a.buffer, 0, compressable); @@ -39,6 +37,8 @@ function equals32Bit(a: Uint8Array, b: Uint8Array): boolean { * @param b second array to check equality */ export function equals(a: Uint8Array, b: Uint8Array): boolean { - if (a.length < 1000) return equalsNaive(a, b); - return equals32Bit(a, b); + if (a.length !== b.length) { + return false; + } + return a.length < 1000 ? equalsNaive(a, b) : equals32Bit(a, b); } diff --git a/bytes/last_index_of_needle_test.ts b/bytes/last_index_of_needle_test.ts index 03a95587c..2c50fdaa7 100644 --- a/bytes/last_index_of_needle_test.ts +++ b/bytes/last_index_of_needle_test.ts @@ -40,3 +40,20 @@ Deno.test("[bytes] lastIndexOfNeedle with start index 2", () => { ); assertEquals(i, -1); }); + +Deno.test("[bytes] lastIndexOfNeedle with start index greater than source index", () => { + const i = lastIndexOfNeedle( + new Uint8Array([0, 1, 2, 0, 1, 2]), + new Uint8Array([0, 1]), + 7, + ); + assertEquals(i, 3); +}); + +Deno.test("[bytes] lastIndexOfNeedle if needle doesn't exist within source", () => { + const i = lastIndexOfNeedle( + new Uint8Array([0, 1, 2, 0, 1, 2]), + new Uint8Array([2, 3]), + ); + assertEquals(i, -1); +}); diff --git a/bytes/repeat.ts b/bytes/repeat.ts index eeeb2da44..285d9850c 100644 --- a/bytes/repeat.ts +++ b/bytes/repeat.ts @@ -22,13 +22,9 @@ export function repeat(source: Uint8Array, count: number): Uint8Array { if (count < 0) { throw new RangeError("bytes: negative repeat count"); - } else if ((source.length * count) / count !== source.length) { - throw new Error("bytes: repeat count causes overflow"); } - const int = Math.floor(count); - - if (int !== count) { + if (!Number.isInteger(count)) { throw new Error("bytes: repeat count must be an integer"); }