aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-09-22 10:46:32 -0400
committerRuss Cox <rsc@golang.org>2021-10-06 15:53:04 +0000
commit4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee (patch)
tree1e850efb295d4c5f0589e46bd8d9f1930d4af0b5 /src/runtime
parent8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff)
downloadgo-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/runtime')
-rw-r--r--src/runtime/debug/mod.go5
-rw-r--r--src/runtime/pprof/pprof_test.go17
-rw-r--r--src/runtime/pprof/proto.go33
-rw-r--r--src/runtime/pprof/proto_test.go5
-rw-r--r--src/runtime/runtime-gdb_test.go2
-rw-r--r--src/runtime/testdata/testprog/numcpu_freebsd.go11
-rw-r--r--src/runtime/testdata/testprog/traceback_ancestors.go38
7 files changed, 47 insertions, 64 deletions
diff --git a/src/runtime/debug/mod.go b/src/runtime/debug/mod.go
index 0381bdcc53..05cad61155 100644
--- a/src/runtime/debug/mod.go
+++ b/src/runtime/debug/mod.go
@@ -70,11 +70,10 @@ func readBuildInfo(data string) (*BuildInfo, bool) {
)
// Reverse of cmd/go/internal/modload.PackageBuildInfo
for len(data) > 0 {
- i := strings.IndexByte(data, '\n')
- if i < 0 {
+ line, data, ok = strings.Cut(data, "\n")
+ if !ok {
break
}
- line, data = data[:i], data[i+1:]
switch {
case strings.HasPrefix(line, pathLine):
elem := line[len(pathLine):]
diff --git a/src/runtime/pprof/pprof_test.go b/src/runtime/pprof/pprof_test.go
index f39d65c0b5..03ff55b541 100644
--- a/src/runtime/pprof/pprof_test.go
+++ b/src/runtime/pprof/pprof_test.go
@@ -1234,11 +1234,10 @@ func TestGoroutineCounts(t *testing.T) {
func containsInOrder(s string, all ...string) bool {
for _, t := range all {
- i := strings.Index(s, t)
- if i < 0 {
+ var ok bool
+ if _, s, ok = strings.Cut(s, t); !ok {
return false
}
- s = s[i+len(t):]
}
return true
}
@@ -1318,18 +1317,18 @@ func TestEmptyCallStack(t *testing.T) {
// stackContainsLabeled takes a spec like funcname;key=value and matches if the stack has that key
// and value and has funcname somewhere in the stack.
func stackContainsLabeled(spec string, count uintptr, stk []*profile.Location, labels map[string][]string) bool {
- semi := strings.Index(spec, ";")
- if semi == -1 {
+ base, kv, ok := strings.Cut(spec, ";")
+ if !ok {
panic("no semicolon in key/value spec")
}
- kv := strings.SplitN(spec[semi+1:], "=", 2)
- if len(kv) != 2 {
+ k, v, ok := strings.Cut(kv, "=")
+ if !ok {
panic("missing = in key/value spec")
}
- if !contains(labels[kv[0]], kv[1]) {
+ if !contains(labels[k], v) {
return false
}
- return stackContains(spec[:semi], count, stk, labels)
+ return stackContains(base, count, stk, labels)
}
func TestCPUProfileLabel(t *testing.T) {
diff --git a/src/runtime/pprof/proto.go b/src/runtime/pprof/proto.go
index 6862513956..54e7a80183 100644
--- a/src/runtime/pprof/proto.go
+++ b/src/runtime/pprof/proto.go
@@ -13,6 +13,7 @@ import (
"os"
"runtime"
"strconv"
+ "strings"
"time"
"unsafe"
)
@@ -581,6 +582,9 @@ func (b *profileBuilder) readMapping() {
}
}
+var space = []byte(" ")
+var newline = []byte("\n")
+
func parseProcSelfMaps(data []byte, addMapping func(lo, hi, offset uint64, file, buildID string)) {
// $ cat /proc/self/maps
// 00400000-0040b000 r-xp 00000000 fc:01 787766 /bin/cat
@@ -607,37 +611,24 @@ func parseProcSelfMaps(data []byte, addMapping func(lo, hi, offset uint64, file,
// next removes and returns the next field in the line.
// It also removes from line any spaces following the field.
next := func() []byte {
- j := bytes.IndexByte(line, ' ')
- if j < 0 {
- f := line
- line = nil
- return f
- }
- f := line[:j]
- line = line[j+1:]
- for len(line) > 0 && line[0] == ' ' {
- line = line[1:]
- }
+ var f []byte
+ f, line, _ = bytes.Cut(line, space)
+ line = bytes.TrimLeft(line, " ")
return f
}
for len(data) > 0 {
- i := bytes.IndexByte(data, '\n')
- if i < 0 {
- line, data = data, nil
- } else {
- line, data = data[:i], data[i+1:]
- }
+ line, data, _ = bytes.Cut(data, newline)
addr := next()
- i = bytes.IndexByte(addr, '-')
- if i < 0 {
+ loStr, hiStr, ok := strings.Cut(string(addr), "-")
+ if !ok {
continue
}
- lo, err := strconv.ParseUint(string(addr[:i]), 16, 64)
+ lo, err := strconv.ParseUint(loStr, 16, 64)
if err != nil {
continue
}
- hi, err := strconv.ParseUint(string(addr[i+1:]), 16, 64)
+ hi, err := strconv.ParseUint(hiStr, 16, 64)
if err != nil {
continue
}
diff --git a/src/runtime/pprof/proto_test.go b/src/runtime/pprof/proto_test.go
index d052b9fa42..4a9749a83f 100644
--- a/src/runtime/pprof/proto_test.go
+++ b/src/runtime/pprof/proto_test.go
@@ -274,11 +274,10 @@ func TestProcSelfMaps(t *testing.T) {
f := func(t *testing.T, input string) {
for tx, tt := range strings.Split(input, "\n\n") {
- i := strings.Index(tt, "->\n")
- if i < 0 {
+ in, out, ok := strings.Cut(tt, "->\n")
+ if !ok {
t.Fatal("malformed test case")
}
- in, out := tt[:i], tt[i+len("->\n"):]
if len(out) > 0 && out[len(out)-1] != '\n' {
out += "\n"
}
diff --git a/src/runtime/runtime-gdb_test.go b/src/runtime/runtime-gdb_test.go
index 8c76a9123c..4a0f489c2f 100644
--- a/src/runtime/runtime-gdb_test.go
+++ b/src/runtime/runtime-gdb_test.go
@@ -267,7 +267,7 @@ func testGdbPython(t *testing.T, cgo bool) {
t.Fatalf("gdb exited with error: %v", err)
}
- firstLine := bytes.SplitN(got, []byte("\n"), 2)[0]
+ firstLine, _, _ := bytes.Cut(got, []byte("\n"))
if string(firstLine) != "Loading Go Runtime support." {
// This can happen when using all.bash with
// GOROOT_FINAL set, because the tests are run before
diff --git a/src/runtime/testdata/testprog/numcpu_freebsd.go b/src/runtime/testdata/testprog/numcpu_freebsd.go
index aff36ec702..7209f67959 100644
--- a/src/runtime/testdata/testprog/numcpu_freebsd.go
+++ b/src/runtime/testdata/testprog/numcpu_freebsd.go
@@ -85,19 +85,18 @@ func getList() ([]string, error) {
if err != nil {
return nil, fmt.Errorf("fail to execute '%s': %s", cmdline, err)
}
- pos := bytes.IndexRune(output, '\n')
- if pos == -1 {
+ output, _, ok := bytes.Cut(output, []byte("\n"))
+ if !ok {
return nil, fmt.Errorf("invalid output from '%s', '\\n' not found: %s", cmdline, output)
}
- output = output[0:pos]
- pos = bytes.IndexRune(output, ':')
- if pos == -1 {
+ _, cpus, ok := bytes.Cut(output, []byte(":"))
+ if !ok {
return nil, fmt.Errorf("invalid output from '%s', ':' not found: %s", cmdline, output)
}
var list []string
- for _, val := range bytes.Split(output[pos+1:], []byte(",")) {
+ for _, val := range bytes.Split(cpus, []byte(",")) {
index := string(bytes.TrimSpace(val))
if len(index) == 0 {
continue
diff --git a/src/runtime/testdata/testprog/traceback_ancestors.go b/src/runtime/testdata/testprog/traceback_ancestors.go
index 0ee402c4bd..1d0d00bab7 100644
--- a/src/runtime/testdata/testprog/traceback_ancestors.go
+++ b/src/runtime/testdata/testprog/traceback_ancestors.go
@@ -33,30 +33,27 @@ func printStack() {
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
- tb := string(buf[:n])
+ all := string(buf[:n])
+ var saved string
// Delete any ignored goroutines, if present.
- pos := 0
- for pos < len(tb) {
- next := pos + strings.Index(tb[pos:], "\n\n")
- if next < pos {
- next = len(tb)
- } else {
- next += len("\n\n")
- }
+ for all != "" {
+ var g string
+ g, all, _ = strings.Cut(all, "\n\n")
- if strings.HasPrefix(tb[pos:], "goroutine ") {
- id := tb[pos+len("goroutine "):]
- id = id[:strings.IndexByte(id, ' ')]
+ if strings.HasPrefix(g, "goroutine ") {
+ id, _, _ := strings.Cut(strings.TrimPrefix(g, "goroutine "), " ")
if ignoreGoroutines[id] {
- tb = tb[:pos] + tb[next:]
- next = pos
+ continue
}
}
- pos = next
+ if saved != "" {
+ saved += "\n\n"
+ }
+ saved += g
}
- fmt.Print(tb)
+ fmt.Print(saved)
return
}
buf = make([]byte, 2*len(buf))
@@ -89,11 +86,10 @@ func recurseThenCallGo(w chan struct{}, frames int, goroutines int, main bool) {
func goroutineID() string {
buf := make([]byte, 128)
runtime.Stack(buf, false)
- const prefix = "goroutine "
- if !bytes.HasPrefix(buf, []byte(prefix)) {
+ prefix := []byte("goroutine ")
+ if !bytes.HasPrefix(buf, prefix) {
panic(fmt.Sprintf("expected %q at beginning of traceback:\n%s", prefix, buf))
}
- buf = buf[len(prefix):]
- n := bytes.IndexByte(buf, ' ')
- return string(buf[:n])
+ id, _, _ := bytes.Cut(bytes.TrimPrefix(buf, prefix), []byte(" "))
+ return string(id)
}