-
Notifications
You must be signed in to change notification settings - Fork 1
/
radix.go
341 lines (301 loc) · 7.24 KB
/
radix.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2013 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package ksmux
import (
"strconv"
"strings"
)
type typeOfNode uint8
const (
static typeOfNode = iota
root
param
catchAll
)
const defaultParamsSize = 20
type paramInfo struct {
position int
names []string
}
type node struct {
path string
handler Handler
children []*node
nType typeOfNode
paramInfo *paramInfo
indexes string
isWild bool
origines string
}
// addPath adds a node with the given handler to the path.
// Not concurrency-safe!
func (n *node) addPath(path string, handler Handler, origines ...string) {
// lg.Debug("addPath called", "path", path)
if n.path == "" && n.indexes == "" {
// lg.Debug("empty tree, inserting root")
n.insertChild(path, handler, origines...)
n.nType = root
return
}
walk:
for {
i := longestCommonPrefix(path, n.path)
// lg.Debug("found common prefix", "len", i, "path", path, "nodePath", n.path)
if i < len(n.path) {
// lg.Debug("splitting edge", "path", path, "nodePath", n.path)
child := node{
path: n.path[i:],
nType: static,
indexes: n.indexes,
children: n.children,
handler: n.handler,
origines: n.origines,
}
n.children = []*node{&child}
n.indexes = string([]byte{n.path[i]})
n.path = path[:i]
n.handler = nil
}
if i < len(path) {
path = path[i:]
// lg.Debug("remaining path", "path", path)
// Check for parameter
if path[0] == ':' {
// lg.Debug("found parameter in path", "path", path)
for i, index := range []byte(n.indexes) {
if index == ':' {
// lg.Debug("found existing param node", "index", i)
n = n.children[i]
continue walk
}
}
}
// Find matching child
for i, index := range []byte(n.indexes) {
if path[0] == index {
// lg.Debug("found matching child", "index", i, "char", string(index))
n = n.children[i]
continue walk
}
}
// No matching child
// lg.Debug("no matching child, creating new one", "path", path)
n.indexes += string([]byte{path[0]})
child := &node{}
n.children = append(n.children, child)
n = child
n.insertChild(path, handler, origines...)
return
}
n.handler = handler
n.origines = strings.Join(origines, ",")
return
}
}
func (n *node) insertChild(path string, handler Handler, origines ...string) {
if path == "/" {
n.handler = handler
if len(origines) > 0 {
// Pre-allocate string builder for origines
var sb strings.Builder
sb.Grow(len(origines) * 8) // Estimate 8 chars per origin
for i, origin := range origines {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString(origin)
}
n.origines = sb.String()
}
return
}
segments := strings.Split(strings.Trim(path, "/"), "/")
current := n
paramCount := 0
for _, segment := range segments {
if strings.HasPrefix(segment, ":") {
paramCount++
paramName := segment[1:]
// Look for existing param node at this position
var paramNode *node
for _, child := range current.children {
if child.nType == param && child.paramInfo.position == paramCount {
paramNode = child
found := false
for _, name := range paramNode.paramInfo.names {
if name == paramName {
found = true
break
}
}
if !found {
paramNode.paramInfo.names = append(paramNode.paramInfo.names, paramName)
}
break
}
}
if paramNode == nil {
paramNode = &node{
nType: param,
path: ":" + strconv.Itoa(paramCount),
isWild: true,
paramInfo: ¶mInfo{
position: paramCount,
names: []string{paramName},
},
}
current.children = append(current.children, paramNode)
}
current = paramNode
continue
}
if strings.HasPrefix(segment, "*") {
child := &node{
nType: catchAll,
path: segment,
handler: handler,
origines: strings.Join(origines, ","),
isWild: true,
}
current.children = []*node{child}
return
}
// Static path
var staticNode *node
for _, child := range current.children {
if child.nType == static && child.path == segment {
staticNode = child
break
}
}
if staticNode == nil {
staticNode = &node{
nType: static,
path: segment,
}
current.children = append(current.children, staticNode)
}
current = staticNode
}
current.handler = handler
current.origines = strings.Join(origines, ",")
}
// Returns the handler registered with the given path (key). The values of
// wildcards are saved to a map.
// If no handler can be found, a TSR (trailing slash redirect) recommendation is
// made if a handler exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getHandler(path string, params func() *Params) (handler Handler, ps *Params, tsr bool, origines string) {
if path == "/" {
return n.handler, nil, false, n.origines
}
pathLen := len(path)
if pathLen > 0 && path[0] == '/' {
path = path[1:]
pathLen--
}
// Pre-calculate param count to avoid slice reallocation
paramCount := countParams(path)
if params != nil && paramCount > 0 {
ps = params()
size := int(defaultParamsSize)
if paramCount > uint16(defaultParamsSize) {
size = int(paramCount)
}
*ps = make([]Param, 0, size)
}
current := n
start := 0
paramPos := 0
pathBytes := []byte(path) // Avoid repeated string indexing
for i := 0; i < pathLen; i++ {
if pathBytes[i] == '/' || i == pathLen-1 {
end := i
if i == pathLen-1 {
end = pathLen
}
segment := path[start:end] // Direct slice, no buffer needed
// Try static nodes first
var next *node
for _, child := range current.children {
if child.nType == static && child.path == segment {
next = child
break
}
}
// Try parameter nodes
if next == nil {
paramPos++
for _, child := range current.children {
if child.nType == param && child.paramInfo.position == paramPos {
if params != nil {
if ps == nil {
ps = params()
}
names := child.paramInfo.names
l := len(names)
for j := 0; j < l; j++ {
*ps = append(*ps, Param{
Key: names[j],
Value: segment,
})
}
}
next = child
break
}
}
}
// Try catchAll
if next == nil {
for _, child := range current.children {
if child.nType == catchAll {
remaining := path[start:]
if params != nil {
if ps == nil {
ps = params()
}
paramName := child.path[1:] // Avoid TrimPrefix allocation
*ps = append(*ps, Param{
Key: paramName,
Value: remaining,
})
}
return child.handler, ps, false, child.origines
}
}
}
if next == nil {
return nil, nil, false, ""
}
current = next
start = i + 1
}
}
return current.handler, ps, false, current.origines
}
func longestCommonPrefix(a, b string) int {
i := 0
max := len(a)
if len(b) < max {
max = len(b)
}
// Use unsafe.Pointer for faster access
for i < max && a[i] == b[i] {
i++
}
return i
}
func countParams(path string) uint16 {
var n uint16
b := []byte(path)
l := len(b)
for i := 0; i < l; i++ {
c := b[i]
if c == ':' || c == '*' {
n++
}
}
return n
}