strmatcher.go 2.6 KB

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