( index: NameSubstringIndex, substring: string, kinds: readonly string[], excludePrefix: boolean, limit: number, order: ScanOrder, )
| 181 | * kind it doesn't hold). `order` is the order the plan reads rows in. |
| 182 | */ |
| 183 | export function searchNameSubstring( |
| 184 | index: NameSubstringIndex, |
| 185 | substring: string, |
| 186 | kinds: readonly string[], |
| 187 | excludePrefix: boolean, |
| 188 | limit: number, |
| 189 | order: ScanOrder, |
| 190 | ): number[] | null { |
| 191 | const needle = asciiLower(substring); |
| 192 | if (needle.length === 0 || needle.includes('%') || needle.includes(SEPARATOR)) return null; |
| 193 | const wanted = new Uint8Array(index.kinds.length); |
| 194 | for (const kind of kinds) { |
| 195 | const code = index.kinds.indexOf(kind); |
| 196 | if (code < 0) return null; |
| 197 | wanted[code] = 1; |
| 198 | } |
| 199 | |
| 200 | const { hay, starts, nameEntryStart, nameEntries, entryKind, entryLength } = index; |
| 201 | const matcher = patternMatcher(needle); |
| 202 | const hits: number[] = []; |
| 203 | for (let at = matcher.next(hay, 0); at >= 0; ) { |
| 204 | const name = nameAt(starts, at); |
| 205 | if (!(excludePrefix && matcher.startsAt(hay, starts[name]!))) { |
| 206 | for (let k = nameEntryStart[name]!; k < nameEntryStart[name + 1]!; k++) { |
| 207 | const entry = nameEntries[k]!; |
| 208 | if (wanted[entryKind[entry]!]) hits.push(entry); |
| 209 | } |
| 210 | } |
| 211 | at = matcher.next(hay, starts[name + 1]!); |
| 212 | } |
| 213 | // Entry numbers follow the kind index, rowids the table: either breaks |
| 214 | // length ties the way SQLite's plan does. |
| 215 | const rowid = index.entryRowid; |
| 216 | hits.sort(order === 'index' |
| 217 | ? (a, b) => (entryLength[a]! - entryLength[b]!) || (a - b) |
| 218 | : (a, b) => (entryLength[a]! - entryLength[b]!) || (rowid[a]! - rowid[b]!)); |
| 219 | // A negative LIMIT is no limit in SQLite. |
| 220 | if (limit >= 0 && hits.length > limit) hits.length = limit; |
| 221 | return hits.map((entry) => index.entryRowid[entry]!); |
| 222 | } |
no test coverage detected