Skip to content
Myr

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 file

Take 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 back
1package fzf
23import (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 back
9 "github.com/junegunn/fzf/src/util"

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 10–27why, one step back
10)
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 termSuffix

Allow ^EqualMatch$Junegunn Choibefore adoption0be4cea

line 28why, one step back
28 termEqual

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 29–40why, one step back
29)
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 back
41 fuzzy bool
42 extended bool

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 43why, one step back
43 caseSensitive bool

Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322

line 44why, one step back
44 forward bool

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 45–47why, one step back
45 text []rune
46 terms []term47 hasInvTerm bool

Fix --with-nth performance; avoid regex if possibleJunegunn Choibefore adoption766427d

line 48why, one step back
48 delimiter Delimiter

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 49why, one step back
49 nth []Range

Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322

line 50why, one step back
50 procFun map[termType]func(bool, bool, []rune, []rune) (int, int)

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 51–66why, one step back
51}
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 since

Make --extended defaultJunegunn Choibefore adoption81a8869

line 67why, one step back
67 // 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 back
68 _patternCache = make(map[string]*Pattern)
69}7071func clearChunkCache() {72 _cache = NewChunkCache()73}7475// BuildPattern builds Pattern object from the given arguments

Make --extended defaultJunegunn Choibefore adoption81a8869

line 76why, one step back
76func BuildPattern(fuzzy bool, extended bool, caseMode Case, forward bool,

Fix --with-nth performance; avoid regex if possibleJunegunn Choibefore adoption766427d

line 77why, one step back
77 nth []Range, delimiter Delimiter, runes []rune) *Pattern {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 78–79why, one step back
78
79 var asString string

Make --extended defaultJunegunn Choibefore adoption81a8869

line 80why, one step back
80 if extended {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 81why, one step back
81 asString = strings.Trim(string(runes), " ")

Make --extended defaultJunegunn Choibefore adoption81a8869

line 82why, one step back
82 } else {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 83–93why, one step back
83 asString = string(runes)
84 }8586 cached, found := _patternCache[asString]87 if found {88 return cached89 }9091 caseSensitive, hasInvTerm := true, false92 terms := []term{}93

Make --extended defaultJunegunn Choibefore adoption81a8869

lines 94–95why, one step back
94 if extended {
95 terms = parseTerms(fuzzy, caseMode, asString)

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 96–100why, one step back
96 for _, term := range terms {
97 if term.inv {98 hasInvTerm = true99 }100 }

Make --extended defaultJunegunn Choibefore adoption81a8869

line 101why, one step back
101 } else {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 102–110why, one step back
102 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 back
111 fuzzy: fuzzy,
112 extended: extended,

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 113why, one step back
113 caseSensitive: caseSensitive,

Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322

line 114why, one step back
114 forward: forward,

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 115–119why, one step back
115 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 back
120 procFun: make(map[termType]func(bool, bool, []rune, []rune) (int, int))}

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 121–122why, one step back
121
122 ptr.procFun[termFuzzy] = algo.FuzzyMatch

Allow ^EqualMatch$Junegunn Choibefore adoption0be4cea

line 123why, one step back
123 ptr.procFun[termEqual] = algo.EqualMatch

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 124–131why, one step back
124 ptr.procFun[termExact] = algo.ExactMatchNaive
125 ptr.procFun[termPrefix] = algo.PrefixMatch126 ptr.procFun[termSuffix] = algo.SuffixMatch127128 _patternCache[asString] = ptr129 return ptr130}131

Make --extended defaultJunegunn Choibefore adoption81a8869

line 132why, one step back
132func parseTerms(fuzzy bool, caseMode Case, str string) []term {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 133–143why, one step back
133 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 back
144 if !fuzzy {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 145–153why, one step back
145 typ = termExact
146 }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 back
154 // Flip exactness
155 if fuzzy {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 156–157why, one step back
156 typ = termExact
157 text = text[1:]

Make --extended defaultJunegunn Choibefore adoption81a8869

line 158why, one step back
158 } else {

Make it possible to unquote the term in extended-exact modeJunegunn Choibefore adoption9017e29

lines 159–160why, one step back
159 typ = termFuzzy
160 text = text[1:]

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 161–162why, one step back
161 }
162 } else if strings.HasPrefix(text, "^") {

Allow ^EqualMatch$Junegunn Choibefore adoption0be4cea

lines 163–169why, one step back
163 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 back
170 } 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 back
189 if !p.extended {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 190–201why, one step back
190 return len(p.text) == 0
191 }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 back
202 if !p.extended {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 203–253why, one step back
203 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 back
254 if !p.extended {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 255why, one step back
255 for _, item := range *chunk {

Make --extended defaultJunegunn Choibefore adoption81a8869

line 256why, one step back
256 if sidx, eidx, tlen := p.basicMatch(item); sidx >= 0 {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 257why, one step back
257 matches = append(matches,

Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9

line 258why, one step back
258 dupItem(item, []Offset{Offset{int32(sidx), int32(eidx), int32(tlen)}}))

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 259–272why, one step back
259 }
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 back
273 if !p.extended {
274 sidx, _, _ := p.basicMatch(item)

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 275–292why, one step back
275 return sidx >= 0
276 }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}292

Make --extended defaultJunegunn Choibefore adoption81a8869

line 293why, one step back
293func (p *Pattern) basicMatch(item *Item) (int, int, int) {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 294why, one step back
294 input := p.prepareInput(item)

Make --extended defaultJunegunn Choibefore adoption81a8869

lines 295–298why, one step back
295 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 back
299}
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 back
306 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 back
307 if term.inv {
308 break309 }

Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9

line 310why, one step back
310 offsets = append(offsets, Offset{int32(sidx), int32(eidx), int32(tlen)})

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 311why, one step back
311 } else if term.inv {

Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9

line 312why, one step back
312 offsets = append(offsets, Offset{0, 0, 0})

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 313–317why, one step back
313 }
314 }315 return offsets316}317

Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632

line 318why, one step back
318func (p *Pattern) prepareInput(item *Item) []Token {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 319–322why, one step back
319 if item.transformed != nil {
320 return item.transformed321 }322

Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632

line 323why, one step back
323 var ret []Token

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 324–327why, one step back
324 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 back
328 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 back
329 }
330 item.transformed = ret331 return ret332}333

Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322

line 334why, one step back
334func (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 back
335 tokens []Token, caseSensitive bool, forward bool, pattern []rune) (int, int, int) {

Performance tuning - eager rune array conversionJunegunn Choibefore adoption0ea6632

line 336why, one step back
336 for _, part := range tokens {

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 337why, one step back
337 prefixLength := part.prefixLength

Fix #344 - Backward scan when `--tiebreak=end`Junegunn Choibefore adoption6444322

line 338why, one step back
338 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 back
339 return sidx + prefixLength, eidx + prefixLength, part.trimLength

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

lines 340–341why, one step back
340 }
341 }

Use trimmed length when --nth is used with --tiebreak=lengthJunegunn Choibefore adoption92a75c9

line 342why, one step back
342 return -1, -1, -1 // math.MaxUint16

[zsh-completion] "\find" to bypass aliasJunegunn Choibefore adoption5a17a63

line 343why, one step back
343}