The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // A typed straight apostrophe finds a curly one: iOS keyboards type "’". |
| 2 | const foldCharacters = (text: string) => text.replace(/ς/g, "σ").replace(/[‘’]/g, "'"); |
| 3 | |
| 4 | function foldThreadSearchCase(text: string): string { |
| 5 | const lowered = foldCharacters(text.toLowerCase()); |
| 6 | if (lowered.length === text.length) return lowered; |
| 7 | |
| 8 | let folded = ""; |
| 9 | for (const character of text) { |
| 10 | // Lowercasing İ adds a combining dot. Drop that expansion to preserve source offsets. |
| 11 | const loweredCharacter = foldCharacters(character.toLowerCase()).replace("i\u0307", "i"); |
| 12 | folded += loweredCharacter.length === character.length ? loweredCharacter : character; |
| 13 | } |
| 14 | return folded; |
| 15 | } |
| 16 | |
| 17 | function scanThreadSearchOccurrences(text: string, query: string, offsets?: number[]): number { |
| 18 | if (text.length === 0 || query.length === 0) return 0; |
| 19 | |
| 20 | const foldedText = foldThreadSearchCase(text); |
| 21 | const foldedQuery = foldThreadSearchCase(query); |
| 22 | let count = 0; |
| 23 | let cursor = 0; |
| 24 | |
| 25 | while (cursor <= foldedText.length - foldedQuery.length) { |
| 26 | const offset = foldedText.indexOf(foldedQuery, cursor); |
| 27 | if (offset === -1) break; |
| 28 | offsets?.push(offset); |
| 29 | count += 1; |
| 30 | cursor = offset + foldedQuery.length; |
| 31 | } |
| 32 | return count; |
| 33 | } |
| 34 | |
| 35 | export function countThreadSearchOccurrences(text: string, query: string): number { |
| 36 | return scanThreadSearchOccurrences(text, query); |
| 37 | } |
| 38 | |
| 39 | export function findThreadSearchOccurrences(text: string, query: string): number[] { |
| 40 | const offsets: number[] = []; |
| 41 | scanThreadSearchOccurrences(text, query, offsets); |
| 42 | return offsets; |
| 43 | } |