summaryrefslogtreecommitdiff
path: root/src/algo
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
commit37dc273148df0893053bf5cda0582a23f5c2b2d2 (patch)
treed90f5e96fa97de429265461268b1a6db2703cb4c /src/algo
parentf7f01d109eb05c7eae82c243b6b6d5c5951ee707 (diff)
downloadfzf-37dc273148df0893053bf5cda0582a23f5c2b2d2.tar.gz
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
Diffstat (limited to 'src/algo')
-rw-r--r--src/algo/algo.go22
-rw-r--r--src/algo/algo_test.go2
2 files changed, 12 insertions, 12 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go
index 8825a059..00265c60 100644
--- a/src/algo/algo.go
+++ b/src/algo/algo.go
@@ -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}
}
diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go
index d6a5d487..7034dcef 100644
--- a/src/algo/algo_test.go
+++ b/src/algo/algo_test.go
@@ -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)
}