]> Cypherpunks repositories - gostls13.git/commitdiff
strings: fix Split("", "", -1)
authorScott Lawrence <bytbox@gmail.com>
Tue, 3 Aug 2010 03:35:14 +0000 (13:35 +1000)
committerRob Pike <r@golang.org>
Tue, 3 Aug 2010 03:35:14 +0000 (13:35 +1000)
Fixes #980.

Made it return an empty array, rather than crash.
Added relevant test cases to strings.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/1914041

src/pkg/strings/strings.go
src/pkg/strings/strings_test.go

index 12be04c239299c31b9cde33ca50fa6d1c3c6a6a7..c332f4567daf2f22bc6ee8d4899941382b42e445 100644 (file)
@@ -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
 }
 
index 9e8feceaa2a750b6349ca98948e6b13ef7e45b48..3206f5e1433311bba3f249bc8b52456312785a66 100644 (file)
@@ -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"}},