code review 7239044: strings: add TrimPrefix and TrimSuffix (issue 7239044)

204 views
Skip to first unread message

brad...@golang.org

unread,
Jan 30, 2013, 2:25:32 PM1/30/13
to golan...@googlegroups.com, re...@codereview-hr.appspotmail.com
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golan...@googlegroups.com,

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
strings: add TrimPrefix and TrimSuffix

Everybody either gets confused and thinks this is
TrimLeft/TrimRight or does this by hand which gets
repetitive looking.

Please review this at https://codereview.appspot.com/7239044/

Affected files:
M src/pkg/strings/strings.go
M src/pkg/strings/strings_test.go


Index: src/pkg/strings/strings.go
===================================================================
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -558,6 +558,24 @@
return TrimFunc(s, unicode.IsSpace)
}

+// TrimPrefix returns s without the provided leading prefix string.
+// If s doesn't start with prefix, s is returned unchanged.
+func TrimPrefix(s, prefix string) string {
+ if HasPrefix(s, prefix) {
+ return s[len(prefix):]
+ }
+ return s
+}
+
+// TrimSuffix returns s without the provided trailing suffix string.
+// If s doesn't end with suffix, s is returned unchanged.
+func TrimSuffix(s, suffix string) string {
+ if HasSuffix(s, suffix) {
+ return s[:len(s)-len(suffix)]
+ }
+ return s
+}
+
// Replace returns a copy of the string s with the first n
// non-overlapping instances of old replaced by new.
// If n < 0, there is no limit on the number of replacements.
Index: src/pkg/strings/strings_test.go
===================================================================
--- a/src/pkg/strings/strings_test.go
+++ b/src/pkg/strings/strings_test.go
@@ -496,8 +496,8 @@
func TestTrimSpace(t *testing.T) { runStringTests(t,
TrimSpace, "TrimSpace", trimSpaceTests) }

var trimTests = []struct {
- f string
- in, cutset, out string
+ f string
+ in, arg, out string
}{
{"Trim", "abba", "a", "bb"},
{"Trim", "abba", "ab", ""},
@@ -520,6 +520,10 @@
{"TrimRight", "", "123", ""},
{"TrimRight", "", "", ""},
{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
+ {"TrimPrefix", "aabb", "a", "abb"},
+ {"TrimPrefix", "aabb", "b", "aabb"},
+ {"TrimSuffix", "aabb", "a", "aabb"},
+ {"TrimSuffix", "aabb", "b", "aab"},
}

func TestTrim(t *testing.T) {
@@ -533,12 +537,16 @@
f = TrimLeft
case "TrimRight":
f = TrimRight
+ case "TrimPrefix":
+ f = TrimPrefix
+ case "TrimSuffix":
+ f = TrimSuffix
default:
t.Errorf("Undefined trim function %s", name)
}
- actual := f(tc.in, tc.cutset)
+ actual := f(tc.in, tc.arg)
if actual != tc.out {
- t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual,
tc.out)
+ t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual,
tc.out)
}
}
}


Brad Fitzpatrick

unread,
Jan 30, 2013, 2:26:20 PM1/30/13
to golan...@googlegroups.com
I can add the bytes versions too if not rejected.

I'd like this to go in, though.

Russ Cox

unread,
Jan 30, 2013, 2:29:46 PM1/30/13
to Brad Fitzpatrick, golang-dev
LGTM

Wait a day or two for others but it sounds good to me. I write that code too much.

brad...@golang.org

unread,
Jan 30, 2013, 2:50:07 PM1/30/13
to golan...@googlegroups.com, golan...@googlegroups.com, re...@codereview-hr.appspotmail.com

Andrew Gerrand

unread,
Jan 30, 2013, 9:42:53 PM1/30/13
to Russ Cox, Brad Fitzpatrick, golang-dev
LGTM too.

roger peppe

unread,
Jan 31, 2013, 12:26:34 PM1/31/13
to Russ Cox, Brad Fitzpatrick, golang-dev
LGTM
me too. and the names are right.

Russ Cox

unread,
Feb 1, 2013, 11:21:55 AM2/1/13
to roger peppe, Brad Fitzpatrick, golang-dev
LGTM

Go ahead and submit this.

Reply all
Reply to author
Forward
0 new messages