mirror of
https://github.com/denoland/deno.git
synced 2025-10-01 14:41:15 +00:00
[bytes] fix bytesFindIndex and bytesFindLastIndex (denoland/deno_std#381)
Original: 8714252916
This commit is contained in:
parent
14afad59aa
commit
480b9e71ff
2 changed files with 22 additions and 7 deletions
|
@ -6,10 +6,11 @@ export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
|
|||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i] !== s) continue;
|
||||
const pin = i;
|
||||
let matched = 1;
|
||||
let matched = 1,
|
||||
j = i;
|
||||
while (matched < pat.length) {
|
||||
i++;
|
||||
if (a[i] !== pat[i - pin]) {
|
||||
j++;
|
||||
if (a[j] !== pat[j - pin]) {
|
||||
break;
|
||||
}
|
||||
matched++;
|
||||
|
@ -27,10 +28,11 @@ export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number {
|
|||
for (let i = a.length - 1; i >= 0; i--) {
|
||||
if (a[i] !== e) continue;
|
||||
const pin = i;
|
||||
let matched = 1;
|
||||
let matched = 1,
|
||||
j = i;
|
||||
while (matched < pat.length) {
|
||||
i--;
|
||||
if (a[i] !== pat[pat.length - 1 - (pin - i)]) {
|
||||
j--;
|
||||
if (a[j] !== pat[pat.length - 1 - (pin - j)]) {
|
||||
break;
|
||||
}
|
||||
matched++;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue