aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Donizetti <alb.donizetti@gmail.com>2017-06-06 11:25:17 +0200
committerAlberto Donizetti <alb.donizetti@gmail.com>2017-06-06 15:48:54 +0000
commit3a27f28e9b62fc4e7229dbf9b08fbb8f3d65157b (patch)
tree875946ae1412d2e3f155b0f709371fc522647a4a
parent2d86f4942868c1309051062237cf4d424d588e9c (diff)
downloadgo-3a27f28e9b62fc4e7229dbf9b08fbb8f3d65157b.tar.xz
strings: document Split{,N,After,AfterN} edge cases
Apparently people get confused by the fact that Split("", ",") returns []{""} instead of []{}. This is actually just a consequence of the fact that if the separator sep (2nd argument) is not found the string s (1st argument), then the Split* functions return a length 1 slice with the string s in it. Document the general case: if sep is not in s, what you get is a len 1 slice with s in it; unless both s and sep are "", in that case you get an empty slice of length 0. Fixes #19726 Change-Id: I64c8220b91acd1e5aa1cc1829199e0cd8c47c404 Reviewed-on: https://go-review.googlesource.com/44950 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
-rw-r--r--src/strings/strings.go26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go
index 1226e1f50f..0c836c09d4 100644
--- a/src/strings/strings.go
+++ b/src/strings/strings.go
@@ -257,33 +257,51 @@ func genSplit(s, sep string, sepSave, n int) []string {
// SplitN slices s into substrings separated by sep and returns a slice of
// the substrings between those separators.
-// If sep is empty, SplitN splits after each UTF-8 sequence.
+//
// The count determines the number of substrings to return:
// n > 0: at most n substrings; the last substring will be the unsplit remainder.
// n == 0: the result is nil (zero substrings)
// n < 0: all substrings
+//
+// Edge cases for s and sep (for example, empty strings) are handled
+// as described in the documentation for Split.
func SplitN(s, sep string, n int) []string { return genSplit(s, sep, 0, n) }
// SplitAfterN slices s into substrings after each instance of sep and
// returns a slice of those substrings.
-// If sep is empty, SplitAfterN splits after each UTF-8 sequence.
+//
// The count determines the number of substrings to return:
// n > 0: at most n substrings; the last substring will be the unsplit remainder.
// n == 0: the result is nil (zero substrings)
// n < 0: all substrings
+//
+// Edge cases for s and sep (for example, empty strings) are handled
+// as described in the documentation for SplitAfter.
func SplitAfterN(s, sep string, n int) []string {
return genSplit(s, sep, len(sep), n)
}
// Split slices s into all substrings separated by sep and returns a slice of
// the substrings between those separators.
-// If sep is empty, Split splits after each UTF-8 sequence.
+//
+// If s does not contain sep and sep is not empty, Split returns a
+// slice of length 1 whose only element is s.
+//
+// If sep is empty, Split splits after each UTF-8 sequence. If both s
+// and sep are empty, Split returns an empty slice.
+//
// It is equivalent to SplitN with a count of -1.
func Split(s, sep string) []string { return genSplit(s, sep, 0, -1) }
// SplitAfter slices s into all substrings after each instance of sep and
// returns a slice of those substrings.
-// If sep is empty, SplitAfter splits after each UTF-8 sequence.
+//
+// If s does not contain sep and sep is not empty, SplitAfter returns
+// a slice of length 1 whose only element is s.
+//
+// If sep is empty, SplitAfter splits after each UTF-8 sequence. If
+// both s and sep are empty, SplitAfter returns an empty slice.
+//
// It is equivalent to SplitAfterN with a count of -1.
func SplitAfter(s, sep string) []string {
return genSplit(s, sep, len(sep), -1)