From 4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 22 Sep 2021 10:46:32 -0400 Subject: all: use bytes.Cut, strings.Cut Many uses of Index/IndexByte/IndexRune/Split/SplitN can be written more clearly using the new Cut functions. Do that. Also rewrite to other functions if that's clearer. For #46336. Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448 Reviewed-on: https://go-review.googlesource.com/c/go/+/351711 Trust: Russ Cox Run-TryBot: Russ Cox TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/html/template/attr.go | 6 +++--- src/html/template/js.go | 4 +--- src/html/template/url.go | 4 +--- 3 files changed, 5 insertions(+), 9 deletions(-) (limited to 'src/html') diff --git a/src/html/template/attr.go b/src/html/template/attr.go index 22922e6038..6c52211fed 100644 --- a/src/html/template/attr.go +++ b/src/html/template/attr.go @@ -143,12 +143,12 @@ func attrType(name string) contentType { // widely applied. // Treat data-action as URL below. name = name[5:] - } else if colon := strings.IndexRune(name, ':'); colon != -1 { - if name[:colon] == "xmlns" { + } else if prefix, short, ok := strings.Cut(name, ":"); ok { + if prefix == "xmlns" { return contentTypeURL } // Treat svg:href and xlink:href as href below. - name = name[colon+1:] + name = short } if t, ok := attrTypeMap[name]; ok { return t diff --git a/src/html/template/js.go b/src/html/template/js.go index ea9c18346b..32a4fbd30a 100644 --- a/src/html/template/js.go +++ b/src/html/template/js.go @@ -398,9 +398,7 @@ func isJSType(mimeType string) bool { // https://tools.ietf.org/html/rfc4329#section-3 // https://www.ietf.org/rfc/rfc4627.txt // discard parameters - if i := strings.Index(mimeType, ";"); i >= 0 { - mimeType = mimeType[:i] - } + mimeType, _, _ = strings.Cut(mimeType, ";") mimeType = strings.ToLower(mimeType) mimeType = strings.TrimSpace(mimeType) switch mimeType { diff --git a/src/html/template/url.go b/src/html/template/url.go index 6f8185a4e9..4b39fddf07 100644 --- a/src/html/template/url.go +++ b/src/html/template/url.go @@ -46,9 +46,7 @@ func urlFilter(args ...interface{}) string { // isSafeURL is true if s is a relative URL or if URL has a protocol in // (http, https, mailto). func isSafeURL(s string) bool { - if i := strings.IndexRune(s, ':'); i >= 0 && !strings.ContainsRune(s[:i], '/') { - - protocol := s[:i] + if protocol, _, ok := strings.Cut(s, ":"); ok && !strings.Contains(protocol, "/") { if !strings.EqualFold(protocol, "http") && !strings.EqualFold(protocol, "https") && !strings.EqualFold(protocol, "mailto") { return false } -- cgit v1.2.1