Repository: gin-gonic/gin
File: tree.go (Go)

Code before:

	// If no wildcard was found, simply insert the path and handle
	n.path = path
	n.handlers = handlers
	n.fullPath = fullPath
}

// nodeValue holds return values of (*Node).getValue method
type nodeValue struct {
	handlers HandlersChain
	params   *Params
	tsr      bool
	fullPath string
}

// Returns the handle registered with the given path (key). The values of
// wildcards are saved to a map.
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, params *Params, unescape bool) (value nodeValue) {
	var (
		skipped *skip
		nroot   = &(*n)                                // not found `level 1 router` use nroot
		ldi     = len(strings.Split(path, "/")[1]) + 1 // level 1 router '/' index
		ri      int                                    //record index
	)

	if len(path) == 1 {
		ldi = 0
	}

walk: // Outer loop for walking the tree
	for {

		prefix := n.path

		// level 1 router search end
		if (strings.HasSuffix(prefix, "/") ||
			n.indices == "/" ||
			len(n.indices) == 0 ||
			strings.Contains(n.fullPath, ":")) && len(prefix) > 1 {
			ri = math.MaxUint8
		}

		if len(path) > len(prefix) {
			if path[:len(prefix)] == prefix {
				path = path[len(prefix):]

				// Try all the non-wildcard children first by matching the indices
				idxc := path[0]

Review comment (suggestion):
these names are a little too short and cryptic for my taste. mind making them longer? (i.e. `recordIndex`)

Code after:

// nodeValue holds return values of (*Node).getValue method
type nodeValue struct {
	handlers HandlersChain
	params   *Params
	tsr      bool
	fullPath string
}

// Returns the handle registered with the given path (key). The values of
// wildcards are saved to a map.
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, params *Params, unescape bool) (value nodeValue) {
	// path: /abc/123/def
	// level 1 router:abc
	// level 2 router:123
	// level 3 router:def
	var (
		skippedPath string
		latestNode  = n // not found `level 2 router` use latestNode

		// match '/' count
		// matchNum < 1: `level 2 router` not found,the current node needs to be equal to latestNode
		// matchNum >= 1: `level (2 or 3 or 4 or ...) router`: Normal handling
		matchNum int // each match will accumulate
	)
	//if path == "/", no need to look for tree node
	if len(path) == 1 {
		matchNum = 1
	}

walk: // Outer loop for walking the tree
	for {
		prefix := n.path
		if len(path) > len(prefix) {
			if path[:len(prefix)] == prefix {
				path = path[len(prefix):]

				// Try all the non-wildcard children first by matching the indices
				idxc := path[0]
				for i, c := range []byte(n.indices) {
					if c == idxc {
						//  strings.HasPrefix(n.children[len(n.children)-1].path, ":") == n.wildChild
						if n.wildChild {
							skippedPath = prefix + path
							latestNode = &node{
								path:      n.path,
								wildChild: n.wildChild,
								nType:     n.nType,