Use slices.Backward where appropriate (#1165)

This commit is contained in:
James Raspass 2026-08-31 10:15:36 +01:00 committed by GitHub
parent 756fcb8d63
commit 77074b34b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 7 deletions

View file

@ -125,9 +125,9 @@ func (x *Context) Clone() *Context {
// URLParam returns the corresponding URL parameter value from the request
// routing context.
func (x *Context) URLParam(key string) string {
for k := len(x.URLParams.Keys) - 1; k >= 0; k-- {
if x.URLParams.Keys[k] == key {
return x.URLParams.Values[k]
for i, k := range slices.Backward(x.URLParams.Keys) {
if k == key {
return x.URLParams.Values[i]
}
}
return ""

View file

@ -5,6 +5,7 @@ import (
"net"
"net/http"
"net/netip"
"slices"
"strings"
)
@ -226,8 +227,7 @@ func GetClientIPAddr(ctx context.Context) netip.Addr {
// comma-separated entries in order received — so an attacker cannot pick
// which value security logic sees by sending a duplicate header.
func walkXFF(headers []string, visit func(entry string) bool) {
for hi := len(headers) - 1; hi >= 0; hi-- {
h := headers[hi]
for _, h := range slices.Backward(headers) {
for h != "" {
var v string
if i := strings.LastIndexByte(h, ','); i >= 0 {

View file

@ -839,8 +839,8 @@ func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }
// tailSort pushes nodes with '/' as the tail to the end of the list for param nodes.
// The list order determines the traversal order.
func (ns nodes) tailSort() {
for i := len(ns) - 1; i >= 0; i-- {
if ns[i].typ > ntStatic && ns[i].tail == '/' {
for i, n := range slices.Backward(ns) {
if n.typ > ntStatic && n.tail == '/' {
ns.Swap(i, len(ns)-1)
return
}