strmatcher.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package strmatcher
  2. import (
  3. "errors"
  4. "regexp"
  5. )
  6. // Matcher is the interface to determine a string matches a pattern.
  7. type Matcher interface {
  8. // Match returns true if the given string matches a predefined pattern.
  9. Match(string) bool
  10. String() string
  11. }
  12. // Type is the type of the matcher.
  13. type Type byte
  14. const (
  15. // Full is the type of matcher that the input string must exactly equal to the pattern.
  16. Full Type = iota
  17. // Substr is the type of matcher that the input string must contain the pattern as a sub-string.
  18. Substr
  19. // Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
  20. Domain
  21. // Regex is the type of matcher that the input string must matches the regular-expression pattern.
  22. Regex
  23. )
  24. // New creates a new Matcher based on the given pattern.
  25. func (t Type) New(pattern string) (Matcher, error) {
  26. // 1. regex matching is case-sensitive
  27. switch t {
  28. case Full:
  29. return fullMatcher(pattern), nil
  30. case Substr:
  31. return substrMatcher(pattern), nil
  32. case Domain:
  33. return domainMatcher(pattern), nil
  34. case Regex:
  35. r, err := regexp.Compile(pattern)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &regexMatcher{
  40. pattern: r,
  41. }, nil
  42. default:
  43. return nil, errors.New("unk type")
  44. }
  45. }
  46. // IndexMatcher is the interface for matching with a group of matchers.
  47. type IndexMatcher interface {
  48. // Match returns the index of a matcher that matches the input. It returns empty array if no such matcher exists.
  49. Match(input string) []uint32
  50. }
  51. type matcherEntry struct {
  52. m Matcher
  53. id uint32
  54. }
  55. // MatcherGroup is an implementation of IndexMatcher.
  56. // Empty initialization works.
  57. type MatcherGroup struct {
  58. count uint32
  59. fullMatcher FullMatcherGroup
  60. domainMatcher DomainMatcherGroup
  61. otherMatchers []matcherEntry
  62. }
  63. // Add adds a new Matcher into the MatcherGroup, and returns its index. The index will never be 0.
  64. func (g *MatcherGroup) Add(m Matcher) uint32 {
  65. g.count++
  66. c := g.count
  67. switch tm := m.(type) {
  68. case fullMatcher:
  69. g.fullMatcher.addMatcher(tm, c)
  70. case domainMatcher:
  71. g.domainMatcher.addMatcher(tm, c)
  72. default:
  73. g.otherMatchers = append(g.otherMatchers, matcherEntry{
  74. m: m,
  75. id: c,
  76. })
  77. }
  78. return c
  79. }
  80. // Match implements IndexMatcher.Match.
  81. func (g *MatcherGroup) Match(pattern string) []uint32 {
  82. result := []uint32{}
  83. result = append(result, g.fullMatcher.Match(pattern)...)
  84. result = append(result, g.domainMatcher.Match(pattern)...)
  85. for _, e := range g.otherMatchers {
  86. if e.m.Match(pattern) {
  87. result = append(result, e.id)
  88. }
  89. }
  90. return result
  91. }
  92. // Size returns the number of matchers in the MatcherGroup.
  93. func (g *MatcherGroup) Size() uint32 {
  94. return g.count
  95. }