diff options
author | Changkun Ou <hi@changkun.de> | 2022-05-19 11:57:50 +0200 |
---|---|---|
committer | Robert Griesemer <gri@google.com> | 2022-08-16 19:48:39 +0000 |
commit | 68005592b38027490a08972f13269406b2556a07 (patch) | |
tree | 3ae9d87901c6a3e2ec762beca84df26bcb48e5e3 /src/bytes | |
parent | 0df7ad2e79ac5ca5197509596446dd83380aaf90 (diff) | |
download | go-68005592b38027490a08972f13269406b2556a07.tar.gz go-68005592b38027490a08972f13269406b2556a07.zip |
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')
-rw-r--r-- | src/bytes/bytes.go | 26 | ||||
-rw-r--r-- | src/bytes/bytes_test.go | 42 |
2 files changed, 68 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 +} diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 392657d1fa..05c7ccc50a 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -1700,6 +1700,48 @@ func TestCut(t *testing.T) { } } +var cutPrefixTests = []struct { + s, sep string + after string + found bool +}{ + {"abc", "a", "bc", true}, + {"abc", "abc", "", true}, + {"abc", "", "abc", true}, + {"abc", "d", "abc", false}, + {"", "d", "", false}, + {"", "", "", true}, +} + +func TestCutPrefix(t *testing.T) { + for _, tt := range cutPrefixTests { + if after, found := CutPrefix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found { + t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found) + } + } +} + +var cutSuffixTests = []struct { + s, sep string + after string + found bool +}{ + {"abc", "bc", "a", true}, + {"abc", "abc", "", true}, + {"abc", "", "abc", true}, + {"abc", "d", "abc", false}, + {"", "d", "", false}, + {"", "", "", true}, +} + +func TestCutSuffix(t *testing.T) { + for _, tt := range cutSuffixTests { + if after, found := CutSuffix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found { + t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found) + } + } +} + func TestBufferGrowNegative(t *testing.T) { defer func() { if err := recover(); err == nil { |