Debian Codegraph @

match/btree.go

raw symbols SCIP
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 // todo?
54 func (self BTree) Index(s string) (index int, segments []int) {
55 //inputLen := len(s)
56 //// try to cut unnecessary parts
57 //// by knowledge of length of right and left part
58 //offset, limit := self.offsetLimit(inputLen)
59 //for offset < limit {
60 // // search for matching part in substring
61 // vi, segments := self.Value.Index(s[offset:limit])
62 // if index == -1 {
63 // return -1, nil
64 // }
65 // if self.Left == nil {
66 // if index != offset {
67 // return -1, nil
68 // }
69 // } else {
70 // left := s[:offset+vi]
71 // i := self.Left.IndexSuffix(left)
72 // if i == -1 {
73 // return -1, nil
74 // }
75 // index = i
76 // }
77 // if self.Right != nil {
78 // for _, seg := range segments {
79 // right := s[:offset+vi+seg]
80 // }
81 // }
82
83 // l := s[:offset+index]
84 // var left bool
85 // if self.Left != nil {
86 // left = self.Left.Index(l)
87 // } else {
88 // left = l == ""
89 // }
90 //}
91
92 return -1, nil
93 }
94
95 func (self BTree) Match(s string) bool {
96 inputLen := len(s)
97 // try to cut unnecessary parts
98 // by knowledge of length of right and left part
99 offset, limit := self.offsetLimit(inputLen)
100
101 for offset < limit {
102 // search for matching part in substring
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 // if there is no string for the right branch
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 // self.Length, self.RLen and self.LLen are values meaning the length of runes for each part
154 // here we manipulating byte length for better optimizations
155 // but these checks still works, cause minLen of 1-rune string is 1 byte.
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 }