| 1 |
package match |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"unicode/utf8" |
| 6 |
) |
| 7 |
|
| 8 |
type BTree struct { |
| 9 |
Value Matcher |
| 10 |
Left Matcher |
| 11 |
Right Matcher |
| 12 |
ValueLengthRunes int |
| 13 |
LeftLengthRunes int |
| 14 |
RightLengthRunes int |
| 15 |
LengthRunes int |
| 16 |
} |
| 17 |
|
| 18 |
func NewBTree(Value, Left, Right Matcher) (tree BTree) { |
| 19 |
tree.Value = Value |
| 20 |
tree.Left = Left |
| 21 |
tree.Right = Right |
| 22 |
|
| 23 |
lenOk := true |
| 24 |
if tree.ValueLengthRunes = Value.Len(); tree.ValueLengthRunes == -1 { |
| 25 |
lenOk = false |
| 26 |
} |
| 27 |
|
| 28 |
if Left != nil { |
| 29 |
if tree.LeftLengthRunes = Left.Len(); tree.LeftLengthRunes == -1 { |
| 30 |
lenOk = false |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if Right != nil { |
| 35 |
if tree.RightLengthRunes = Right.Len(); tree.RightLengthRunes == -1 { |
| 36 |
lenOk = false |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
if lenOk { |
| 41 |
tree.LengthRunes = tree.LeftLengthRunes + tree.ValueLengthRunes + tree.RightLengthRunes |
| 42 |
} else { |
| 43 |
tree.LengthRunes = -1 |
| 44 |
} |
| 45 |
|
| 46 |
return tree |
| 47 |
} |
| 48 |
|
| 49 |
func (self BTree) Len() int { |
| 50 |
return self.LengthRunes |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (self BTree) Index(s string) (index int, segments []int) { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
return -1, nil |
| 93 |
} |
| 94 |
|
| 95 |
func (self BTree) Match(s string) bool { |
| 96 |
inputLen := len(s) |
| 97 |
|
| 98 |
|
| 99 |
offset, limit := self.offsetLimit(inputLen) |
| 100 |
|
| 101 |
for offset < limit { |
| 102 |
|
| 103 |
index, segments := self.Value.Index(s[offset:limit]) |
| 104 |
if index == -1 { |
| 105 |
releaseSegments(segments) |
| 106 |
return false |
| 107 |
} |
| 108 |
|
| 109 |
l := s[:offset+index] |
| 110 |
var left bool |
| 111 |
if self.Left != nil { |
| 112 |
left = self.Left.Match(l) |
| 113 |
} else { |
| 114 |
left = l == "" |
| 115 |
} |
| 116 |
|
| 117 |
if left { |
| 118 |
for i := len(segments) - 1; i >= 0; i-- { |
| 119 |
length := segments[i] |
| 120 |
|
| 121 |
var right bool |
| 122 |
var r string |
| 123 |
|
| 124 |
if inputLen <= offset+index+length { |
| 125 |
r = "" |
| 126 |
} else { |
| 127 |
r = s[offset+index+length:] |
| 128 |
} |
| 129 |
|
| 130 |
if self.Right != nil { |
| 131 |
right = self.Right.Match(r) |
| 132 |
} else { |
| 133 |
right = r == "" |
| 134 |
} |
| 135 |
|
| 136 |
if right { |
| 137 |
releaseSegments(segments) |
| 138 |
return true |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
_, step := utf8.DecodeRuneInString(s[offset+index:]) |
| 144 |
offset += index + step |
| 145 |
|
| 146 |
releaseSegments(segments) |
| 147 |
} |
| 148 |
|
| 149 |
return false |
| 150 |
} |
| 151 |
|
| 152 |
func (self BTree) offsetLimit(inputLen int) (offset int, limit int) { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if self.LengthRunes != -1 && self.LengthRunes > inputLen { |
| 157 |
return 0, 0 |
| 158 |
} |
| 159 |
if self.LeftLengthRunes >= 0 { |
| 160 |
offset = self.LeftLengthRunes |
| 161 |
} |
| 162 |
if self.RightLengthRunes >= 0 { |
| 163 |
limit = inputLen - self.RightLengthRunes |
| 164 |
} else { |
| 165 |
limit = inputLen |
| 166 |
} |
| 167 |
return offset, limit |
| 168 |
} |
| 169 |
|
| 170 |
func (self BTree) String() string { |
| 171 |
const n string = "<nil>" |
| 172 |
var l, r string |
| 173 |
if self.Left == nil { |
| 174 |
l = n |
| 175 |
} else { |
| 176 |
l = self.Left.String() |
| 177 |
} |
| 178 |
if self.Right == nil { |
| 179 |
r = n |
| 180 |
} else { |
| 181 |
r = self.Right.String() |
| 182 |
} |
| 183 |
|
| 184 |
return fmt.Sprintf("<btree:[%s<-%s->%s]>", l, self.Value, r) |
| 185 |
} |