mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 00:59:24 -05:00
17663c1232
Original: c0390ade3d
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
/** Find first index of binary pattern from a. If not found, then return -1 **/
|
|
export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
|
|
const s = pat[0];
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (a[i] !== s) continue;
|
|
const pin = i;
|
|
let matched = 1;
|
|
while (matched < pat.length) {
|
|
i++;
|
|
if (a[i] !== pat[i - pin]) {
|
|
break;
|
|
}
|
|
matched++;
|
|
}
|
|
if (matched === pat.length) {
|
|
return pin;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/** Find last index of binary pattern from a. If not found, then return -1 **/
|
|
export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number {
|
|
const e = pat[pat.length - 1];
|
|
for (let i = a.length - 1; i >= 0; i--) {
|
|
if (a[i] !== e) continue;
|
|
const pin = i;
|
|
let matched = 1;
|
|
while (matched < pat.length) {
|
|
i--;
|
|
if (a[i] !== pat[pat.length - 1 - (pin - i)]) {
|
|
break;
|
|
}
|
|
matched++;
|
|
}
|
|
if (matched === pat.length) {
|
|
return pin - pat.length + 1;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/** Check whether binary arrays are equal to each other **/
|
|
export function bytesEqual(a: Uint8Array, match: Uint8Array): boolean {
|
|
if (a.length !== match.length) return false;
|
|
for (let i = 0; i < match.length; i++) {
|
|
if (a[i] !== match[i]) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/** Check whether binary array has binary prefix **/
|
|
export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean {
|
|
for (let i = 0, max = prefix.length; i < max; i++) {
|
|
if (a[i] !== prefix[i]) return false;
|
|
}
|
|
return true;
|
|
}
|