summaryrefslogtreecommitdiff
path: root/src/strings/strings_test.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/strings/strings_test.go
parent0df7ad2e79ac5ca5197509596446dd83380aaf90 (diff)
downloadgo-git-68005592b38027490a08972f13269406b2556a07.tar.gz
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/strings/strings_test.go')
-rw-r--r--src/strings/strings_test.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go
index a1604c2c47..d6153aa226 100644
--- a/src/strings/strings_test.go
+++ b/src/strings/strings_test.go
@@ -1611,6 +1611,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(tt.s, tt.sep); 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(tt.s, tt.sep); 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 makeBenchInputHard() string {
tokens := [...]string{
"<a>", "<p>", "<b>", "<strong>",