glob/match/every_of.go

80 lines
1.1 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
)
2016-01-12 14:06:59 +03:00
type EveryOf struct {
2016-01-08 20:14:31 +03:00
Matchers Matchers
}
2016-01-12 14:06:59 +03:00
func (self *EveryOf) 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-12 14:06:59 +03:00
func (self EveryOf) Len() (l int) {
2016-01-09 02:34:41 +03:00
for _, m := range self.Matchers {
if ml := m.Len(); l > 0 {
l += ml
} else {
return -1
}
}
return
2016-01-08 20:14:31 +03:00
}
2016-01-12 14:06:59 +03:00
func (self EveryOf) Index(s string) (int, []int) {
var index int
var offset int
var segments []int
sub := s
for _, m := range self.Matchers {
idx, seg := m.Index(sub)
if idx == -1 {
return -1, nil
}
var sum []int
if segments == nil {
sum = seg
} else {
delta := index - (idx + offset)
for _, ex := range segments {
for _, n := range seg {
if ex+delta == n {
sum = append(sum, n)
}
}
}
}
if len(sum) == 0 {
return -1, nil
}
segments = sum
index = idx + offset
sub = s[index:]
offset += idx
}
return index, segments
}
func (self EveryOf) Match(s string) bool {
2016-01-08 20:14:31 +03:00
for _, m := range self.Matchers {
if !m.Match(s) {
return false
}
}
return true
}
2016-01-12 14:06:59 +03:00
func (self EveryOf) String() string {
2016-01-13 01:26:48 +03:00
return fmt.Sprintf("<every_of:[%s]>", self.Matchers)
2016-01-08 20:14:31 +03:00
}