aboutsummaryrefslogtreecommitdiff
path: root/src/bytes/bytes.go
diff options
context:
space:
mode:
authorChangkun Ou <hi@changkun.de>2022-05-19 11:57:50 +0200
committerRobert Griesemer <gri@google.com>2022-08-16 19:48:39 +0000
commit68005592b38027490a08972f13269406b2556a07 (patch)
tree3ae9d87901c6a3e2ec762beca84df26bcb48e5e3 /src/bytes/bytes.go
parent0df7ad2e79ac5ca5197509596446dd83380aaf90 (diff)
downloadgo-68005592b38027490a08972f13269406b2556a07.tar.xz
strings, bytes: add CutPrefix and CutSuffix
Fixes #42537 Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45 Reviewed-on: https://go-review.googlesource.com/c/go/+/407176 Reviewed-by: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Changkun Ou <mail@changkun.de> Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/bytes/bytes.go')
-rw-r--r--src/bytes/bytes.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 27834fc6db..c0cd704180 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -1309,3 +1309,29 @@ func Clone(b []byte) []byte {
}
return append([]byte{}, b...)
}
+
+// CutPrefix returns s without the provided leading prefix byte slice
+// and reports whether it found the prefix.
+// If s doesn't start with prefix, CutPrefix returns s, false.
+// If prefix is the empty byte slice, CutPrefix returns s, true.
+//
+// CutPrefix returns slices of the original slice s, not copies.
+func CutPrefix(s, prefix []byte) (after []byte, found bool) {
+ if !HasPrefix(s, prefix) {
+ return s, false
+ }
+ return s[len(prefix):], true
+}
+
+// CutSuffix returns s without the provided ending suffix byte slice
+// and reports whether it found the suffix.
+// If s doesn't end with suffix, CutSuffix returns s, false.
+// If suffix is the empty byte slice, CutSuffix returns s, true.
+//
+// CutSuffix returns slices of the original slice s, not copies.
+func CutSuffix(s, suffix []byte) (after []byte, found bool) {
+ if !HasSuffix(s, suffix) {
+ return s, false
+ }
+ return s[:len(s)-len(suffix)], true
+}