Skip to content
Myr

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

4 files+19 added−16 removed

modified:src/algo/algo_test.go+2 added-1 removed
15 unmodified lines
16func 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.End
161 unmodified lines
modified:src/algo/algo.go+12 added-12 removed
227 unmodified lines
228 }229 return 0230}231232func bonusAt(input util.Chars, idx int) int16 {232func 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 lines
250251// Algo functions make two assumptions252// 1. "pattern" is given in lowercase if "caseSensitive" is false253// 2. "pattern" is already normalized if "normalize" is true254type Algo func(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)254type Algo func(caseSensitive bool, normalize bool, forward bool, input *util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int)255256func trySkip(input *util.Chars, caseSensitive bool, b byte, from int) int {257 byteArray := input.Bytes()[from:]258 idx := bytes.IndexByte(byteArray, b)
52 unmodified lines
311 }312 return firstIdx313}314315func FuzzyMatchV2(caseSensitive bool, normalize bool, forward bool, input util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {315func 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 lines
329 return FuzzyMatchV1(caseSensitive, normalize, forward, input, pattern, withPos, slab)330 }331332 // 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 lines
524 return Result{j, maxScorePos + 1, int(maxScore)}, pos525}526527// Implement the same sorting criteria as V2528func calculateScore(caseSensitive bool, normalize bool, text util.Chars, pattern []rune, sidx int, eidx int, withPos bool) (int, *[]int) {528func 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 lines
584 return score, pos585}586587// FuzzyMatchV1 performs fuzzy-match588func FuzzyMatchV1(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {588func 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 }595596 pidx := 0
73 unmodified lines
670// 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.674func ExactMatchNaive(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {674func 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 lines
682 if lenRunes < lenPattern {683 return Result{-1, -1, 0}, nil684 }685686 if asciiFuzzyIndex(&text, pattern, caseSensitive) < 0 {686 if asciiFuzzyIndex(text, pattern, caseSensitive) < 0 {687 return Result{-1, -1, 0}, nil688 }689690 // For simplicity, only look at the bonus at the first character position
49 unmodified lines
740 return Result{-1, -1, 0}, nil741}742743// PrefixMatch performs prefix-match744func PrefixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {744func 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 lines
767 return Result{0, lenPattern, score}, nil768}769770// SuffixMatch performs suffix-match771func SuffixMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {771func 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}, nil
22 unmodified lines
798 return Result{sidx, eidx, score}, nil799}800801// EqualMatch performs equal-match802func EqualMatch(caseSensitive bool, normalize bool, forward bool, text util.Chars, pattern []rune, withPos bool, slab *util.Slab) (Result, *[]int) {802func 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 lines
modified:src/pattern_test.go+4 added-2 removed
67 unmodified lines
68 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 lines
85 clearPatternCache()86 pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("^AbC$"))8788 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 lines
modified:src/pattern.go+1 added-1 removed
385 unmodified lines
386}387388func (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