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.
*
* @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;

View File

@ -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.

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}