From 03a6299c0c3926f6c36daaf7f4b2482300a2f431 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 17 Apr 2024 15:01:37 +1000 Subject: [PATCH] refactor(bytes): minor cleanups (#4602) --- bytes/concat.ts | 14 +++++++------- bytes/copy.ts | 6 +++--- bytes/ends_with.ts | 9 +++++---- bytes/index_of_needle.ts | 12 ++++-------- bytes/last_index_of_needle.ts | 12 +++++------- bytes/repeat.ts | 11 +++++------ 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/bytes/concat.ts b/bytes/concat.ts index 23a4bcac9..c3b1112f8 100644 --- a/bytes/concat.ts +++ b/bytes/concat.ts @@ -4,7 +4,7 @@ /** * Concatenate an array of byte slices into a single slice. * - * @param buf Array of byte slices to concatenate. + * @param buffers Array of byte slices to concatenate. * * @example Basic usage * ```ts @@ -16,16 +16,16 @@ * concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ] * ``` */ -export function concat(buf: Uint8Array[]): Uint8Array { +export function concat(buffers: Uint8Array[]): Uint8Array { let length = 0; - for (const b of buf) { - length += b.length; + for (const buffer of buffers) { + length += buffer.length; } const output = new Uint8Array(length); let index = 0; - for (const b of buf) { - output.set(b, index); - index += b.length; + for (const buffer of buffers) { + output.set(buffer, index); + index += buffer.length; } return output; diff --git a/bytes/copy.ts b/bytes/copy.ts index 49a149c03..7dfaf8c40 100644 --- a/bytes/copy.ts +++ b/bytes/copy.ts @@ -2,10 +2,10 @@ // This module is browser compatible. /** - * Copy bytes from the `src` array to the `dst` array and returns the number of - * bytes copied. + * Copy bytes from the source array to the destination array and returns the + * number of bytes copied. * - * If the `src` array is larger than what the `dst` array can hold, only the + * If the source array is larger than what the `dst` array can hold, only the * amount of bytes that fit in the `dst` array are copied. * * @param src Source array to copy from. diff --git a/bytes/ends_with.ts b/bytes/ends_with.ts index 23f9285e6..58475abaf 100644 --- a/bytes/ends_with.ts +++ b/bytes/ends_with.ts @@ -23,10 +23,11 @@ * ``` */ export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean { - let srci = source.length - 1; - let sfxi = suffix.length - 1; - for (; sfxi >= 0; srci--, sfxi--) { - if (source[srci] !== suffix[sfxi]) return false; + const diff = source.length - suffix.length; + for (let i = suffix.length - 1; i >= 0; i--) { + if (source[diff + i] !== suffix[i]) { + return false; + } } return true; } diff --git a/bytes/index_of_needle.ts b/bytes/index_of_needle.ts index 436c86d54..2cbccbc1c 100644 --- a/bytes/index_of_needle.ts +++ b/bytes/index_of_needle.ts @@ -54,18 +54,14 @@ export function indexOfNeedle( const s = needle[0]; for (let i = start; i < source.length; i++) { if (source[i] !== s) continue; - const pin = i; let matched = 1; - let j = i; - while (matched < needle.length) { - j++; - if (source[j] !== needle[j - pin]) { - break; - } + let j = i + 1; + while (matched < needle.length && source[j] === needle[j - i]) { matched++; + j++; } if (matched === needle.length) { - return pin; + return i; } } return -1; diff --git a/bytes/last_index_of_needle.ts b/bytes/last_index_of_needle.ts index ecdaa6415..0b07bda48 100644 --- a/bytes/last_index_of_needle.ts +++ b/bytes/last_index_of_needle.ts @@ -51,18 +51,16 @@ export function lastIndexOfNeedle( const e = needle[needle.length - 1]; for (let i = start; i >= 0; i--) { if (source[i] !== e) continue; - const pin = i; let matched = 1; let j = i; - while (matched < needle.length) { - j--; - if (source[j] !== needle[needle.length - 1 - (pin - j)]) { - break; - } + while ( + matched < needle.length && + source[--j] === needle[needle.length - 1 - (i - j)] + ) { matched++; } if (matched === needle.length) { - return pin - needle.length + 1; + return i - needle.length + 1; } } return -1; diff --git a/bytes/repeat.ts b/bytes/repeat.ts index b9d5ad9bd..d3a056b55 100644 --- a/bytes/repeat.ts +++ b/bytes/repeat.ts @@ -39,13 +39,12 @@ export function repeat(source: Uint8Array, count: number): Uint8Array { throw new Error("bytes: repeat count must be an integer"); } - const nb = new Uint8Array(source.length * count); + const repeated = new Uint8Array(source.length * count); + let offset = 0; - let bp = copy(source, nb); - - for (; bp < nb.length; bp *= 2) { - copy(nb.slice(0, bp), nb, bp); + while (offset < repeated.length) { + offset += copy(source, repeated, offset); } - return nb; + return repeated; }