strmatcher.go 2.6 KB

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