all: use strings.Cut even more

Change-Id: I943ce72c6f339589235bddbe10d07799c4e37979
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/4232/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent f30473211b
commit f2041c9088

@ -116,10 +116,7 @@ func doLocalRequestNiceError(req *http.Request) (*http.Response, error) {
if ue, ok := err.(*url.Error); ok { if ue, ok := err.(*url.Error); ok {
if oe, ok := ue.Err.(*net.OpError); ok && oe.Op == "dial" { if oe, ok := ue.Err.(*net.OpError); ok && oe.Op == "dial" {
path := req.URL.Path path := req.URL.Path
pathPrefix := path pathPrefix, _, _ := strings.Cut(path, "?")
if i := strings.Index(path, "?"); i != -1 {
pathPrefix = path[:i]
}
return nil, fmt.Errorf("Failed to connect to local Tailscale daemon for %s; %s Error: %w", pathPrefix, tailscaledConnectHint(), oe) return nil, fmt.Errorf("Failed to connect to local Tailscale daemon for %s; %s Error: %w", pathPrefix, tailscaledConnectHint(), oe)
} }
} }
@ -520,8 +517,8 @@ func tailscaledConnectHint() string {
// SubState=dead // SubState=dead
st := map[string]string{} st := map[string]string{}
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {
if i := strings.Index(line, "="); i != -1 { if k, v, ok := strings.Cut(line, "="); ok {
st[line[:i]] = strings.TrimSpace(line[i+1:]) st[k] = strings.TrimSpace(v)
} }
} }
if st["LoadState"] == "loaded" && if st["LoadState"] == "loaded" &&

@ -206,8 +206,6 @@ func root(w http.ResponseWriter, r *http.Request) {
// firstLabel s up until the first period, if any. // firstLabel s up until the first period, if any.
func firstLabel(s string) string { func firstLabel(s string) string {
if i := strings.Index(s, "."); i != -1 { s, _, _ = strings.Cut(s, ".")
return s[:i]
}
return s return s
} }

@ -65,10 +65,7 @@ func Parse(r io.Reader) (*Config, error) {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
i := strings.IndexByte(line, '#') line, _, _ = strings.Cut(line, "#") // remove any comments
if i >= 0 {
line = line[:i]
}
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if strings.HasPrefix(line, "nameserver") { if strings.HasPrefix(line, "nameserver") {

@ -196,12 +196,11 @@ func maxDoHInFlight(goos string) int {
// Unknown iOS version, be cautious. // Unknown iOS version, be cautious.
return 10 return 10
} }
idx := strings.Index(ver, ".") major, _, ok := strings.Cut(ver, ".")
if idx == -1 { if !ok {
// Unknown iOS version, be cautious. // Unknown iOS version, be cautious.
return 10 return 10
} }
major := ver[:idx]
if m, err := strconv.Atoi(major); err != nil || m < 15 { if m, err := strconv.Atoi(major); err != nil || m < 15 {
return 10 return 10
} }

@ -37,13 +37,13 @@ func TestResolversWithDelays(t *testing.T) {
o := func(ss ...string) (rr []resolverAndDelay) { o := func(ss ...string) (rr []resolverAndDelay) {
for _, s := range ss { for _, s := range ss {
var d time.Duration var d time.Duration
if i := strings.Index(s, "+"); i != -1 { s, durStr, hasPlus := strings.Cut(s, "+")
if hasPlus {
var err error var err error
d, err = time.ParseDuration(s[i+1:]) d, err = time.ParseDuration(durStr)
if err != nil { if err != nil {
panic(fmt.Sprintf("parsing duration in %q: %v", s, err)) panic(fmt.Sprintf("parsing duration in %q: %v", s, err))
} }
s = s[:i]
} }
host, _, err := net.SplitHostPort(s) host, _, err := net.SplitHostPort(s)
if err != nil { if err != nil {

@ -676,7 +676,7 @@ func pfx(strs ...string) (ret []netaddr.IPPrefix) {
func nets(nets ...string) (ret []netaddr.IPPrefix) { func nets(nets ...string) (ret []netaddr.IPPrefix) {
for _, s := range nets { for _, s := range nets {
if i := strings.IndexByte(s, '/'); i == -1 { if !strings.Contains(s, "/") {
ip, err := netaddr.ParseIP(s) ip, err := netaddr.ParseIP(s)
if err != nil { if err != nil {
panic(err) panic(err)

@ -188,8 +188,8 @@ func peerDebugName(p *tailcfg.Node) string {
return "" return ""
} }
n := p.Name n := p.Name
if i := strings.Index(n, "."); i != -1 { if base, _, ok := strings.Cut(n, "."); ok {
return n[:i] return base
} }
return p.Hostinfo.Hostname() return p.Hostinfo.Hostname()
} }

@ -905,9 +905,7 @@ func (c *Conn) Ping(peer *tailcfg.Node, res *ipnstate.PingResult, cb func(*ipnst
if res.NodeName == "" { if res.NodeName == "" {
res.NodeName = peer.Hostinfo.Hostname() // else hostname res.NodeName = peer.Hostinfo.Hostname() // else hostname
} else { } else {
if i := strings.Index(res.NodeName, "."); i != -1 { res.NodeName, _, _ = strings.Cut(res.NodeName, ".")
res.NodeName = res.NodeName[:i]
}
} }
ep, ok := c.peerMap.endpointForNodeKey(peer.Key) ep, ok := c.peerMap.endpointForNodeKey(peer.Key)

Loading…
Cancel
Save