Skip to content
Myr

Fix #344 - Backward scan when `--tiebreak=end`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent 9017e29 · Browse files · 6 files+112 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+112 added−58 removed

  • tests:+11 added−0 removed
  • other:+101 added−58 removed

6 files+112 added−58 removed

modified:src/algo/algo_test.go+37 added-19 removed
3 unmodified lines
4 "strings"5 "testing"6)78func assertMatch(t *testing.T, fun func(bool, []rune, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {8func assertMatch(t *testing.T, fun func(bool, bool, []rune, []rune) (int, int), caseSensitive bool, forward bool, input string, pattern string, sidx int, eidx int) {9 if !caseSensitive {10 pattern = strings.ToLower(pattern)11 }12 s, e := fun(caseSensitive, []rune(input), []rune(pattern))12 s, e := fun(caseSensitive, forward, []rune(input), []rune(pattern))13 if s != sidx {14 t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)15 }16 if e != eidx {
1 unmodified line
18 }19}2021func TestFuzzyMatch(t *testing.T) {22 assertMatch(t, FuzzyMatch, false, "fooBarbaz", "oBZ", 2, 9)23 assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBZ", -1, -1)24 assertMatch(t, FuzzyMatch, true, "fooBarbaz", "oBz", 2, 9)25 assertMatch(t, FuzzyMatch, true, "fooBarbaz", "fooBarbazz", -1, -1)22 assertMatch(t, FuzzyMatch, false, true, "fooBarbaz", "oBZ", 2, 9)23 assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBZ", -1, -1)24 assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "oBz", 2, 9)25 assertMatch(t, FuzzyMatch, true, true, "fooBarbaz", "fooBarbazz", -1, -1)26}2728func TestFuzzyMatchBackward(t *testing.T) {29 assertMatch(t, FuzzyMatch, false, true, "foobar fb", "fb", 0, 4)30 assertMatch(t, FuzzyMatch, false, false, "foobar fb", "fb", 7, 9)31}3233func TestExactMatchNaive(t *testing.T) {34 for _, dir := range []bool{true, false} {29 assertMatch(t, ExactMatchNaive, false, "fooBarbaz", "oBA", 2, 5)30 assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "oBA", -1, -1)31 assertMatch(t, ExactMatchNaive, true, "fooBarbaz", "fooBarbazz", -1, -1)35 assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5)36 assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1)37 assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1)38 }39}4041func TestExactMatchNaiveBackward(t *testing.T) {42 assertMatch(t, FuzzyMatch, false, true, "foobar foob", "oo", 1, 3)43 assertMatch(t, FuzzyMatch, false, false, "foobar foob", "oo", 8, 10)44}4546func TestPrefixMatch(t *testing.T) {47 for _, dir := range []bool{true, false} {35 assertMatch(t, PrefixMatch, false, "fooBarbaz", "Foo", 0, 3)36 assertMatch(t, PrefixMatch, true, "fooBarbaz", "Foo", -1, -1)37 assertMatch(t, PrefixMatch, false, "fooBarbaz", "baz", -1, -1)48 assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3)49 assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1)50 assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1)51 }52}5354func TestSuffixMatch(t *testing.T) {55 for _, dir := range []bool{true, false} {41 assertMatch(t, SuffixMatch, false, "fooBarbaz", "Foo", -1, -1)42 assertMatch(t, SuffixMatch, false, "fooBarbaz", "baz", 6, 9)43 assertMatch(t, SuffixMatch, true, "fooBarbaz", "Baz", -1, -1)56 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1)57 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9)58 assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1)59 }60}6162func TestEmptyPattern(t *testing.T) {63 for _, dir := range []bool{true, false} {47 assertMatch(t, FuzzyMatch, true, "foobar", "", 0, 0)48 assertMatch(t, ExactMatchNaive, true, "foobar", "", 0, 0)49 assertMatch(t, PrefixMatch, true, "foobar", "", 0, 0)50 assertMatch(t, SuffixMatch, true, "foobar", "", 6, 6)64 assertMatch(t, FuzzyMatch, true, dir, "foobar", "", 0, 0)65 assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0)66 assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0)67 assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6)68 }69}
modified:src/algo/algo.go+41 added-19 removed
13 unmodified lines
14 *15 * In short: They try to do as little work as possible.16 */1718func runeAt(runes []rune, index int, max int, forward bool) rune {19 if forward {20 return runes[index]21 }22 return runes[max-index-1]23}2425// FuzzyMatch performs fuzzy-match19func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {26func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {27 if len(pattern) == 0 {28 return 0, 029 }30
9 unmodified lines
40 pidx := 041 sidx := -142 eidx := -14344 lenRunes := len(runes)45 lenPattern := len(pattern)4637 for index, char := range runes {47 for index := range runes {48 char := runeAt(runes, index, lenRunes, forward)49 // This is considerably faster than blindly applying strings.ToLower to the50 // whole string51 if !caseSensitive {52 // Partially inlining `unicode.ToLower`. Ugly, but makes a noticeable
4 unmodified lines
57 } else if char > unicode.MaxASCII {58 char = unicode.To(unicode.LowerCase, char)59 }60 }50 if char == pattern[pidx] {61 pchar := runeAt(pattern, pidx, lenPattern, forward)62 if char == pchar {63 if sidx < 0 {64 sidx = index65 }54 if pidx++; pidx == len(pattern) {66 if pidx++; pidx == lenPattern {67 eidx = index + 168 break69 }70 }
1 unmodified line
7273 if sidx >= 0 && eidx >= 0 {74 pidx--75 for index := eidx - 1; index >= sidx; index-- {64 char := runes[index]76 char := runeAt(runes, index, lenRunes, forward)77 if !caseSensitive {78 if char >= 'A' && char <= 'Z' {79 char += 3280 } else if char > unicode.MaxASCII {81 char = unicode.To(unicode.LowerCase, char)82 }83 }72 if char == pattern[pidx] {8485 pchar := runeAt(pattern, pidx, lenPattern, forward)86 if char == pchar {87 if pidx--; pidx < 0 {88 sidx = index89 break90 }91 }92 }93 if forward {79 return sidx, eidx94 return sidx, eidx95 }96 return lenRunes - eidx, lenRunes - sidx97 }98 return -1, -199}100
3 unmodified lines
104// strings and patterns are not very long.105//106// We might try to implement better algorithms in the future:107// http://en.wikipedia.org/wiki/String_searching_algorithm91func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int) {108func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {109 if len(pattern) == 0 {110 return 0, 0111 }11296 numRunes := len(runes)97 plen := len(pattern)98 if numRunes < plen {113 lenRunes := len(runes)114 lenPattern := len(pattern)115116 if lenRunes < lenPattern {117 return -1, -1118 }119120 pidx := 0103 for index := 0; index < numRunes; index++ {104 char := runes[index]121 for index := 0; index < lenRunes; index++ {122 char := runeAt(runes, index, lenRunes, forward)123 if !caseSensitive {124 if char >= 'A' && char <= 'Z' {125 char += 32126 } else if char > unicode.MaxASCII {127 char = unicode.To(unicode.LowerCase, char)128 }129 }112 if pattern[pidx] == char {130 pchar := runeAt(pattern, pidx, lenPattern, forward)131 if pchar == char {132 pidx++114 if pidx == plen {115 return index - plen + 1, index + 1133 if pidx == lenPattern {134 if forward {135 return index - lenPattern + 1, index + 1136 }137 return lenRunes - (index + 1), lenRunes - (index - lenPattern + 1)138 }139 } else {140 index -= pidx141 pidx = 0
2 unmodified lines
144 return -1, -1145}146147// PrefixMatch performs prefix-match126func PrefixMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {148func PrefixMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {149 if len(runes) < len(pattern) {150 return -1, -1151 }152
9 unmodified lines
162 return 0, len(pattern)163}164165// SuffixMatch performs suffix-match144func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {166func SuffixMatch(caseSensitive bool, forward bool, input []rune, pattern []rune) (int, int) {167 runes := util.TrimRight(input)168 trimmedLen := len(runes)169 diff := trimmedLen - len(pattern)170 if diff < 0 {
12 unmodified lines
183 return trimmedLen - len(pattern), trimmedLen184}185186// EqualMatch performs equal-match165func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {187func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) (int, int) {188 if len(runes) != len(pattern) {189 return -1, -1190 }191 runesStr := string(runes)
8 unmodified lines
modified:src/core.go+2 added-1 removed
141 unmodified lines
142143 // Matcher144 patternBuilder := func(runes []rune) *Pattern {145 return BuildPattern(146 opts.Mode, opts.Case, opts.Nth, opts.Delimiter, runes)146 opts.Mode, opts.Case, opts.Tiebreak != byEnd,147 opts.Nth, opts.Delimiter, runes)148 }149 matcher := NewMatcher(patternBuilder, sort, opts.Tac, eventBox)150151 // Filtering mode
123 unmodified lines
modified:src/pattern_test.go+11 added-11 removed
56 unmodified lines
5758func TestExact(t *testing.T) {59 defer clearPatternCache()60 clearPatternCache()61 pattern := BuildPattern(ModeExtended, CaseSmart,61 pattern := BuildPattern(ModeExtended, CaseSmart, true,62 []Range{}, Delimiter{}, []rune("'abc"))63 sidx, eidx := algo.ExactMatchNaive(64 pattern.caseSensitive, []rune("aabbcc abc"), pattern.terms[0].text)64 pattern.caseSensitive, pattern.forward, []rune("aabbcc abc"), pattern.terms[0].text)65 if sidx != 7 || eidx != 10 {66 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)67 }68}6970func TestEqual(t *testing.T) {71 defer clearPatternCache()72 clearPatternCache()73 pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("^AbC$"))73 pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("^AbC$"))7475 match := func(str string, sidxExpected int, eidxExpected int) {76 sidx, eidx := algo.EqualMatch(77 pattern.caseSensitive, []rune(str), pattern.terms[0].text)77 pattern.caseSensitive, pattern.forward, []rune(str), pattern.terms[0].text)78 if sidx != sidxExpected || eidx != eidxExpected {79 t.Errorf("%s / %d / %d", pattern.terms, sidx, eidx)80 }81 }
3 unmodified lines
8586func TestCaseSensitivity(t *testing.T) {87 defer clearPatternCache()88 clearPatternCache()89 pat1 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("abc"))89 pat1 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("abc"))90 clearPatternCache()91 pat2 := BuildPattern(ModeFuzzy, CaseSmart, []Range{}, Delimiter{}, []rune("Abc"))91 pat2 := BuildPattern(ModeFuzzy, CaseSmart, true, []Range{}, Delimiter{}, []rune("Abc"))92 clearPatternCache()93 pat3 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("abc"))93 pat3 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("abc"))94 clearPatternCache()95 pat4 := BuildPattern(ModeFuzzy, CaseIgnore, []Range{}, Delimiter{}, []rune("Abc"))95 pat4 := BuildPattern(ModeFuzzy, CaseIgnore, true, []Range{}, Delimiter{}, []rune("Abc"))96 clearPatternCache()97 pat5 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("abc"))97 pat5 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("abc"))98 clearPatternCache()99 pat6 := BuildPattern(ModeFuzzy, CaseRespect, []Range{}, Delimiter{}, []rune("Abc"))99 pat6 := BuildPattern(ModeFuzzy, CaseRespect, true, []Range{}, Delimiter{}, []rune("Abc"))100101 if string(pat1.text) != "abc" || pat1.caseSensitive != false ||102 string(pat2.text) != "Abc" || pat2.caseSensitive != true ||103 string(pat3.text) != "abc" || pat3.caseSensitive != false ||
4 unmodified lines
108 }109}110111func TestOrigTextAndTransformed(t *testing.T) {112 pattern := BuildPattern(ModeExtended, CaseSmart, []Range{}, Delimiter{}, []rune("jg"))112 pattern := BuildPattern(ModeExtended, CaseSmart, true, []Range{}, Delimiter{}, []rune("jg"))113 tokens := Tokenize([]rune("junegunn"), Delimiter{})114 trans := Transform(tokens, []Range{Range{1, 1}})115116 origRunes := []rune("junegunn.choi")
16 unmodified lines
modified:src/pattern.go+10 added-8 removed
37 unmodified lines
38// Pattern represents search pattern39type Pattern struct {40 mode Mode41 caseSensitive bool42 forward bool43 text []rune44 terms []term45 hasInvTerm bool46 delimiter Delimiter47 nth []Range47 procFun map[termType]func(bool, []rune, []rune) (int, int)48 procFun map[termType]func(bool, bool, []rune, []rune) (int, int)49}5051var (52 _patternCache map[string]*Pattern
17 unmodified lines
70 _cache = NewChunkCache()71}7273// BuildPattern builds Pattern object from the given arguments73func BuildPattern(mode Mode, caseMode Case,74func BuildPattern(mode Mode, caseMode Case, forward bool,75 nth []Range, delimiter Delimiter, runes []rune) *Pattern {7677 var asString string78 switch mode {
30 unmodified lines
109110 ptr := &Pattern{111 mode: mode,112 caseSensitive: caseSensitive,113 forward: forward,114 text: []rune(asString),115 terms: terms,116 hasInvTerm: hasInvTerm,117 nth: nth,118 delimiter: delimiter,117 procFun: make(map[termType]func(bool, []rune, []rune) (int, int))}119 procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}120121 ptr.procFun[termFuzzy] = algo.FuzzyMatch122 ptr.procFun[termEqual] = algo.EqualMatch123 ptr.procFun[termExact] = algo.ExactMatchNaive
165 unmodified lines
289}290291func (p *Pattern) fuzzyMatch(item *Item) (int, int) {292 input := p.prepareInput(item)291 return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.text)293 return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.forward, p.text)294}295296func (p *Pattern) extendedMatch(item *Item) []Offset {297 input := p.prepareInput(item)298 offsets := []Offset{}299 for _, term := range p.terms {300 pfun := p.procFun[term.typ]299 if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {301 if sidx, eidx := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {302 if term.inv {303 break304 }305 offsets = append(offsets, Offset{int32(sidx), int32(eidx)})
19 unmodified lines
325 item.transformed = ret326 return ret327}328327func (p *Pattern) iter(pfun func(bool, []rune, []rune) (int, int),328 tokens []Token, caseSensitive bool, pattern []rune) (int, int) {329func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),330 tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int) {331 for _, part := range tokens {332 prefixLength := part.prefixLength331 if sidx, eidx := pfun(caseSensitive, part.text, pattern); sidx >= 0 {333 if sidx, eidx := pfun(caseSensitive, forward, part.text, pattern); sidx >= 0 {334 return sidx + prefixLength, eidx + prefixLength335 }336 }337 return -1, -1
1 unmodified line
modified:test/test_go.rb+11 added
525 unmodified lines
526 ]527 assert_equal output, `cat #{tempname} | #{FZF} -fh -n2 -d:`.split($/)528 end529530 def test_tiebreak_end_backward_scan531 input = %w[532 foobar-fb533 fubar534 ]535 writelines tempname, input536537 assert_equal input.reverse, `cat #{tempname} | #{FZF} -f fb`.split($/)538 assert_equal input, `cat #{tempname} | #{FZF} -f fb --tiebreak=end`.split($/)539 end540541 def test_invalid_cache542 tmux.send_keys "(echo d; echo D; echo x) | #{fzf '-q d'}", :Enter543 tmux.until { |lines| lines[-2].include? '2/3' }544 tmux.send_keys :BSpace
500 unmodified lines