diff options
author | Scott Lawrence <bytbox@gmail.com> | 2010-08-03 13:35:14 +1000 |
---|---|---|
committer | Scott Lawrence <bytbox@gmail.com> | 2010-08-03 13:35:14 +1000 |
commit | e8668c5c68fe80f4d48cea2c747245d5993eef5c (patch) | |
tree | 655399f570648097b8ec8a8da7e6ec5a5a4dea08 /src/pkg/strings | |
parent | 1f3e074c26e40b3314d42bc632ed57ac3ece2afe (diff) | |
download | go-e8668c5c68fe80f4d48cea2c747245d5993eef5c.tar.gz |
strings: fix Split("", "", -1)
Fixes issue 980.
Made it return an empty array, rather than crash.
Added relevant test cases to strings.
R=golang-dev, r
CC=golang-dev
http://codereview.appspot.com/1914041
Committer: Rob Pike <r@golang.org>
Diffstat (limited to 'src/pkg/strings')
-rw-r--r-- | src/pkg/strings/strings.go | 6 | ||||
-rw-r--r-- | src/pkg/strings/strings_test.go | 1 |
2 files changed, 5 insertions, 2 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 12be04c23..c332f4567 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -28,8 +28,10 @@ func explode(s string, n int) []string { a[i] = string(rune) cur += size } - // add the rest - a[i] = s[cur:] + // add the rest, if there is any + if cur < len(s) { + a[i] = s[cur:] + } return a } diff --git a/src/pkg/strings/strings_test.go b/src/pkg/strings/strings_test.go index 9e8feceaa..3206f5e14 100644 --- a/src/pkg/strings/strings_test.go +++ b/src/pkg/strings/strings_test.go @@ -109,6 +109,7 @@ type ExplodeTest struct { } var explodetests = []ExplodeTest{ + ExplodeTest{"", -1, []string{}}, ExplodeTest{abcd, 4, []string{"a", "b", "c", "d"}}, ExplodeTest{faces, 3, []string{"☺", "☻", "☹"}}, ExplodeTest{abcd, 2, []string{"a", "bcd"}}, |