@@ -536,6 +536,9 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
536536 }
537537
538538 paramNames := make ([]string , 0 )
539+ // Positions of parameter markers after names are removed. Literal colons
540+ // remain ordinary path bytes, so no sentinel byte is reserved.
541+ paramMarkers := make ([]int , 0 )
539542 originalPath := path
540543 wasAdded := false
541544 var ri RouteInfo
@@ -549,11 +552,12 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
549552 }
550553 j := i + 1
551554
552- r .insert (staticKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }})
555+ r .insert (staticKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }}, paramMarkers )
553556 for ; i < lcpIndex && path [i ] != '/' ; i ++ {
554557 }
555558
556559 paramNames = append (paramNames , path [j :i ])
560+ paramMarkers = append (paramMarkers , j - 1 )
557561 path = path [:j ] + path [i :]
558562 i , lcpIndex = j , len (path )
559563
@@ -566,14 +570,14 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
566570 orgRouteInfo : ri ,
567571 wrappedHeadHandler : headH ,
568572 }
569- r .insert (paramKind , path [:i ], method , rm )
573+ r .insert (paramKind , path [:i ], method , rm , paramMarkers )
570574 wasAdded = true
571575 break
572576 } else {
573- r .insert (paramKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }})
577+ r .insert (paramKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }}, paramMarkers )
574578 }
575579 } else if path [i ] == anyLabel {
576- r .insert (staticKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }})
580+ r .insert (staticKind , path [:i ], method , routeMethod {RouteInfo : & RouteInfo {Method : method }}, paramMarkers )
577581 paramNames = append (paramNames , "*" )
578582 ri = route .ToRouteInfo (paramNames )
579583 rm := routeMethod {
@@ -582,7 +586,7 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
582586 orgRouteInfo : ri ,
583587 wrappedHeadHandler : headH ,
584588 }
585- r .insert (anyKind , path [:i + 1 ], method , rm )
589+ r .insert (anyKind , path [:i + 1 ], method , rm , paramMarkers )
586590 wasAdded = true
587591 break
588592 }
@@ -596,7 +600,7 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
596600 orgRouteInfo : ri ,
597601 wrappedHeadHandler : headH ,
598602 }
599- r .insert (staticKind , path , method , rm )
603+ r .insert (staticKind , path , method , rm , paramMarkers )
600604 }
601605
602606 r .storeRouteInfo (ri )
@@ -623,12 +627,13 @@ func (r *DefaultRouter) storeRouteInfo(ri RouteInfo) {
623627 r .routes = append (r .routes , ri )
624628}
625629
626- func (r * DefaultRouter ) insert (t kind , path string , method string , ri routeMethod ) {
630+ func (r * DefaultRouter ) insert (t kind , path string , method string , ri routeMethod , paramMarkers [] int ) {
627631 if len (ri .Parameters ) > r .maxPathParamsLength {
628632 r .maxPathParamsLength = len (ri .Parameters )
629633 }
630634 currentNode := r .tree // Current node as root
631635 search := path
636+ searchOffset := 0
632637
633638 for {
634639 searchLen := len (search )
@@ -717,8 +722,16 @@ func (r *DefaultRouter) insert(t kind, path string, method string, ri routeMetho
717722 }
718723 currentNode .refreshLeaf ()
719724 } else if lcpLen < searchLen {
725+ searchOffset += lcpLen
720726 search = search [lcpLen :]
721- c := currentNode .findChildWithLabel (search [0 ])
727+ isParamMarker := false
728+ for _ , marker := range paramMarkers {
729+ if marker == searchOffset {
730+ isParamMarker = true
731+ break
732+ }
733+ }
734+ c := currentNode .findChildWithLabel (search [0 ], isParamMarker )
722735 if c != nil {
723736 // Go deeper
724737 currentNode = c
@@ -810,13 +823,13 @@ func (n *node) findStaticChild(l byte) *node {
810823 return nil
811824}
812825
813- func (n * node ) findChildWithLabel (l byte ) * node {
826+ func (n * node ) findChildWithLabel (l byte , isParamMarker bool ) * node {
827+ if isParamMarker {
828+ return n .paramChild
829+ }
814830 if c := n .findStaticChild (l ); c != nil {
815831 return c
816832 }
817- if l == paramLabel {
818- return n .paramChild
819- }
820833 if l == anyLabel {
821834 return n .anyChild
822835 }
@@ -937,8 +950,8 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
937950 searchIndex -= len (previous .prefix )
938951 } else {
939952 paramIndex --
940- // for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
941- // for that index as it would also contain part of path we cut off before moving into node we are backtracking from
953+ // param/any node prefixes are a single marker byte, so restore searchIndex
954+ // from the value stored for that param instead
942955 searchIndex -= len (pathValues [paramIndex ].Value )
943956 pathValues [paramIndex ].Value = ""
944957 }
0 commit comments