2016-01-08 20:14:31 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AnyOf struct {
|
|
|
|
Matchers Matchers
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:36:15 +03:00
|
|
|
func NewAnyOf(m ...Matcher) AnyOf {
|
|
|
|
return AnyOf{Matchers(m)}
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
func (self *AnyOf) Add(m Matcher) error {
|
2016-01-08 20:14:31 +03:00
|
|
|
self.Matchers = append(self.Matchers, m)
|
2016-01-09 02:34:41 +03:00
|
|
|
return nil
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self AnyOf) Match(s string) bool {
|
|
|
|
for _, m := range self.Matchers {
|
|
|
|
if m.Match(s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-05 17:29:41 +03:00
|
|
|
func (self AnyOf) Index(s string) (int, []int) {
|
2016-01-12 14:06:59 +03:00
|
|
|
index := -1
|
2016-02-05 16:57:42 +03:00
|
|
|
|
2016-02-23 00:26:06 +03:00
|
|
|
segments := acquireSegments(len(s))
|
2016-01-12 14:06:59 +03:00
|
|
|
for _, m := range self.Matchers {
|
2016-02-05 17:29:41 +03:00
|
|
|
idx, seg := m.Index(s)
|
2016-01-12 14:06:59 +03:00
|
|
|
if idx == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if index == -1 || idx < index {
|
|
|
|
index = idx
|
2016-02-02 22:03:37 +03:00
|
|
|
segments = append(segments[:0], seg...)
|
2016-01-12 14:06:59 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx > index {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:03:37 +03:00
|
|
|
// here idx == index
|
|
|
|
segments = appendMerge(segments, seg)
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if index == -1 {
|
2016-02-23 00:26:06 +03:00
|
|
|
releaseSegments(segments)
|
2016-01-12 14:06:59 +03:00
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:03:37 +03:00
|
|
|
return index, segments
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
2016-01-11 10:17:19 +03:00
|
|
|
|
|
|
|
func (self AnyOf) Len() (l int) {
|
|
|
|
l = -1
|
|
|
|
for _, m := range self.Matchers {
|
|
|
|
ml := m.Len()
|
2016-05-14 21:38:00 +03:00
|
|
|
switch {
|
|
|
|
case l == -1:
|
2016-01-11 10:17:19 +03:00
|
|
|
l = ml
|
|
|
|
continue
|
|
|
|
|
2016-05-14 21:38:00 +03:00
|
|
|
case ml == -1:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
case l != ml:
|
2016-01-11 10:17:19 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2016-01-08 20:14:31 +03:00
|
|
|
func (self AnyOf) String() string {
|
2016-01-13 01:26:48 +03:00
|
|
|
return fmt.Sprintf("<any_of:[%s]>", self.Matchers)
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|