* A matcher for a LIKE pattern without `%`: where the next match at or after * `from` starts, and whether one starts exactly at `at`. Without `_` this is a * plain string search; each `_` matches any one character but the separator.
(needle: string)
| 143 | * plain string search; each `_` matches any one character but the separator. |
| 144 | */ |
| 145 | function patternMatcher(needle: string): { next(hay: string, from: number): number; startsAt(hay: string, at: number): boolean } { |
| 146 | if (!needle.includes('_')) { |
| 147 | return { |
| 148 | next: (hay, from) => hay.indexOf(needle, from), |
| 149 | startsAt: (hay, at) => hay.startsWith(needle, at), |
| 150 | }; |
| 151 | } |
| 152 | // `u`, so a wildcard takes a whole character, as SQLite's `_` does. |
| 153 | const source = needle |
| 154 | .split('_') |
| 155 | .map((part) => part.replace(/[\^$\\.*+?()[\]{}|/]/g, '\\$&')) |
| 156 | .join('[^\\u0000]'); |
| 157 | const anywhere = new RegExp(source, 'gu'); |
| 158 | const anchored = new RegExp(source, 'uy'); |
| 159 | return { |
| 160 | next(hay, from) { |
| 161 | anywhere.lastIndex = from; |
| 162 | const match = anywhere.exec(hay); |
| 163 | return match ? match.index : -1; |
| 164 | }, |
| 165 | startsAt(hay, at) { |
| 166 | anchored.lastIndex = at; |
| 167 | return anchored.test(hay); |
| 168 | }, |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * The order SQLite's plan reads candidate rows in, which its ORDER BY keeps |
no test coverage detected