diff options
author | Rob Pike <r@golang.org> | 2010-07-01 14:08:14 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2010-07-01 14:08:14 -0700 |
commit | b3fb215bd4973f0a8b52d49bcb90f31ca64ae781 (patch) | |
tree | 5b188afe344ef0e4cb39832efc2d76613bc2e5e2 /src/pkg/bytes/bytes.go | |
parent | 32de5c228f33e6bcfef081d51da8263747a6c448 (diff) | |
download | golang-b3fb215bd4973f0a8b52d49bcb90f31ca64ae781.tar.gz |
strings and bytes.Split: make count of 0 mean 0, not infinite.
Use a count of -1 for infinity. Ditto for Replace.
R=rsc
CC=golang-dev
http://codereview.appspot.com/1704044
Committer: Rob Pike <r@golang.org>
Diffstat (limited to 'src/pkg/bytes/bytes.go')
-rw-r--r-- | src/pkg/bytes/bytes.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 64292ef64..bcf7b8609 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -154,10 +154,13 @@ func IndexAny(s []byte, chars string) int { // Generic split: splits after each instance of sep, // including sepSave bytes of sep in the subarrays. func genSplit(s, sep []byte, sepSave, n int) [][]byte { + if n == 0 { + return nil + } if len(sep) == 0 { return explode(s, n) } - if n <= 0 { + if n < 0 { n = Count(s, sep) + 1 } c := sep[0] @@ -178,13 +181,15 @@ func genSplit(s, sep []byte, sepSave, n int) [][]byte { // Split splits the array s around each instance of sep, returning an array of subarrays of s. // If sep is empty, Split splits s after each UTF-8 sequence. -// If n > 0, Split splits s into at most n subarrays; the last subarray will contain an unsplit remainder. +// If n >= 0, Split splits s into at most n subarrays; the last subarray will contain an unsplit remainder. +// Thus if n == 0, the result will ne nil. func Split(s, sep []byte, n int) [][]byte { return genSplit(s, sep, 0, n) } // SplitAfter splits the array s after each instance of sep, returning an array of subarrays of s. // If sep is empty, SplitAfter splits s after each UTF-8 sequence. -// If n > 0, SplitAfter splits s into at most n subarrays; the last subarray will contain an +// If n >= 0, SplitAfter splits s into at most n subarrays; the last subarray will contain an // unsplit remainder. +// Thus if n == 0, the result will ne nil. func SplitAfter(s, sep []byte, n int) [][]byte { return genSplit(s, sep, len(sep), n) } @@ -465,8 +470,11 @@ func Runes(s []byte) []int { // Replace returns a copy of the slice s with the first n // non-overlapping instances of old replaced by new. -// If n <= 0, there is no limit on the number of replacements. +// If n < 0, there is no limit on the number of replacements. func Replace(s, old, new []byte, n int) []byte { + if n == 0 { + return s // avoid allocation + } // Compute number of replacements. if m := Count(s, old); m == 0 { return s // avoid allocation |