mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-15 04:05:48 -07:00
Micro-optimizations
- Make structs smaller - Introduce Result struct and use it to represent matched items instead of reusing Item struct for that purpose - Avoid unnecessary memory allocation - Avoid growing slice from the initial capacity - Code cleanup
This commit is contained in:
@@ -24,12 +24,12 @@ func indexAt(index int, max int, forward bool) int {
|
||||
|
||||
// Result conatins the results of running a match function.
|
||||
type Result struct {
|
||||
Start int32
|
||||
End int32
|
||||
Start int
|
||||
End int
|
||||
|
||||
// Items are basically sorted by the lengths of matched substrings.
|
||||
// But we slightly adjust the score with bonus for better results.
|
||||
Bonus int32
|
||||
Bonus int
|
||||
}
|
||||
|
||||
type charClass int
|
||||
@@ -42,8 +42,8 @@ const (
|
||||
charNumber
|
||||
)
|
||||
|
||||
func evaluateBonus(caseSensitive bool, text util.Chars, pattern []rune, sidx int, eidx int) int32 {
|
||||
var bonus int32
|
||||
func evaluateBonus(caseSensitive bool, text util.Chars, pattern []rune, sidx int, eidx int) int {
|
||||
var bonus int
|
||||
pidx := 0
|
||||
lenPattern := len(pattern)
|
||||
consecutive := false
|
||||
@@ -63,7 +63,7 @@ func evaluateBonus(caseSensitive bool, text util.Chars, pattern []rune, sidx int
|
||||
class = charNonWord
|
||||
}
|
||||
|
||||
var point int32
|
||||
var point int
|
||||
if prevClass == charNonWord && class != charNonWord {
|
||||
// Word boundary
|
||||
point = 2
|
||||
@@ -181,7 +181,7 @@ func FuzzyMatch(caseSensitive bool, forward bool, text util.Chars, pattern []run
|
||||
sidx, eidx = lenRunes-eidx, lenRunes-sidx
|
||||
}
|
||||
|
||||
return Result{int32(sidx), int32(eidx),
|
||||
return Result{sidx, eidx,
|
||||
evaluateBonus(caseSensitive, text, pattern, sidx, eidx)}
|
||||
}
|
||||
return Result{-1, -1, 0}
|
||||
@@ -228,7 +228,7 @@ func ExactMatchNaive(caseSensitive bool, forward bool, text util.Chars, pattern
|
||||
sidx = lenRunes - (index + 1)
|
||||
eidx = lenRunes - (index - lenPattern + 1)
|
||||
}
|
||||
return Result{int32(sidx), int32(eidx),
|
||||
return Result{sidx, eidx,
|
||||
evaluateBonus(caseSensitive, text, pattern, sidx, eidx)}
|
||||
}
|
||||
} else {
|
||||
@@ -255,7 +255,7 @@ func PrefixMatch(caseSensitive bool, forward bool, text util.Chars, pattern []ru
|
||||
}
|
||||
}
|
||||
lenPattern := len(pattern)
|
||||
return Result{0, int32(lenPattern),
|
||||
return Result{0, lenPattern,
|
||||
evaluateBonus(caseSensitive, text, pattern, 0, lenPattern)}
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ func SuffixMatch(caseSensitive bool, forward bool, text util.Chars, pattern []ru
|
||||
lenPattern := len(pattern)
|
||||
sidx := trimmedLen - lenPattern
|
||||
eidx := trimmedLen
|
||||
return Result{int32(sidx), int32(eidx),
|
||||
return Result{sidx, eidx,
|
||||
evaluateBonus(caseSensitive, text, pattern, sidx, eidx)}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ func EqualMatch(caseSensitive bool, forward bool, text util.Chars, pattern []run
|
||||
runesStr = strings.ToLower(runesStr)
|
||||
}
|
||||
if runesStr == string(pattern) {
|
||||
return Result{0, int32(len(pattern)), 0}
|
||||
return Result{0, len(pattern), 0}
|
||||
}
|
||||
return Result{-1, -1, 0}
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/junegunn/fzf/src/util"
|
||||
)
|
||||
|
||||
func assertMatch(t *testing.T, fun func(bool, bool, util.Chars, []rune) Result, caseSensitive, forward bool, input, pattern string, sidx int32, eidx int32, bonus int32) {
|
||||
func assertMatch(t *testing.T, fun func(bool, bool, util.Chars, []rune) Result, caseSensitive, forward bool, input, pattern string, sidx int, eidx int, bonus int) {
|
||||
if !caseSensitive {
|
||||
pattern = strings.ToLower(pattern)
|
||||
}
|
||||
|
Reference in New Issue
Block a user