mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(bytes): minor tweaks and fixes for full cov. (#2969)
This commit is contained in:
parent
cc22ee1ca7
commit
b454363655
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user