Implement exact-boundary match type
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #3963
authored by Junegunn Choi · committed by Junegunn Choi · parent e8a6909 · Browse files · 3 files·+38 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+38 added−11 removed
- other:+38 added−11 removed
Provenance
- Commit
6a67712- Tree
90e9c6c- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
e8a6909
3 files changed+38 added−11 removed
modified:src/algo/algo.go+18 added-1 removed
796 unmodified lines797 // bonus point, instead of stopping immediately after finding the first match.798 // The solution is much cheaper since there is only one possible alignment of799 // the pattern.800 func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {801+ return exactMatchNaive(caseSensitive, normalize, forward, false, text, pattern, withPos, slab)802+}803+804+func ExactMatchBoundary(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {805+ return exactMatchNaive(caseSensitive, normalize, forward, true, text, pattern, withPos, slab)806+}807+808+func exactMatchNaive(caseSensitive bool, normalize bool, forward bool, boundaryCheck bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {809 if len(pattern) == 0 {810 return Result{0, 0, 0}, nil811 }812 26 unmodified lines839 char = normalizeRune(char)840 }841 pidx_ := indexAt(pidx, lenPattern, forward)842 pchar := pattern[pidx_]835- if pchar == char {843+ ok := pchar == char844+ if ok {845 if pidx_ == 0 {846 bonus = bonusAt(text, index_)847 }848+ if boundaryCheck {849+ ok = bonus >= bonusBoundary850+ if ok && pidx_ == len(pattern)-1 {851+ ok = index_ == lenRunes-1 || charClassOf(text.Get(index_+1)) <= charDelimiter852+ }853+ }854+ }855+ if ok {856 pidx++857 if pidx == lenPattern {858 if bonus > bestBonus {859 bestPos, bestBonus = index, bonus142 unmodified linesmodified:src/pattern.go+10 added-1 removed
21 unmodified lines22 23 const (24 termFuzzy termType = iota25 termExact26+ termExactBoundary27 termPrefix28 termSuffix29 termEqual30 )116 unmodified lines147 ptr.cacheKey = ptr.buildCacheKey()148 ptr.procFun[termFuzzy] = fuzzyAlgo149 ptr.procFun[termEqual] = algo.EqualMatch150 ptr.procFun[termExact] = algo.ExactMatchNaive151+ ptr.procFun[termExactBoundary] = algo.ExactMatchBoundary152 ptr.procFun[termPrefix] = algo.PrefixMatch153 ptr.procFun[termSuffix] = algo.SuffixMatch154 155 patternCache[asString] = ptr38 unmodified lines194 typ = termSuffix195 text = text[:len(text)-1]196 }197 198+ if fuzzy && len(text) > 2 && strings.HasPrefix(text, "'") && strings.HasSuffix(text, "'") ||199+ !fuzzy && !strings.HasPrefix(text, "'") && strings.HasSuffix(text, "'") {200+ typ = termExactBoundary201+ if fuzzy {202+ text = text[1:]203+ }204+ text = text[:len(text)-1]196- if strings.HasPrefix(text, "'") {205+ } else if strings.HasPrefix(text, "'") {206 // Flip exactness207 if fuzzy && !inv {208 typ = termExact209 } else {214 unmodified linesmodified:README.md+10 added-9 removed
374 unmodified lines375 Unless otherwise specified, fzf starts in "extended-search mode" where you can376 type in multiple search terms delimited by spaces. e.g. `^music .mp3$ sbtrkt377 !fire`378 379-| Token | Match type | Description |380-| --------- | -------------------------- | ------------------------------------ |381-| `sbtrkt` | fuzzy-match | Items that match `sbtrkt` |382-| `'wild` | exact-match (quoted) | Items that include `wild` |383-| `^music` | prefix-exact-match | Items that start with `music` |384-| `.mp3$` | suffix-exact-match | Items that end with `.mp3` |385-| `!fire` | inverse-exact-match | Items that do not include `fire` |386-| `!^music` | inverse-prefix-exact-match | Items that do not start with `music` |387-| `!.mp3$` | inverse-suffix-exact-match | Items that do not end with `.mp3` |379+| Token | Match type | Description |380+| --------- | -------------------------------------- | ------------------------------------------ |381+| `sbtrkt` | fuzzy-match | Items that match `sbtrkt` |382+| `'wild` | exact-match (quoted) | Items that include `wild` |383+| `'wild'` | exact-boundary-match (quoted both ends) | Items that include `wild` at word boundaries |384+| `^music` | prefix-exact-match | Items that start with `music` |385+| `.mp3$` | suffix-exact-match | Items that end with `.mp3` |386+| `!fire` | inverse-exact-match | Items that do not include `fire` |387+| `!^music` | inverse-prefix-exact-match | Items that do not start with `music` |388+| `!.mp3$` | inverse-suffix-exact-match | Items that do not end with `.mp3` |389 390 If you don't prefer fuzzy matching and do not wish to "quote" every word,391 start fzf with `-e` or `--exact` option. Note that when `--exact` is set,392 `'`-prefix "unquotes" the term.517 unmodified lines