Skip to content
Myr

pattern.go

This file

Path

src/pattern.go

328 lines · 7.4 KiB · Go

Last changed by

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

History of this file

Take it

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 1–29why, one step back
1package fzf
23import (4 "regexp"5 "sort"6 "strings"78 "github.com/junegunn/fzf/src/algo"9)1011// fuzzy12// 'exact13// ^exact-prefix14// exact-suffix$15// !not-fuzzy16// !'not-exact17// !^not-exact-prefix18// !not-exact-suffix$1920type termType int2122const (23 termFuzzy termType = iota24 termExact25 termPrefix26 termSuffix27)2829type term struct {

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

lines 30–34why, one step back
30 typ termType
31 inv bool32 text []rune33 caseSensitive bool34 origText []rune

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 35–45why, one step back
35}
3637// Pattern represents search pattern38type Pattern struct {39 mode Mode40 caseSensitive bool41 text []rune42 terms []term43 hasInvTerm bool44 delimiter *regexp.Regexp45 nth []Range

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

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

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 47–56why, one step back
47}
4849var (50 _patternCache map[string]*Pattern51 _splitRegex *regexp.Regexp52 _cache ChunkCache53)5455func init() {56 _splitRegex = regexp.MustCompile("\\s+")

Implement --toggle-sort option (#173)Junegunn Choibefore adoption50292ad

lines 57–58why, one step back
57 clearPatternCache()
58 clearChunkCache()

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 59–61why, one step back
59}
6061func clearPatternCache() {

Implement --toggle-sort option (#173)Junegunn Choibefore adoption50292ad

lines 62–63why, one step back
62 // We can uniquely identify the pattern for a given string since
63 // mode and caseMode do not change while the program is running

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

line 64why, one step back
64 _patternCache = make(map[string]*Pattern)

Implement --toggle-sort option (#173)Junegunn Choibefore adoption50292ad

lines 65–68why, one step back
65}
6667func clearChunkCache() {68 _cache = NewChunkCache()

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 69–92why, one step back
69}
7071// BuildPattern builds Pattern object from the given arguments72func BuildPattern(mode Mode, caseMode Case,73 nth []Range, delimiter *regexp.Regexp, runes []rune) *Pattern {7475 var asString string76 switch mode {77 case ModeExtended, ModeExtendedExact:78 asString = strings.Trim(string(runes), " ")79 default:80 asString = string(runes)81 }8283 cached, found := _patternCache[asString]84 if found {85 return cached86 }8788 caseSensitive, hasInvTerm := true, false89 terms := []term{}9091 switch mode {92 case ModeExtended, ModeExtendedExact:

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 93why, one step back
93 terms = parseTerms(mode, caseMode, asString)

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 94–98why, one step back
94 for _, term := range terms {
95 if term.inv {96 hasInvTerm = true97 }98 }

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

lines 99–105why, one step back
99 default:
100 lowerString := strings.ToLower(asString)101 caseSensitive = caseMode == CaseRespect ||102 caseMode == CaseSmart && lowerString != asString103 if !caseSensitive {104 asString = lowerString105 }

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 106–110why, one step back
106 }
107108 ptr := &Pattern{109 mode: mode,110 caseSensitive: caseSensitive,

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 111why, one step back
111 text: []rune(asString),

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 112–115why, one step back
112 terms: terms,
113 hasInvTerm: hasInvTerm,114 nth: nth,115 delimiter: delimiter,

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

line 116why, one step back
116 procFun: make(map[termType]func(bool, *[]rune, []rune) (int, int))}

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 117–126why, one step back
117
118 ptr.procFun[termFuzzy] = algo.FuzzyMatch119 ptr.procFun[termExact] = algo.ExactMatchNaive120 ptr.procFun[termPrefix] = algo.PrefixMatch121 ptr.procFun[termSuffix] = algo.SuffixMatch122123 _patternCache[asString] = ptr124 return ptr125}126

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 127why, one step back
127func parseTerms(mode Mode, caseMode Case, str string) []term {

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 128–131why, one step back
128 tokens := _splitRegex.Split(str, -1)
129 terms := []term{}130 for _, token := range tokens {131 typ, inv, text := termFuzzy, false, token

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

lines 132–137why, one step back
132 lowerText := strings.ToLower(text)
133 caseSensitive := caseMode == CaseRespect ||134 caseMode == CaseSmart && text != lowerText135 if !caseSensitive {136 text = lowerText137 }

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 138–162why, one step back
138 origText := []rune(text)
139 if mode == ModeExtendedExact {140 typ = termExact141 }142143 if strings.HasPrefix(text, "!") {144 inv = true145 text = text[1:]146 }147148 if strings.HasPrefix(text, "'") {149 if mode == ModeExtended {150 typ = termExact151 text = text[1:]152 }153 } else if strings.HasPrefix(text, "^") {154 typ = termPrefix155 text = text[1:]156 } else if strings.HasSuffix(text, "$") {157 typ = termSuffix158 text = text[:len(text)-1]159 }160161 if len(text) > 0 {162 terms = append(terms, term{

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

lines 163–167why, one step back
163 typ: typ,
164 inv: inv,165 text: []rune(text),166 caseSensitive: caseSensitive,167 origText: origText})

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 168–229why, one step back
168 }
169 }170 return terms171}172173// IsEmpty returns true if the pattern is effectively empty174func (p *Pattern) IsEmpty() bool {175 if p.mode == ModeFuzzy {176 return len(p.text) == 0177 }178 return len(p.terms) == 0179}180181// AsString returns the search query in string type182func (p *Pattern) AsString() string {183 return string(p.text)184}185186// CacheKey is used to build string to be used as the key of result cache187func (p *Pattern) CacheKey() string {188 if p.mode == ModeFuzzy {189 return p.AsString()190 }191 cacheableTerms := []string{}192 for _, term := range p.terms {193 if term.inv {194 continue195 }196 cacheableTerms = append(cacheableTerms, string(term.origText))197 }198 return strings.Join(cacheableTerms, " ")199}200201// Match returns the list of matches Items in the given Chunk202func (p *Pattern) Match(chunk *Chunk) []*Item {203 space := chunk204205 // ChunkCache: Exact match206 cacheKey := p.CacheKey()207 if !p.hasInvTerm { // Because we're excluding Inv-term from cache key208 if cached, found := _cache.Find(chunk, cacheKey); found {209 return cached210 }211 }212213 // ChunkCache: Prefix/suffix match214Loop:215 for idx := 1; idx < len(cacheKey); idx++ {216 // [---------| ] | [ |---------]217 // [--------| ] | [ |--------]218 // [-------| ] | [ |-------]219 prefix := cacheKey[:len(cacheKey)-idx]220 suffix := cacheKey[idx:]221 for _, substr := range [2]*string{&prefix, &suffix} {222 if cached, found := _cache.Find(chunk, *substr); found {223 cachedChunk := Chunk(cached)224 space = &cachedChunk225 break Loop226 }227 }228 }229

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

line 230why, one step back
230 matches := p.matchChunk(space)

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 231–237why, one step back
231
232 if !p.hasInvTerm {233 _cache.Add(chunk, cacheKey, matches)234 }235 return matches236}237

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

