2018-02-16 17:36:02 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2018-10-02 22:52:57 +03:00
|
|
|
"github.com/gobwas/glob/internal/debug"
|
2018-02-16 17:36:02 +03:00
|
|
|
"github.com/gobwas/glob/util/runes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tree struct {
|
|
|
|
value MatchIndexer
|
|
|
|
left Matcher
|
|
|
|
right Matcher
|
|
|
|
|
|
|
|
minLen int
|
|
|
|
|
|
|
|
runes int
|
|
|
|
vrunes int
|
|
|
|
lrunes int
|
|
|
|
rrunes int
|
|
|
|
}
|
|
|
|
|
|
|
|
type SizedTree struct {
|
|
|
|
Tree
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:05 +03:00
|
|
|
type IndexedTree struct {
|
|
|
|
value MatchIndexer
|
|
|
|
left MatchIndexer
|
|
|
|
right MatchIndexer
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (st SizedTree) RunesCount() int {
|
|
|
|
return st.Tree.runes
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTree(v MatchIndexer, l, r Matcher) Matcher {
|
|
|
|
tree := Tree{
|
|
|
|
value: v,
|
|
|
|
left: l,
|
|
|
|
right: r,
|
|
|
|
}
|
|
|
|
tree.minLen = v.MinLen()
|
|
|
|
if l != nil {
|
|
|
|
tree.minLen += l.MinLen()
|
|
|
|
}
|
|
|
|
if r != nil {
|
|
|
|
tree.minLen += r.MinLen()
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
ls, lsz = l.(Sizer)
|
|
|
|
rs, rsz = r.(Sizer)
|
|
|
|
vs, vsz = v.(Sizer)
|
|
|
|
)
|
|
|
|
if lsz {
|
|
|
|
tree.lrunes = ls.RunesCount()
|
|
|
|
}
|
|
|
|
if rsz {
|
|
|
|
tree.rrunes = rs.RunesCount()
|
|
|
|
}
|
|
|
|
if vsz {
|
|
|
|
tree.vrunes = vs.RunesCount()
|
|
|
|
}
|
2019-02-10 21:25:05 +03:00
|
|
|
//li, lix := l.(MatchIndexer)
|
|
|
|
//ri, rix := r.(MatchIndexer)
|
2018-02-16 17:36:02 +03:00
|
|
|
if vsz && lsz && rsz {
|
|
|
|
tree.runes = tree.vrunes + tree.lrunes + tree.rrunes
|
|
|
|
return SizedTree{tree}
|
|
|
|
}
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) MinLen() int {
|
|
|
|
return t.minLen
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:43:38 +03:00
|
|
|
func (t Tree) Content(cb func(Matcher)) {
|
|
|
|
if t.left != nil {
|
|
|
|
cb(t.left)
|
|
|
|
}
|
|
|
|
cb(t.value)
|
|
|
|
if t.right != nil {
|
|
|
|
cb(t.right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (t Tree) Match(s string) (ok bool) {
|
2018-10-02 22:52:57 +03:00
|
|
|
if debug.Enabled {
|
2019-02-06 23:43:38 +03:00
|
|
|
done := debug.Matching("tree", s)
|
|
|
|
defer func() { done(ok) }()
|
2018-10-02 22:52:57 +03:00
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
|
2019-02-10 21:25:05 +03:00
|
|
|
n := len(s)
|
2018-02-16 17:36:02 +03:00
|
|
|
offset, limit := t.offsetLimit(s)
|
|
|
|
|
2019-02-10 21:25:05 +03:00
|
|
|
for len(s)-offset-limit >= t.vrunes {
|
|
|
|
if debug.Enabled {
|
|
|
|
debug.Logf(
|
|
|
|
"value %s indexing: %q (offset=%d; limit=%d)",
|
|
|
|
t.value, s[offset:n-limit], offset, limit,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
index, segments := t.value.Index(s[offset : n-limit])
|
|
|
|
if debug.Enabled {
|
|
|
|
debug.Logf(
|
|
|
|
"value %s index: %d; %v",
|
|
|
|
t.value, index, segments,
|
|
|
|
)
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
if index == -1 {
|
|
|
|
releaseSegments(segments)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:05 +03:00
|
|
|
if debug.Enabled {
|
|
|
|
debug.Logf("matching left: %q", s[:offset+index])
|
2018-02-16 17:36:02 +03:00
|
|
|
}
|
2019-02-10 21:25:05 +03:00
|
|
|
left := t.left.Match(s[:offset+index])
|
2018-10-02 22:52:57 +03:00
|
|
|
if debug.Enabled {
|
2019-02-10 21:25:05 +03:00
|
|
|
debug.Logf("matching left: -> %t", left)
|
2018-10-02 22:52:57 +03:00
|
|
|
}
|
2019-02-10 21:25:05 +03:00
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
if left {
|
|
|
|
for _, seg := range segments {
|
2019-02-10 21:25:05 +03:00
|
|
|
if debug.Enabled {
|
|
|
|
debug.Logf("matching right: %q", s[offset+index+seg:])
|
2018-02-16 17:36:02 +03:00
|
|
|
}
|
2019-02-10 21:25:05 +03:00
|
|
|
right := t.right.Match(s[offset+index+seg:])
|
2018-10-02 22:52:57 +03:00
|
|
|
if debug.Enabled {
|
2019-02-10 21:25:05 +03:00
|
|
|
debug.Logf("matching right: -> %t", right)
|
2018-10-02 22:52:57 +03:00
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
if right {
|
|
|
|
releaseSegments(segments)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseSegments(segments)
|
2019-02-10 21:25:05 +03:00
|
|
|
|
|
|
|
_, x := utf8.DecodeRuneInString(s[offset+index:])
|
|
|
|
if x == 0 {
|
|
|
|
// No progress.
|
|
|
|
break
|
2018-10-02 22:52:57 +03:00
|
|
|
}
|
2019-02-10 21:25:05 +03:00
|
|
|
offset = offset + index + x
|
2018-02-16 17:36:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retuns substring and offset/limit pair in bytes.
|
|
|
|
func (t Tree) offsetLimit(s string) (offset, limit int) {
|
|
|
|
n := utf8.RuneCountInString(s)
|
|
|
|
if t.runes > n {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
if n := t.lrunes; n > 0 {
|
|
|
|
offset = len(runes.Head(s, n))
|
|
|
|
}
|
|
|
|
if n := t.rrunes; n > 0 {
|
|
|
|
limit = len(runes.Tail(s, n))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) String() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"<btree:[%v<-%s->%v]>",
|
|
|
|
t.left, t.value, t.right,
|
|
|
|
)
|
|
|
|
}
|