diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-20 03:33:55 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-20 12:29:11 +0900 |
| commit | a2a4df088646a5f363f9321c52ef9ab9ba4706aa (patch) | |
| tree | f4ff2f96b19be37582453bb1c28968c0b58d1549 /src/algo | |
| parent | 3399e39968473b1920db19ef1969b9df34bdd3c1 (diff) | |
| download | fzf-a2a4df088646a5f363f9321c52ef9ab9ba4706aa.tar.gz | |
Pass util.Chars by pointer
Diffstat (limited to 'src/algo')
| -rw-r--r-- | src/algo/algo.go | 24 | ||||
| -rw-r--r-- | src/algo/algo_test.go | 3 |
2 files changed, 14 insertions, 13 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go index 74c4ad5a..925ef286 100644 --- a/src/algo/algo.go +++ b/src/algo/algo.go @@ -229,7 +229,7 @@ func bonusFor(prevClass charClass, class charClass) int16 { return 0 } -func bonusAt(input util.Chars, idx int) int16 { +func bonusAt(input *util.Chars, idx int) int16 { if idx == 0 { return bonusBoundary } @@ -251,7 +251,7 @@ func normalizeRune(r rune) rune { // Algo functions make two assumptions // 1. "pattern" is given in lowercase if "caseSensitive" is false // 2. "pattern" is already normalized if "normalize" is true -type Algo func(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) +type Algo func(caseSensitive bool, normalize bool, forward bool, input *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) func trySkip(input *util.Chars, caseSensitive bool, b byte, from int) int { byteArray := input.Bytes()[from:] @@ -312,7 +312,7 @@ func asciiFuzzyIndex(input *util.Chars, pattern []rune, caseSensitive bool) int return firstIdx } -func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { // Assume that pattern is given in lowercase if case-insensitive. // First check if there's a match and calculate bonus for each position. // If the input string is too long, consider finding the matching chars in @@ -330,7 +330,7 @@ func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.C } // Phase 1. Optimized search for ASCII string - idx := asciiFuzzyIndex(&input, pattern, caseSensitive) + idx := asciiFuzzyIndex(input, pattern, caseSensitive) if idx < 0 { return Result{-1, -1, 0}, nil } @@ -525,7 +525,7 @@ func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.C } // Implement the same sorting criteria as V2 -func calculateScore(caseSensitive bool, normalize bool, text util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) { +func calculateScore(caseSensitive bool, normalize bool, text *util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) { pidx, score, inGap, consecutive, firstBonus := 0, 0, false, 0, int16(0) pos := posArray(withPos, len(pattern)) prevClass := charNonWord @@ -585,11 +585,11 @@ func calculateScore(caseSensitive bool, normalize bool, text util.Chars, pattern } // FuzzyMatchV1 performs fuzzy-match -func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { if len(pattern) == 0 { return Result{0, 0, 0}, nil } - if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 { + if asciiFuzzyIndex(text, pattern, caseSensitive) < 0 { return Result{-1, -1, 0}, nil } @@ -671,7 +671,7 @@ func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text util.Ch // bonus point, instead of stopping immediately after finding the first match. // The solution is much cheaper since there is only one possible alignment of // the pattern. -func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { if len(pattern) == 0 { return Result{0, 0, 0}, nil } @@ -683,7 +683,7 @@ func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text util return Result{-1, -1, 0}, nil } - if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 { + if asciiFuzzyIndex(text, pattern, caseSensitive) < 0 { return Result{-1, -1, 0}, nil } @@ -741,7 +741,7 @@ func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text util } // PrefixMatch performs prefix-match -func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { if len(pattern) == 0 { return Result{0, 0, 0}, nil } @@ -768,7 +768,7 @@ func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text util.Cha } // SuffixMatch performs suffix-match -func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { lenRunes := text.Length() trimmedLen := lenRunes - text.TrailingWhitespaces() if len(pattern) == 0 { @@ -799,7 +799,7 @@ func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text util.Cha } // EqualMatch performs equal-match -func EqualMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { +func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) { lenPattern := len(pattern) if text.Length() != lenPattern { return Result{-1, -1, 0}, nil diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go index 2da0b3c3..610c30e5 100644 --- a/src/algo/algo_test.go +++ b/src/algo/algo_test.go @@ -17,7 +17,8 @@ func assertMatch2(t *testing.T, fun Algo, caseSensitive, normalize, forward bool if !caseSensitive { pattern = strings.ToLower(pattern) } - res, pos := fun(caseSensitive, normalize, forward, util.ToChars([]byte(input)), []rune(pattern), true, nil) + chars := util.ToChars([]byte(input)) + res, pos := fun(caseSensitive, normalize, forward, &chars, []rune(pattern), true, nil) var start, end int if pos == nil || len(*pos) == 0 { start = res.Start |