lines 238–266why, one step back
238func (p *Pattern) matchChunk(chunk *Chunk) []*Item {
239 matches := []*Item{}240 if p.mode == ModeFuzzy {241 for _, item := range *chunk {242 if sidx, eidx := p.fuzzyMatch(item); sidx >= 0 {243 matches = append(matches,244 dupItem(item, []Offset{Offset{int32(sidx), int32(eidx)}}))245 }246 }247 } else {248 for _, item := range *chunk {249 if offsets := p.extendedMatch(item); len(offsets) == len(p.terms) {250 matches = append(matches, dupItem(item, offsets))251 }252 }253 }254 return matches255}256257// MatchItem returns true if the Item is a match258func (p *Pattern) MatchItem(item *Item) bool {259 if p.mode == ModeFuzzy {260 sidx, _ := p.fuzzyMatch(item)261 return sidx >= 0262 }263 offsets := p.extendedMatch(item)264 return len(offsets) == len(p.terms)265}266

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 267–274why, one step back
267func dupItem(item *Item, offsets []Offset) *Item {
268 sort.Sort(ByOrder(offsets))269 return &Item{270 text: item.text,271 origText: item.origText,272 transformed: item.transformed,273 index: item.index,274 offsets: offsets,

Add support for ANSI color codesJunegunn Choibefore adoptione70a2a5

line 275why, one step back
275 colors: item.colors,

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 276–278why, one step back
276 rank: Rank{0, 0, item.index}}
277}278

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

lines 279–280why, one step back
279func (p *Pattern) fuzzyMatch(item *Item) (int, int) {
280 input := p.prepareInput(item)

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 281why, one step back
281 return p.iter(algo.FuzzyMatch, input, p.caseSensitive, p.text)

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 282–283why, one step back
282}
283

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

lines 284–288why, one step back
284func (p *Pattern) extendedMatch(item *Item) []Offset {
285 input := p.prepareInput(item)286 offsets := []Offset{}287 for _, term := range p.terms {288 pfun := p.procFun[term.typ]

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 289why, one step back
289 if sidx, eidx := p.iter(pfun, input, term.caseSensitive, term.text); sidx >= 0 {

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

lines 290–291why, one step back
290 if term.inv {
291 break

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

line 292why, one step back
292 }

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

lines 293–295why, one step back
293 offsets = append(offsets, Offset{int32(sidx), int32(eidx)})
294 } else if term.inv {295 offsets = append(offsets, Offset{0, 0})

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 296–297why, one step back
296 }
297 }

Merge pull request #134 from junegunn/develJunegunn Choibefore adoptiond4b41c5

line 298why, one step back
298 return offsets

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 299–300why, one step back
299}
300

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

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

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 302–305why, one step back
302 if item.transformed != nil {
303 return item.transformed304 }305

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

line 306why, one step back
306 var ret *[]Token

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 307–310why, one step back
307 if len(p.nth) > 0 {
308 tokens := Tokenize(item.text, p.delimiter)309 ret = Transform(tokens, p.nth)310 } else {

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

lines 311–312why, one step back
311 runes := []rune(*item.text)
312 trans := []Token{Token{text: &runes, prefixLength: 0}}

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 313–318why, one step back
313 ret = &trans
314 }315 item.transformed = ret316 return ret317}318

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

line 319why, one step back
319func (p *Pattern) iter(pfun func(bool, *[]rune, []rune) (int, int),

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 320why, one step back
320 tokens *[]Token, caseSensitive bool, pattern []rune) (int, int) {

Improvements in performance and memory usageJunegunn Choibefore adoption2fe1e28

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

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

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

Smart-case for each term in extended-search modeJunegunn Choibefore adoptione82eb27

line 323why, one step back
323 if sidx, eidx := pfun(caseSensitive, part.text, pattern); sidx >= 0 {

Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3

lines 324–328why, one step back
324 return sidx + prefixLength, eidx + prefixLength
325 }326 }327 return -1, -1328}