refactor(bytes): minor cleanups (#4602)

This commit is contained in:
Asher Gomez 2024-04-17 15:01:37 +10:00 committed by GitHub
parent d87ef8078f
commit 03a6299c0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 35 deletions

View File

@ -4,7 +4,7 @@
/** /**
* Concatenate an array of byte slices into a single slice. * 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 * @example Basic usage
* ```ts * ```ts
@ -16,16 +16,16 @@
* concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ] * 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; let length = 0;
for (const b of buf) { for (const buffer of buffers) {
length += b.length; length += buffer.length;
} }
const output = new Uint8Array(length); const output = new Uint8Array(length);
let index = 0; let index = 0;
for (const b of buf) { for (const buffer of buffers) {
output.set(b, index); output.set(buffer, index);
index += b.length; index += buffer.length;
} }
return output; return output;

View File

@ -2,10 +2,10 @@
// This module is browser compatible. // This module is browser compatible.
/** /**
* Copy bytes from the `src` array to the `dst` array and returns the number of * Copy bytes from the source array to the destination array and returns the
* bytes copied. * 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. * amount of bytes that fit in the `dst` array are copied.
* *
* @param src Source array to copy from. * @param src Source array to copy from.

View File

@ -23,10 +23,11 @@
* ``` * ```
*/ */
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean { export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
let srci = source.length - 1; const diff = source.length - suffix.length;
let sfxi = suffix.length - 1; for (let i = suffix.length - 1; i >= 0; i--) {
for (; sfxi >= 0; srci--, sfxi--) { if (source[diff + i] !== suffix[i]) {
if (source[srci] !== suffix[sfxi]) return false; return false;
}
} }
return true; return true;
} }

View File

@ -54,18 +54,14 @@ export function indexOfNeedle(
const s = needle[0]; const s = needle[0];
for (let i = start; i < source.length; i++) { for (let i = start; i < source.length; i++) {
if (source[i] !== s) continue; if (source[i] !== s) continue;
const pin = i;
let matched = 1; let matched = 1;
let j = i; let j = i + 1;
while (matched < needle.length) { while (matched < needle.length && source[j] === needle[j - i]) {
j++;
if (source[j] !== needle[j - pin]) {
break;
}
matched++; matched++;
j++;
} }
if (matched === needle.length) { if (matched === needle.length) {
return pin; return i;
} }
} }
return -1; return -1;

View File

@ -51,18 +51,16 @@ export function lastIndexOfNeedle(
const e = needle[needle.length - 1]; const e = needle[needle.length - 1];
for (let i = start; i >= 0; i--) { for (let i = start; i >= 0; i--) {
if (source[i] !== e) continue; if (source[i] !== e) continue;
const pin = i;
let matched = 1; let matched = 1;
let j = i; let j = i;
while (matched < needle.length) { while (
j--; matched < needle.length &&
if (source[j] !== needle[needle.length - 1 - (pin - j)]) { source[--j] === needle[needle.length - 1 - (i - j)]
break; ) {
}
matched++; matched++;
} }
if (matched === needle.length) { if (matched === needle.length) {
return pin - needle.length + 1; return i - needle.length + 1;
} }
} }
return -1; return -1;

View File

@ -39,13 +39,12 @@ export function repeat(source: Uint8Array, count: number): Uint8Array {
throw new Error("bytes: repeat count must be an integer"); 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); while (offset < repeated.length) {
offset += copy(source, repeated, offset);
for (; bp < nb.length; bp *= 2) {
copy(nb.slice(0, bp), nb, bp);
} }
return nb; return repeated;
} }