@@ -59,6 +59,8 @@ func (trees methodTrees) get(method string) *node {
5959 return nil
6060}
6161
62+ // longestCommonPrefix returns the length in bytes of the longest common prefix
63+ // of the two input strings `a` and `b`.
6264func longestCommonPrefix (a , b string ) int {
6365 i := 0
6466 max_ := min (len (a ), len (b ))
@@ -419,6 +421,19 @@ type skippedNode struct {
419421 paramsCount int16
420422}
421423
424+ // ensureParamsCapacity ensures that the params slice has capacity for at least needed elements.
425+ // It preserves existing length and content.
426+ func ensureParamsCapacity (params * Params , needed int ) {
427+ if params == nil {
428+ return
429+ }
430+ if cap (* params ) < needed {
431+ newParams := make (Params , len (* params ), needed )
432+ copy (newParams , * params )
433+ * params = newParams
434+ }
435+ }
436+
422437// Returns the handle registered with the given path (key). The values of
423438// wildcards are saved to a map.
424439// If no handle can be found, a TSR (trailing slash redirect) recommendation is
@@ -506,11 +521,7 @@ walk: // Outer loop for walking the tree
506521 // Save param value
507522 if params != nil {
508523 // Preallocate capacity if necessary
509- if cap (* params ) < int (globalParamsCount ) {
510- newParams := make (Params , len (* params ), globalParamsCount )
511- copy (newParams , * params )
512- * params = newParams
513- }
524+ ensureParamsCapacity (params , int (globalParamsCount ))
514525
515526 if value .params == nil {
516527 value .params = params
@@ -559,11 +570,7 @@ walk: // Outer loop for walking the tree
559570 // Save param value
560571 if params != nil {
561572 // Preallocate capacity if necessary
562- if cap (* params ) < int (globalParamsCount ) {
563- newParams := make (Params , len (* params ), globalParamsCount )
564- copy (newParams , * params )
565- * params = newParams
566- }
573+ ensureParamsCapacity (params , int (globalParamsCount ))
567574
568575 if value .params == nil {
569576 value .params = params
0 commit comments