glob/match/every_of.go

100 lines
1.7 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 {
2018-02-16 17:36:02 +03:00
ms []Matcher
min int
2016-01-08 20:14:31 +03:00
}
2018-02-16 17:36:02 +03:00
func NewEveryOf(ms []Matcher) Matcher {
e := EveryOf{ms, minLen(ms)}
if mis, ok := MatchIndexers(ms); ok {
return IndexedEveryOf{e, mis}
}
return e
}
2018-02-16 17:36:02 +03:00
func (e EveryOf) MinLen() (n int) {
return e.min
2016-01-09 02:34:41 +03:00
}
2018-02-16 17:36:02 +03:00
func (e EveryOf) Match(s string) bool {
for _, m := range e.ms {
if !m.Match(s) {
return false
2016-01-09 02:34:41 +03:00
}
}
2018-02-16 17:36:02 +03:00
return true
}
func (e EveryOf) String() string {
return fmt.Sprintf("<every_of:[%s]>", e.ms)
}
2016-01-09 02:34:41 +03:00
2018-02-16 17:36:02 +03:00
type IndexedEveryOf struct {
EveryOf
ms []MatchIndexer
2016-01-08 20:14:31 +03:00
}
2018-02-16 17:36:02 +03:00
func (e IndexedEveryOf) 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
2016-02-23 14:46:20 +03:00
next := acquireSegments(len(s))
current := acquireSegments(len(s))
2016-01-12 14:06:59 +03:00
sub := s
2018-02-16 17:36:02 +03:00
for i, m := range e.ms {
2016-02-05 17:29:41 +03:00
idx, seg := m.Index(sub)
2016-01-12 14:06:59 +03:00
if idx == -1 {
releaseSegments(next)
releaseSegments(current)
2016-01-12 14:06:59 +03:00
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 {
releaseSegments(next)
releaseSegments(current)
2016-02-05 16:57:42 +03:00
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
}
releaseSegments(next)
2016-02-05 17:29:41 +03:00
return index, current
2016-01-12 14:06:59 +03:00
}
2018-02-16 17:36:02 +03:00
func (e IndexedEveryOf) String() string {
return fmt.Sprintf("<indexed_every_of:[%s]>", e.ms)
2016-01-08 20:14:31 +03:00
}