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-02-05 17:29:41 +03:00
|
|
|
func (self EveryOf) Index(s string) (int, []int) {
|
2016-01-12 14:06:59 +03:00
|
|
|
var index int
|
|
|
|
var offset int
|
2016-02-05 16:57:42 +03:00
|
|
|
|
|
|
|
// make `in` with cap as len(s),
|
|
|
|
// cause it is the maximum size of output segments values
|
|
|
|
next := make([]int, 0, len(s))
|
|
|
|
current := make([]int, 0, len(s))
|
2016-01-12 14:06:59 +03:00
|
|
|
|
|
|
|
sub := s
|
2016-02-02 22:03:37 +03:00
|
|
|
for i, m := range self.Matchers {
|
2016-02-05 17:29:41 +03:00
|
|
|
idx, seg := m.Index(sub)
|
2016-01-12 14:06:59 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:03:37 +03:00
|
|
|
if i == 0 {
|
2016-02-05 16:57:42 +03:00
|
|
|
// we use copy here instead of `current = seg`
|
|
|
|
// cause seg is a slice from reusable buffer `in`
|
|
|
|
// and it could be overwritten in next iteration
|
|
|
|
current = append(current, seg...)
|
2016-01-12 14:06:59 +03:00
|
|
|
} else {
|
2016-02-05 16:57:42 +03:00
|
|
|
// clear the next
|
|
|
|
next = next[:0]
|
|
|
|
|
2016-01-12 14:06:59 +03:00
|
|
|
delta := index - (idx + offset)
|
2016-02-02 22:03:37 +03:00
|
|
|
for _, ex := range current {
|
2016-01-12 14:06:59 +03:00
|
|
|
for _, n := range seg {
|
|
|
|
if ex+delta == n {
|
2016-02-02 22:03:37 +03:00
|
|
|
next = append(next, n)
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:57:42 +03:00
|
|
|
if len(next) == 0 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
2016-02-02 22:03:37 +03:00
|
|
|
|
2016-02-05 16:57:42 +03:00
|
|
|
current = append(current[:0], next...)
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
index = idx + offset
|
|
|
|
sub = s[index:]
|
|
|
|
offset += idx
|
|
|
|
}
|
|
|
|
|
2016-02-05 17:29:41 +03:00
|
|
|
return index, current
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|