glob/match/any_of.go

85 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-01-12 14:06:59 +03:00
func (self AnyOf) Index(s string) (int, []int) {
if len(self.Matchers) == 0 {
return -1, nil
}
// segments to merge
var segments [][]int
index := -1
for _, m := range self.Matchers {
idx, seg := m.Index(s)
if idx == -1 {
continue
}
if index == -1 || idx < index {
index = idx
segments = [][]int{seg}
continue
}
if idx > index {
continue
}
segments = append(segments, seg)
}
if index == -1 {
return -1, nil
}
return index, mergeSegments(segments)
}
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
}