Skip to content
Myr

Simplify nth comparison when reusing transformed tokens

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 ba0935c · Browse files · 4 files+14 added−19 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+14 added−19 removed

  • other:+14 added−19 removed

4 files+14 added−19 removed

modified:src/core.go+3 added-1 removed
190 unmodified lines
191 }192 }193194 nth := opts.Nth195 nthRevision := 0196 patternCache := make(map[string]*Pattern)197 patternBuilder := func(runes []rune) *Pattern {198 return BuildPattern(cache, patternCache,199 opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos,199 opts.Filter == nil, nth, opts.Delimiter, runes)200 opts.Filter == nil, nth, opts.Delimiter, nthRevision, runes)201 }202 inputRevision := revision{}203 snapshotRevision := revision{}204 matcher := NewMatcher(cache, patternBuilder, sort, opts.Tac, eventBox, inputRevision)
173 unmodified lines
378 changed = val.changed379 if val.nth != nil {380 // Change nth and clear caches381 nth = *val.nth382 nthRevision++383 patternCache = make(map[string]*Pattern)384 cache.Clear()385 inputRevision.bumpMinor()386 }
90 unmodified lines
modified:src/item.go+3 added-3 removed
6 unmodified lines
7)89type transformed struct {10 // Because nth can be changed dynamically by change-nth action, we need to11 // keep the nth value at the time of transformation.12 nth []Range13 tokens []Token11 // keep the revision number at the time of transformation.12 revision int13 tokens []Token14}1516// Item represents each input line. 56 bytes.17type Item struct {
36 unmodified lines
modified:src/pattern_test.go+2 added-2 removed
66 unmodified lines
67func buildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,68 withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {69 return BuildPattern(NewChunkCache(), make(map[string]*Pattern),70 fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward,71 withPos, cacheable, nth, delimiter, runes)71 withPos, cacheable, nth, delimiter, 0, runes)72}7374func TestExact(t *testing.T) {75 pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true,
58 unmodified lines
134 chunk := Chunk{count: 1}135 chunk.items[0] = Item{136 text: util.ToChars([]byte("junegunn")),137 origText: &origBytes,138 transformed: &transformed{pattern.nth, trans}}138 transformed: &transformed{pattern.revision, trans}}139 pattern.extended = extended140 matches := pattern.matchChunk(&chunk, nil, slab) // No cache141 if !(matches[0].item.text.ToString() == "junegunn" &&142 string(*matches[0].item.origText) == "junegunn.choi" &&
59 unmodified lines
modified:src/pattern.go+6 added-13 removed
58 unmodified lines
59 cacheable bool60 cacheKey string61 delimiter Delimiter62 nth []Range63 revision int64 procFun map[termType]algo.Algo65 cache *ChunkCache66}67
4 unmodified lines
72}7374// BuildPattern builds Pattern object from the given arguments75func BuildPattern(cache *ChunkCache, patternCache map[string]*Pattern, fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,75 withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {76 withPos bool, cacheable bool, nth []Range, delimiter Delimiter, revision int, runes []rune) *Pattern {7778 var asString string79 if extended {80 asString = strings.TrimLeft(string(runes), " ")
59 unmodified lines
140 termSets: termSets,141 sortable: sortable,142 cacheable: cacheable,143 nth: nth,144 revision: revision,145 delimiter: delimiter,146 cache: cache,147 procFun: make(map[termType]algo.Algo)}148
246 unmodified lines
395396func (p *Pattern) transformInput(item *Item) []Token {397 if item.transformed != nil {398 transformed := *item.transformed397 if len(transformed.nth) == len(p.nth) {398 same := true399 for idx, rangeItem := range transformed.nth {400 if rangeItem != p.nth[idx] {401 same = false402 break403 }404 }405 if same {406 return transformed.tokens407 }399 if transformed.revision == p.revision {400 return transformed.tokens401 }402 }403404 tokens := Tokenize(item.text.ToString(), p.delimiter)405 ret := Transform(tokens, p.nth)413 item.transformed = &transformed{p.nth, ret}406 item.transformed = &transformed{p.revision, ret}407 return ret408}409410func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, normalize bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
14 unmodified lines