all: use Go 1.18's strings.Cut

More remain.

Change-Id: I6ec562cc1f687600758deae1c9d7dbd0d04004cb
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/4188/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent 1f22507c06
commit 61ee72940c

@ -448,12 +448,12 @@ func (h *Handler) serveFilePut(w http.ResponseWriter, r *http.Request) {
} }
upath := strings.TrimPrefix(r.URL.EscapedPath(), "/localapi/v0/file-put/") upath := strings.TrimPrefix(r.URL.EscapedPath(), "/localapi/v0/file-put/")
slash := strings.Index(upath, "/") stableIDStr, filenameEscaped, ok := strings.Cut(upath, "/")
if slash == -1 { if !ok {
http.Error(w, "bogus URL", 400) http.Error(w, "bogus URL", 400)
return return
} }
stableID, filenameEscaped := tailcfg.StableNodeID(upath[:slash]), upath[slash+1:] stableID := tailcfg.StableNodeID(stableIDStr)
var ft *apitype.FileTarget var ft *apitype.FileTarget
for _, x := range fts { for _, x := range fts {

@ -976,17 +976,17 @@ const (
// lb._dns-sd._udp.<domain>. // lb._dns-sd._udp.<domain>.
func hasRDNSBonjourPrefix(name dnsname.FQDN) bool { func hasRDNSBonjourPrefix(name dnsname.FQDN) bool {
s := name.WithTrailingDot() s := name.WithTrailingDot()
dot := strings.IndexByte(s, '.') base, rest, ok := strings.Cut(s, ".")
if dot == -1 { if !ok {
return false // shouldn't happen return false // shouldn't happen
} }
switch s[:dot] { switch base {
case "b", "db", "r", "dr", "lb": case "b", "db", "r", "dr", "lb":
default: default:
return false return false
} }
return strings.HasPrefix(s[dot:], "._dns-sd._udp.") return strings.HasPrefix(rest, "_dns-sd._udp.")
} }
// rawNameToLower converts a raw DNS name to a string, lowercasing it. // rawNameToLower converts a raw DNS name to a string, lowercasing it.

@ -145,19 +145,10 @@ func AcceptsEncoding(r *http.Request, enc string) bool {
} }
remain := h remain := h
for len(remain) > 0 { for len(remain) > 0 {
comma := strings.Index(remain, ",")
var part string var part string
if comma == -1 { part, remain, _ = strings.Cut(remain, ",")
part = remain
remain = ""
} else {
part = remain[:comma]
remain = remain[comma+1:]
}
part = strings.TrimSpace(part) part = strings.TrimSpace(part)
if i := strings.Index(part, ";"); i != -1 { part, _, _ = strings.Cut(part, ";")
part = part[:i]
}
if part == enc { if part == enc {
return true return true
} }

@ -370,8 +370,8 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
} }
if strings.HasPrefix(key, "labelmap_") { if strings.HasPrefix(key, "labelmap_") {
key = strings.TrimPrefix(key, "labelmap_") key = strings.TrimPrefix(key, "labelmap_")
if i := strings.Index(key, "_"); i != -1 { if a, b, ok := strings.Cut(key, "_"); ok {
label, key = key[:i], key[i+1:] label, key = a, b
} }
} }
name := prefix + key name := prefix + key
@ -541,9 +541,7 @@ func foreachExportedStructField(rv reflect.Value, f func(fieldOrJSONName, metric
sf := t.Field(i) sf := t.Field(i)
name := sf.Name name := sf.Name
if v := sf.Tag.Get("json"); v != "" { if v := sf.Tag.Get("json"); v != "" {
if i := strings.Index(v, ","); i != -1 { v, _, _ = strings.Cut(v, ",")
v = v[:i]
}
if v == "-" { if v == "-" {
// Skip it, regardless of its metrictype. // Skip it, regardless of its metrictype.
continue continue

@ -78,8 +78,8 @@ type NetworkMap struct {
// It will neither start nor end with a period. // It will neither start nor end with a period.
func (nm *NetworkMap) MagicDNSSuffix() string { func (nm *NetworkMap) MagicDNSSuffix() string {
name := strings.Trim(nm.Name, ".") name := strings.Trim(nm.Name, ".")
if i := strings.Index(name, "."); i != -1 { if _, rest, ok := strings.Cut(name, "."); ok {
name = name[i+1:] return rest
} }
return name return name
} }

@ -185,10 +185,8 @@ func NumLabels(hostname string) int {
// FirstLabel returns the first DNS label of hostname. // FirstLabel returns the first DNS label of hostname.
func FirstLabel(hostname string) string { func FirstLabel(hostname string) string {
if i := strings.IndexByte(hostname, '.'); i != -1 { first, _, _ := strings.Cut(hostname, ".")
return hostname[:i] return first
}
return hostname
} }
var separators = map[byte]bool{ var separators = map[byte]bool{

@ -114,8 +114,7 @@ func parseIPSet(arg string, bits *int) ([]netaddr.IPPrefix, error) {
return []netaddr.IPPrefix{pfx}, nil return []netaddr.IPPrefix{pfx}, nil
} }
if strings.Count(arg, "-") == 1 { if strings.Count(arg, "-") == 1 {
i := strings.Index(arg, "-") ip1s, ip2s, _ := strings.Cut(arg, "-")
ip1s, ip2s := arg[:i], arg[i+1:]
ip1, err := netaddr.ParseIP(ip1s) ip1, err := netaddr.ParseIP(ip1s)
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save