本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.2KB

  1. package match
  2. import "fmt"
  3. type AnyOf struct {
  4. Matchers Matchers
  5. }
  6. func NewAnyOf(m ...Matcher) AnyOf {
  7. return AnyOf{Matchers(m)}
  8. }
  9. func (self *AnyOf) Add(m Matcher) error {
  10. self.Matchers = append(self.Matchers, m)
  11. return nil
  12. }
  13. func (self AnyOf) Match(s string) bool {
  14. for _, m := range self.Matchers {
  15. if m.Match(s) {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func (self AnyOf) Index(s string) (int, []int) {
  22. index := -1
  23. segments := acquireSegments(len(s))
  24. for _, m := range self.Matchers {
  25. idx, seg := m.Index(s)
  26. if idx == -1 {
  27. continue
  28. }
  29. if index == -1 || idx < index {
  30. index = idx
  31. segments = append(segments[:0], seg...)
  32. continue
  33. }
  34. if idx > index {
  35. continue
  36. }
  37. // here idx == index
  38. segments = appendMerge(segments, seg)
  39. }
  40. if index == -1 {
  41. releaseSegments(segments)
  42. return -1, nil
  43. }
  44. return index, segments
  45. }
  46. func (self AnyOf) Len() (l int) {
  47. l = -1
  48. for _, m := range self.Matchers {
  49. ml := m.Len()
  50. switch {
  51. case l == -1:
  52. l = ml
  53. continue
  54. case ml == -1:
  55. return -1
  56. case l != ml:
  57. return -1
  58. }
  59. }
  60. return
  61. }
  62. func (self AnyOf) String() string {
  63. return fmt.Sprintf("<any_of:[%s]>", self.Matchers)
  64. }
上海开阖软件有限公司 沪ICP备12045867号-1