includesNeedle

import { includesNeedle } from "jsr:@ende/bytes";

function

function includesNeedle(source: Uint8Array, needle: Uint8Array, start: number = 0): boolean

Determines whether the source array contains the needle array.

The complexity of this function is O(source.length * needle.length).

source: Uint8Array

Source array to check.

needle: Uint8Array

Needle array to check for.

start: number = 0

Start index in the source array to begin the search. Defaults to 0.

Returns

true if the source array contains the needle array, false otherwise.

Example: Basic usage

import { includesNeedle } from "@ende/bytes/includes-needle";
import { assertEquals } from "@ende/assert";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const needle = new Uint8Array([1, 2]);

assertEquals(includesNeedle(source, needle), true);

Example: Start index

import { includesNeedle } from "@ende/bytes/includes-needle";
import { assertEquals } from "@ende/assert";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const needle = new Uint8Array([1, 2]);

assertEquals(includesNeedle(source, needle, 3), true);
assertEquals(includesNeedle(source, needle, 6), false);

The search will start at the specified index in the source array.

Source