Merge 38c24307a96a49e25391b9fe12d6c803ac2c0a82 into d3ffc9985281dcf4d3bef604cce4e662b1a327a6

This commit is contained in:
Milad 2026-03-18 15:48:29 +08:00 committed by GitHub
commit e6f7ce8a79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

27
tree.go
View File

@ -58,6 +58,8 @@ func (trees methodTrees) get(method string) *node {
return nil return nil
} }
// longestCommonPrefix returns the length in bytes of the longest common prefix
// of the two input strings `a` and `b`.
func longestCommonPrefix(a, b string) int { func longestCommonPrefix(a, b string) int {
i := 0 i := 0
max_ := min(len(a), len(b)) max_ := min(len(a), len(b))
@ -410,6 +412,19 @@ type skippedNode struct {
paramsCount int16 paramsCount int16
} }
// ensureParamsCapacity ensures that the params slice has capacity for at least needed elements.
// It preserves existing length and content.
func ensureParamsCapacity(params *Params, needed int) {
if params == nil {
return
}
if cap(*params) < needed {
newParams := make(Params, len(*params), needed)
copy(newParams, *params)
*params = newParams
}
}
// Returns the handle registered with the given path (key). The values of // Returns the handle registered with the given path (key). The values of
// wildcards are saved to a map. // wildcards are saved to a map.
// If no handle can be found, a TSR (trailing slash redirect) recommendation is // If no handle can be found, a TSR (trailing slash redirect) recommendation is
@ -497,11 +512,7 @@ walk: // Outer loop for walking the tree
// Save param value // Save param value
if params != nil { if params != nil {
// Preallocate capacity if necessary // Preallocate capacity if necessary
if cap(*params) < int(globalParamsCount) { ensureParamsCapacity(params, int(globalParamsCount))
newParams := make(Params, len(*params), globalParamsCount)
copy(newParams, *params)
*params = newParams
}
if value.params == nil { if value.params == nil {
value.params = params value.params = params
@ -550,11 +561,7 @@ walk: // Outer loop for walking the tree
// Save param value // Save param value
if params != nil { if params != nil {
// Preallocate capacity if necessary // Preallocate capacity if necessary
if cap(*params) < int(globalParamsCount) { ensureParamsCapacity(params, int(globalParamsCount))
newParams := make(Params, len(*params), globalParamsCount)
copy(newParams, *params)
*params = newParams
}
if value.params == nil { if value.params == nil {
value.params = params value.params = params