@@ -107,16 +107,15 @@ type node struct {
107
107
108
108
// increments priority of the given child and reorders if necessary.
109
109
func (n * node ) incrementChildPrio (pos int ) int {
110
- n .children [pos ].priority ++
111
- prio := n .children [pos ].priority
110
+ cs := n .children
111
+ cs [pos ].priority ++
112
+ prio := cs [pos ].priority
112
113
113
- // adjust position (move to front)
114
+ // Adjust position (move to front)
114
115
newPos := pos
115
- for newPos > 0 && n .children [newPos - 1 ].priority < prio {
116
- // swap node positions
117
- n .children [newPos - 1 ], n .children [newPos ] = n .children [newPos ], n .children [newPos - 1 ]
118
-
119
- newPos --
116
+ for ; newPos > 0 && cs [newPos - 1 ].priority < prio ; newPos -- {
117
+ // Swap node positions
118
+ cs [newPos - 1 ], cs [newPos ] = cs [newPos ], cs [newPos - 1 ]
120
119
}
121
120
122
121
// build new index char string
@@ -231,7 +230,7 @@ walk:
231
230
}
232
231
233
232
// Check if a child with the next path byte exists
234
- for i := 0 ; i < len (n .indices ); i ++ {
233
+ for i , max := 0 , len (n .indices ); i < max ; i ++ {
235
234
if c == n .indices [i ] {
236
235
parentFullPathIndex += len (n .path )
237
236
i = n .incrementChildPrio (i )
@@ -404,17 +403,20 @@ func (n *node) getValue(path string, po Params, unescape bool) (value nodeValue)
404
403
value .params = po
405
404
walk: // Outer loop for walking the tree
406
405
for {
407
- if len (path ) > len (n .path ) {
408
- if path [:len (n .path )] == n .path {
409
- path = path [len (n .path ):]
406
+ prefix := n .path
407
+ if len (path ) > len (prefix ) {
408
+ if path [:len (prefix )] == prefix {
409
+ path = path [len (prefix ):]
410
410
// If this node does not have a wildcard (param or catchAll)
411
411
// child, we can just look up the next child node and continue
412
412
// to walk down the tree
413
413
if ! n .wildChild {
414
414
c := path [0 ]
415
- for i := 0 ; i < len (n .indices ); i ++ {
416
- if c == n .indices [i ] {
415
+ indices := n .indices
416
+ for i , max := 0 , len (indices ); i < max ; i ++ {
417
+ if c == indices [i ] {
417
418
n = n .children [i ]
419
+ prefix = n .path
418
420
continue walk
419
421
}
420
422
}
@@ -458,6 +460,7 @@ walk: // Outer loop for walking the tree
458
460
if len (n .children ) > 0 {
459
461
path = path [end :]
460
462
n = n .children [0 ]
463
+ prefix = n .path
461
464
continue walk
462
465
}
463
466
@@ -504,7 +507,7 @@ walk: // Outer loop for walking the tree
504
507
panic ("invalid node type" )
505
508
}
506
509
}
507
- } else if path == n . path {
510
+ } else if path == prefix {
508
511
// We should have reached the node containing the handle.
509
512
// Check if this node has a handle registered.
510
513
if value .handlers = n .handlers ; value .handlers != nil {
@@ -519,8 +522,9 @@ walk: // Outer loop for walking the tree
519
522
520
523
// No handle found. Check if a handle for this path + a
521
524
// trailing slash exists for trailing slash recommendation
522
- for i := 0 ; i < len (n .indices ); i ++ {
523
- if n .indices [i ] == '/' {
525
+ indices := n .indices
526
+ for i , max := 0 , len (indices ); i < max ; i ++ {
527
+ if indices [i ] == '/' {
524
528
n = n .children [i ]
525
529
value .tsr = (len (n .path ) == 1 && n .handlers != nil ) ||
526
530
(n .nType == catchAll && n .children [0 ].handlers != nil )
@@ -534,8 +538,8 @@ walk: // Outer loop for walking the tree
534
538
// Nothing found. We can recommend to redirect to the same URL with an
535
539
// extra trailing slash if a leaf exists for that path
536
540
value .tsr = (path == "/" ) ||
537
- (len (n . path ) == len (path )+ 1 && n . path [len (path )] == '/' &&
538
- path == n . path [:len (n . path )- 1 ] && n .handlers != nil )
541
+ (len (prefix ) == len (path )+ 1 && prefix [len (path )] == '/' &&
542
+ path == prefix [:len (prefix )- 1 ] && n .handlers != nil )
539
543
return
540
544
}
541
545
}
@@ -601,33 +605,34 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPa
601
605
n = n .children [0 ]
602
606
switch n .nType {
603
607
case param :
604
- // find param end (either '/' or path end)
605
- k := 0
606
- for k < len (path ) && path [k ] != '/' {
607
- k ++
608
+ // Find param end (either '/' or path end)
609
+ end := 0
610
+ for end < len (path ) && path [end ] != '/' {
611
+ end ++
608
612
}
609
613
610
614
// add param value to case insensitive path
611
- ciPath = append (ciPath , path [:k ]... )
615
+ ciPath = append (ciPath , path [:end ]... )
612
616
613
617
// we need to go deeper!
614
- if k < len (path ) {
618
+ if end < len (path ) {
615
619
if len (n .children ) > 0 {
616
- path = path [k :]
620
+ path = path [end :]
617
621
n = n .children [0 ]
618
622
continue
619
623
}
620
624
621
625
// ... but we can't
622
- if fixTrailingSlash && len (path ) == k + 1 {
626
+ if fixTrailingSlash && len (path ) == end + 1 {
623
627
return ciPath , true
624
628
}
625
629
return
626
630
}
627
631
628
632
if n .handlers != nil {
629
633
return ciPath , true
630
- } else if fixTrailingSlash && len (n .children ) == 1 {
634
+ }
635
+ if fixTrailingSlash && len (n .children ) == 1 {
631
636
// No handle found. Check if a handle for this path + a
632
637
// trailing slash exists
633
638
n = n .children [0 ]
0 commit comments