FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go


Description:
regexp: add Split() method

As discussed in issue 2672 and on golang-nuts, this CL adds a Split()
method
to regexp. It is based on returning the "opposite" of FindAllString() so
that
the returned substrings are everything not matched by the expression.

See:
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/xodBZh9Lh2E

Fixes issue 2762.

Please review this at http://codereview.appspot.com/6846048/

Affected files:
M src/pkg/regexp/all_test.go
M src/pkg/regexp/regexp.go


Index: src/pkg/regexp/all_test.go
===================================================================
--- a/src/pkg/regexp/all_test.go
+++ b/src/pkg/regexp/all_test.go
@@ -416,6 +416,49 @@
}
}

+func TestSplit(t *testing.T) {
+ var tests = []struct {
+ s string
+ r string
+ n int
+ out []string
+ }{
+ {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
+ {"foo:and:bar", ":", 1, []string{"foo", "and:bar"}},
+ {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
+ {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
+ {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
+ {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
+ {"baabaab", "a*", -1, []string{"", "b", "b", "b", ""}},
+ {"baabaab", "ba*", -1, []string{"", "", "", ""}},
+ {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r", ""}},
+ {"foobar", "f+.*b+", -1, []string{"", "ar"}},
+ {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
+ {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c", "d,e,f"}},
+ {"a,b,c,d,e,f", ",", 0, []string{"a,b,c,d,e,f"}},
+ }
+
+ for i, test := range tests {
+ re, err := Compile(test.r)
+ if err != nil {
+ t.Errorf("Split: test %d: expression doesn't compile: %s", i, test.r)
+ continue
+ }
+
+ split := re.Split(test.s, test.n)
+ if len(split) != len(test.out) {
+ t.Errorf("Split: test %d: len(split) = %d; want = %d", i, len(split),
len(test.out))
+ continue
+ }
+
+ for j, str := range split {
+ if str != test.out[j] {
+ t.Errorf("Split: test %d: substring = %s; want = %s", i, str,
test.out[j])
+ }
+ }
+ }
+}
+
func BenchmarkLiteral(b *testing.B) {
x := strings.Repeat("x", 50) + "y"
b.StopTimer()
Index: src/pkg/regexp/regexp.go
===================================================================
--- a/src/pkg/regexp/regexp.go
+++ b/src/pkg/regexp/regexp.go
@@ -1048,3 +1048,26 @@
}
return result
}
+
+// Split slices s into all substrings separated by the expression and
returns
+// a slice of the substrings between the expression matches. The integer
argument n
+// indicates the maximum number of splits to perform when n >= 0.
+//
+// The slice returned by this method consists of all the substrings of s
+// not contained in the slice returned by FindAllString().
+//
+// The length of the returned slice is equal to the number of matches
found - 1.
+func (re *Regexp) Split(s string, n int) []string {
+ matches := re.FindAllStringIndex(s, n)
+ strings := make([]string, 0, len(matches)+1)
+
+ beg := 0
+ end := 0
+ for _, match := range matches {
+ end = match[0]
+ strings = append(strings, s[beg:end])
+ beg = match[1]
+ }
+ strings = append(strings, s[beg:])
+ return strings
+}

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedNov 13, '12 at 11:24p
activeNov 27, '12 at 5:58p
posts23
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase