-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.go
252 lines (234 loc) · 6.95 KB
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package path
import (
"errors"
"sort"
"strings"
)
type stackSegment struct {
currentNodeChildren uint16
urlSegmentIndex uint8
}
type node struct {
maxMatchableSegments uint8
priority uint64
segment *segment
pattern *Pattern
parent *node
children []*node
}
func (n *node) canMatchPathWithLength(urlPathLen uint8) bool {
return urlPathLen <= n.maxMatchableSegments
}
func (n *node) isLeaf() bool {
return n.pattern != nil
}
func (n *node) String() string {
if n.segment == nil {
return "/"
}
return n.segment.val
}
type Matcher struct {
caseInsensitive bool
rootPathMatcher *Pattern
trieRoot *node
}
func NewMatcher(caseInsensitive bool) *Matcher {
return &Matcher{
caseInsensitive: caseInsensitive,
trieRoot: &node{},
}
}
func (m *Matcher) AddPattern(pattern *Pattern) error {
if len(pattern.segments) == 0 {
if m.rootPathMatcher != nil {
return errors.New("duplicated pattern detected: '/'")
}
m.rootPathMatcher = pattern
return nil
}
var inserted bool
var trieDepth int
segmentsLength := len(pattern.segments)
currentNode := m.trieRoot
for segmentIndex := 0; segmentIndex < segmentsLength; segmentIndex++ {
segment := pattern.segments[segmentIndex]
var found bool
var maxMatchableSegments uint8
if pattern.isGreedy() {
maxMatchableSegments = greedyPatternMaxMatchableSegments
} else {
maxMatchableSegments = pattern.maxMatchableSegments - uint8(segmentIndex)
}
children := currentNode.children
for i := 0; i < len(children); i++ {
child := children[i]
if child.segment.matchType == segment.matchType {
var hasSameVal bool
if child.segment.matchType == MatchTypeCaptureVar ||
child.segment.matchType == MatchTypeSingleSegment ||
child.segment.matchType == MatchTypeMultipleSegments {
hasSameVal = true
} else if child.segment.matchType == MatchTypeConstraintCaptureVar {
hasSameVal = segment.captureVarPattern.String() == child.segment.val
} else {
if m.caseInsensitive {
hasSameVal = strings.EqualFold(segment.val, child.segment.val)
} else {
hasSameVal = segment.val == child.segment.val
}
}
if hasSameVal {
if child.maxMatchableSegments < maxMatchableSegments {
child.maxMatchableSegments = maxMatchableSegments
}
currentNode = child
found = true
if currentNode.parent != nil {
trieDepth++
}
break
}
}
}
if !found {
newNode := &node{
priority: pattern.priority,
segment: segment,
parent: currentNode,
}
// the last node on the branch tree is a leaf
if segmentIndex == segmentsLength-1 {
newNode.pattern = pattern
}
children = append(children, newNode)
sort.SliceStable(children, func(i, j int) bool {
iChild := children[i]
jChild := children[j]
if iChild.priority == jChild.priority {
return strings.Compare(iChild.segment.val, jChild.segment.val) <= 0
}
return iChild.priority < jChild.priority
})
currentNode.children = children
currentNode = newNode
newNode.maxMatchableSegments = maxMatchableSegments
inserted = true
} else {
if segmentIndex == segmentsLength-1 {
if currentNode.isLeaf() {
return errors.New("duplicated pattern detected: '" + pattern.String() + "'")
}
currentNode.pattern = pattern
inserted = true
}
}
}
if !inserted && currentNode.isLeaf() {
return errors.New("duplicated pattern detected: '" + pattern.String() + "'")
}
return nil
}
func (m *Matcher) Match(urlPath string, mc *MatchingContext) *Pattern {
if len(mc.PathSegments) > MaxPathSegments {
return nil
}
if len(mc.PathSegments) == 0 && m.rootPathMatcher != nil {
return m.rootPathMatcher
}
var treeDepth int
urlLen := uint8(len(mc.PathSegments))
nodeStack := make([]stackSegment, MaxPathSegments)
urlSegmentMatchTypeStack := make([]MatchType, MaxPathSegments)
currentNode := m.trieRoot
var urlSegmentIndex uint8
for urlSegmentIndex < urlLen {
var matched bool
urlSegment := &mc.PathSegments[urlSegmentIndex]
//urlSegmentVal := urlPath[urlSegment.startIndex:urlSegment.endIndex]
if urlSegmentMatchTypeStack[urlSegmentIndex] != 0 {
urlSegment.matchType = urlSegmentMatchTypeStack[urlSegmentIndex]
}
children := currentNode.children
childrenLen := uint16(len(children))
for ci := nodeStack[treeDepth].currentNodeChildren; ci < childrenLen; ci++ {
childNode := children[ci]
if !childNode.canMatchPathWithLength(urlLen - urlSegmentIndex) {
continue
}
urlSegmentMatchType := childNode.segment.matchUrlPathSegment(urlPath, urlSegment, m.caseInsensitive)
if urlSegmentMatchType == MatchTypeMultipleSegments {
greedyChildren := childNode.children
greedyChildrenLen := len(greedyChildren)
if greedyChildrenLen == 0 && childNode.isLeaf() {
//fill until the end the remained URL segments with MatchTypeMultipleSegments match type
for i := urlSegmentIndex; i < urlLen; i++ {
urlSegment := &mc.PathSegments[i]
urlSegment.matchType = MatchTypeMultipleSegments
}
mc.matchedPattern = childNode.pattern
return childNode.pattern
}
for urlSegmentIndex < urlLen {
urlSegment := &mc.PathSegments[urlSegmentIndex]
urlSegment.matchType = MatchTypeMultipleSegments
for gci := 0; gci < greedyChildrenLen; gci++ {
greedyChildNode := greedyChildren[gci]
if greedyChildNode.segment.matchUrlPathSegment(urlPath, urlSegment, m.caseInsensitive) != MatchTypeUnknown {
nodeStack[treeDepth] = stackSegment{
currentNodeChildren: ci,
urlSegmentIndex: urlSegmentIndex + 1,
}
urlSegmentMatchTypeStack[urlSegmentIndex] = MatchTypeMultipleSegments
matched = true
treeDepth++
urlSegmentIndex-- //the urlSegmentIndex should not be increased. We have to decrease it here because will be increased at the end of the main loop
currentNode = childNode
goto END
}
}
urlSegmentIndex++
}
return nil
} else if urlSegmentMatchType != MatchTypeUnknown {
urlSegment.matchType = urlSegmentMatchType
if urlSegmentIndex == urlLen-1 {
if childNode.isLeaf() {
mc.matchedPattern = childNode.pattern
return childNode.pattern
}
goto END
}
matched = true
urlSegment.matchType = urlSegmentMatchType
nodeStack[treeDepth] = stackSegment{
currentNodeChildren: ci + 1,
urlSegmentIndex: urlSegmentIndex,
}
treeDepth++
currentNode = childNode
goto END
}
}
END:
if matched {
if urlSegmentIndex == urlLen-1 && currentNode.isLeaf() {
mc.matchedPattern = currentNode.pattern
return currentNode.pattern
}
urlSegmentIndex++
} else {
if currentNode.parent != nil {
//stack cleanup
urlSegmentMatchTypeStack[urlSegmentIndex] = 0
nodeStack[treeDepth] = stackSegment{}
treeDepth--
urlSegmentIndex = nodeStack[treeDepth].urlSegmentIndex
currentNode = currentNode.parent
} else {
break
}
}
}
return nil
}