Skip to content

Commit c654485

Browse files
thinkerouappleboy
authored andcommitted
tree: sync httprouter update (#2171)
1 parent 7c21e04 commit c654485

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

tree.go

+33-28
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,15 @@ type node struct {
107107

108108
// increments priority of the given child and reorders if necessary.
109109
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
112113

113-
// adjust position (move to front)
114+
// Adjust position (move to front)
114115
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]
120119
}
121120

122121
// build new index char string
@@ -231,7 +230,7 @@ walk:
231230
}
232231

233232
// 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++ {
235234
if c == n.indices[i] {
236235
parentFullPathIndex += len(n.path)
237236
i = n.incrementChildPrio(i)
@@ -404,17 +403,20 @@ func (n *node) getValue(path string, po Params, unescape bool) (value nodeValue)
404403
value.params = po
405404
walk: // Outer loop for walking the tree
406405
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):]
410410
// If this node does not have a wildcard (param or catchAll)
411411
// child, we can just look up the next child node and continue
412412
// to walk down the tree
413413
if !n.wildChild {
414414
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] {
417418
n = n.children[i]
419+
prefix = n.path
418420
continue walk
419421
}
420422
}
@@ -458,6 +460,7 @@ walk: // Outer loop for walking the tree
458460
if len(n.children) > 0 {
459461
path = path[end:]
460462
n = n.children[0]
463+
prefix = n.path
461464
continue walk
462465
}
463466

@@ -504,7 +507,7 @@ walk: // Outer loop for walking the tree
504507
panic("invalid node type")
505508
}
506509
}
507-
} else if path == n.path {
510+
} else if path == prefix {
508511
// We should have reached the node containing the handle.
509512
// Check if this node has a handle registered.
510513
if value.handlers = n.handlers; value.handlers != nil {
@@ -519,8 +522,9 @@ walk: // Outer loop for walking the tree
519522

520523
// No handle found. Check if a handle for this path + a
521524
// 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] == '/' {
524528
n = n.children[i]
525529
value.tsr = (len(n.path) == 1 && n.handlers != nil) ||
526530
(n.nType == catchAll && n.children[0].handlers != nil)
@@ -534,8 +538,8 @@ walk: // Outer loop for walking the tree
534538
// Nothing found. We can recommend to redirect to the same URL with an
535539
// extra trailing slash if a leaf exists for that path
536540
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)
539543
return
540544
}
541545
}
@@ -601,33 +605,34 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPa
601605
n = n.children[0]
602606
switch n.nType {
603607
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++
608612
}
609613

610614
// add param value to case insensitive path
611-
ciPath = append(ciPath, path[:k]...)
615+
ciPath = append(ciPath, path[:end]...)
612616

613617
// we need to go deeper!
614-
if k < len(path) {
618+
if end < len(path) {
615619
if len(n.children) > 0 {
616-
path = path[k:]
620+
path = path[end:]
617621
n = n.children[0]
618622
continue
619623
}
620624

621625
// ... but we can't
622-
if fixTrailingSlash && len(path) == k+1 {
626+
if fixTrailingSlash && len(path) == end+1 {
623627
return ciPath, true
624628
}
625629
return
626630
}
627631

628632
if n.handlers != nil {
629633
return ciPath, true
630-
} else if fixTrailingSlash && len(n.children) == 1 {
634+
}
635+
if fixTrailingSlash && len(n.children) == 1 {
631636
// No handle found. Check if a handle for this path + a
632637
// trailing slash exists
633638
n = n.children[0]

0 commit comments

Comments
 (0)