aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/bytes.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-09-21 10:59:16 -0400
committerRuss Cox <rsc@golang.org>2021-10-06 15:53:00 +0000
commit8e36ab055162efa6f67f3b9ee62f625ac8874901 (patch)
treecf4109df9a5bdea03cc5a2fb25da85d2afd7ee73 /src/bytes/bytes.go
parent810b08b8ec28ea00bce4c008f7c1b48bc9f3e134 (diff)
downloadgo-8e36ab055162efa6f67f3b9ee62f625ac8874901.tar.xz
bytes, strings: add Cut
Using Cut is a clearer way to write the vast majority (>70%) of existing code that calls Index, IndexByte, IndexRune, and SplitN. There is more discussion on https://golang.org/issue/46336. Fixes #46336. Change-Id: Ia418ed7c3706c65bf61e1b2c5baf534cb783e4d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/351710 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/bytes/bytes.go')
-rw-r--r--src/bytes/bytes.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index cd859d086d..a9f10031c4 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -1192,3 +1192,16 @@ func Index(s, sep []byte) int {
}
return -1
}
+
+// Cut slices s around the first instance of sep,
+// returning the text before and after sep.
+// The found result reports whether sep appears in s.
+// If sep does not appear in s, cut returns s, "", false.
+//
+// Cut returns slices of the original slice s, not copies.
+func Cut(s, sep []byte) (before, after []byte, found bool) {
+ if i := Index(s, sep); i >= 0 {
+ return s[:i], s[i+len(sep):], true
+ }
+ return s, nil, false
+}