glob/match/btree.go

125 lines
2.0 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
2016-01-09 02:34:41 +03:00
"unicode/utf8"
2016-01-08 20:14:31 +03:00
)
type BTree struct {
2016-01-12 14:06:59 +03:00
Value, Left, Right Matcher
2016-01-14 18:29:13 +03:00
VLen, LLen, RLen int
Length int
2016-01-08 20:14:31 +03:00
}
2016-01-14 18:29:13 +03:00
func NewBTree(Value, Left, Right Matcher) (tree BTree) {
tree.Value = Value
tree.Left = Left
tree.Right = Right
2016-01-08 20:14:31 +03:00
2016-01-14 18:29:13 +03:00
lenOk := true
if tree.VLen = Value.Len(); tree.VLen == -1 {
lenOk = false
}
if Left != nil {
if tree.LLen = Left.Len(); tree.LLen == -1 {
lenOk = false
}
}
2016-01-09 02:34:41 +03:00
2016-01-14 18:29:13 +03:00
if Right != nil {
if tree.RLen = Right.Len(); tree.RLen == -1 {
lenOk = false
}
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
if lenOk {
tree.Length = tree.LLen + tree.VLen + tree.RLen
} else {
tree.Length = -1
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
return tree
}
2016-01-09 02:34:41 +03:00
func (self BTree) Len() int {
2016-01-14 18:29:13 +03:00
return self.Length
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
// todo?
2016-01-12 14:06:59 +03:00
func (self BTree) Index(s string) (int, []int) {
return -1, nil
}
2016-01-08 20:14:31 +03:00
func (self BTree) Match(s string) bool {
2016-01-09 02:34:41 +03:00
inputLen := len(s)
2016-01-14 18:29:13 +03:00
if self.Length != -1 && self.Length > inputLen {
2016-01-09 02:34:41 +03:00
return false
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
var offset, limit int
2016-01-14 18:29:13 +03:00
if self.LLen >= 0 {
offset = self.LLen
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
if self.RLen >= 0 {
limit = inputLen - self.RLen
2016-01-09 02:34:41 +03:00
} else {
limit = inputLen
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
for offset < limit {
index, segments := self.Value.Index(s[offset:limit])
2016-01-08 20:14:31 +03:00
if index == -1 {
return false
}
2016-01-09 02:34:41 +03:00
l := string(s[:offset+index])
var left bool
if self.Left != nil {
left = self.Left.Match(l)
} else {
left = l == ""
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
if left {
for i := len(segments) - 1; i >= 0; i-- {
length := segments[i]
2016-01-08 20:14:31 +03:00
2016-01-14 18:29:13 +03:00
if self.RLen >= 0 && inputLen-(offset+index+length) != self.RLen {
2016-01-09 02:34:41 +03:00
continue
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
var right bool
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
var r string
// if there is no string for the right branch
if inputLen <= offset+index+length {
r = ""
} else {
r = s[offset+index+length:]
}
if self.Right != nil {
right = self.Right.Match(r)
} else {
right = r == ""
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
if right {
return true
}
2016-01-08 20:14:31 +03:00
}
}
2016-01-09 02:34:41 +03:00
_, step := utf8.DecodeRuneInString(s[offset+index:])
offset += index + step
2016-01-08 20:14:31 +03:00
}
return false
}
func (self BTree) String() string {
2016-01-13 01:26:48 +03:00
return fmt.Sprintf("<btree:[%s<-%s->%s]>", self.Left, self.Value, self.Right)
2016-01-08 20:14:31 +03:00
}