chore: update tree (#2371)
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
3b5e861bb1
commit
a573ec6a37
53
tree.go
53
tree.go
@ -119,7 +119,6 @@ func (n *node) incrementChildPrio(pos int) int {
|
|||||||
for ; newPos > 0 && cs[newPos-1].priority < prio; newPos-- {
|
for ; newPos > 0 && cs[newPos-1].priority < prio; newPos-- {
|
||||||
// Swap node positions
|
// Swap node positions
|
||||||
cs[newPos-1], cs[newPos] = cs[newPos], cs[newPos-1]
|
cs[newPos-1], cs[newPos] = cs[newPos], cs[newPos-1]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build new index char string
|
// Build new index char string
|
||||||
@ -559,8 +558,8 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) ([]by
|
|||||||
// Use a static sized buffer on the stack in the common case.
|
// Use a static sized buffer on the stack in the common case.
|
||||||
// If the path is too long, allocate a buffer on the heap instead.
|
// If the path is too long, allocate a buffer on the heap instead.
|
||||||
buf := make([]byte, 0, stackBufSize)
|
buf := make([]byte, 0, stackBufSize)
|
||||||
if l := len(path) + 1; l > stackBufSize {
|
if length := len(path) + 1; length > stackBufSize {
|
||||||
buf = make([]byte, 0, l)
|
buf = make([]byte, 0, length)
|
||||||
}
|
}
|
||||||
|
|
||||||
ciPath := n.findCaseInsensitivePathRec(
|
ciPath := n.findCaseInsensitivePathRec(
|
||||||
@ -600,7 +599,30 @@ walk: // Outer loop for walking the tree
|
|||||||
path = path[npLen:]
|
path = path[npLen:]
|
||||||
ciPath = append(ciPath, n.path...)
|
ciPath = append(ciPath, n.path...)
|
||||||
|
|
||||||
if len(path) > 0 {
|
if len(path) == 0 {
|
||||||
|
// We should have reached the node containing the handle.
|
||||||
|
// Check if this node has a handle registered.
|
||||||
|
if n.handlers != nil {
|
||||||
|
return ciPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// No handle found.
|
||||||
|
// Try to fix the path by adding a trailing slash
|
||||||
|
if fixTrailingSlash {
|
||||||
|
for i, c := range []byte(n.indices) {
|
||||||
|
if c == '/' {
|
||||||
|
n = n.children[i]
|
||||||
|
if (len(n.path) == 1 && n.handlers != nil) ||
|
||||||
|
(n.nType == catchAll && n.children[0].handlers != nil) {
|
||||||
|
return append(ciPath, '/')
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// If this node does not have a wildcard (param or catchAll) child,
|
// If this node does not have a wildcard (param or catchAll) child,
|
||||||
// we can just look up the next child node and continue to walk down
|
// we can just look up the next child node and continue to walk down
|
||||||
// the tree
|
// the tree
|
||||||
@ -735,29 +757,6 @@ walk: // Outer loop for walking the tree
|
|||||||
default:
|
default:
|
||||||
panic("invalid node type")
|
panic("invalid node type")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// We should have reached the node containing the handle.
|
|
||||||
// Check if this node has a handle registered.
|
|
||||||
if n.handlers != nil {
|
|
||||||
return ciPath
|
|
||||||
}
|
|
||||||
|
|
||||||
// No handle found.
|
|
||||||
// Try to fix the path by adding a trailing slash
|
|
||||||
if fixTrailingSlash {
|
|
||||||
for i, c := range []byte(n.indices) {
|
|
||||||
if c == '/' {
|
|
||||||
n = n.children[i]
|
|
||||||
if (len(n.path) == 1 && n.handlers != nil) ||
|
|
||||||
(n.nType == catchAll && n.children[0].handlers != nil) {
|
|
||||||
return append(ciPath, '/')
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing found.
|
// Nothing found.
|
||||||
|
Loading…
Reference in New Issue
Block a user