glob/match/any_of.go

83 lines
1.1 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
)
type AnyOf struct {
Matchers Matchers
}
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-02 22:03:37 +03:00
func (self AnyOf) Index(s string, segments []int) (int, []int) {
2016-01-12 14:06:59 +03:00
index := -1
2016-02-05 16:57:42 +03:00
// create reusable segments
in := make([]int, 0, len(s))
2016-01-12 14:06:59 +03:00
for _, m := range self.Matchers {
2016-02-05 16:57:42 +03:00
idx, seg := m.Index(s, in[:0])
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 {
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()
if ml == -1 {
return -1
}
if l == -1 {
l = ml
continue
}
if l != ml {
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
}