indexmatcher_linear.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package strmatcher
  2. // LinearIndexMatcher is an implementation of IndexMatcher.
  3. // Empty initialization works.
  4. type LinearIndexMatcher struct {
  5. count uint32
  6. fullMatcher FullMatcherGroup
  7. domainMatcher DomainMatcherGroup
  8. substrMatcher SubstrMatcherGroup
  9. otherMatchers SimpleMatcherGroup
  10. }
  11. func NewLinearIndexMatcher() *LinearIndexMatcher {
  12. return new(LinearIndexMatcher)
  13. }
  14. // Add implements IndexMatcher.Add.
  15. func (g *LinearIndexMatcher) Add(matcher Matcher) uint32 {
  16. g.count++
  17. index := g.count
  18. switch matcher := matcher.(type) {
  19. case FullMatcher:
  20. g.fullMatcher.AddFullMatcher(matcher, index)
  21. case DomainMatcher:
  22. g.domainMatcher.AddDomainMatcher(matcher, index)
  23. case SubstrMatcher:
  24. g.substrMatcher.AddSubstrMatcher(matcher, index)
  25. default:
  26. g.otherMatchers.AddMatcher(matcher, index)
  27. }
  28. return index
  29. }
  30. // Build implements IndexMatcher.Build.
  31. func (*LinearIndexMatcher) Build() error {
  32. return nil
  33. }
  34. // Match implements IndexMatcher.Match.
  35. func (g *LinearIndexMatcher) Match(input string) []uint32 {
  36. result := []uint32{}
  37. result = append(result, g.fullMatcher.Match(input)...)
  38. result = append(result, g.domainMatcher.Match(input)...)
  39. result = append(result, g.substrMatcher.Match(input)...)
  40. result = append(result, g.otherMatchers.Match(input)...)
  41. return result
  42. }
  43. // MatchAny implements IndexMatcher.MatchAny.
  44. func (g *LinearIndexMatcher) MatchAny(input string) bool {
  45. return len(g.Match(input)) > 0
  46. }
  47. // Size implements IndexMatcher.Size.
  48. func (g *LinearIndexMatcher) Size() uint32 {
  49. return g.count
  50. }