use a more modern writing style to simplify code (#7182)

Signed-off-by: joemicky <joemickychang@outlook.com>
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
This commit is contained in:
joemicky 2025-08-21 02:41:21 +09:00 committed by GitHub
parent b15ed9b084
commit 5125fbed41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 19 additions and 30 deletions

View File

@ -207,7 +207,7 @@ func TestETags(t *testing.T) {
}
func BenchmarkLoad(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
Load(testCfg, true)
}
}

View File

@ -535,7 +535,7 @@ func BenchmarkMatchExpressionMatch(b *testing.B) {
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
tc.expression.MatchWithError(req)
}
})

View File

@ -452,8 +452,7 @@ func (rw *responseWriter) init() {
func hasVaryValue(hdr http.Header, target string) bool {
for _, vary := range hdr.Values("Vary") {
vals := strings.Split(vary, ",")
for _, val := range vals {
for val := range strings.SplitSeq(vary, ",") {
if strings.EqualFold(strings.TrimSpace(val), target) {
return true
}
@ -478,7 +477,7 @@ func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
prefs := []encodingPreference{}
for _, accepted := range strings.Split(acceptEncHeader, ",") {
for accepted := range strings.SplitSeq(acceptEncHeader, ",") {
parts := strings.Split(accepted, ";")
encName := strings.ToLower(strings.TrimSpace(parts[0]))

View File

@ -9,7 +9,7 @@ import (
func BenchmarkOpenResponseWriter(b *testing.B) {
enc := new(Encode)
for n := 0; n < b.N; n++ {
for b.Loop() {
enc.openResponseWriter("test", nil, false)
}
}

View File

@ -947,7 +947,7 @@ func BenchmarkHeaderREMatcher(b *testing.B) {
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
req = req.WithContext(ctx)
addHTTPVarsToReplacer(repl, req, httptest.NewRecorder())
for run := 0; run < b.N; run++ {
for b.Loop() {
match.MatchWithError(req)
}
}
@ -992,7 +992,6 @@ func TestVarREMatcher(t *testing.T) {
expect: true,
},
} {
i := i // capture range value
tc := tc // capture range value
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
@ -1180,8 +1179,7 @@ func BenchmarkLargeHostMatcher(b *testing.B) {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
matcher.MatchWithError(req)
}
}
@ -1194,8 +1192,7 @@ func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
match := MatchHost{"localhost"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
match.MatchWithError(req)
}
}
@ -1212,8 +1209,7 @@ func BenchmarkHostMatcherWithPlaceholder(b *testing.B) {
req = req.WithContext(ctx)
match := MatchHost{"{env.GO_BENCHMARK_DOMAIN}"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
match.MatchWithError(req)
}
}

View File

@ -41,7 +41,7 @@ func parseLinkHeader(header string) []linkResource {
return resources
}
for _, link := range strings.Split(header, comma) {
for link := range strings.SplitSeq(header, comma) {
l := linkResource{params: make(map[string]string)}
li, ri := strings.Index(link, "<"), strings.Index(link, ">")
@ -51,7 +51,7 @@ func parseLinkHeader(header string) []linkResource {
l.uri = strings.TrimSpace(link[li+1 : ri])
for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolon) {
for param := range strings.SplitSeq(strings.TrimSpace(link[ri+1:]), semicolon) {
before, after, isCut := strings.Cut(strings.TrimSpace(param), equal)
key := strings.TrimSpace(before)
if key == "" {

View File

@ -1356,7 +1356,7 @@ func upgradeType(h http.Header) string {
// See RFC 7230, section 6.1
func removeConnectionHeaders(h http.Header) {
for _, f := range h["Connection"] {
for _, sf := range strings.Split(f, ",") {
for sf := range strings.SplitSeq(f, ",") {
if sf = textproto.TrimString(sf); sf != "" {
h.Del(sf)
}

View File

@ -985,10 +985,10 @@ func trustedRealClientIP(r *http.Request, headers []string, clientIP string) str
// Since there can be many header values, we need to
// join them together before splitting to get the full list
allValues := strings.Split(strings.Join(values, ","), ",")
allValues := strings.SplitSeq(strings.Join(values, ","), ",")
// Get first valid left-most IP address
for _, part := range allValues {
for part := range allValues {
// Some proxies may retain the port number, so split if possible
host, _, err := net.SplitHostPort(part)
if err != nil {

View File

@ -116,9 +116,7 @@ func BenchmarkServer_LogRequest(b *testing.B) {
buf := io.Discard
accLog := testLogger(buf.Write)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
}
}
@ -139,9 +137,7 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
accLog := zap.NewNop()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
}
}
@ -165,9 +161,7 @@ func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
buf := io.Discard
accLog := testLogger(buf.Write)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
s.logRequest(accLog, req, wrec, &duration, repl, bodyReader, false)
}
}

View File

@ -255,7 +255,7 @@ func (m IPMaskFilter) Filter(in zapcore.Field) zapcore.Field {
func (m IPMaskFilter) mask(s string) string {
output := ""
for _, value := range strings.Split(s, ",") {
for value := range strings.SplitSeq(s, ",") {
value = strings.TrimSpace(value)
host, port, err := net.SplitHostPort(value)
if err != nil {

View File

@ -516,7 +516,7 @@ func BenchmarkReplacer(b *testing.B) {
},
} {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for b.Loop() {
rep.ReplaceAll(bm.input, bm.empty)
}
})