diff options
| author | David Symonds <dsymonds@golang.org> | 2009-06-24 19:02:29 -0700 |
|---|---|---|
| committer | David Symonds <dsymonds@golang.org> | 2009-06-24 19:02:29 -0700 |
| commit | e3fe6186634f9b74b9f39924516e24b068b9e141 (patch) | |
| tree | c423ba821d41ea2d673a7420ecf7c082aa827863 /src/pkg/strings/strings.go | |
| parent | c9af30256dfa40f26f2a60e88b68dadfbabdd2ba (diff) | |
| download | golang-e3fe6186634f9b74b9f39924516e24b068b9e141.tar.gz | |
Change strings.Split, bytes.Split to take a maximum substring count argument.
R=rsc
APPROVED=r
DELTA=131 (39 added, 10 deleted, 82 changed)
OCL=30669
CL=30723
Diffstat (limited to 'src/pkg/strings/strings.go')
| -rw-r--r-- | src/pkg/strings/strings.go | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 035090777..9b0f031b9 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -7,19 +7,27 @@ package strings import "utf8" -// Explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings). +// explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings) up to a maximum of n (n <= 0 means no limit). // Invalid UTF-8 sequences become correct encodings of U+FFF8. -func Explode(s string) []string { - a := make([]string, utf8.RuneCountInString(s)); +func explode(s string, n int) []string { + if n <= 0 { + n = len(s); + } + a := make([]string, n); var size, rune int; - i := 0; + na := 0; for len(s) > 0 { + if na+1 >= n { + a[na] = s; + na++; + break + } rune, size = utf8.DecodeRuneInString(s); s = s[size:len(s)]; - a[i] = string(rune); - i++; + a[na] = string(rune); + na++; } - return a + return a[0:na] } // Count counts the number of non-overlapping instances of sep in s. @@ -68,27 +76,30 @@ func LastIndex(s, sep string) int { return -1 } -// Split returns the array representing the substrings of s separated by string sep. Adjacent -// occurrences of sep produce empty substrings. If sep is empty, it is the same as Explode. -func Split(s, sep string) []string { +// Split splits the string s around each instance of sep, returning an array of substrings of s. +// If sep is empty, Split splits s after each UTF-8 sequence. +// If n > 0, split Splits s into at most n substrings; the last subarray will contain an unsplit remainder string. +func Split(s, sep string, n int) []string { if sep == "" { - return Explode(s) + return explode(s, n) + } + if n <= 0 { + n = Count(s, sep) + 1; } c := sep[0]; start := 0; - n := Count(s, sep)+1; a := make([]string, n); na := 0; - for i := 0; i+len(sep) <= len(s); i++ { + for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ { if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) { a[na] = s[start:i]; na++; start = i+len(sep); - i += len(sep)-1 + i += len(sep)-1; } } a[na] = s[start:len(s)]; - return a + return a[0:na+1] } // Join concatenates the elements of a to create a single string. The separator string |
