@@ -10,6 +10,7 @@ const UPPERCASE = /\p{Lu}/u; |
10 | 10 | const DIGIT = /\p{N}/u; |
11 | 11 | const LETTER = /\p{L}/u; |
12 | 12 | const COMBINING_MARKS = /\p{M}/gu; |
| 13 | +const DOT_FROM_LOWERCASED_I = /(?<=i)\u0307/g; |
13 | 14 | // Emoji and other symbols can be searched for, so they start words too. |
14 | 15 | const SYMBOL = /\p{So}/u; |
15 | 16 | const APOSTROPHE = /['’]/; |
@@ -832,34 +833,47 @@ function shortestSpan(places: ReadonlyArray<ReadonlyArray<number>>): { |
832 | 833 | */ |
833 | 834 | export function findSearchTokenRanges(text: string, query: string): SearchRange[] { |
834 | 835 | const { long, words, exact } = messageSearchTokens(query); |
| 836 | + // Each word also without the dot lowercasing "İ" adds, which the text, |
| 837 | + // folded in place, does not have. Other marks stay: dropping a Hindi vowel |
| 838 | + // sign would leave a lone consonant lighting up word starts. |
| 839 | + const undotted = (word: string) => word.replace(DOT_FROM_LOWERCASED_I, ""); |
835 | 840 | // An exact query lights up only where its words are written together. |
836 | | - const phrase = exact |
837 | | - ? wordsTogether( |
838 | | - words.map((word) => foldInPlace(word)), |
839 | | - true, |
840 | | - ) |
841 | | - : null; |
842 | | - if (phrase) { |
| 841 | + if (exact && words.length > 1) { |
| 842 | + const folded = words.map((word) => foldInPlace(word)); |
| 843 | + const bare = folded.map(undotted); |
843 | 844 | const searched = foldInPlace(text); |
844 | 845 | const ranges: SearchRange[] = []; |
845 | | - for ( |
846 | | - let found = findExactPhrase(text, searched, phrase); |
847 | | - found !== null; |
848 | | - found = findExactPhrase(text, searched, phrase, found.end) |
849 | | - ) { |
850 | | - ranges.push(found); |
| 846 | + for (const phraseWords of bare.join(" ") === folded.join(" ") ? [folded] : [folded, bare]) { |
| 847 | + const phrase = wordsTogether(phraseWords, true)!; |
| 848 | + for ( |
| 849 | + let found = findExactPhrase(text, searched, phrase); |
| 850 | + found !== null; |
| 851 | + found = findExactPhrase(text, searched, phrase, found.end) |
| 852 | + ) { |
| 853 | + ranges.push(found); |
| 854 | + } |
851 | 855 | } |
852 | | - return ranges; |
| 856 | + // Overlaps go, longest first; touching matches stay apart, so find opens |
| 857 | + // on the first one alone. |
| 858 | + const kept: SearchRange[] = []; |
| 859 | + for (const range of ranges.sort( |
| 860 | + (left, right) => left.start - right.start || right.end - left.end, |
| 861 | + )) { |
| 862 | + if (range.start >= (kept.at(-1)?.end ?? 0)) kept.push(range); |
| 863 | + } |
| 864 | + return kept; |
853 | 865 | } |
854 | | - const tokens = new Map( |
855 | | - long |
856 | | - .map( |
857 | | - (token) => |
858 | | - [foldQueryWords(token), Array.from(token).length < MESSAGE_WORD_START_LENGTH] as const, |
859 | | - ) |
| 866 | + // Each folded token, and whether it must start a word; a spelling any token |
| 867 | + // allows mid-word lights up mid-word. |
| 868 | + const tokens = new Map<string, boolean>(); |
| 869 | + for (const token of long) { |
| 870 | + const short = Array.from(token).length < MESSAGE_WORD_START_LENGTH; |
| 871 | + const word = foldQueryWords(token); |
| 872 | + for (const key of [word, undotted(word)]) { |
860 | 873 | // Some tokens fold to nothing ("´´´"), which would match everywhere. |
861 | | - .filter(([token]) => token.length > 0), |
862 | | - ); |
| 874 | + if (key.length > 0) tokens.set(key, short && (tokens.get(key) ?? true)); |
| 875 | + } |
| 876 | + } |
863 | 877 | if (tokens.size === 0) return []; |
864 | 878 | const field = foldSearchField(text); |
865 | 879 | const ranges: SearchRange[] = []; |
|