@@ -13,11 +13,11 @@ type (
13
13
prefix string
14
14
parent * node
15
15
children children
16
- // pchild *node // Param child
17
- // cchild *node // Catch-all child
18
- handler HandlerFunc
19
- pnames []string
20
- echo * Echo
16
+ pchild * node // Param child
17
+ cchild * node // Catch-all child
18
+ handler HandlerFunc
19
+ pnames []string
20
+ echo * Echo
21
21
}
22
22
ntype uint8
23
23
children []* node
@@ -96,11 +96,18 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
96
96
// Split node
97
97
n := newNode (t , cn .prefix [l :], cn , cn .children , cn .handler , cn .pnames , cn .echo )
98
98
cn .children = children {n } // Add to parent
99
+ // if n.typ == ptype {
100
+ // cn.pchild = n
101
+ // } else if n.typ == ctype {
102
+ // cn.cchild = n
103
+ // }
99
104
100
105
// Reset parent node
101
106
cn .typ = stype
102
107
cn .label = cn .prefix [0 ]
103
108
cn .prefix = cn .prefix [:l ]
109
+ // cn.pchild = nil
110
+ // cn.cchild = nil
104
111
cn .handler = nil
105
112
cn .pnames = nil
106
113
cn .echo = nil
@@ -115,6 +122,11 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
115
122
// Create child node
116
123
n = newNode (t , search [l :], cn , children {}, h , pnames , echo )
117
124
cn .children = append (cn .children , n )
125
+ // if n.typ == ptype {
126
+ // cn.pchild = n
127
+ // } else if n.typ == ctype {
128
+ // cn.cchild = n
129
+ // }
118
130
}
119
131
} else if l < sl {
120
132
search = search [l :]
@@ -127,6 +139,11 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
127
139
// Create child node
128
140
n := newNode (t , search , cn , children {}, h , pnames , echo )
129
141
cn .children = append (cn .children , n )
142
+ // if n.typ == ptype {
143
+ // cn.pchild = n
144
+ // } else if n.typ == ctype {
145
+ // cn.cchild = n
146
+ // }
130
147
} else {
131
148
// Node already exists
132
149
if h != nil {
@@ -139,8 +156,8 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
139
156
}
140
157
}
141
158
142
- func newNode (t ntype , pfx string , p * node , c children , h HandlerFunc , pnames []string , echo * Echo ) ( n * node ) {
143
- n = & node {
159
+ func newNode (t ntype , pfx string , p * node , c children , h HandlerFunc , pnames []string , echo * Echo ) * node {
160
+ return & node {
144
161
typ : t ,
145
162
label : pfx [0 ],
146
163
prefix : pfx ,
@@ -150,7 +167,9 @@ func newNode(t ntype, pfx string, p *node, c children, h HandlerFunc, pnames []s
150
167
pnames : pnames ,
151
168
echo : echo ,
152
169
}
153
- return
170
+ }
171
+
172
+ func (n * node ) addChild (c * node ) {
154
173
}
155
174
156
175
func (n * node ) findChild (l byte ) * node {
@@ -201,20 +220,22 @@ func lcp(a, b string) (i int) {
201
220
return
202
221
}
203
222
204
- func (r * router ) Find (method , path string , c * Context ) (h HandlerFunc , echo * Echo ) {
223
+ func (r * router ) Find (method , path string , ctx * Context ) (h HandlerFunc , echo * Echo ) {
205
224
cn := r .trees [method ] // Current node as root
206
225
search := path
207
- chn := new (node ) // Child node
208
- n := 0 // Param counter
226
+ c := new (node ) // Child node
227
+ n := 0 // Param counter
209
228
210
229
// Search order static > param > catch-all
211
230
for {
212
231
if search == "" || search == cn .prefix {
213
- // Found
214
- h = cn .handler
215
- c .pnames = cn .pnames
216
- echo = cn .echo
217
- return
232
+ if cn .handler != nil {
233
+ // Found
234
+ h = cn .handler
235
+ ctx .pnames = cn .pnames
236
+ echo = cn .echo
237
+ return
238
+ }
218
239
}
219
240
220
241
pl := len (cn .prefix )
@@ -226,32 +247,40 @@ func (r *router) Find(method, path string, c *Context) (h HandlerFunc, echo *Ech
226
247
goto Up
227
248
}
228
249
250
+ // Check for catch-all with empty string
251
+ if len (search ) == 0 {
252
+ goto CatchAll
253
+ }
254
+
229
255
// Static node
230
- chn = cn .findSchild (search [0 ])
231
- if chn != nil {
232
- cn = chn
256
+ c = cn .findSchild (search [0 ])
257
+ if c != nil {
258
+ cn = c
233
259
continue
234
260
}
235
261
236
262
// Param node
237
263
Param:
238
- chn = cn .findPchild ()
239
- if chn != nil {
240
- cn = chn
264
+ c = cn .findPchild ()
265
+ // c = cn.pchild
266
+ if c != nil {
267
+ cn = c
241
268
i , l := 0 , len (search )
242
269
for ; i < l && search [i ] != '/' ; i ++ {
243
270
}
244
- c .pvalues [n ] = search [:i ]
271
+ ctx .pvalues [n ] = search [:i ]
245
272
n ++
246
273
search = search [i :]
247
274
continue
248
275
}
249
276
250
277
// Catch-all node
251
- chn = cn .findCchild ()
252
- if chn != nil {
253
- cn = chn
254
- c .pvalues [n ] = search
278
+ CatchAll:
279
+ // c = cn.cchild
280
+ c = cn .findCchild ()
281
+ if c != nil {
282
+ cn = c
283
+ ctx .pvalues [n ] = search
255
284
search = "" // End search
256
285
continue
257
286
}
0 commit comments