-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.ts
More file actions
69 lines (66 loc) · 2.47 KB
/
Copy pathsearch.ts
File metadata and controls
69 lines (66 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @file `search` — like `String.prototype.search` but with a configurable
* starting index. The native `RegExp.exec` + `lastIndex` dance handles this,
* but with side-effect risk on shared regexes. This wrapper offers a clean,
* side-effect-free version.
*/
import { MathMax } from '../primordials/math'
import {
StringPrototypeSearch,
StringPrototypeSlice,
} from '../primordials/string'
import type { SearchOptions } from './types'
/**
* Search for a regular expression in a string starting from an index.
*
* Similar to `String.prototype.search()` but allows specifying a starting
* position. Returns the index of the first match at or after `fromIndex`, or -1
* if no match is found. Negative `fromIndex` values count back from the end of
* the string.
*
* Equivalent to `str.slice(fromIndex).search(regexp)` with the result mapped
* back onto the original string, so `regexp` sees `fromIndex` as the start of
* input: `^` anchors there and lookbehind cannot reach the characters before
* it. Those are the semantics that make the call side-effect free, and they
* differ from seeking a shared regexp's `lastIndex` to `fromIndex`.
*
* Searching a substring allocates one per call. On a hot path matching a
* single character or a character class, scan char codes instead of calling
* this.
*
* @example
* ;```ts
* search('hello world hello', /hello/, { fromIndex: 0 }) // 0
* search('hello world hello', /hello/, { fromIndex: 6 }) // 12
* search('hello world', /goodbye/, { fromIndex: 0 }) // -1
* search('hello world', /hello/, { fromIndex: -5 }) // -1
* ```
*
* @param str - The string to search in.
* @param regexp - The regular expression to search for.
* @param options - Configuration options.
*
* @returns The index of the first match, or -1 if not found
*/
export function search(
str: string,
regexp: RegExp,
options?: SearchOptions | undefined,
): number {
const { fromIndex = 0 } = { __proto__: null, ...options } as SearchOptions
const { length } = str
// `fromIndex === length` is still a valid start: a zero-width pattern
// (e.g. /c?/) can match the empty tail. Only past-the-end is a miss.
if (fromIndex > length) {
return -1
}
if (fromIndex === 0) {
return StringPrototypeSearch(str, regexp)
}
const offset = fromIndex < 0 ? MathMax(length + fromIndex, 0) : fromIndex
const result = StringPrototypeSearch(
StringPrototypeSlice(str, offset),
regexp,
)
return result === -1 ? -1 : result + offset
}