From e8405f40fe2eb3675f1cb4f69e825eff5f13f269 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 7 May 2024 01:06:42 +0900 Subject: Refactor the code so that fzf can be used as a library (#3769) --- src/pattern.go | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'src/pattern.go') 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 } -- cgit v1.2.3