glob/match/every_of.go

103 lines
1.5 KiB
Go
Raw Permalink 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-02-02 22:03:37 +03:00
func max(a, b int) int {
if a >= b {
return a
}
return b
}
func (self EveryOf) Index(s string, out []int) (int, []int) {
2016-01-12 14:06:59 +03:00
var index int
var offset int
2016-02-02 22:03:37 +03:00
var current []int
2016-01-12 14:06:59 +03:00
sub := s
2016-02-02 22:03:37 +03:00
for i, m := range self.Matchers {
in := acquireSegments(len(sub))
idx, seg := m.Index(sub, in)
2016-01-12 14:06:59 +03:00
if idx == -1 {
2016-02-02 22:03:37 +03:00
releaseSegments(in)
if cap(current) > 0 {
releaseSegments(current)
}
2016-01-12 14:06:59 +03:00
return -1, nil
}
2016-02-02 22:03:37 +03:00
next := acquireSegments(max(len(seg), len(current)))
if i == 0 {
next = append(next, seg...)
2016-01-12 14:06:59 +03:00
} else {
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-02 22:03:37 +03:00
if cap(current) > 0 {
releaseSegments(current)
}
releaseSegments(in)
if len(next) == 0 {
releaseSegments(next)
2016-01-12 14:06:59 +03:00
return -1, nil
}
2016-02-02 22:03:37 +03:00
current = next
2016-01-12 14:06:59 +03:00
index = idx + offset
sub = s[index:]
offset += idx
}
2016-02-02 22:03:37 +03:00
out = append(out, current...)
releaseSegments(current)
return index, out
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
}