Skip to content
Myr

Fix prefix/suffix/equal matcher to trim whitespaces

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

- Prefix matcher will trim leading whitespaces only when the pattern doesn't start with a whitespace - Suffix matcher will trim trailing whitespaces only when the pattern doesn't end with a whitespace - Equal matcher will trim leading whitespaces only when the pattern doesn't start with a whitespace, and trim trailing whitespaces only when the pattern doesn't end with a whitespace Previously, only suffix matcher would trim whitespaces unconditionally. Fix #1894

authored by Junegunn Choi · committed by Junegunn Choi · parent b2c0413 · Browse files · 4 files+60 added−9 removed
This commit

State

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

Diff stats

4 files+60 added−9 removed

  • other:+60 added−9 removed

4 files+60 added−9 removed

modified:src/algo/algo_test.go+11 added
134 unmodified lines
135 assertMatch(t, PrefixMatch, false, dir, "fooBarBaz", "baz", -1, -1, 0)136 assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, score)137 assertMatch(t, PrefixMatch, false, dir, "foOBarBaZ", "foo", 0, 3, score)138 assertMatch(t, PrefixMatch, false, dir, "f-oBarbaz", "f-o", 0, 3, score)139140 assertMatch(t, PrefixMatch, false, dir, " fooBar", "foo", 1, 4, score)141 assertMatch(t, PrefixMatch, false, dir, " fooBar", " fo", 0, 3, score)142 assertMatch(t, PrefixMatch, false, dir, " fo", "foo", -1, -1, 0)143 }144}145146func TestSuffixMatch(t *testing.T) {
4 unmodified lines
151 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9,152 scoreMatch*3+bonusConsecutive*2)153 assertMatch(t, SuffixMatch, false, dir, "fooBarBaZ", "baz", 6, 9,154 (scoreMatch+bonusCamel123)*3+bonusCamel123*(bonusFirstCharMultiplier-1))155156 // Strip trailing white space from the string157 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz ", "baz", 6, 9,158 scoreMatch*3+bonusConsecutive*2)159 // Only when the pattern doesn't end with a space160 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz ", "baz ", 6, 10,161 scoreMatch*4+bonusConsecutive*2+bonusNonWord)162 }163}164165func TestEmptyPattern(t *testing.T) {
32 unmodified lines
modified:src/algo/algo.go+34 added-9 removed
771 unmodified lines
772 if len(pattern) == 0 {773 return Result{0, 0, 0}, nil774 }775776 trimmedLen := 0777 if !unicode.IsSpace(pattern[0]) {778 trimmedLen = text.LeadingWhitespaces()779 }780776 if text.Length() < len(pattern) {781 if text.Length()-trimmedLen < len(pattern) {782 return Result{-1, -1, 0}, nil783 }784785 for index, r := range pattern {781 char := text.Get(index)786 char := text.Get(trimmedLen + index)787 if !caseSensitive {788 char = unicode.ToLower(char)789 }790 if normalize {
3 unmodified lines
794 return Result{-1, -1, 0}, nil795 }796 }797 lenPattern := len(pattern)793 score, _ := calculateScore(caseSensitive, normalize, text, pattern, 0, lenPattern, false)794 return Result{0, lenPattern, score}, nil798 score, _ := calculateScore(caseSensitive, normalize, text, pattern, trimmedLen, trimmedLen+lenPattern, false)799 return Result{trimmedLen, trimmedLen + lenPattern, score}, nil800}801802// SuffixMatch performs suffix-match803func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {804 lenRunes := text.Length()800 trimmedLen := lenRunes - text.TrailingWhitespaces()805 trimmedLen := lenRunes806 if len(pattern) == 0 || !unicode.IsSpace(pattern[len(pattern)-1]) {807 trimmedLen -= text.TrailingWhitespaces()808 }809 if len(pattern) == 0 {810 return Result{trimmedLen, trimmedLen, 0}, nil811 }812 diff := trimmedLen - len(pattern)
22 unmodified lines
835836// EqualMatch performs equal-match837func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {838 lenPattern := len(pattern)831 if text.Length() != lenPattern {839 if lenPattern == 0 {840 return Result{-1, -1, 0}, nil841 }842843 // Strip leading whitespaces844 trimmedLen := 0845 if !unicode.IsSpace(pattern[0]) {846 trimmedLen = text.LeadingWhitespaces()847 }848849 // Strip trailing whitespaces850 trimmedEndLen := 0851 if !unicode.IsSpace(pattern[lenPattern-1]) {852 trimmedEndLen = text.TrailingWhitespaces()853 }854855 if text.Length()-trimmedLen-trimmedEndLen != lenPattern {856 return Result{-1, -1, 0}, nil857 }858 match := true859 if normalize {860 runes := text.ToRunes()861 for idx, pchar := range pattern {838 char := runes[idx]862 char := runes[trimmedLen+idx]863 if !caseSensitive {864 char = unicode.To(unicode.LowerCase, char)865 }866 if normalizeRune(pchar) != normalizeRune(char) {
1 unmodified line
868 break869 }870 }871 } else {848 runesStr := text.ToString()872 runes := text.ToRunes()873 runesStr := string(runes[trimmedLen : len(runes)-trimmedEndLen])874 if !caseSensitive {875 runesStr = strings.ToLower(runesStr)876 }877 match = runesStr == string(pattern)878 }879 if match {855 return Result{0, lenPattern, (scoreMatch+bonusBoundary)*lenPattern +880 return Result{trimmedLen, trimmedLen + lenPattern, (scoreMatch+bonusBoundary)*lenPattern +881 (bonusFirstCharMultiplier-1)*bonusBoundary}, nil882 }883 return Result{-1, -1, 0}, nil884}
modified:src/util/chars.go+12 added
128 unmodified lines
129 chars.trimLength = AsUint16(i - j + 1)130 return chars.trimLength131}132133func (chars *Chars) LeadingWhitespaces() int {134 whitespaces := 0135 for i := 0; i < chars.Length(); i++ {136 char := chars.Get(i)137 if !unicode.IsSpace(char) {138 break139 }140 whitespaces++141 }142 return whitespaces143}144145func (chars *Chars) TrailingWhitespaces() int {146 whitespaces := 0147 for i := chars.Length() - 1; i >= 0; i-- {148 char := chars.Get(i)
50 unmodified lines
modified:src/pattern_test.go+3 added
96 unmodified lines
97 }98 }99 match("ABC", -1, -1)100 match("AbC", 0, 3)101 match("AbC ", 0, 3)102 match(" AbC ", 1, 4)103 match(" AbC", 2, 5)104}105106func TestCaseSensitivity(t *testing.T) {107 defer clearPatternCache()
102 unmodified lines