2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-29 06:01:21 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
import { indexOfNeedle } from "./index_of_needle.ts";
|
|
|
|
|
2024-04-16 06:59:52 +00:00
|
|
|
/**
|
|
|
|
* Determines whether the source array contains the needle array.
|
2022-11-29 06:01:21 +00:00
|
|
|
*
|
2024-04-16 06:59:52 +00:00
|
|
|
* The complexity of this function is `O(source.length * needle.length)`.
|
2022-11-29 06:01:21 +00:00
|
|
|
*
|
2024-04-16 06:59:52 +00:00
|
|
|
* @param source Source array to check.
|
|
|
|
* @param needle Needle array to check for.
|
|
|
|
* @param start 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.
|
2022-11-29 06:01:21 +00:00
|
|
|
*
|
2024-04-16 06:59:52 +00:00
|
|
|
* @example Basic usage
|
2022-11-29 06:01:21 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { includesNeedle } from "@std/bytes/includes-needle";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-04-16 06:59:52 +00:00
|
|
|
*
|
|
|
|
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
|
|
* const needle = new Uint8Array([1, 2]);
|
|
|
|
*
|
2024-05-08 06:18:26 +00:00
|
|
|
* assertEquals(includesNeedle(source, needle), true);
|
2024-04-16 06:59:52 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Start index
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { includesNeedle } from "@std/bytes/includes-needle";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-04-16 06:59:52 +00:00
|
|
|
*
|
2022-11-29 06:01:21 +00:00
|
|
|
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
|
|
|
|
* const needle = new Uint8Array([1, 2]);
|
2024-04-16 06:59:52 +00:00
|
|
|
*
|
2024-05-08 06:18:26 +00:00
|
|
|
* assertEquals(includesNeedle(source, needle, 3), true);
|
|
|
|
* assertEquals(includesNeedle(source, needle, 6), false);
|
2022-11-29 06:01:21 +00:00
|
|
|
* ```
|
2024-04-16 06:59:52 +00:00
|
|
|
* The search will start at the specified index in the source array.
|
2022-11-29 06:01:21 +00:00
|
|
|
*/
|
|
|
|
export function includesNeedle(
|
|
|
|
source: Uint8Array,
|
|
|
|
needle: Uint8Array,
|
|
|
|
start = 0,
|
|
|
|
): boolean {
|
|
|
|
return indexOfNeedle(source, needle, start) !== -1;
|
|
|
|
}
|