Apply new ranking algorithm to exact match as well
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 654a7df · Browse files · 2 files·+95 added−74 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+95 added−74 removed
- other:+95 added−74 removed
Provenance
- Commit
4bde8de- Tree
6dfcc5c- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
654a7df
2 files changed+95 added−74 removed
modified:src/algo/algo_test.go+13 added-6 removed
49 unmodified lines50 }51 52 func TestExactMatchNaive(t *testing.T) {53 for _, dir := range []bool{true, false} {54- assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5, 0)54+ assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5, 3)55 assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1, 0)56 assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1, 0)57+58+ assertMatch(t, ExactMatchNaive, false, dir, "/AutomatorDocument.icns", "rdoc", 9, 13, 4)59+ assertMatch(t, ExactMatchNaive, false, dir, "/man1/zshcompctl.1", "zshc", 6, 10, 7)60+ assertMatch(t, ExactMatchNaive, false, dir, "/.oh-my-zsh/cache", "zsh/c", 8, 13, 10)61 }62 }63 64 func TestExactMatchNaiveBackward(t *testing.T) {61- assertMatch(t, ExactMatchNaive, false, true, "foobar foob", "oo", 1, 3, 0)62- assertMatch(t, ExactMatchNaive, false, false, "foobar foob", "oo", 8, 10, 0)65+ assertMatch(t, ExactMatchNaive, false, true, "foobar foob", "oo", 1, 3, 1)66+ assertMatch(t, ExactMatchNaive, false, false, "foobar foob", "oo", 8, 10, 1)67 }68 69 func TestPrefixMatch(t *testing.T) {70 for _, dir := range []bool{true, false} {67- assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, 0)71 assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1, 0)69- assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "baz", -1, -1, 0)72+ assertMatch(t, PrefixMatch, false, dir, "fooBarBaz", "baz", -1, -1, 0)73+ assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, 6)74+ assertMatch(t, PrefixMatch, false, dir, "foOBarBaZ", "foo", 0, 3, 7)75+ assertMatch(t, PrefixMatch, false, dir, "f-oBarbaz", "f-o", 0, 3, 8)76 }77 }78 79 func TestSuffixMatch(t *testing.T) {80 for _, dir := range []bool{true, false} {81 assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1, 0)76- assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9, 0)82+ assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9, 2)83+ assertMatch(t, SuffixMatch, false, dir, "fooBarBaZ", "baz", 6, 9, 5)84 assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1, 0)85 }86 }87 8 unmodified linesmodified:src/algo/algo.go+82 added-68 removed
40 unmodified lines41 charLetter42 charNumber43 )44 45+func evaluateBonus(caseSensitive bool, runes []rune, pattern []rune, sidx int, eidx int) int32 {46+ var bonus int3247+ pidx := 048+ lenPattern := len(pattern)49+ consecutive := false50+ prevClass := charNonWord51+ for index := 0; index < eidx; index++ {52+ char := runes[index]53+ var class charClass54+ if unicode.IsLower(char) {55+ class = charLower56+ } else if unicode.IsUpper(char) {57+ class = charUpper58+ } else if unicode.IsLetter(char) {59+ class = charLetter60+ } else if unicode.IsNumber(char) {61+ class = charNumber62+ } else {63+ class = charNonWord64+ }65+66+ var point int3267+ if prevClass == charNonWord && class != charNonWord {68+ // Word boundary69+ point = 270+ } else if prevClass == charLower && class == charUpper ||71+ prevClass != charNumber && class == charNumber {72+ // camelCase letter12373+ point = 174+ }75+ prevClass = class76+77+ if index >= sidx {78+ if !caseSensitive {79+ if char >= 'A' && char <= 'Z' {80+ char += 3281+ } else if char > unicode.MaxASCII {82+ char = unicode.To(unicode.LowerCase, char)83+ }84+ }85+ pchar := pattern[pidx]86+ if pchar == char {87+ // Boost bonus for the first character in the pattern88+ if pidx == 0 {89+ point *= 290+ }91+ // Bonus to consecutive matching chars92+ if consecutive {93+ point++94+ }95+ bonus += point96+97+ if pidx++; pidx == lenPattern {98+ break99+ }100+ consecutive = true101+ } else {102+ consecutive = false103+ }104+ }105+ }106+ return bonus107+}108+109 // FuzzyMatch performs fuzzy-match110 func FuzzyMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) Result {111 if len(pattern) == 0 {112 return Result{0, 0, 0}67 unmodified lines180 if !forward {181 sidx, eidx = lenRunes-eidx, lenRunes-sidx182 }183 120- var bonus int32121- pidx := 0122- consecutive := false123- prevClass := charNonWord124- for index := 0; index < eidx; index++ {125- char := runes[index]126- var class charClass127- if unicode.IsLower(char) {128- class = charLower129- } else if unicode.IsUpper(char) {130- class = charUpper131- } else if unicode.IsLetter(char) {132- class = charLetter133- } else if unicode.IsNumber(char) {134- class = charNumber135- } else {136- class = charNonWord137- }138-139- var point int32140- if prevClass == charNonWord && class != charNonWord {141- // Word boundary142- point = 2143- } else if prevClass == charLower && class == charUpper ||144- prevClass != charNumber && class == charNumber {145- // camelCase letter123146- point = 1147- }148- prevClass = class149-150- if index >= sidx {151- if !caseSensitive {152- if char >= 'A' && char <= 'Z' {153- char += 32154- } else if char > unicode.MaxASCII {155- char = unicode.To(unicode.LowerCase, char)156- }157- }158- pchar := pattern[pidx]159- if pchar == char {160- // Boost bonus for the first character in the pattern161- if pidx == 0 {162- point *= 2163- }164- // Bonus to consecutive matching chars165- if consecutive {166- point++167- }168- bonus += point169-170- if pidx++; pidx == lenPattern {171- break172- }173- consecutive = true174- } else {175- consecutive = false176- }177- }178- }179-180- return Result{int32(sidx), int32(eidx), bonus}184+ return Result{int32(sidx), int32(eidx),185+ evaluateBonus(caseSensitive, runes, pattern, sidx, eidx)}186 }187 return Result{-1, -1, 0}188 }189 4 unmodified lines194 //195 // We might try to implement better algorithms in the future:196 // http://en.wikipedia.org/wiki/String_searching_algorithm197 func ExactMatchNaive(caseSensitive bool, forward bool, runes []rune, pattern []rune) Result {193- // Note: ExactMatchNaive always return a zero bonus.198 if len(pattern) == 0 {199 return Result{0, 0, 0}200 }201 17 unmodified lines219 pchar := runeAt(pattern, pidx, lenPattern, forward)220 if pchar == char {221 pidx++222 if pidx == lenPattern {223+ var sidx, eidx int224 if forward {220- return Result{int32(index - lenPattern + 1), int32(index + 1), 0}225+ sidx = index - lenPattern + 1226+ eidx = index + 1227+ } else {228+ sidx = lenRunes - (index + 1)229+ eidx = lenRunes - (index - lenPattern + 1)230 }222- return Result{int32(lenRunes - (index + 1)), int32(lenRunes - (index - lenPattern + 1)), 0}231+ return Result{int32(sidx), int32(eidx),232+ evaluateBonus(caseSensitive, runes, pattern, sidx, eidx)}233 }234 } else {235 index -= pidx236 pidx = 03 unmodified lines240 }241 242 // PrefixMatch performs prefix-match243 func PrefixMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) Result {234- // Note: PrefixMatch always return a zero bonus.244 if len(runes) < len(pattern) {245 return Result{-1, -1, 0}246 }247 5 unmodified lines253 if char != r {254 return Result{-1, -1, 0}255 }256 }257+ lenPattern := len(pattern)248- return Result{0, int32(len(pattern)), 0}258+ return Result{0, int32(lenPattern),259+ evaluateBonus(caseSensitive, runes, pattern, 0, lenPattern)}260 }261 262 // SuffixMatch performs suffix-match263 func SuffixMatch(caseSensitive bool, forward bool, input []rune, pattern []rune) Result {253- // Note: SuffixMatch always return a zero bonus.264 runes := util.TrimRight(input)265 trimmedLen := len(runes)266 diff := trimmedLen - len(pattern)267 if diff < 0 {8 unmodified lines276 if char != r {277 return Result{-1, -1, 0}278 }279 }270- return Result{int32(trimmedLen - len(pattern)), int32(trimmedLen), 0}280+ lenPattern := len(pattern)281+ sidx := trimmedLen - lenPattern282+ eidx := trimmedLen283+ return Result{int32(sidx), int32(eidx),284+ evaluateBonus(caseSensitive, runes, pattern, sidx, eidx)}285 }286 287 // EqualMatch performs equal-match288 func EqualMatch(caseSensitive bool, forward bool, runes []rune, pattern []rune) Result {13 unmodified lines