Pass util.Chars by pointer
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 3399e39 · Browse files · 4 files·+19 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+19 added−16 removed
- other:+19 added−16 removed
Provenance
- Commit
a2a4df0- Tree
f4ff2f9- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
3399e39
4 files changed+19 added−16 removed
modified:src/algo/algo_test.go+2 added-1 removed
15 unmodified lines16 func assertMatch2(t *testing.T, fun Algo, caseSensitive, normalize, forward bool, input, pattern string, sidx int, eidx int, score int) {17 if !caseSensitive {18 pattern = strings.ToLower(pattern)19 }20+ chars := util.ToChars([]byte(input))20- res, pos := fun(caseSensitive, normalize, forward, util.ToChars([]byte(input)), []rune(pattern), true, nil)21+ res, pos := fun(caseSensitive, normalize, forward, &chars, []rune(pattern), true, nil)22 var start, end int23 if pos == nil || len(*pos) == 0 {24 start = res.Start25 end = res.End161 unmodified linesmodified:src/algo/algo.go+12 added-12 removed
227 unmodified lines228 }229 return 0230 }231 232-func bonusAt(input util.Chars, idx int) int16 {232+func bonusAt(input *util.Chars, idx int) int16 {233 if idx == 0 {234 return bonusBoundary235 }236 return bonusFor(charClassOf(input.Get(idx-1)), charClassOf(input.Get(idx)))13 unmodified lines250 251 // Algo functions make two assumptions252 // 1. "pattern" is given in lowercase if "caseSensitive" is false253 // 2. "pattern" is already normalized if "normalize" is true254-type Algo func(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)254+type Algo func(caseSensitive bool, normalize bool, forward bool, input *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)255 256 func trySkip(input *util.Chars, caseSensitive bool, b byte, from int) int {257 byteArray := input.Bytes()[from:]258 idx := bytes.IndexByte(byteArray, b)52 unmodified lines311 }312 return firstIdx313 }314 315-func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {315+func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {316 // Assume that pattern is given in lowercase if case-insensitive.317 // First check if there's a match and calculate bonus for each position.318 // If the input string is too long, consider finding the matching chars in319 // this phase as well (non-optimal alignment).9 unmodified lines329 return FuzzyMatchV1(caseSensitive, normalize, forward, input, pattern, withPos, slab)330 }331 332 // Phase 1. Optimized search for ASCII string333- idx := asciiFuzzyIndex(&input, pattern, caseSensitive)333+ idx := asciiFuzzyIndex(input, pattern, caseSensitive)334 if idx < 0 {335 return Result{-1, -1, 0}, nil336 }337 186 unmodified lines524 return Result{j, maxScorePos + 1, int(maxScore)}, pos525 }526 527 // Implement the same sorting criteria as V2528-func calculateScore(caseSensitive bool, normalize bool, text util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) {528+func calculateScore(caseSensitive bool, normalize bool, text *util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) {529 pidx, score, inGap, consecutive, firstBonus := 0, 0, false, 0, int16(0)530 pos := posArray(withPos, len(pattern))531 prevClass := charNonWord532 if sidx > 0 {51 unmodified lines584 return score, pos585 }586 587 // FuzzyMatchV1 performs fuzzy-match588-func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {588+func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {589 if len(pattern) == 0 {590 return Result{0, 0, 0}, nil591 }592- if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 {592+ if asciiFuzzyIndex(text, pattern, caseSensitive) < 0 {593 return Result{-1, -1, 0}, nil594 }595 596 pidx := 073 unmodified lines670 // Since 0.15.0, this function searches for the match with the highest671 // bonus point, instead of stopping immediately after finding the first match.672 // The solution is much cheaper since there is only one possible alignment of673 // the pattern.674-func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {674+func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {675 if len(pattern) == 0 {676 return Result{0, 0, 0}, nil677 }678 3 unmodified lines682 if lenRunes < lenPattern {683 return Result{-1, -1, 0}, nil684 }685 686- if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 {686+ if asciiFuzzyIndex(text, pattern, caseSensitive) < 0 {687 return Result{-1, -1, 0}, nil688 }689 690 // For simplicity, only look at the bonus at the first character position49 unmodified lines740 return Result{-1, -1, 0}, nil741 }742 743 // PrefixMatch performs prefix-match744-func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {744+func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {745 if len(pattern) == 0 {746 return Result{0, 0, 0}, nil747 }748 18 unmodified lines767 return Result{0, lenPattern, score}, nil768 }769 770 // SuffixMatch performs suffix-match771-func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {771+func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {772 lenRunes := text.Length()773 trimmedLen := lenRunes - text.TrailingWhitespaces()774 if len(pattern) == 0 {775 return Result{trimmedLen, trimmedLen, 0}, nil22 unmodified lines798 return Result{sidx, eidx, score}, nil799 }800 801 // EqualMatch performs equal-match802-func EqualMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {802+func EqualMatch(caseSensitive bool, normalize bool, forward bool, text *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {803 lenPattern := len(pattern)804 if text.Length() != lenPattern {805 return Result{-1, -1, 0}, nil806 }26 unmodified linesmodified:src/pattern_test.go+4 added-2 removed
67 unmodified lines68 defer clearPatternCache()69 clearPatternCache()70 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true,71 []Range{}, Delimiter{}, []rune("'abc"))72+ chars := util.ToChars([]byte("aabbcc abc"))73 res, pos := algo.ExactMatchNaive(73- pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte("aabbcc abc")), pattern.termSets[0][0].text, true, nil)74+ pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)75 if res.Start != 7 || res.End != 10 {76 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)77 }78 if pos != nil {6 unmodified lines85 clearPatternCache()86 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))87 88 match := func(str string, sidxExpected int, eidxExpected int) {89+ chars := util.ToChars([]byte(str))90 res, pos := algo.EqualMatch(89- pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte(str)), pattern.termSets[0][0].text, true, nil)91+ pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil)92 if res.Start != sidxExpected || res.End != eidxExpected {93 t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End)94 }95 if pos != nil {111 unmodified linesmodified:src/pattern.go+1 added-1 removed
385 unmodified lines386 }387 388 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) {389 for _, part := range tokens {390- if res, pos := pfun(caseSensitive, normalize, forward, *part.text, pattern, withPos, slab); res.Start >= 0 {390+ if res, pos := pfun(caseSensitive, normalize, forward, part.text, pattern, withPos, slab); res.Start >= 0 {391 sidx := int32(res.Start) + part.prefixLength392 eidx := int32(res.End) + part.prefixLength393 if pos != nil {394 for idx := range *pos {8 unmodified lines