strmatcher.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package strmatcher
  2. // Type is the type of the matcher.
  3. type Type byte
  4. const (
  5. // Full is the type of matcher that the input string must exactly equal to the pattern.
  6. Full Type = 0
  7. // Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
  8. Domain Type = 1
  9. // Substr is the type of matcher that the input string must contain the pattern as a sub-string.
  10. Substr Type = 2
  11. // Regex is the type of matcher that the input string must matches the regular-expression pattern.
  12. Regex Type = 3
  13. )
  14. // Matcher is the interface to determine a string matches a pattern.
  15. // - This is a basic matcher to represent a certain kind of match semantic(full, substr, domain or regex).
  16. type Matcher interface {
  17. // Type returns the matcher's type.
  18. Type() Type
  19. // Pattern returns the matcher's raw string representation.
  20. Pattern() string
  21. // String returns a string representation of the matcher containing its type and pattern.
  22. String() string
  23. // Match returns true if the given string matches a predefined pattern.
  24. // * This method is seldom used for performance reason
  25. // and is generally taken over by their corresponding MatcherGroup.
  26. Match(input string) bool
  27. }
  28. // MatcherGroup is an advanced type of matcher to accept a bunch of basic Matchers (of certain type, not all matcher types).
  29. // For example:
  30. // - FullMatcherGroup accepts FullMatcher and uses a hash table to facilitate lookup.
  31. // - DomainMatcherGroup accepts DomainMatcher and uses a trie to optimize both memory consumption and lookup speed.
  32. type MatcherGroup interface {
  33. // Match returns all matched matchers with their corresponding values.
  34. Match(input string) []uint32
  35. // MatchAny returns true as soon as one matching matcher is found.
  36. MatchAny(input string) bool
  37. }
  38. // IndexMatcher is a general type of matcher thats accepts all kinds of basic matchers.
  39. // It should:
  40. // - Accept all Matcher types with no exception.
  41. // - Optimize string matching with a combination of MatcherGroups.
  42. // - Obey certain priority order specification when returning matched Matchers.
  43. type IndexMatcher interface {
  44. // Size returns number of matchers added to IndexMatcher.
  45. Size() uint32
  46. // Add adds a new Matcher to IndexMatcher, and returns its index. The index will never be 0.
  47. Add(matcher Matcher) uint32
  48. // Build builds the IndexMatcher to be ready for matching.
  49. Build() error
  50. // Match returns the indices of all matchers that matches the input.
  51. // * Empty array is returned if no such matcher exists.
  52. // * The order of returned matchers should follow priority specification.
  53. // Priority specification:
  54. // 1. Priority between matcher types: full > domain > substr > regex.
  55. // 2. Priority of same-priority matchers matching at same position: the early added takes precedence.
  56. // 3. Priority of domain matchers matching at different levels: the further matched domain takes precedence.
  57. // 4. Priority of substr matchers matching at different positions: the further matched substr takes precedence.
  58. Match(input string) []uint32
  59. // MatchAny returns true as soon as one matching matcher is found.
  60. MatchAny(input string) bool
  61. }