From 0cd877725597a9e7c35afdcdb1dbcd1c2c4264b8 Mon Sep 17 00:00:00 2001 From: Scott Lawrence Date: Tue, 3 Aug 2010 13:35:14 +1000 Subject: [PATCH] strings: fix Split("", "", -1) 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 | 6 ++++-- 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 12be04c239..c332f4567d 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 9e8feceaa2..3206f5e143 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"}}, -- 2.48.1