diff --git a/src/archive/tar/strconv.go b/src/archive/tar/strconv.go
index 217efe9..b0ac619 100644
--- a/src/archive/tar/strconv.go
+++ b/src/archive/tar/strconv.go
@@ -53,8 +53,8 @@
// parseString parses bytes as a NUL-terminated C-style string.
// If a NUL byte is not found then the whole slice is returned as a string.
func (*parser) parseString(b []byte) string {
- if i := bytes.IndexByte(b, 0); i >= 0 {
- return string(b[:i])
+ if before, _, ok := bytes.Cut(b, []byte{0}); ok {
+ return string(before)
}
return string(b)
}
diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go
index 0b26b8e..5c29f6b 100644
--- a/src/encoding/json/decode_test.go
+++ b/src/encoding/json/decode_test.go
@@ -127,11 +127,11 @@
}
func (u *unmarshalerText) UnmarshalText(b []byte) error {
- pos := bytes.IndexByte(b, ':')
- if pos == -1 {
+ before, after, ok := bytes.Cut(b, []byte{':'})
+ if !ok {
return errors.New("missing separator")
}
- u.A, u.B = string(b[:pos]), string(b[pos+1:])
+ u.A, u.B = string(before), string(after)
return nil
}
diff --git a/src/go/doc/comment/parse.go b/src/go/doc/comment/parse.go
index 4d5784a..5e03012 100644
--- a/src/go/doc/comment/parse.go
+++ b/src/go/doc/comment/parse.go
@@ -679,8 +679,8 @@
text := line[1:i]
url := strings.TrimSpace(line[i+3:])
- j := strings.Index(url, "://")
- if j < 0 || !isScheme(url[:j]) {
+ before, _, ok := strings.Cut(url, "://")
+ if !ok || !isScheme(before) {
return nil, false
}
diff --git a/src/go/doc/example.go b/src/go/doc/example.go
index 8c01bf0..9ba5460 100644
--- a/src/go/doc/example.go
+++ b/src/go/doc/example.go
@@ -689,8 +689,8 @@
//
// Adapted from debug/gosym/symtab.go:Sym.nameWithoutInst.
func nameWithoutInst(name string) string {
- start := strings.Index(name, "[")
- if start < 0 {
+ before, _, ok := strings.Cut(name, "[")
+ if !ok {
return name
}
end := strings.LastIndex(name, "]")
@@ -698,7 +698,7 @@
// Malformed name, should contain closing bracket too.
return name
}
- return name[0:start] + name[end+1:]
+ return before + name[end+1:]
}
// splitExampleName attempts to split example name s at index i,
diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go
index a7e041e..ee31012 100644
--- a/src/go/types/api_test.go
+++ b/src/go/types/api_test.go
@@ -62,8 +62,8 @@
// pkgName extracts the package name from src, which must contain a package header.
func pkgName(src string) string {
const kw = "package "
- if i := strings.Index(src, kw); i >= 0 {
- after := src[i+len(kw):]
+ if _, after, ok := strings.Cut(src, kw); ok {
+ after := after
n := len(after)
if i := strings.IndexAny(after, "\n\t ;/"); i >= 0 {
n = i
diff --git a/src/go/types/check_test.go b/src/go/types/check_test.go
index 44e4fd0..e83cfdc 100644
--- a/src/go/types/check_test.go
+++ b/src/go/types/check_test.go
@@ -109,7 +109,7 @@
return nil // first line is not a line comment
}
src = src[len(prefix):]
- if i := bytes.Index(src, []byte("-")); i < 0 || len(bytes.TrimSpace(src[:i])) != 0 {
+ if before, _, ok := bytes.Cut(src, []byte("-")); !ok || len(bytes.TrimSpace(before)) != 0 {
return nil // comment doesn't start with a "-"
}
end := bytes.Index(src, []byte("\n"))
diff --git a/src/html/template/css.go b/src/html/template/css.go
index f650d8b..79789c7 100644
--- a/src/html/template/css.go
+++ b/src/html/template/css.go
@@ -56,8 +56,8 @@
// backed by a new array.
// https://www.w3.org/TR/css3-syntax/#SUBTOK-stringchar defines stringchar.
func decodeCSS(s []byte) []byte {
- i := bytes.IndexByte(s, '\\')
- if i == -1 {
+ found := bytes.Contains(s, []byte{'\\'})
+ if !found {
return s
}
// The UTF-8 sequence for a codepoint is never longer than 1 + the
diff --git a/src/math/big/ratconv.go b/src/math/big/ratconv.go
index 229f31b..1d7f2d5 100644
--- a/src/math/big/ratconv.go
+++ b/src/math/big/ratconv.go
@@ -62,11 +62,11 @@
// len(s) > 0
// parse fraction a/b, if any
- if sep := strings.Index(s, "/"); sep >= 0 {
- if _, ok := z.a.SetString(s[:sep], 0); !ok {
+ if before, after, ok := strings.Cut(s, "/"); ok {
+ if _, ok := z.a.SetString(before, 0); !ok {
return nil, false
}
- r := strings.NewReader(s[sep+1:])
+ r := strings.NewReader(after)
var err error
if z.b.abs, _, _, err = z.b.abs.scan(r, 0, false); err != nil {
return nil, false
diff --git a/src/testing/fstest/mapfs.go b/src/testing/fstest/mapfs.go
index 5ce0398..637a024 100644
--- a/src/testing/fstest/mapfs.go
+++ b/src/testing/fstest/mapfs.go
@@ -68,13 +68,13 @@
var need = make(map[string]bool)
if realName == "." {
for fname, f := range fsys {
- i := strings.Index(fname, "/")
- if i < 0 {
+ before, _, ok0 := strings.Cut(fname, "/")
+ if !ok0 {
if fname != "." {
list = append(list, mapFileInfo{fname, f})
}
} else {
- need[fname[:i]] = true
+ need[before] = true
}
}
} else {
diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go
index 72830a0..5efb027 100644
--- a/src/testing/fstest/testfs.go
+++ b/src/testing/fstest/testfs.go
@@ -615,12 +615,12 @@
if file == "." {
bad = append(bad, "/")
}
- if i := strings.Index(file, "/"); i >= 0 {
+ if before, after, ok := strings.Cut(file, "/"); ok {
bad = append(bad,
- file[:i]+"//"+file[i+1:],
- file[:i]+"/./"+file[i+1:],
- file[:i]+`\`+file[i+1:],
- file[:i]+"/../"+file,
+ before+"//"+after,
+ before+"/./"+after,
+ before+`\`+after,
+ before+"/../"+file,
)
}
if i := strings.LastIndex(file, "/"); i >= 0 {