Skip to content
Myr

Optimize exact match by applying the same trick for fuzzy match

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 69aa2fe · Browse files · 1 file+43 added−19 removed
This commit

State

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

Diff stats

1 file+43 added−19 removed

  • other:+43 added−19 removed

1 file+43 added−19 removed

modified:src/algo/algo.go+43 added-19 removed
272 unmodified lines
273 }274 return from + idx275}276277func isAscii(runes []rune) bool {278 for _, r := range runes {279 if r >= utf8.RuneSelf {280 return false281 }282 }283 return true284}285286func asciiFuzzyIndex(input *util.Chars, pattern []rune, caseSensitive bool) int {287 // Can't determine288 if !input.IsBytes() {289 return 0290 }291292 // Not possible293 if !isAscii(pattern) {294 return -1295 }296297 firstIdx, idx := 0, 0298 for pidx := 0; pidx < len(pattern); pidx++ {299 idx = trySkip(input, caseSensitive, byte(pattern[pidx]), idx)300 if idx < 0 {301 return -1302 }303 if pidx == 0 && idx > 0 {304 // Step back to find the right bonus point305 firstIdx = idx - 1306 }307 idx++308 }309 return firstIdx310}311312func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {313 // Assume that pattern is given in lowercase if case-insensitive.314 // First check if there's a match and calculate bonus for each position.315 // If the input string is too long, consider finding the matching chars in
20 unmodified lines
336 // Rune array337 offset32, T := alloc32(offset32, slab, N, false)338339 // Phase 1. Optimized search for ASCII string305 firstIdx := 0306 if input.IsBytes() {307 idx := 0308 for pidx := 0; pidx < M; pidx++ {309 // Not possible310 if pattern[pidx] >= utf8.RuneSelf {311 return Result{-1, -1, 0}, nil312 }313 idx = trySkip(&input, caseSensitive, byte(pattern[pidx]), idx)314 if idx < 0 {315 return Result{-1, -1, 0}, nil340 idx := asciiFuzzyIndex(&input, pattern, caseSensitive)341 if idx < 0 {342 return Result{-1, -1, 0}, nil316 }317 if pidx == 0 && idx > 0 {318 // Step back to find the right bonus point319 firstIdx = idx - 1320 }321 idx++322 }343 }344345 // Phase 2. Calculate bonus for each point346 pidx, lastIdx, prevClass := 0, 0, charNonWord347 input.CopyRunes(T)328 for idx := firstIdx; idx < N; idx++ {348 for ; idx < N; idx++ {349 char := T[idx]350 var class charClass351 if char <= unicode.MaxASCII {352 class = charClassOfAscii(char)
323 unmodified lines
676 if lenRunes < lenPattern {677 return Result{-1, -1, 0}, nil678 }679680 if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 {681 return Result{-1, -1, 0}, nil682 }683684 // For simplicity, only look at the bonus at the first character position685 pidx := 0686 bestPos, bonus, bestBonus := -1, int16(0), int16(-1)687 for index := 0; index < lenRunes; index++ {
139 unmodified lines