mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(bytes): minor cleanups (#4602)
This commit is contained in:
parent
d87ef8078f
commit
03a6299c0c
@ -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;
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user