2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-02-10 18:49:48 -05:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
/** Find first index of binary pattern from source. If not found, then return -1
|
2020-06-25 06:40:51 -04:00
|
|
|
* @param source source array
|
2020-05-20 10:32:28 -04:00
|
|
|
* @param pat pattern to find in source array
|
2020-12-06 09:51:13 -05:00
|
|
|
* @param start the index to start looking in the source
|
2020-05-20 10:32:28 -04:00
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function indexOf(
|
|
|
|
source: Uint8Array,
|
|
|
|
pat: Uint8Array,
|
|
|
|
start = 0,
|
|
|
|
): number {
|
|
|
|
if (start >= source.length) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
2019-02-10 18:49:48 -05:00
|
|
|
const s = pat[0];
|
2020-12-06 09:51:13 -05:00
|
|
|
for (let i = start; i < source.length; i++) {
|
2020-05-20 10:32:28 -04:00
|
|
|
if (source[i] !== s) continue;
|
2019-02-10 18:49:48 -05:00
|
|
|
const pin = i;
|
2020-03-22 14:49:09 -04:00
|
|
|
let matched = 1;
|
|
|
|
let j = i;
|
2019-02-10 18:49:48 -05:00
|
|
|
while (matched < pat.length) {
|
2019-05-07 12:02:31 -04:00
|
|
|
j++;
|
2020-05-20 10:32:28 -04:00
|
|
|
if (source[j] !== pat[j - pin]) {
|
2019-02-10 18:49:48 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
matched++;
|
|
|
|
}
|
|
|
|
if (matched === pat.length) {
|
|
|
|
return pin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
/** Find last index of binary pattern from source. If not found, then return -1.
|
2020-06-25 06:40:51 -04:00
|
|
|
* @param source source array
|
2020-05-20 10:32:28 -04:00
|
|
|
* @param pat pattern to find in source array
|
2020-12-06 09:51:13 -05:00
|
|
|
* @param start the index to start looking in the source
|
2020-05-20 10:32:28 -04:00
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function lastIndexOf(
|
|
|
|
source: Uint8Array,
|
|
|
|
pat: Uint8Array,
|
|
|
|
start = source.length - 1,
|
|
|
|
): number {
|
|
|
|
if (start < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (start >= source.length) {
|
|
|
|
start = source.length - 1;
|
|
|
|
}
|
2019-02-10 18:49:48 -05:00
|
|
|
const e = pat[pat.length - 1];
|
2020-12-06 09:51:13 -05:00
|
|
|
for (let i = start; i >= 0; i--) {
|
2020-05-20 10:32:28 -04:00
|
|
|
if (source[i] !== e) continue;
|
2019-02-10 18:49:48 -05:00
|
|
|
const pin = i;
|
2020-03-22 14:49:09 -04:00
|
|
|
let matched = 1;
|
|
|
|
let j = i;
|
2019-02-10 18:49:48 -05:00
|
|
|
while (matched < pat.length) {
|
2019-05-07 12:02:31 -04:00
|
|
|
j--;
|
2020-05-20 10:32:28 -04:00
|
|
|
if (source[j] !== pat[pat.length - 1 - (pin - j)]) {
|
2019-02-10 18:49:48 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
matched++;
|
|
|
|
}
|
|
|
|
if (matched === pat.length) {
|
|
|
|
return pin - pat.length + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
/** Check whether binary arrays are equal to each other.
|
2020-12-06 09:51:13 -05:00
|
|
|
* @param a first array to check equality
|
|
|
|
* @param b second array to check equality
|
2020-05-20 10:32:28 -04:00
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function equals(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;
|
2019-02-10 18:49:48 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
/** Check whether binary array starts with prefix.
|
2020-12-06 09:51:13 -05:00
|
|
|
* @param source source array
|
2020-05-20 10:32:28 -04:00
|
|
|
* @param prefix prefix array to check in source
|
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
|
2019-02-10 18:49:48 -05:00
|
|
|
for (let i = 0, max = prefix.length; i < max; i++) {
|
2020-05-20 10:32:28 -04:00
|
|
|
if (source[i] !== prefix[i]) return false;
|
2019-02-10 18:49:48 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-24 07:44:13 -04:00
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
/** Check whether binary array ends with suffix.
|
2020-06-25 06:40:51 -04:00
|
|
|
* @param source source array
|
2020-05-20 10:32:28 -04:00
|
|
|
* @param suffix suffix array to check in source
|
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
|
2020-05-20 10:32:28 -04:00
|
|
|
for (
|
|
|
|
let srci = source.length - 1, sfxi = suffix.length - 1;
|
|
|
|
sfxi >= 0;
|
|
|
|
srci--, sfxi--
|
|
|
|
) {
|
|
|
|
if (source[srci] !== suffix[sfxi]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
|
|
|
|
* @param origin The origin bytes
|
2019-05-24 07:44:13 -04:00
|
|
|
* @param count The count you want to repeat.
|
2020-12-06 09:51:13 -05:00
|
|
|
* @throws `RangeError` When count is negative
|
2019-05-24 07:44:13 -04:00
|
|
|
*/
|
2020-05-20 10:32:28 -04:00
|
|
|
export function repeat(origin: Uint8Array, count: number): Uint8Array {
|
2019-05-24 07:44:13 -04:00
|
|
|
if (count === 0) {
|
|
|
|
return new Uint8Array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < 0) {
|
2020-12-06 09:51:13 -05:00
|
|
|
throw new RangeError("bytes: negative repeat count");
|
2020-05-20 10:32:28 -04:00
|
|
|
} else if ((origin.length * count) / count !== origin.length) {
|
2019-05-24 07:44:13 -04:00
|
|
|
throw new Error("bytes: repeat count causes overflow");
|
|
|
|
}
|
|
|
|
|
|
|
|
const int = Math.floor(count);
|
|
|
|
|
|
|
|
if (int !== count) {
|
|
|
|
throw new Error("bytes: repeat count must be an integer");
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
const nb = new Uint8Array(origin.length * count);
|
2019-05-24 07:44:13 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
let bp = copy(origin, nb);
|
2019-05-24 07:44:13 -04:00
|
|
|
|
|
|
|
for (; bp < nb.length; bp *= 2) {
|
2020-12-06 09:51:13 -05:00
|
|
|
copy(nb.slice(0, bp), nb, bp);
|
2019-05-24 07:44:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nb;
|
|
|
|
}
|
2020-03-22 14:49:09 -04:00
|
|
|
|
2020-12-06 09:51:13 -05:00
|
|
|
/** Concatenate multiple binary arrays and return new one.
|
|
|
|
* @param buf binary arrays to concatenate
|
2020-05-20 10:32:28 -04:00
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function concat(...buf: Uint8Array[]): Uint8Array {
|
|
|
|
let length = 0;
|
|
|
|
for (const b of buf) {
|
|
|
|
length += b.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = new Uint8Array(length);
|
|
|
|
let index = 0;
|
|
|
|
for (const b of buf) {
|
|
|
|
output.set(b, index);
|
|
|
|
index += b.length;
|
|
|
|
}
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
return output;
|
|
|
|
}
|
2020-05-20 10:32:28 -04:00
|
|
|
|
2020-06-25 06:40:51 -04:00
|
|
|
/** Check source array contains pattern array.
|
|
|
|
* @param source source array
|
2020-05-20 10:32:28 -04:00
|
|
|
* @param pat patter array
|
|
|
|
*/
|
|
|
|
export function contains(source: Uint8Array, pat: Uint8Array): boolean {
|
2020-12-06 09:51:13 -05:00
|
|
|
return indexOf(source, pat) != -1;
|
2020-05-20 10:32:28 -04:00
|
|
|
}
|
2020-06-25 06:40:51 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy bytes from one Uint8Array to another. Bytes from `src` which don't fit
|
|
|
|
* into `dst` will not be copied.
|
|
|
|
*
|
|
|
|
* @param src Source byte array
|
|
|
|
* @param dst Destination byte array
|
|
|
|
* @param off Offset into `dst` at which to begin writing values from `src`.
|
|
|
|
* @return number of bytes copied
|
|
|
|
*/
|
2020-12-06 09:51:13 -05:00
|
|
|
export function copy(src: Uint8Array, dst: Uint8Array, off = 0): number {
|
2020-06-25 06:40:51 -04:00
|
|
|
off = Math.max(0, Math.min(off, dst.byteLength));
|
|
|
|
const dstBytesAvailable = dst.byteLength - off;
|
|
|
|
if (src.byteLength > dstBytesAvailable) {
|
|
|
|
src = src.subarray(0, dstBytesAvailable);
|
|
|
|
}
|
|
|
|
dst.set(src, off);
|
|
|
|
return src.byteLength;
|
|
|
|
}
|