trie.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /**
  2. * Hostbane-Optimized Trie based on Mnemonist Trie
  3. */
  4. import { fastStringCompare } from './misc';
  5. import util from 'node:util';
  6. import { noop } from 'foxts/noop';
  7. import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
  8. import { deleteBit, getBit, missingBit, setBit } from 'foxts/bitwise';
  9. const START = 1 << 1;
  10. const INCLUDE_ALL_SUBDOMAIN = 1 << 2;
  11. type TrieNode<Meta = any> = [
  12. flag: number, /** end, includeAllSubdomain (.example.org, ||example.com) */
  13. TrieNode | null, /** parent */
  14. Map<string, TrieNode>, /** children */
  15. Meta /** meta */
  16. ];
  17. function deepTrieNodeToJSON<Meta = unknown>(node: TrieNode,
  18. unpackMeta: ((meta?: Meta) => string) | undefined) {
  19. const obj: Record<string, unknown> = {};
  20. obj['[start]'] = getBit(node[0], START);
  21. obj['[subdomain]'] = getBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  22. if (node[3] != null) {
  23. if (unpackMeta) {
  24. obj['[meta]'] = unpackMeta(node[3]);
  25. } else {
  26. obj['[meta]'] = node[3];
  27. }
  28. }
  29. node[2].forEach((value, key) => {
  30. obj[key] = deepTrieNodeToJSON(value, unpackMeta);
  31. });
  32. return obj;
  33. }
  34. const createNode = <Meta = unknown>(parent: TrieNode | null = null): TrieNode => [1, parent, new Map<string, TrieNode>(), null] as TrieNode<Meta>;
  35. function hostnameToTokens(hostname: string, hostnameFromIndex: number): string[] {
  36. const tokens = hostname.split('.');
  37. const results: string[] = [];
  38. let token = '';
  39. for (let i = hostnameFromIndex, l = tokens.length; i < l; i++) {
  40. token = tokens[i];
  41. if (token.length > 0) {
  42. results.push(token);
  43. } else {
  44. throw new TypeError(JSON.stringify({ hostname, hostnameFromIndex }, null, 2));
  45. }
  46. }
  47. return results;
  48. }
  49. function walkHostnameTokens(hostname: string, onToken: (token: string) => boolean | null, hostnameFromIndex: number): boolean | null {
  50. const tokens = hostname.split('.');
  51. const l = tokens.length - 1;
  52. // we are at the first of hostname, no splitor there
  53. let token = '';
  54. for (let i = l; i >= hostnameFromIndex; i--) {
  55. token = tokens[i];
  56. if (token.length > 0) {
  57. const t = onToken(token);
  58. if (t === null) {
  59. return null;
  60. }
  61. // if the callback returns true, we should skip the rest
  62. if (t) {
  63. return true;
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. interface FindSingleChildLeafResult<Meta> {
  70. node: TrieNode<Meta>,
  71. toPrune: TrieNode<Meta> | null,
  72. tokenToPrune: string | null,
  73. parent: TrieNode<Meta>
  74. }
  75. abstract class Triebase<Meta = unknown> {
  76. protected readonly $root: TrieNode<Meta> = createNode();
  77. protected $size = 0;
  78. get root() {
  79. return this.$root;
  80. }
  81. constructor(from?: string[] | Set<string> | null) {
  82. // Actually build trie
  83. if (Array.isArray(from)) {
  84. for (let i = 0, l = from.length; i < l; i++) {
  85. this.add(from[i]);
  86. }
  87. } else if (from) {
  88. from.forEach((value) => this.add(value));
  89. }
  90. }
  91. public abstract add(suffix: string, includeAllSubdomain?: boolean, meta?: Meta, hostnameFromIndex?: number): void;
  92. protected walkIntoLeafWithTokens(
  93. tokens: string[],
  94. onLoop: (node: TrieNode, parent: TrieNode, token: string) => void = noop
  95. ) {
  96. let node: TrieNode = this.$root;
  97. let parent: TrieNode = node;
  98. let token: string;
  99. let child: Map<string, TrieNode<Meta>> = node[2];
  100. // reverse lookup from end to start
  101. for (let i = tokens.length - 1; i >= 0; i--) {
  102. token = tokens[i];
  103. // if (token === '') {
  104. // break;
  105. // }
  106. parent = node;
  107. child = node[2];
  108. // cache node index access is 20% faster than direct access when doing twice
  109. if (child.has(token)) {
  110. node = child.get(token)!;
  111. } else {
  112. return null;
  113. }
  114. onLoop(node, parent, token);
  115. }
  116. return { node, parent };
  117. };
  118. protected walkIntoLeafWithSuffix(
  119. suffix: string,
  120. hostnameFromIndex: number,
  121. onLoop: (node: TrieNode, parent: TrieNode, token: string) => void = noop
  122. ) {
  123. let node: TrieNode = this.$root;
  124. let parent: TrieNode = node;
  125. let child: Map<string, TrieNode<Meta>> = node[2];
  126. const onToken = (token: string) => {
  127. // if (token === '') {
  128. // return true;
  129. // }
  130. parent = node;
  131. child = node[2];
  132. if (child.has(token)) {
  133. node = child.get(token)!;
  134. } else {
  135. return null;
  136. }
  137. onLoop(node, parent, token);
  138. return false;
  139. };
  140. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex) === null) {
  141. return null;
  142. }
  143. return { node, parent };
  144. };
  145. public contains(suffix: string, includeAllSubdomain = suffix[0] === '.'): boolean {
  146. const hostnameFromIndex = suffix[0] === '.' ? 1 : 0;
  147. const res = this.walkIntoLeafWithSuffix(suffix, hostnameFromIndex);
  148. if (!res) return false;
  149. if (includeAllSubdomain) return getBit(res.node[0], INCLUDE_ALL_SUBDOMAIN);
  150. return true;
  151. };
  152. private static bfsResults: [node: TrieNode | null, suffix: string[]] = [null, []];
  153. private static dfs<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
  154. const node = nodeStack.pop()!;
  155. const suffix = suffixStack.pop()!;
  156. node[2].forEach((childNode, k) => {
  157. // Pushing the child node to the stack for next iteration of DFS
  158. nodeStack.push(childNode);
  159. suffixStack.push([k, ...suffix]);
  160. });
  161. Triebase.bfsResults[0] = node;
  162. Triebase.bfsResults[1] = suffix;
  163. return Triebase.bfsResults;
  164. }
  165. private static dfsWithSort<Meta>(this: void, nodeStack: Array<TrieNode<Meta>>, suffixStack: string[][]) {
  166. const node = nodeStack.pop()!;
  167. const suffix = suffixStack.pop()!;
  168. const child = node[2];
  169. if (child.size) {
  170. const keys = Array.from(child.keys()).sort(Triebase.compare);
  171. for (let i = 0, l = keys.length; i < l; i++) {
  172. const key = keys[i];
  173. const childNode = child.get(key)!;
  174. // Pushing the child node to the stack for next iteration of DFS
  175. nodeStack.push(childNode);
  176. suffixStack.push([key, ...suffix]);
  177. }
  178. }
  179. Triebase.bfsResults[0] = node;
  180. Triebase.bfsResults[1] = suffix;
  181. return Triebase.bfsResults;
  182. }
  183. private walk(
  184. onMatches: (suffix: string[], subdomain: boolean, meta: Meta) => void,
  185. initialNode = this.$root,
  186. initialSuffix: string[] = [],
  187. withSort = false
  188. ) {
  189. const bfsImpl = withSort ? Triebase.dfsWithSort : Triebase.dfs;
  190. const nodeStack: Array<TrieNode<Meta>> = [];
  191. nodeStack.push(initialNode);
  192. // Resolving initial string (begin the start of the stack)
  193. const suffixStack: string[][] = [];
  194. suffixStack.push(initialSuffix);
  195. let node: TrieNode<Meta> = initialNode;
  196. let r;
  197. do {
  198. r = bfsImpl(nodeStack, suffixStack);
  199. node = r[0]!;
  200. const suffix = r[1];
  201. // If the node is a sentinel, we push the suffix to the results
  202. if (getBit(node[0], START)) {
  203. onMatches(suffix, getBit(node[0], INCLUDE_ALL_SUBDOMAIN), node[3]);
  204. }
  205. } while (nodeStack.length);
  206. };
  207. static compare(this: void, a: string, b: string) {
  208. if (a === b) return 0;
  209. return (a.length - b.length) || fastStringCompare(a, b);
  210. }
  211. private walkWithSort(
  212. onMatches: (suffix: string[], subdomain: boolean, meta: Meta) => void,
  213. initialNode = this.$root,
  214. initialSuffix: string[] = []
  215. ) {
  216. const nodeStack: Array<TrieNode<Meta>> = [];
  217. nodeStack.push(initialNode);
  218. // Resolving initial string (begin the start of the stack)
  219. const suffixStack: string[][] = [];
  220. suffixStack.push(initialSuffix);
  221. let node: TrieNode<Meta> = initialNode;
  222. let child: Map<string, TrieNode<Meta>> = node[2];
  223. do {
  224. node = nodeStack.pop()!;
  225. const suffix = suffixStack.pop()!;
  226. child = node[2];
  227. if (child.size) {
  228. const keys = Array.from(child.keys()).sort(Triebase.compare);
  229. for (let i = 0, l = keys.length; i < l; i++) {
  230. const key = keys[i];
  231. const childNode = child.get(key)!;
  232. // Pushing the child node to the stack for next iteration of DFS
  233. nodeStack.push(childNode);
  234. suffixStack.push([key, ...suffix]);
  235. }
  236. }
  237. // If the node is a sentinel, we push the suffix to the results
  238. if (getBit(node[0], START)) {
  239. onMatches(suffix, getBit(node[0], INCLUDE_ALL_SUBDOMAIN), node[3]);
  240. }
  241. } while (nodeStack.length);
  242. };
  243. protected getSingleChildLeaf(tokens: string[]): FindSingleChildLeafResult<Meta> | null {
  244. let toPrune: TrieNode | null = null;
  245. let tokenToPrune: string | null = null;
  246. const onLoop = (node: TrieNode, parent: TrieNode, token: string) => {
  247. // Keeping track of a potential branch to prune
  248. const child = node[2];
  249. // console.log({
  250. // child, parent, token
  251. // });
  252. // console.log(this.inspect(0));
  253. if (toPrune !== null) { // the most near branch that could potentially being pruned
  254. if (child.size > 1) {
  255. // The branch has some children, the branch need retain.
  256. // And we need to abort prune that parent branch, so we set it to null
  257. toPrune = null;
  258. tokenToPrune = null;
  259. }
  260. } else if (child.size < 1) {
  261. // There is only one token child, or no child at all, we can prune it safely
  262. // It is now the top-est branch that could potentially being pruned
  263. toPrune = parent;
  264. tokenToPrune = token;
  265. }
  266. };
  267. const res = this.walkIntoLeafWithTokens(tokens, onLoop);
  268. if (res === null) return null;
  269. return { node: res.node, toPrune, tokenToPrune, parent: res.parent };
  270. };
  271. /**
  272. * Method used to retrieve every item in the trie with the given prefix.
  273. */
  274. public find(
  275. inputSuffix: string,
  276. subdomainOnly = inputSuffix[0] === '.',
  277. hostnameFromIndex = inputSuffix[0] === '.' ? 1 : 0
  278. // /** @default true */ includeEqualWithSuffix = true
  279. ): string[] {
  280. const inputTokens = hostnameToTokens(inputSuffix, hostnameFromIndex);
  281. const res = this.walkIntoLeafWithTokens(inputTokens);
  282. if (res === null) return [];
  283. const results: string[] = [];
  284. const onMatches = subdomainOnly
  285. ? (suffix: string[], subdomain: boolean) => { // fast path (default option)
  286. const d = fastStringArrayJoin(suffix, '.');
  287. if (!subdomain && subStringEqual(inputSuffix, d, 1)) return;
  288. results.push(subdomain ? '.' + d : d);
  289. }
  290. : (suffix: string[], subdomain: boolean) => { // fast path (default option)
  291. const d = fastStringArrayJoin(suffix, '.');
  292. results.push(subdomain ? '.' + d : d);
  293. };
  294. this.walk(
  295. onMatches,
  296. res.node, // Performing DFS from prefix
  297. inputTokens
  298. );
  299. return results;
  300. };
  301. /**
  302. * Method used to delete a prefix from the trie.
  303. */
  304. public remove(suffix: string): boolean {
  305. const res = this.getSingleChildLeaf(hostnameToTokens(suffix, 0));
  306. if (res === null) return false;
  307. if (missingBit(res.node[0], START)) return false;
  308. this.$size--;
  309. const { node, toPrune, tokenToPrune } = res;
  310. if (tokenToPrune && toPrune) {
  311. toPrune[2].delete(tokenToPrune);
  312. } else {
  313. node[0] = deleteBit(node[0], START);
  314. }
  315. return true;
  316. };
  317. // eslint-disable-next-line @typescript-eslint/unbound-method -- safe
  318. public delete = this.remove;
  319. /**
  320. * Method used to assert whether the given prefix exists in the Trie.
  321. */
  322. public has(suffix: string, includeAllSubdomain = suffix[0] === '.'): boolean {
  323. const hostnameFromIndex = suffix[0] === '.' ? 1 : 0;
  324. const res = this.walkIntoLeafWithSuffix(suffix, hostnameFromIndex);
  325. if (res === null) return false;
  326. if (missingBit(res.node[0], START)) return false;
  327. if (includeAllSubdomain) return getBit(res.node[0], INCLUDE_ALL_SUBDOMAIN);
  328. return true;
  329. };
  330. public dumpWithoutDot(onSuffix: (suffix: string, subdomain: boolean) => void, withSort = false) {
  331. const handleSuffix = (suffix: string[], subdomain: boolean) => {
  332. onSuffix(fastStringArrayJoin(suffix, '.'), subdomain);
  333. };
  334. if (withSort) {
  335. this.walkWithSort(handleSuffix);
  336. } else {
  337. this.walk(handleSuffix);
  338. }
  339. }
  340. public dump(onSuffix: (suffix: string) => void, withSort?: boolean): void;
  341. public dump(onSuffix?: null, withSort?: boolean): string[];
  342. public dump(onSuffix?: ((suffix: string) => void) | null, withSort = false): string[] | void {
  343. const results: string[] = [];
  344. const handleSuffix = onSuffix
  345. ? (suffix: string[], subdomain: boolean) => {
  346. const d = fastStringArrayJoin(suffix, '.');
  347. onSuffix(subdomain ? '.' + d : d);
  348. }
  349. : (suffix: string[], subdomain: boolean) => {
  350. const d = fastStringArrayJoin(suffix, '.');
  351. results.push(subdomain ? '.' + d : d);
  352. };
  353. if (withSort) {
  354. this.walkWithSort(handleSuffix);
  355. } else {
  356. this.walk(handleSuffix);
  357. }
  358. return results;
  359. };
  360. public dumpMeta(onMeta: (meta: Meta) => void, withSort?: boolean): void;
  361. public dumpMeta(onMeta?: null, withSort?: boolean): Meta[];
  362. public dumpMeta(onMeta?: ((meta: Meta) => void) | null, withSort = false): Meta[] | void {
  363. const results: Meta[] = [];
  364. const handleMeta = onMeta
  365. ? (_suffix: string[], _subdomain: boolean, meta: Meta) => onMeta(meta)
  366. : (_suffix: string[], _subdomain: boolean, meta: Meta) => results.push(meta);
  367. if (withSort) {
  368. this.walkWithSort(handleMeta);
  369. } else {
  370. this.walk(handleMeta);
  371. }
  372. return results;
  373. };
  374. public dumpWithMeta(onSuffix: (suffix: string, meta: Meta | undefined) => void, withSort?: boolean): void;
  375. public dumpWithMeta(onMeta?: null, withSort?: boolean): Array<[string, Meta | undefined]>;
  376. public dumpWithMeta(onSuffix?: ((suffix: string, meta: Meta | undefined) => void) | null, withSort = false): Array<[string, Meta | undefined]> | void {
  377. const results: Array<[string, Meta | undefined]> = [];
  378. const handleSuffix = onSuffix
  379. ? (suffix: string[], subdomain: boolean, meta: Meta | undefined) => {
  380. const d = fastStringArrayJoin(suffix, '.');
  381. return onSuffix(subdomain ? '.' + d : d, meta);
  382. }
  383. : (suffix: string[], subdomain: boolean, meta: Meta | undefined) => {
  384. const d = fastStringArrayJoin(suffix, '.');
  385. results.push([subdomain ? '.' + d : d, meta]);
  386. };
  387. if (withSort) {
  388. this.walkWithSort(handleSuffix);
  389. } else {
  390. this.walk(handleSuffix);
  391. }
  392. return results;
  393. };
  394. public inspect(depth: number, unpackMeta?: (meta?: Meta) => any) {
  395. return fastStringArrayJoin(
  396. JSON.stringify(deepTrieNodeToJSON(this.$root, unpackMeta), null, 2).split('\n').map((line) => ' '.repeat(depth) + line),
  397. '\n'
  398. );
  399. }
  400. public [util.inspect.custom](depth: number) {
  401. return this.inspect(depth);
  402. };
  403. public merge(trie: Triebase<Meta>) {
  404. const handleSuffix = (suffix: string[], subdomain: boolean, meta: Meta) => {
  405. this.add(fastStringArrayJoin(suffix, '.'), subdomain, meta);
  406. };
  407. trie.walk(handleSuffix);
  408. return this;
  409. }
  410. }
  411. export class HostnameSmolTrie<Meta = unknown> extends Triebase<Meta> {
  412. public smolTree = true;
  413. add(suffix: string, includeAllSubdomain = suffix[0] === '.', meta?: Meta, hostnameFromIndex = suffix[0] === '.' ? 1 : 0): void {
  414. let node: TrieNode<Meta> = this.$root;
  415. let curNodeChildren: Map<string, TrieNode<Meta>> = node[2];
  416. const onToken = (token: string) => {
  417. curNodeChildren = node[2];
  418. if (curNodeChildren.has(token)) {
  419. node = curNodeChildren.get(token)!;
  420. // During the adding of `[start]blog|.skk.moe` and find out that there is a `[start].skk.moe` in the trie, skip adding the rest of the node
  421. if (getBit(node[0], INCLUDE_ALL_SUBDOMAIN)) {
  422. return true;
  423. }
  424. } else {
  425. const newNode = createNode(node);
  426. curNodeChildren.set(token, newNode);
  427. node = newNode;
  428. }
  429. return false;
  430. };
  431. // When walkHostnameTokens returns true, we should skip the rest
  432. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex)) {
  433. return;
  434. }
  435. // If we are in smolTree mode, we need to do something at the end of the loop
  436. if (includeAllSubdomain) {
  437. // Trying to add `[.]sub.example.com` where there is already a `blog.sub.example.com` in the trie
  438. // Make sure parent `[start]sub.example.com` (without dot) is removed (SETINEL to false)
  439. // (/** parent */ node[2]!)[0] = false;
  440. // Removing the rest of the parent's child nodes
  441. node[2].clear();
  442. // The SENTINEL of this node will be set to true at the end of the function, so we don't need to set it here
  443. // we can use else-if here, because the children is now empty, we don't need to check the leading "."
  444. } else if (getBit(node[0], INCLUDE_ALL_SUBDOMAIN)) {
  445. // Trying to add `example.com` when there is already a `.example.com` in the trie
  446. // No need to increment size and set SENTINEL to true (skip this "new" item)
  447. return;
  448. }
  449. node[0] = setBit(node[0], START);
  450. if (includeAllSubdomain) {
  451. node[0] = setBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  452. } else {
  453. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  454. }
  455. node[3] = meta!;
  456. }
  457. public whitelist(suffix: string, includeAllSubdomain = suffix[0] === '.', hostnameFromIndex = suffix[0] === '.' ? 1 : 0) {
  458. const tokens = hostnameToTokens(suffix, hostnameFromIndex);
  459. const res = this.getSingleChildLeaf(tokens);
  460. if (res === null) return;
  461. const { node, toPrune, tokenToPrune } = res;
  462. // Trying to whitelist `[start].sub.example.com` where there might already be a `[start]blog.sub.example.com` in the trie
  463. if (includeAllSubdomain) {
  464. // If there is a `[start]sub.example.com` here, remove it
  465. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  466. node[0] = deleteBit(node[0], START);
  467. // Removing all the child nodes by empty the children
  468. node[2].clear();
  469. } else {
  470. // Trying to whitelist `example.com` when there is already a `.example.com` in the trie
  471. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  472. }
  473. // return early if not found
  474. if (missingBit(node[0], START)) return;
  475. if (toPrune && tokenToPrune) {
  476. toPrune[2].delete(tokenToPrune);
  477. } else {
  478. node[0] = deleteBit(node[0], START);
  479. }
  480. };
  481. }
  482. export class HostnameTrie<Meta = unknown> extends Triebase<Meta> {
  483. get size() {
  484. return this.$size;
  485. }
  486. add(suffix: string, includeAllSubdomain = suffix[0] === '.', meta?: Meta, hostnameFromIndex = suffix[0] === '.' ? 1 : 0): void {
  487. let node: TrieNode<Meta> = this.$root;
  488. let child: Map<string, TrieNode<Meta>> = node[2];
  489. const onToken = (token: string) => {
  490. child = node[2];
  491. if (child.has(token)) {
  492. node = child.get(token)!;
  493. } else {
  494. const newNode = createNode(node);
  495. child.set(token, newNode);
  496. node = newNode;
  497. }
  498. return false;
  499. };
  500. // When walkHostnameTokens returns true, we should skip the rest
  501. if (walkHostnameTokens(suffix, onToken, hostnameFromIndex)) {
  502. return;
  503. }
  504. // if same entry has been added before, skip
  505. if (getBit(node[0], START)) {
  506. return;
  507. }
  508. this.$size++;
  509. node[0] = setBit(node[0], START);
  510. if (includeAllSubdomain) {
  511. node[0] = setBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  512. } else {
  513. node[0] = deleteBit(node[0], INCLUDE_ALL_SUBDOMAIN);
  514. }
  515. node[3] = meta!;
  516. }
  517. }
  518. // function deepEqualArray(a: string[], b: string[]) {
  519. // let len = a.length;
  520. // if (len !== b.length) return false;
  521. // while (len--) {
  522. // if (a[len] !== b[len]) return false;
  523. // }
  524. // return true;
  525. // };
  526. function subStringEqual(needle: string, haystack: string, needleIndex = 0) {
  527. for (let i = 0, l = haystack.length; i < l; i++) {
  528. if (needle[i + needleIndex] !== haystack[i]) return false;
  529. }
  530. return true;
  531. }