summaryrefslogtreecommitdiff
path: root/src/pattern.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-05-07 01:06:42 +0900
committerGitHub <noreply@github.com>2024-05-07 01:06:42 +0900
commite8405f40fe2eb3675f1cb4f69e825eff5f13f269 (patch)
treec917367f1f0098939f9cdf7376a2a135907024fc /src/pattern.go
parent065b9e6fb2ce3e6e50ff423c3786989afa04ee14 (diff)
downloadfzf-e8405f40fe2eb3675f1cb4f69e825eff5f13f269.tar.gz
Refactor the code so that fzf can be used as a library (#3769)
Diffstat (limited to 'src/pattern.go')
-rw-r--r--src/pattern.go34
1 files changed, 11 insertions, 23 deletions
diff --git a/src/pattern.go b/src/pattern.go
index bf92ca19..cbe73dc4 100644
--- a/src/pattern.go
+++ b/src/pattern.go
@@ -60,32 +60,17 @@ type Pattern struct {
delimiter Delimiter
nth []Range
procFun map[termType]algo.Algo
+ cache *ChunkCache
}
-var (
- _patternCache map[string]*Pattern
- _splitRegex *regexp.Regexp
- _cache ChunkCache
-)
+var _splitRegex *regexp.Regexp
func init() {
_splitRegex = regexp.MustCompile(" +")
- clearPatternCache()
- clearChunkCache()
-}
-
-func clearPatternCache() {
- // We can uniquely identify the pattern for a given string since
- // search mode and caseMode do not change while the program is running
- _patternCache = make(map[string]*Pattern)
-}
-
-func clearChunkCache() {
- _cache = NewChunkCache()
}
// BuildPattern builds Pattern object from the given arguments
-func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,
+func BuildPattern(cache *ChunkCache, patternCache map[string]*Pattern, fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,
withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
var asString string
@@ -98,7 +83,9 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case,
asString = string(runes)
}
- cached, found := _patternCache[asString]
+ // We can uniquely identify the pattern for a given string since
+ // search mode and caseMode do not change while the program is running
+ cached, found := patternCache[asString]
if found {
return cached
}
@@ -153,6 +140,7 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case,
cacheable: cacheable,
nth: nth,
delimiter: delimiter,
+ cache: cache,
procFun: make(map[termType]algo.Algo)}
ptr.cacheKey = ptr.buildCacheKey()
@@ -162,7 +150,7 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case,
ptr.procFun[termPrefix] = algo.PrefixMatch
ptr.procFun[termSuffix] = algo.SuffixMatch
- _patternCache[asString] = ptr
+ patternCache[asString] = ptr
return ptr
}
@@ -282,18 +270,18 @@ func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []Result {
// ChunkCache: Exact match
cacheKey := p.CacheKey()
if p.cacheable {
- if cached := _cache.Lookup(chunk, cacheKey); cached != nil {
+ if cached := p.cache.Lookup(chunk, cacheKey); cached != nil {
return cached
}
}
// Prefix/suffix cache
- space := _cache.Search(chunk, cacheKey)
+ space := p.cache.Search(chunk, cacheKey)
matches := p.matchChunk(chunk, space, slab)
if p.cacheable {
- _cache.Add(chunk, cacheKey, matches)
+ p.cache.Add(chunk, cacheKey, matches)
}
return matches
}