2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
2018-02-17 22:01:06 +00:00
|
|
|
"fmt"
|
2015-01-01 19:49:30 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-01-12 03:56:17 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/algo"
|
2015-10-02 09:40:20 +00:00
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// fuzzy
|
|
|
|
// 'exact
|
2017-08-08 04:22:30 +00:00
|
|
|
// ^prefix-exact
|
|
|
|
// suffix-exact$
|
|
|
|
// !inverse-exact
|
|
|
|
// !'inverse-fuzzy
|
|
|
|
// !^inverse-prefix-exact
|
|
|
|
// !inverse-suffix-exact$
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
type termType int
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
const (
|
2015-01-11 18:01:24 +00:00
|
|
|
termFuzzy termType = iota
|
|
|
|
termExact
|
|
|
|
termPrefix
|
|
|
|
termSuffix
|
2015-06-08 14:16:31 +00:00
|
|
|
termEqual
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
type term struct {
|
2015-04-21 13:18:05 +00:00
|
|
|
typ termType
|
|
|
|
inv bool
|
|
|
|
text []rune
|
|
|
|
caseSensitive bool
|
2020-07-28 04:06:57 +00:00
|
|
|
normalize bool
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 22:01:06 +00:00
|
|
|
// String returns the string representation of a term.
|
|
|
|
func (t term) String() string {
|
|
|
|
return fmt.Sprintf("term{typ: %d, inv: %v, text: []rune(%q), caseSensitive: %v}", t.typ, t.inv, string(t.text), t.caseSensitive)
|
|
|
|
}
|
|
|
|
|
2015-11-08 15:58:20 +00:00
|
|
|
type termSet []term
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Pattern represents search pattern
|
2015-01-01 19:49:30 +00:00
|
|
|
type Pattern struct {
|
2015-11-03 13:49:32 +00:00
|
|
|
fuzzy bool
|
2016-09-07 00:58:18 +00:00
|
|
|
fuzzyAlgo algo.Algo
|
2015-11-03 13:49:32 +00:00
|
|
|
extended bool
|
2015-01-01 19:49:30 +00:00
|
|
|
caseSensitive bool
|
2017-01-08 18:12:23 +00:00
|
|
|
normalize bool
|
2015-09-12 02:37:55 +00:00
|
|
|
forward bool
|
2015-01-01 19:49:30 +00:00
|
|
|
text []rune
|
2015-11-08 15:58:20 +00:00
|
|
|
termSets []termSet
|
2018-12-19 14:05:29 +00:00
|
|
|
sortable bool
|
2015-11-08 15:58:20 +00:00
|
|
|
cacheable bool
|
2017-08-10 14:18:52 +00:00
|
|
|
cacheKey string
|
2015-08-10 09:34:20 +00:00
|
|
|
delimiter Delimiter
|
2015-01-01 19:49:30 +00:00
|
|
|
nth []Range
|
2016-09-07 00:58:18 +00:00
|
|
|
procFun map[termType]algo.Algo
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2017-08-11 04:09:33 +00:00
|
|
|
_patternCache map[string]*Pattern
|
|
|
|
_splitRegex *regexp.Regexp
|
|
|
|
_cache ChunkCache
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-08-09 14:25:32 +00:00
|
|
|
_splitRegex = regexp.MustCompile(" +")
|
2015-03-31 13:05:02 +00:00
|
|
|
clearPatternCache()
|
|
|
|
clearChunkCache()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clearPatternCache() {
|
2015-03-31 13:05:02 +00:00
|
|
|
// We can uniquely identify the pattern for a given string since
|
2015-11-03 13:49:32 +00:00
|
|
|
// search mode and caseMode do not change while the program is running
|
2015-01-01 19:49:30 +00:00
|
|
|
_patternCache = make(map[string]*Pattern)
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:05:02 +00:00
|
|
|
func clearChunkCache() {
|
|
|
|
_cache = NewChunkCache()
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// BuildPattern builds Pattern object from the given arguments
|
2017-01-08 18:12:23 +00:00
|
|
|
func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool,
|
2016-08-19 17:06:57 +00:00
|
|
|
cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
var asString string
|
2015-11-03 13:49:32 +00:00
|
|
|
if extended {
|
2017-08-09 14:25:32 +00:00
|
|
|
asString = strings.TrimLeft(string(runes), " ")
|
|
|
|
for strings.HasSuffix(asString, " ") && !strings.HasSuffix(asString, "\\ ") {
|
|
|
|
asString = asString[:len(asString)-1]
|
|
|
|
}
|
2015-11-03 13:49:32 +00:00
|
|
|
} else {
|
2015-01-01 19:49:30 +00:00
|
|
|
asString = string(runes)
|
|
|
|
}
|
|
|
|
|
|
|
|
cached, found := _patternCache[asString]
|
|
|
|
if found {
|
|
|
|
return cached
|
|
|
|
}
|
|
|
|
|
2016-08-19 17:06:57 +00:00
|
|
|
caseSensitive := true
|
2018-12-19 14:05:29 +00:00
|
|
|
sortable := true
|
2015-11-08 15:58:20 +00:00
|
|
|
termSets := []termSet{}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-11-03 13:49:32 +00:00
|
|
|
if extended {
|
2017-01-09 00:52:17 +00:00
|
|
|
termSets = parseTerms(fuzzy, caseMode, normalize, asString)
|
2018-12-19 14:05:29 +00:00
|
|
|
// We should not sort the result if there are only inverse search terms
|
|
|
|
sortable = false
|
2015-11-08 15:58:20 +00:00
|
|
|
Loop:
|
|
|
|
for _, termSet := range termSets {
|
|
|
|
for idx, term := range termSet {
|
2018-12-19 14:05:29 +00:00
|
|
|
if !term.inv {
|
|
|
|
sortable = true
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
// If the query contains inverse search terms or OR operators,
|
|
|
|
// we cannot cache the search scope
|
2017-08-08 04:22:30 +00:00
|
|
|
if !cacheable || idx > 0 || term.inv || fuzzy && term.typ != termFuzzy || !fuzzy && term.typ != termExact {
|
2015-11-08 15:58:20 +00:00
|
|
|
cacheable = false
|
2018-12-19 14:05:29 +00:00
|
|
|
if sortable {
|
|
|
|
// Can't break until we see at least one non-inverse term
|
|
|
|
break Loop
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-03 13:49:32 +00:00
|
|
|
} else {
|
2015-04-21 13:18:05 +00:00
|
|
|
lowerString := strings.ToLower(asString)
|
2020-07-28 04:06:57 +00:00
|
|
|
normalize = normalize &&
|
|
|
|
lowerString == string(algo.NormalizeRunes([]rune(lowerString)))
|
2015-04-21 13:18:05 +00:00
|
|
|
caseSensitive = caseMode == CaseRespect ||
|
|
|
|
caseMode == CaseSmart && lowerString != asString
|
|
|
|
if !caseSensitive {
|
|
|
|
asString = lowerString
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr := &Pattern{
|
2015-11-03 13:49:32 +00:00
|
|
|
fuzzy: fuzzy,
|
2016-09-07 00:58:18 +00:00
|
|
|
fuzzyAlgo: fuzzyAlgo,
|
2015-11-03 13:49:32 +00:00
|
|
|
extended: extended,
|
2015-01-01 19:49:30 +00:00
|
|
|
caseSensitive: caseSensitive,
|
2017-01-08 18:12:23 +00:00
|
|
|
normalize: normalize,
|
2015-09-12 02:37:55 +00:00
|
|
|
forward: forward,
|
2015-04-21 13:18:05 +00:00
|
|
|
text: []rune(asString),
|
2015-11-08 15:58:20 +00:00
|
|
|
termSets: termSets,
|
2018-12-19 14:05:29 +00:00
|
|
|
sortable: sortable,
|
2015-11-08 15:58:20 +00:00
|
|
|
cacheable: cacheable,
|
2015-01-01 19:49:30 +00:00
|
|
|
nth: nth,
|
|
|
|
delimiter: delimiter,
|
2016-09-07 00:58:18 +00:00
|
|
|
procFun: make(map[termType]algo.Algo)}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2017-08-10 14:18:52 +00:00
|
|
|
ptr.cacheKey = ptr.buildCacheKey()
|
2016-09-07 00:58:18 +00:00
|
|
|
ptr.procFun[termFuzzy] = fuzzyAlgo
|
2015-06-08 14:16:31 +00:00
|
|
|
ptr.procFun[termEqual] = algo.EqualMatch
|
2015-01-12 03:56:17 +00:00
|
|
|
ptr.procFun[termExact] = algo.ExactMatchNaive
|
|
|
|
ptr.procFun[termPrefix] = algo.PrefixMatch
|
|
|
|
ptr.procFun[termSuffix] = algo.SuffixMatch
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
_patternCache[asString] = ptr
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
2017-01-09 00:52:17 +00:00
|
|
|
func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet {
|
2017-08-09 14:25:32 +00:00
|
|
|
str = strings.Replace(str, "\\ ", "\t", -1)
|
2015-01-01 19:49:30 +00:00
|
|
|
tokens := _splitRegex.Split(str, -1)
|
2015-11-08 15:58:20 +00:00
|
|
|
sets := []termSet{}
|
|
|
|
set := termSet{}
|
|
|
|
switchSet := false
|
2017-08-10 15:07:18 +00:00
|
|
|
afterBar := false
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, token := range tokens {
|
2017-08-09 14:25:32 +00:00
|
|
|
typ, inv, text := termFuzzy, false, strings.Replace(token, "\t", " ", -1)
|
2015-04-21 13:18:05 +00:00
|
|
|
lowerText := strings.ToLower(text)
|
|
|
|
caseSensitive := caseMode == CaseRespect ||
|
|
|
|
caseMode == CaseSmart && text != lowerText
|
2020-07-28 04:06:57 +00:00
|
|
|
normalizeTerm := normalize &&
|
|
|
|
lowerText == string(algo.NormalizeRunes([]rune(lowerText)))
|
2015-04-21 13:18:05 +00:00
|
|
|
if !caseSensitive {
|
|
|
|
text = lowerText
|
|
|
|
}
|
2015-11-03 13:49:32 +00:00
|
|
|
if !fuzzy {
|
2015-01-11 18:01:24 +00:00
|
|
|
typ = termExact
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:07:18 +00:00
|
|
|
if len(set) > 0 && !afterBar && text == "|" {
|
2015-11-08 15:58:20 +00:00
|
|
|
switchSet = false
|
2017-08-10 15:07:18 +00:00
|
|
|
afterBar = true
|
2015-11-08 15:58:20 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-08-10 15:07:18 +00:00
|
|
|
afterBar = false
|
2015-11-08 15:58:20 +00:00
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
if strings.HasPrefix(text, "!") {
|
|
|
|
inv = true
|
2016-10-03 17:09:03 +00:00
|
|
|
typ = termExact
|
2015-01-01 19:49:30 +00:00
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:59:40 +00:00
|
|
|
if text != "$" && strings.HasSuffix(text, "$") {
|
2017-08-11 04:09:33 +00:00
|
|
|
typ = termSuffix
|
|
|
|
text = text[:len(text)-1]
|
2017-08-09 14:25:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:09:33 +00:00
|
|
|
if strings.HasPrefix(text, "'") {
|
2015-11-03 13:49:32 +00:00
|
|
|
// Flip exactness
|
2016-10-03 17:09:03 +00:00
|
|
|
if fuzzy && !inv {
|
2015-01-11 18:01:24 +00:00
|
|
|
typ = termExact
|
2015-01-01 19:49:30 +00:00
|
|
|
text = text[1:]
|
2015-11-03 13:49:32 +00:00
|
|
|
} else {
|
2015-09-12 02:00:30 +00:00
|
|
|
typ = termFuzzy
|
|
|
|
text = text[1:]
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(text, "^") {
|
2017-08-09 14:25:32 +00:00
|
|
|
if typ == termSuffix {
|
2015-06-08 14:16:31 +00:00
|
|
|
typ = termEqual
|
|
|
|
} else {
|
|
|
|
typ = termPrefix
|
|
|
|
}
|
2017-08-09 14:25:32 +00:00
|
|
|
text = text[1:]
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(text) > 0 {
|
2015-11-08 15:58:20 +00:00
|
|
|
if switchSet {
|
|
|
|
sets = append(sets, set)
|
|
|
|
set = termSet{}
|
|
|
|
}
|
2017-01-09 00:52:17 +00:00
|
|
|
textRunes := []rune(text)
|
2020-07-28 04:06:57 +00:00
|
|
|
if normalizeTerm {
|
2017-01-09 00:52:17 +00:00
|
|
|
textRunes = algo.NormalizeRunes(textRunes)
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
set = append(set, term{
|
2015-04-21 13:18:05 +00:00
|
|
|
typ: typ,
|
|
|
|
inv: inv,
|
2017-01-09 00:52:17 +00:00
|
|
|
text: textRunes,
|
2020-07-28 04:06:57 +00:00
|
|
|
caseSensitive: caseSensitive,
|
|
|
|
normalize: normalizeTerm})
|
2015-11-08 15:58:20 +00:00
|
|
|
switchSet = true
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
if len(set) > 0 {
|
|
|
|
sets = append(sets, set)
|
|
|
|
}
|
|
|
|
return sets
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// IsEmpty returns true if the pattern is effectively empty
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) IsEmpty() bool {
|
2015-11-03 13:49:32 +00:00
|
|
|
if !p.extended {
|
2015-01-01 19:49:30 +00:00
|
|
|
return len(p.text) == 0
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
return len(p.termSets) == 0
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// AsString returns the search query in string type
|
2015-01-01 19:49:30 +00:00
|
|
|
func (p *Pattern) AsString() string {
|
|
|
|
return string(p.text)
|
|
|
|
}
|
|
|
|
|
2017-08-10 14:18:52 +00:00
|
|
|
func (p *Pattern) buildCacheKey() string {
|
2015-11-03 13:49:32 +00:00
|
|
|
if !p.extended {
|
2015-01-01 19:49:30 +00:00
|
|
|
return p.AsString()
|
|
|
|
}
|
|
|
|
cacheableTerms := []string{}
|
2015-11-08 15:58:20 +00:00
|
|
|
for _, termSet := range p.termSets {
|
2016-04-23 18:30:51 +00:00
|
|
|
if len(termSet) == 1 && !termSet[0].inv && (p.fuzzy || termSet[0].typ == termExact) {
|
2017-08-08 04:22:30 +00:00
|
|
|
cacheableTerms = append(cacheableTerms, string(termSet[0].text))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 14:25:32 +00:00
|
|
|
return strings.Join(cacheableTerms, "\t")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 14:18:52 +00:00
|
|
|
// CacheKey is used to build string to be used as the key of result cache
|
|
|
|
func (p *Pattern) CacheKey() string {
|
|
|
|
return p.cacheKey
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Match returns the list of matches Items in the given Chunk
|
2017-07-17 18:10:49 +00:00
|
|
|
func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []Result {
|
2015-01-01 19:49:30 +00:00
|
|
|
// ChunkCache: Exact match
|
|
|
|
cacheKey := p.CacheKey()
|
2015-11-08 15:58:20 +00:00
|
|
|
if p.cacheable {
|
2017-07-16 14:31:19 +00:00
|
|
|
if cached := _cache.Lookup(chunk, cacheKey); cached != nil {
|
2015-01-01 19:49:30 +00:00
|
|
|
return cached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:39:32 +00:00
|
|
|
// Prefix/suffix cache
|
2017-07-15 10:35:27 +00:00
|
|
|
space := _cache.Search(chunk, cacheKey)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
matches := p.matchChunk(chunk, space, slab)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-11-08 15:58:20 +00:00
|
|
|
if p.cacheable {
|
2015-01-01 19:49:30 +00:00
|
|
|
_cache.Add(chunk, cacheKey, matches)
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func (p *Pattern) matchChunk(chunk *Chunk, space []Result, slab *util.Slab) []Result {
|
|
|
|
matches := []Result{}
|
2016-08-18 17:39:32 +00:00
|
|
|
|
|
|
|
if space == nil {
|
2017-08-14 16:10:41 +00:00
|
|
|
for idx := 0; idx < chunk.count; idx++ {
|
|
|
|
if match, _, _ := p.MatchItem(&chunk.items[idx], false, slab); match != nil {
|
2017-07-17 18:10:49 +00:00
|
|
|
matches = append(matches, *match)
|
2015-03-01 02:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-18 17:39:32 +00:00
|
|
|
for _, result := range space {
|
2016-09-07 00:58:18 +00:00
|
|
|
if match, _, _ := p.MatchItem(result.item, false, slab); match != nil {
|
2017-07-17 18:10:49 +00:00
|
|
|
matches = append(matches, *match)
|
2015-03-01 02:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchItem returns true if the Item is a match
|
2016-09-07 00:58:18 +00:00
|
|
|
func (p *Pattern) MatchItem(item *Item, withPos bool, slab *util.Slab) (*Result, []Offset, *[]int) {
|
2016-08-18 17:39:32 +00:00
|
|
|
if p.extended {
|
2017-06-02 04:25:35 +00:00
|
|
|
if offsets, bonus, pos := p.extendedMatch(item, withPos, slab); len(offsets) == len(p.termSets) {
|
2017-07-17 18:10:49 +00:00
|
|
|
result := buildResult(item, offsets, bonus)
|
|
|
|
return &result, offsets, pos
|
2016-08-18 17:39:32 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
return nil, nil, nil
|
2015-03-01 02:16:38 +00:00
|
|
|
}
|
2017-06-02 04:25:35 +00:00
|
|
|
offset, bonus, pos := p.basicMatch(item, withPos, slab)
|
2016-08-18 17:39:32 +00:00
|
|
|
if sidx := offset[0]; sidx >= 0 {
|
2016-08-19 16:46:54 +00:00
|
|
|
offsets := []Offset{offset}
|
2017-07-17 18:10:49 +00:00
|
|
|
result := buildResult(item, offsets, bonus)
|
|
|
|
return &result, offsets, pos
|
2016-08-18 17:39:32 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
return nil, nil, nil
|
2015-01-10 16:47:46 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 04:25:35 +00:00
|
|
|
func (p *Pattern) basicMatch(item *Item, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
|
2017-07-30 18:21:35 +00:00
|
|
|
var input []Token
|
|
|
|
if len(p.nth) == 0 {
|
|
|
|
input = []Token{Token{text: &item.text, prefixLength: 0}}
|
|
|
|
} else {
|
|
|
|
input = p.transformInput(item)
|
|
|
|
}
|
2015-11-03 13:49:32 +00:00
|
|
|
if p.fuzzy {
|
2017-01-08 18:12:23 +00:00
|
|
|
return p.iter(p.fuzzyAlgo, input, p.caseSensitive, p.normalize, p.forward, p.text, withPos, slab)
|
2015-11-03 13:49:32 +00:00
|
|
|
}
|
2017-01-08 18:12:23 +00:00
|
|
|
return p.iter(algo.ExactMatchNaive, input, p.caseSensitive, p.normalize, p.forward, p.text, withPos, slab)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 04:25:35 +00:00
|
|
|
func (p *Pattern) extendedMatch(item *Item, withPos bool, slab *util.Slab) ([]Offset, int, *[]int) {
|
2017-07-30 18:21:35 +00:00
|
|
|
var input []Token
|
|
|
|
if len(p.nth) == 0 {
|
|
|
|
input = []Token{Token{text: &item.text, prefixLength: 0}}
|
|
|
|
} else {
|
|
|
|
input = p.transformInput(item)
|
|
|
|
}
|
2015-03-01 02:16:38 +00:00
|
|
|
offsets := []Offset{}
|
2016-09-07 00:58:18 +00:00
|
|
|
var totalScore int
|
|
|
|
var allPos *[]int
|
|
|
|
if withPos {
|
|
|
|
allPos = &[]int{}
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
for _, termSet := range p.termSets {
|
2016-08-18 17:39:32 +00:00
|
|
|
var offset Offset
|
2016-09-07 00:58:18 +00:00
|
|
|
var currentScore int
|
2016-08-18 17:39:32 +00:00
|
|
|
matched := false
|
2015-11-08 15:58:20 +00:00
|
|
|
for _, term := range termSet {
|
|
|
|
pfun := p.procFun[term.typ]
|
2020-07-28 04:06:57 +00:00
|
|
|
off, score, pos := p.iter(pfun, input, term.caseSensitive, term.normalize, p.forward, term.text, withPos, slab)
|
2016-04-16 05:02:43 +00:00
|
|
|
if sidx := off[0]; sidx >= 0 {
|
2015-11-08 15:58:20 +00:00
|
|
|
if term.inv {
|
2015-11-09 16:50:41 +00:00
|
|
|
continue
|
2015-11-08 15:58:20 +00:00
|
|
|
}
|
2017-06-02 04:25:35 +00:00
|
|
|
offset, currentScore = off, score
|
2016-08-18 17:39:32 +00:00
|
|
|
matched = true
|
2016-09-07 00:58:18 +00:00
|
|
|
if withPos {
|
|
|
|
if pos != nil {
|
|
|
|
*allPos = append(*allPos, *pos...)
|
|
|
|
} else {
|
|
|
|
for idx := off[0]; idx < off[1]; idx++ {
|
|
|
|
*allPos = append(*allPos, int(idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 15:58:20 +00:00
|
|
|
break
|
|
|
|
} else if term.inv {
|
2017-06-02 04:25:35 +00:00
|
|
|
offset, currentScore = Offset{0, 0}, 0
|
2016-08-18 17:39:32 +00:00
|
|
|
matched = true
|
2015-11-09 16:50:41 +00:00
|
|
|
continue
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 17:39:32 +00:00
|
|
|
if matched {
|
|
|
|
offsets = append(offsets, offset)
|
2016-09-07 00:58:18 +00:00
|
|
|
totalScore += currentScore
|
2015-11-09 16:50:41 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-06-02 04:25:35 +00:00
|
|
|
return offsets, totalScore, allPos
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 18:21:35 +00:00
|
|
|
func (p *Pattern) transformInput(item *Item) []Token {
|
2017-07-16 14:31:19 +00:00
|
|
|
if item.transformed != nil {
|
|
|
|
return *item.transformed
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-07-16 14:31:19 +00:00
|
|
|
|
2017-07-19 17:44:30 +00:00
|
|
|
tokens := Tokenize(item.text.ToString(), p.delimiter)
|
2017-07-16 14:31:19 +00:00
|
|
|
ret := Transform(tokens, p.nth)
|
|
|
|
item.transformed = &ret
|
2015-01-01 19:49:30 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2017-06-02 04:25:35 +00:00
|
|
|
func (p *Pattern) iter(pfun algo.Algo, tokens []Token, caseSensitive bool, normalize bool, forward bool, pattern []rune, withPos bool, slab *util.Slab) (Offset, int, *[]int) {
|
2015-08-02 05:00:18 +00:00
|
|
|
for _, part := range tokens {
|
2017-08-19 18:33:55 +00:00
|
|
|
if res, pos := pfun(caseSensitive, normalize, forward, part.text, pattern, withPos, slab); res.Start >= 0 {
|
2016-08-18 17:39:32 +00:00
|
|
|
sidx := int32(res.Start) + part.prefixLength
|
|
|
|
eidx := int32(res.End) + part.prefixLength
|
2016-09-07 00:58:18 +00:00
|
|
|
if pos != nil {
|
|
|
|
for idx := range *pos {
|
|
|
|
(*pos)[idx] += int(part.prefixLength)
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 04:25:35 +00:00
|
|
|
return Offset{sidx, eidx}, res.Score, pos
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-02 04:25:35 +00:00
|
|
|
return Offset{-1, -1}, 0, nil
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|