[go] all: replace strings.Index etc. with strings.Cut

3 views
Skip to first unread message

Kirill Kolyshkin (Gerrit)

unread,
Dec 17, 2025, 8:03:31 PM (2 days ago) Dec 17
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Kirill Kolyshkin has uploaded the change for review

Commit message

all: replace strings.Index etc. with strings.Cut

Replaces certain patterns of use of strings.Index and string slicing by
strings.Cut, added in Go 1.18.

Generated by

go fix -stringscut ./...
using

go version go1.26-devel_ad85395442 Wed Dec 17 15:56:48 2025 -0800 linux/amd64
Change-Id: Ibb02f7510b7576ead9a78b632cbc7ac1349419d3

Change diff

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 {

Change information

Files:
  • M src/archive/tar/strconv.go
  • M src/encoding/json/decode_test.go
  • M src/go/doc/comment/parse.go
  • M src/go/doc/example.go
  • M src/go/types/api_test.go
  • M src/go/types/check_test.go
  • M src/html/template/css.go
  • M src/math/big/ratconv.go
  • M src/testing/fstest/mapfs.go
  • M src/testing/fstest/testfs.go
Change size: M
Delta: 10 files changed, 26 insertions(+), 26 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ibb02f7510b7576ead9a78b632cbc7ac1349419d3
Gerrit-Change-Number: 730962
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 17, 2025, 8:08:37 PM (2 days ago) Dec 17
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ibb02f7510b7576ead9a78b632cbc7ac1349419d3
Gerrit-Change-Number: 730962
Gerrit-PatchSet: 1
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 01:08:34 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Kirill Kolyshkin (Gerrit)

unread,
Dec 18, 2025, 2:19:53 PM (20 hours ago) Dec 18
to goph...@pubsubhelper.golang.org, Go LUCI, Alan Donovan, Damien Neil, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Alan Donovan, Damien Neil and Robert Griesemer

Kirill Kolyshkin voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
  • Damien Neil
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ibb02f7510b7576ead9a78b632cbc7ac1349419d3
Gerrit-Change-Number: 730962
Gerrit-PatchSet: 2
Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Comment-Date: Thu, 18 Dec 2025 19:19:50 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Alan Donovan (Gerrit)

unread,
Dec 18, 2025, 4:41:14 PM (18 hours ago) Dec 18
to Kirill Kolyshkin, goph...@pubsubhelper.golang.org, Go LUCI, Damien Neil, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Damien Neil, Kirill Kolyshkin and Robert Griesemer

Alan Donovan voted and added 1 comment

Votes added by Alan Donovan

Hold+1

1 comment

Patchset-level comments
File-level comment, Patchset 2 (Latest):
Alan Donovan . resolved

Again, thanks for this CL and for the bug it discovered in rangeint, but I have to place it on hold until after the freeze (at which point it might be simplest to regenerate it).

Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Kirill Kolyshkin
  • Robert Griesemer
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ibb02f7510b7576ead9a78b632cbc7ac1349419d3
    Gerrit-Change-Number: 730962
    Gerrit-PatchSet: 2
    Gerrit-Owner: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Damien Neil <dn...@google.com>
    Gerrit-Reviewer: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Kirill Kolyshkin <koly...@gmail.com>
    Gerrit-Attention: Damien Neil <dn...@google.com>
    Gerrit-Attention: Robert Griesemer <g...@golang.org>
    Gerrit-Comment-Date: Thu, 18 Dec 2025 21:41:10 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages