@@ -339,34 +339,37 @@ func (r *Router) Find(method, path string, c Context) {
339
339
pvalues = ctx .pvalues // Use the internal slice so the interface can keep the illusion of a dynamic slice
340
340
)
341
341
342
- // backtracking happens when we reach dead end when matching nodes in the router tree. To backtrack we set
343
- // current node to parent to current node (this was previous node we checked) and choose next node kind to check.
344
- // Next node kind relies on routing priority (static->param->any). So for example if there is no static node match we
345
- // should check parent next sibling by kind (param). Backtracking itself does not check if there is next sibling this
346
- // is left up to matching logic
342
+ // Backtracking is needed when a dead end (leaf node) is reached in the router tree.
343
+ // To backtrack the current node will be changed to the parent node and the next kind for the
344
+ // router logic will be returned based on fromKind or kind of the dead end node ( static > param > any).
345
+ // For example if there is no static node match we should check parent next sibling by kind (param).
346
+ // Backtracking itself does not check if there is a next sibling, this is done by the router logic.
347
347
backtrackToNextNodeKind := func (fromKind kind ) (nextNodeKind kind , valid bool ) {
348
348
previous := cn
349
349
cn = previous .parent
350
350
valid = cn != nil
351
351
352
- // next node type by priority
353
- // NOTE: by current implementation we never backtrack from any route so `previous.kind` value is here always static or any
354
- // if this requirement is to change then for any route next kind would be static kind and this statement should be changed
352
+ // Next node type by priority
353
+ // NOTE: With the current implementation we never backtrack from an `any` route, so `previous.kind` is
354
+ // always `static` or `any`
355
+ // If this is changed then for any route next kind would be `static` and this statement should be changed
355
356
nextNodeKind = previous .kind + 1
356
357
357
358
if fromKind == skind {
358
359
// when backtracking is done from static kind block we did not change search so nothing to restore
359
360
return
360
361
}
361
362
363
+ // restore search to value it was before we move to current node we are backtracking from.
362
364
if previous .kind == skind {
363
365
searchIndex -= len (previous .prefix )
364
- search = path [searchIndex :]
365
366
} else {
366
367
n --
368
+ // for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
369
+ // for that index as it would also contain part of path we cut off before moving into node we are backtracking from
367
370
searchIndex -= len (pvalues [n ])
368
- search = path [searchIndex :]
369
371
}
372
+ search = path [searchIndex :]
370
373
return
371
374
}
372
375
0 commit comments