Skip to content

Commit e235052

Browse files
authored
Make use of strings.Cut (#1005)
1 parent 1be7ad9 commit e235052

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

middleware/content_charset.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ func contentEncoding(ce string, charsets ...string) bool {
4040

4141
// Split a string in two parts, cleaning any whitespace.
4242
func split(str, sep string) (string, string) {
43-
var a, b string
44-
var parts = strings.SplitN(str, sep, 2)
45-
a = strings.TrimSpace(parts[0])
46-
if len(parts) == 2 {
47-
b = strings.TrimSpace(parts[1])
43+
a, b, found := strings.Cut(str, sep)
44+
a = strings.TrimSpace(a)
45+
if found {
46+
b = strings.TrimSpace(b)
4847
}
4948

5049
return a, b

middleware/content_type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func AllowContentType(contentTypes ...string) func(http.Handler) http.Handler {
3131
return
3232
}
3333

34-
s := strings.ToLower(strings.TrimSpace(strings.Split(r.Header.Get("Content-Type"), ";")[0]))
34+
s, _, _ := strings.Cut(r.Header.Get("Content-Type"), ";")
35+
s = strings.ToLower(strings.TrimSpace(s))
3536

3637
if _, ok := allowedContentTypes[s]; ok {
3738
next.ServeHTTP(w, r)
@@ -42,4 +43,3 @@ func AllowContentType(contentTypes ...string) func(http.Handler) http.Handler {
4243
})
4344
}
4445
}
45-

mux.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler) {
107107
// Handle adds the route `pattern` that matches any http method to
108108
// execute the `handler` http.Handler.
109109
func (mx *Mux) Handle(pattern string, handler http.Handler) {
110-
parts := strings.SplitN(pattern, " ", 2)
111-
if len(parts) == 2 {
112-
mx.Method(parts[0], parts[1], handler)
110+
if method, rest, found := strings.Cut(pattern, " "); found {
111+
mx.Method(method, rest, handler)
113112
return
114113
}
115114

@@ -119,9 +118,8 @@ func (mx *Mux) Handle(pattern string, handler http.Handler) {
119118
// HandleFunc adds the route `pattern` that matches any http method to
120119
// execute the `handlerFn` http.HandlerFunc.
121120
func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
122-
parts := strings.SplitN(pattern, " ", 2)
123-
if len(parts) == 2 {
124-
mx.Method(parts[0], parts[1], handlerFn)
121+
if method, rest, found := strings.Cut(pattern, " "); found {
122+
mx.Method(method, rest, handlerFn)
125123
return
126124
}
127125

0 commit comments

Comments
 (0)