strmatcher.go 2.5 KB

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