Skip to content

Commit

Permalink
Use strings.Cut in a few places (#971)
Browse files Browse the repository at this point in the history
Now that we're 1.20+ instead of 1.14+ we can use 1.18's strings.Cut in a
few places to simplify code.
  • Loading branch information
JRaspass authored Feb 4, 2025
1 parent 71307f9 commit 877e876
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 21 deletions.
7 changes: 2 additions & 5 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,13 @@ type compressResponseWriter struct {
func (cw *compressResponseWriter) isCompressible() bool {
// Parse the first part of the Content-Type response header.
contentType := cw.Header().Get("Content-Type")
if idx := strings.Index(contentType, ";"); idx >= 0 {
contentType = contentType[0:idx]
}
contentType, _, _ = strings.Cut(contentType, ";")

// Is the content type compressible?
if _, ok := cw.contentTypes[contentType]; ok {
return true
}
if idx := strings.Index(contentType, "/"); idx > 0 {
contentType = contentType[0:idx]
if contentType, _, hadSlash := strings.Cut(contentType, "/"); hadSlash {
_, ok := cw.contentWildcards[contentType]
return ok
}
Expand Down
6 changes: 1 addition & 5 deletions middleware/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func realIP(r *http.Request) string {
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
ip = xrip
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
i := strings.Index(xff, ",")
if i == -1 {
i = len(xff)
}
ip = xff[:i]
ip, _, _ = strings.Cut(xff, ",")
}
if ip == "" || net.ParseIP(ip) == nil {
return ""
Expand Down
8 changes: 1 addition & 7 deletions middleware/route_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,7 @@ type Pattern struct {

func NewPattern(value string) Pattern {
p := Pattern{}
if i := strings.IndexByte(value, '*'); i >= 0 {
p.wildcard = true
p.prefix = value[0:i]
p.suffix = value[i+1:]
} else {
p.prefix = value
}
p.prefix, p.suffix, p.wildcard = strings.Cut(value, "*")
return p
}

Expand Down
6 changes: 2 additions & 4 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,9 @@ func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
tail = pattern[pe]
}

var rexpat string
if idx := strings.Index(key, ":"); idx >= 0 {
key, rexpat, isRegexp := strings.Cut(key, ":")
if isRegexp {
nt = ntRegexp
rexpat = key[idx+1:]
key = key[:idx]
}

if len(rexpat) > 0 {
Expand Down

0 comments on commit 877e876

Please sign in to comment.