2021-10-31 06:01:13 -04:00
|
|
|
package strmatcher
|
|
|
|
|
|
|
|
// LinearIndexMatcher is an implementation of IndexMatcher.
|
|
|
|
type LinearIndexMatcher struct {
|
2022-09-16 02:40:03 -04:00
|
|
|
count uint32
|
|
|
|
full *FullMatcherGroup
|
|
|
|
domain *DomainMatcherGroup
|
|
|
|
substr *SubstrMatcherGroup
|
|
|
|
regex *SimpleMatcherGroup
|
2021-10-31 06:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLinearIndexMatcher() *LinearIndexMatcher {
|
|
|
|
return new(LinearIndexMatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add implements IndexMatcher.Add.
|
|
|
|
func (g *LinearIndexMatcher) Add(matcher Matcher) uint32 {
|
|
|
|
g.count++
|
|
|
|
index := g.count
|
|
|
|
|
|
|
|
switch matcher := matcher.(type) {
|
|
|
|
case FullMatcher:
|
2022-09-16 02:40:03 -04:00
|
|
|
if g.full == nil {
|
|
|
|
g.full = NewFullMatcherGroup()
|
|
|
|
}
|
|
|
|
g.full.AddFullMatcher(matcher, index)
|
2021-10-31 06:01:13 -04:00
|
|
|
case DomainMatcher:
|
2022-09-16 02:40:03 -04:00
|
|
|
if g.domain == nil {
|
|
|
|
g.domain = NewDomainMatcherGroup()
|
|
|
|
}
|
|
|
|
g.domain.AddDomainMatcher(matcher, index)
|
2021-10-31 06:01:13 -04:00
|
|
|
case SubstrMatcher:
|
2022-09-16 02:40:03 -04:00
|
|
|
if g.substr == nil {
|
|
|
|
g.substr = new(SubstrMatcherGroup)
|
|
|
|
}
|
|
|
|
g.substr.AddSubstrMatcher(matcher, index)
|
2021-10-31 06:01:13 -04:00
|
|
|
default:
|
2022-09-16 02:40:03 -04:00
|
|
|
if g.regex == nil {
|
|
|
|
g.regex = new(SimpleMatcherGroup)
|
|
|
|
}
|
|
|
|
g.regex.AddMatcher(matcher, index)
|
2021-10-31 06:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements IndexMatcher.Build.
|
|
|
|
func (*LinearIndexMatcher) Build() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match implements IndexMatcher.Match.
|
|
|
|
func (g *LinearIndexMatcher) Match(input string) []uint32 {
|
2022-09-16 02:40:03 -04:00
|
|
|
// Allocate capacity to prevent matches escaping to heap
|
|
|
|
result := make([][]uint32, 0, 5)
|
|
|
|
if g.full != nil {
|
|
|
|
if matches := g.full.Match(input); len(matches) > 0 {
|
|
|
|
result = append(result, matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g.domain != nil {
|
|
|
|
if matches := g.domain.Match(input); len(matches) > 0 {
|
|
|
|
result = append(result, matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g.substr != nil {
|
|
|
|
if matches := g.substr.Match(input); len(matches) > 0 {
|
|
|
|
result = append(result, matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g.regex != nil {
|
|
|
|
if matches := g.regex.Match(input); len(matches) > 0 {
|
|
|
|
result = append(result, matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CompositeMatches(result)
|
2021-10-31 06:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MatchAny implements IndexMatcher.MatchAny.
|
|
|
|
func (g *LinearIndexMatcher) MatchAny(input string) bool {
|
2022-09-16 02:40:03 -04:00
|
|
|
if g.full != nil && g.full.MatchAny(input) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if g.domain != nil && g.domain.MatchAny(input) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if g.substr != nil && g.substr.MatchAny(input) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return g.regex != nil && g.regex.MatchAny(input)
|
2021-10-31 06:01:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements IndexMatcher.Size.
|
|
|
|
func (g *LinearIndexMatcher) Size() uint32 {
|
|
|
|
return g.count
|
|
|
|
}
|