diff options
| author | Russ Cox <rsc@golang.org> | 2021-09-22 10:46:32 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2021-10-06 15:53:04 +0000 |
| commit | 4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee (patch) | |
| tree | 1e850efb295d4c5f0589e46bd8d9f1930d4af0b5 /src/os | |
| parent | 8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff) | |
| download | go-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.tar.xz | |
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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/os')
| -rw-r--r-- | src/os/exec/exec.go | 10 | ||||
| -rw-r--r-- | src/os/exec/exec_test.go | 8 | ||||
| -rw-r--r-- | src/os/os_test.go | 4 | ||||
| -rw-r--r-- | src/os/user/cgo_lookup_unix.go | 4 | ||||
| -rw-r--r-- | src/os/user/lookup_unix.go | 4 |
5 files changed, 11 insertions, 19 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 0c49575511..9551c22d6e 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -748,12 +748,11 @@ func dedupEnvCase(caseInsensitive bool, env []string) []string { out := make([]string, 0, len(env)) saw := make(map[string]int, len(env)) // key => index into out for _, kv := range env { - eq := strings.Index(kv, "=") - if eq < 0 { + k, _, ok := strings.Cut(kv, "=") + if !ok { out = append(out, kv) continue } - k := kv[:eq] if caseInsensitive { k = strings.ToLower(k) } @@ -775,11 +774,10 @@ func addCriticalEnv(env []string) []string { return env } for _, kv := range env { - eq := strings.Index(kv, "=") - if eq < 0 { + k, _, ok := strings.Cut(kv, "=") + if !ok { continue } - k := kv[:eq] if strings.EqualFold(k, "SYSTEMROOT") { // We already have it. return env diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index d854e0de84..459ba39dff 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -166,12 +166,10 @@ func TestCatGoodAndBadFile(t *testing.T) { if _, ok := err.(*exec.ExitError); !ok { t.Errorf("expected *exec.ExitError from cat combined; got %T: %v", err, err) } - s := string(bs) - sp := strings.SplitN(s, "\n", 2) - if len(sp) != 2 { - t.Fatalf("expected two lines from cat; got %q", s) + errLine, body, ok := strings.Cut(string(bs), "\n") + if !ok { + t.Fatalf("expected two lines from cat; got %q", bs) } - errLine, body := sp[0], sp[1] if !strings.HasPrefix(errLine, "Error: open /bogus/file.foo") { t.Errorf("expected stderr to complain about file; got %q", errLine) } diff --git a/src/os/os_test.go b/src/os/os_test.go index 506f1fb0ee..62173d9bf4 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -1761,8 +1761,8 @@ func TestHostname(t *testing.T) { // and the /bin/hostname only returns the first component want := runBinHostname(t) if hostname != want { - i := strings.Index(hostname, ".") - if i < 0 || hostname[0:i] != want { + host, _, ok := strings.Cut(hostname, ".") + if !ok || host != want { t.Errorf("Hostname() = %q, want %q", hostname, want) } } diff --git a/src/os/user/cgo_lookup_unix.go b/src/os/user/cgo_lookup_unix.go index abc9e9ce6d..5c972e77cf 100644 --- a/src/os/user/cgo_lookup_unix.go +++ b/src/os/user/cgo_lookup_unix.go @@ -125,9 +125,7 @@ func buildUser(pwd *C.struct_passwd) *User { // say: "It is expected to be a comma separated list of // personal data where the first item is the full name of the // user." - if i := strings.Index(u.Name, ","); i >= 0 { - u.Name = u.Name[:i] - } + u.Name, _, _ = strings.Cut(u.Name, ",") return u } diff --git a/src/os/user/lookup_unix.go b/src/os/user/lookup_unix.go index ac4f1502af..1cabdb5b71 100644 --- a/src/os/user/lookup_unix.go +++ b/src/os/user/lookup_unix.go @@ -174,9 +174,7 @@ func matchUserIndexValue(value string, idx int) lineFunc { // say: "It is expected to be a comma separated list of // personal data where the first item is the full name of the // user." - if i := strings.Index(u.Name, ","); i >= 0 { - u.Name = u.Name[:i] - } + u.Name, _, _ = strings.Cut(u.Name, ",") return u, nil } } |
