1package match 2 3import "fmt" 4 5type AnyOf struct { 6 Matchers Matchers 7} 8 9func NewAnyOf(m ...Matcher) AnyOf { 10 return AnyOf{Matchers(m)} 11} 12 13func (self *AnyOf) Add(m Matcher) error { 14 self.Matchers = append(self.Matchers, m) 15 return nil 16} 17 18func (self AnyOf) Match(s string) bool { 19 for _, m := range self.Matchers { 20 if m.Match(s) { 21 return true 22 } 23 } 24 25 return false 26} 27 28func (self AnyOf) Index(s string) (int, []int) { 29 index := -1 30 31 segments := acquireSegments(len(s)) 32 for _, m := range self.Matchers { 33 idx, seg := m.Index(s) 34 if idx == -1 { 35 continue 36 } 37 38 if index == -1 || idx < index { 39 index = idx 40 segments = append(segments[:0], seg...) 41 continue 42 } 43 44 if idx > index { 45 continue 46 } 47 48 // here idx == index 49 segments = appendMerge(segments, seg) 50 } 51 52 if index == -1 { 53 releaseSegments(segments) 54 return -1, nil 55 } 56 57 return index, segments 58} 59 60func (self AnyOf) Len() (l int) { 61 l = -1 62 for _, m := range self.Matchers { 63 ml := m.Len() 64 switch { 65 case l == -1: 66 l = ml 67 continue 68 69 case ml == -1: 70 return -1 71 72 case l != ml: 73 return -1 74 } 75 } 76 77 return 78} 79 80func (self AnyOf) String() string { 81 return fmt.Sprintf("<any_of:[%s]>", self.Matchers) 82} 83