pattern.go
This file
Path
src/pattern.go
343 lines · 7.8 KiB · Go
Last changed by
Make --extended defaultJunegunn Choibefore adoption81a8869
History of this fileTake it
The walk stopped after 200 commits: lines older than 5a17a63 point at it.
[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 1–8why, one step back1package fzf23import (4 "regexp"5 "sort"6 "strings"78 "github.com/junegunn/fzf/src/algo"Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 9why, one step back9 "github.com/junegunn/fzf/src/util"[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 10–27why, one step back10)1112// fuzzy13// 'exact14// ^exact-prefix15// exact-suffix$16// !not-fuzzy17// !'not-exact18// !^not-exact-prefix19// !not-exact-suffix$2021type termType int2223const (24 termFuzzy termType = iota25 termExact26 termPrefix27 termSuffixAllow ^EqualMatch$Junegunn Choibefore adoption0be4cea
line 28why, one step back28 termEqual[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 29–40why, one step back29)3031type term struct {32 typ termType33 inv bool34 text []rune35 caseSensitive bool36 origText []rune37}3839// Pattern represents search pattern40type Pattern struct {Make --extended defaultJunegunn Choibefore adoption81a8869
lines 41–42why, one step back41 fuzzy bool42 extended bool[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 43why, one step back43 caseSensitive boolFix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 44why, one step back44 forward bool[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 45–47why, one step back45 text []rune46 terms []term47 hasInvTerm boolFix --with-nth performance; avoid regex if possibleJunegunn Choibefore adoption766427d
line 48why, one step back48 delimiter Delimiter[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 49why, one step back49 nth []RangeFix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 50why, one step back50 procFun map[termType]func(bool, bool, []rune, []rune) (int, int)[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 51–66why, one step back51}5253var (54 _patternCache map[string]*Pattern55 _splitRegex *regexp.Regexp56 _cache ChunkCache57)5859func init() {60 _splitRegex = regexp.MustCompile("\\s+")61 clearPatternCache()62 clearChunkCache()63}6465func clearPatternCache() {66 // We can uniquely identify the pattern for a given string sinceMake --extended defaultJunegunn Choibefore adoption81a8869
line 67why, one step back67 // search mode and caseMode do not change while the program is running[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 68–75why, one step back68 _patternCache = make(map[string]*Pattern)69}7071func clearChunkCache() {72 _cache = NewChunkCache()73}7475// BuildPattern builds Pattern object from the given argumentsMake --extended defaultJunegunn Choibefore adoption81a8869
line 76why, one step back76func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,Fix --with-nth performance; avoid regex if possibleJunegunn Choibefore adoption766427d
line 77why, one step back77 nth []Range, delimiter Delimiter, runes []rune) *Pattern {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 78–79why, one step back7879 var asString stringMake --extended defaultJunegunn Choibefore adoption81a8869
line 80why, one step back80 if extended {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 81why, one step back81 asString = strings.Trim(string(runes), " ")Make --extended defaultJunegunn Choibefore adoption81a8869
line 82why, one step back82 } else {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 83–93why, one step back83 asString = string(runes)84 }8586 cached, found := _patternCache[asString]87 if found {88 return cached89 }9091 caseSensitive, hasInvTerm := true, false92 terms := []term{}93Make --extended defaultJunegunn Choibefore adoption81a8869
lines 94–95why, one step back94 if extended {95 terms = parseTerms(fuzzy, caseMode, asString)[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 96–100why, one step back96 for _, term := range terms {97 if term.inv {98 hasInvTerm = true99 }100 }Make --extended defaultJunegunn Choibefore adoption81a8869
line 101why, one step back101 } else {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 102–110why, one step back102 lowerString := strings.ToLower(asString)103 caseSensitive = caseMode == CaseRespect ||104 caseMode == CaseSmart && lowerString != asString105 if !caseSensitive {106 asString = lowerString107 }108 }109110 ptr := &Pattern{Make --extended defaultJunegunn Choibefore adoption81a8869
lines 111–112why, one step back111 fuzzy: fuzzy,112 extended: extended,[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 113why, one step back113 caseSensitive: caseSensitive,Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 114why, one step back114 forward: forward,[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 115–119why, one step back115 text: []rune(asString),116 terms: terms,117 hasInvTerm: hasInvTerm,118 nth: nth,119 delimiter: delimiter,Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 120why, one step back120 procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 121–122why, one step back121122 ptr.procFun[termFuzzy] = algo.FuzzyMatchAllow ^EqualMatch$Junegunn Choibefore adoption0be4cea
line 123why, one step back123 ptr.procFun[termEqual] = algo.EqualMatch[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 124–131why, one step back124 ptr.procFun[termExact] = algo.ExactMatchNaive125 ptr.procFun[termPrefix] = algo.PrefixMatch126 ptr.procFun[termSuffix] = algo.SuffixMatch127128 _patternCache[asString] = ptr129 return ptr130}131Make --extended defaultJunegunn Choibefore adoption81a8869
line 132why, one step back132func parseTerms(fuzzy bool, caseMode Case, str string) []term {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 133–143why, one step back133 tokens := _splitRegex.Split(str, -1)134 terms := []term{}135 for _, token := range tokens {136 typ, inv, text := termFuzzy, false, token137 lowerText := strings.ToLower(text)138 caseSensitive := caseMode == CaseRespect ||139 caseMode == CaseSmart && text != lowerText140 if !caseSensitive {141 text = lowerText142 }143 origText := []rune(text)Make --extended defaultJunegunn Choibefore adoption81a8869
line 144why, one step back144 if !fuzzy {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 145–153why, one step back145 typ = termExact146 }147148 if strings.HasPrefix(text, "!") {149 inv = true150 text = text[1:]151 }152153 if strings.HasPrefix(text, "'") {Make --extended defaultJunegunn Choibefore adoption81a8869
lines 154–155why, one step back154 // Flip exactness155 if fuzzy {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 156–157why, one step back156 typ = termExact157 text = text[1:]Make --extended defaultJunegunn Choibefore adoption81a8869
line 158why, one step back158 } else {Make it possible to unquote the term in extended-exact modeJunegunn Choibefore adoption9017e29
lines 159–160why, one step back159 typ = termFuzzy160 text = text[1:][zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 161–162why, one step back161 }162 } else if strings.HasPrefix(text, "^") {Allow ^EqualMatch$Junegunn Choibefore adoption0be4cea
lines 163–169why, one step back163 if strings.HasSuffix(text, "$") {164 typ = termEqual165 text = text[1 : len(text)-1]166 } else {167 typ = termPrefix168 text = text[1:]169 }[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 170–188why, one step back170 } else if strings.HasSuffix(text, "$") {171 typ = termSuffix172 text = text[:len(text)-1]173 }174175 if len(text) > 0 {176 terms = append(terms, term{177 typ: typ,178 inv: inv,179 text: []rune(text),180 caseSensitive: caseSensitive,181 origText: origText})182 }183 }184 return terms185}186187// IsEmpty returns true if the pattern is effectively empty188func (p *Pattern) IsEmpty() bool {Make --extended defaultJunegunn Choibefore adoption81a8869
line 189why, one step back189 if !p.extended {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 190–201why, one step back190 return len(p.text) == 0191 }192 return len(p.terms) == 0193}194195// AsString returns the search query in string type196func (p *Pattern) AsString() string {197 return string(p.text)198}199200// CacheKey is used to build string to be used as the key of result cache201func (p *Pattern) CacheKey() string {Make --extended defaultJunegunn Choibefore adoption81a8869
line 202why, one step back202 if !p.extended {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 203–253why, one step back203 return p.AsString()204 }205 cacheableTerms := []string{}206 for _, term := range p.terms {207 if term.inv {208 continue209 }210 cacheableTerms = append(cacheableTerms, string(term.origText))211 }212 return strings.Join(cacheableTerms, " ")213}214215// Match returns the list of matches Items in the given Chunk216func (p *Pattern) Match(chunk *Chunk) []*Item {217 space := chunk218219 // ChunkCache: Exact match220 cacheKey := p.CacheKey()221 if !p.hasInvTerm { // Because we're excluding Inv-term from cache key222 if cached, found := _cache.Find(chunk, cacheKey); found {223 return cached224 }225 }226227 // ChunkCache: Prefix/suffix match228Loop:229 for idx := 1; idx < len(cacheKey); idx++ {230 // [---------| ] | [ |---------]231 // [--------| ] | [ |--------]232 // [-------| ] | [ |-------]233 prefix := cacheKey[:len(cacheKey)-idx]234 suffix := cacheKey[idx:]235 for _, substr := range [2]*string{&prefix, &suffix} {236 if cached, found := _cache.Find(chunk, *substr); found {237 cachedChunk := Chunk(cached)238 space = &cachedChunk239 break Loop240 }241 }242 }243244 matches := p.matchChunk(space)245246 if !p.hasInvTerm {247 _cache.Add(chunk, cacheKey, matches)248 }249 return matches250}251252func (p *Pattern) matchChunk(chunk *Chunk) []*Item {253 matches := []*Item{}Make --extended defaultJunegunn Choibefore adoption81a8869
line 254why, one step back254 if !p.extended {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 255why, one step back255 for _, item := range *chunk {Make --extended defaultJunegunn Choibefore adoption81a8869
line 256why, one step back256 if sidx, eidx, tlen := p.basicMatch(item); sidx >= 0 {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 257why, one step back257 matches = append(matches,Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 258why, one step back258 dupItem(item, []Offset{Offset{int32(sidx), int32(eidx), int32(tlen)}}))[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 259–272why, one step back259 }260 }261 } else {262 for _, item := range *chunk {263 if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {264 matches = append(matches, dupItem(item, offsets))265 }266 }267 }268 return matches269}270271// MatchItem returns true if the Item is a match272func (p *Pattern) MatchItem(item *Item) bool {Make --extended defaultJunegunn Choibefore adoption81a8869
lines 273–274why, one step back273 if !p.extended {274 sidx, _, _ := p.basicMatch(item)[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 275–292why, one step back275 return sidx >= 0276 }277 offsets := p.extendedMatch(item)278 return len(offsets) == len(p.terms)279}280281func dupItem(item *Item, offsets []Offset) *Item {282 sort.Sort(ByOrder(offsets))283 return &Item{284 text: item.text,285 origText: item.origText,286 transformed: item.transformed,287 index: item.index,288 offsets: offsets,289 colors: item.colors,290 rank: Rank{0, 0, item.index}}291}292Make --extended defaultJunegunn Choibefore adoption81a8869
line 293why, one step back293func (p *Pattern) basicMatch(item *Item) (int, int, int) {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 294why, one step back294 input := p.prepareInput(item)Make --extended defaultJunegunn Choibefore adoption81a8869
lines 295–298why, one step back295 if p.fuzzy {296 return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.forward, p.text)297 }298 return p.iter(algo.ExactMatchNaive, input, p.caseSensitive, p.forward, p.text)[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 299–305why, one step back299}300301func (p *Pattern) extendedMatch(item *Item) []Offset {302 input := p.prepareInput(item)303 offsets := []Offset{}304 for _, term := range p.terms {305 pfun := p.procFun[term.typ]Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 306why, one step back306 if sidx, eidx, tlen := p.iter(pfun, input, term.caseSensitive, p.forward, term.text); sidx >= 0 {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 307–309why, one step back307 if term.inv {308 break309 }Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 310why, one step back310 offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 311why, one step back311 } else if term.inv {Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 312why, one step back312 offsets = append(offsets, Offset{0, 0, 0})[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 313–317why, one step back313 }314 }315 return offsets316}317Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632
line 318why, one step back318func (p *Pattern) prepareInput(item *Item) []Token {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 319–322why, one step back319 if item.transformed != nil {320 return item.transformed321 }322Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632
line 323why, one step back323 var ret []Token[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 324–327why, one step back324 if len(p.nth) > 0 {325 tokens := Tokenize(item.text, p.delimiter)326 ret = Transform(tokens, p.nth)327 } else {Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 328why, one step back328 ret = []Token{Token{text: item.text, prefixLength: 0, trimLength: util.TrimLen(item.text)}}[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 329–333why, one step back329 }330 item.transformed = ret331 return ret332}333Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 334why, one step back334func (p *Pattern) iter(pfun func(bool, bool, []rune, []rune) (int, int),Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 335why, one step back335 tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int, int) {Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632
line 336why, one step back336 for _, part := range tokens {[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 337why, one step back337 prefixLength := part.prefixLengthFix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322
line 338why, one step back338 if sidx, eidx := pfun(caseSensitive, forward, part.text, pattern); sidx >= 0 {Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 339why, one step back339 return sidx + prefixLength, eidx + prefixLength, part.trimLength[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
lines 340–341why, one step back340 }341 }Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9
line 342why, one step back342 return -1, -1, -1 // math.MaxUint16[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63
line 343why, one step back343}