From a2a4df088646a5f363f9321c52ef9ab9ba4706aa Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 20 Aug 2017 03:33:55 +0900 Subject: [PATCH] Pass util.Chars by pointer --- src/algo/algo.go | 24 ++++++++++++------------ src/algo/algo_test.go | 3 ++- src/pattern.go | 2 +- src/pattern_test.go | 6 ++++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/algo/algo.go b/src/algo/algo.go index 74c4ad5..925ef28 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 2da0b3c..610c30e 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 diff --git a/src/pattern.go b/src/pattern.go index 94615ad..636ae1e 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -387,7 +387,7 @@ func (p *Pattern) transformInput(item *Item) []Token { func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, normalize bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, *[]int) { for _, part := range tokens { - if res, pos := pfun(caseSensitive, normalize, forward, *part.text, pattern, withPos, slab); res.Start >= 0 { + if res, pos := pfun(caseSensitive, normalize, forward, part.text, pattern, withPos, slab); res.Start >= 0 { sidx := int32(res.Start) + part.prefixLength eidx := int32(res.End) + part.prefixLength if pos != nil { diff --git a/src/pattern_test.go b/src/pattern_test.go index 54d6f51..1930ddd 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -69,8 +69,9 @@ func TestExact(t *testing.T) { clearPatternCache() pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("'abc")) + chars := util.ToChars([]byte("aabbcc abc")) res, pos := algo.ExactMatchNaive( - pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte("aabbcc abc")), pattern.termSets[0][0].text, true, nil) + pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) if res.Start != 7 || res.End != 10 { t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) } @@ -85,8 +86,9 @@ func TestEqual(t *testing.T) { pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$")) match := func(str string, sidxExpected int, eidxExpected int) { + chars := util.ToChars([]byte(str)) res, pos := algo.EqualMatch( - pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte(str)), pattern.termSets[0][0].text, true, nil) + pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) if res.Start != sidxExpected || res.End != eidxExpected { t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) }