Repository: labstack/echo
File: router.go (Go)

Code before:

	// backtracking happens when we reach dead end when matching nodes in the router tree. To backtrack we set
	// current node to parent to current node (this was previous node we checked) and choose next node kind to check.
	// Next node kind relies on routing priority (static->param->any). So for example if there is no static node match we
	// should check parent next sibling by kind (param). Backtracking itself does not check if there is next sibling this
	// is left up to matching logic
	backtrackToNextNodeKind := func(fromKind kind) (nextNodeKind kind, valid bool) {
		previous := cn
		cn = previous.parent
		valid = cn != nil

		// next node type by priority
		// NOTE: by current implementation we never backtrack from any route so `previous.kind` value is here always static or any
		// if this requirement is to change then for any route next kind would be static kind and this statement should be changed
		nextNodeKind = previous.kind + 1

		if fromKind == skind {
			// when backtracking is done from static kind block we did not change search so nothing to restore
			return
		}

		if previous.kind == skind {
			searchIndex -= len(previous.prefix)
			search = path[searchIndex:]
		} else {
			n--
			searchIndex -= len(pvalues[n])
			search = path[searchIndex:]
		}
		return
	}

	// Search order static > param > any
	for {
		pl := 0 // Prefix length
		l := 0  // LCP length

		if cn.label != ':' {
			sl := len(search)
			pl = len(cn.prefix)

			// LCP
			max := pl
			if sl < max {
				max = sl
			}
			for ; l < max && search[l] == cn.prefix[l]; l++ {
			}
		}

		if l != pl {

Review comment (suggestion):
Should either add a comment on what we process here, currently it's an any node, some code should fit for param node too.

Code after:

	// Backtracking is needed when a dead end (leaf node) is reached in the router tree.
	// To backtrack the current node will be changed to the parent node and the next kind for the
	// router logic will be returned based on fromKind or kind of the dead end node (static > param > any).
	// For example if there is no static node match we should check parent next sibling by kind (param).
	// Backtracking itself does not check if there is a next sibling, this is done by the router logic.
	backtrackToNextNodeKind := func(fromKind kind) (nextNodeKind kind, valid bool) {
		previous := cn
		cn = previous.parent
		valid = cn != nil

		// Next node type by priority
		// NOTE: With the current implementation we never backtrack from an `any` route, so `previous.kind` is
		// always `static` or `any`
		// If this is changed then for any route next kind would be `static` and this statement should be changed
		nextNodeKind = previous.kind + 1

		if fromKind == skind {
			// when backtracking is done from static kind block we did not change search so nothing to restore
			return
		}

		// restore search to value it was before we move to current node we are backtracking from.
		if previous.kind == skind {
			searchIndex -= len(previous.prefix)
		} else {
			n--
			// for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
			// for that index as it would also contain part of path we cut off before moving into node we are backtracking from
			searchIndex -= len(pvalues[n])
		}
		search = path[searchIndex:]
		return
	}

	// Search order static > param > any
	for {
		pl := 0 // Prefix length
		l := 0  // LCP length

		if cn.label != ':' {
			sl := len(search)
			pl = len(cn.prefix)

			// LCP
			max := pl
			if sl < max {
				max = sl
			}
			for ; l < max && search[l] == cn.prefix[l]; l++ {
			}