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/")
slash := strings.Index(upath, "/")
if slash == -1 {
stableIDStr, filenameEscaped, ok := strings.Cut(upath, "/")
if !ok {
http.Error(w, "bogus URL", 400)
return
}
stableID, filenameEscaped := tailcfg.StableNodeID(upath[:slash]), upath[slash+1:]
stableID := tailcfg.StableNodeID(stableIDStr)
var ft *apitype.FileTarget
for _, x := range fts {

@ -976,17 +976,17 @@ const (
// lb._dns-sd._udp.<domain>.
func hasRDNSBonjourPrefix(name dnsname.FQDN) bool {
s := name.WithTrailingDot()
dot := strings.IndexByte(s, '.')
if dot == -1 {
base, rest, ok := strings.Cut(s, ".")
if !ok {
return false // shouldn't happen
}
switch s[:dot] {
switch base {
case "b", "db", "r", "dr", "lb":
default:
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.

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

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

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

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

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

Loading…
Cancel
Save