diff options
| author | Rick Hudson <rlh@golang.org> | 2016-04-27 18:19:16 -0400 |
|---|---|---|
| committer | Rick Hudson <rlh@golang.org> | 2016-04-27 18:46:52 -0400 |
| commit | 23aeb34df172b17b7bfaa85fb59ca64bef9073bb (patch) | |
| tree | a8ab866f1e50f0059856ce628f036d93ab620155 /src/strings/strings.go | |
| parent | 1354b32cd70f2702381764fd595dd2faa996840c (diff) | |
| parent | d3c79d324acd7300b6f705e66af8ca711af00d9f (diff) | |
| download | go-23aeb34df172b17b7bfaa85fb59ca64bef9073bb.tar.xz | |
[dev.garbage] Merge remote-tracking branch 'origin/master' into HEAD
Change-Id: I282fd9ce9db435dfd35e882a9502ab1abc185297
Diffstat (limited to 'src/strings/strings.go')
| -rw-r--r-- | src/strings/strings.go | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go index c24c77b9dd..919e8c8354 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -12,32 +12,25 @@ import ( "unicode/utf8" ) -// 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. +// explode splits s into a slice of UTF-8 strings, +// one string per Unicode character up to a maximum of n (n < 0 means no limit). +// Invalid UTF-8 sequences become correct encodings of U+FFFD. func explode(s string, n int) []string { - if n == 0 { - return nil - } l := utf8.RuneCountInString(s) - if n <= 0 || n > l { + if n < 0 || n > l { n = l } a := make([]string, n) - var size int - var ch rune - i, cur := 0, 0 - for ; i+1 < n; i++ { - ch, size = utf8.DecodeRuneInString(s[cur:]) + for i := 0; i < n-1; i++ { + ch, size := utf8.DecodeRuneInString(s) + a[i] = s[:size] + s = s[size:] if ch == utf8.RuneError { a[i] = string(utf8.RuneError) - } else { - a[i] = s[cur : cur+size] } - cur += size } - // add the rest, if there is any - if cur < len(s) { - a[i] = s[cur:] + if n > 0 { + a[n-1] = s } return a } |
