diff --git a/src/renderer/src/lib/palette-match/palette-assignment-ranking.ts b/src/renderer/src/lib/palette-match/palette-assignment-ranking.ts index 64b9bb76b7a..202beb7c84b 100644 --- a/src/renderer/src/lib/palette-match/palette-assignment-ranking.ts +++ b/src/renderer/src/lib/palette-match/palette-assignment-ranking.ts @@ -34,10 +34,23 @@ function phrasePlacement(field: PaletteIndexedField, normalizedQuery: string): n if (text.startsWith(normalizedQuery)) { return 0 } - for (const word of field.words) { - if (text.startsWith(normalizedQuery, word.start)) { + // Merge the ordered occurrence and word-start streams: occurrences only count at word + // starts, so each side advances monotonically and never rescans the other. + let index = text.indexOf(normalizedQuery, 1) + let wordIndex = 0 + while (index !== -1) { + let wordStart = field.words[wordIndex]?.start + while (wordStart !== undefined && wordStart < index) { + wordIndex += 1 + wordStart = field.words[wordIndex]?.start + } + if (wordStart === undefined) { + break + } + if (wordStart === index) { return 1 } + index = text.indexOf(normalizedQuery, wordStart) } return 2 }