summaryrefslogtreecommitdiff
path: root/src/pkg/strings
diff options
context:
space:
mode:
authorSpring Mc <heresy.mc@gmail.com>2010-03-04 16:15:51 -0800
committerSpring Mc <heresy.mc@gmail.com>2010-03-04 16:15:51 -0800
commitdffdaa1772ee2621b2fc8a3591009d6d68936c71 (patch)
tree131160a0d36e136d410bd086771edde1ed264f09 /src/pkg/strings
parent78b368c5c4a3c9d4277de9acc968e774a3b98099 (diff)
downloadgo-dffdaa1772ee2621b2fc8a3591009d6d68936c71.tar.gz
strings: make Split(s, "", n) faster
R=rsc CC=golang-dev http://codereview.appspot.com/223096 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/pkg/strings')
-rw-r--r--src/pkg/strings/strings.go41
1 files changed, 24 insertions, 17 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index 80e820795..a8f3150c3 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -13,24 +13,21 @@ import (
// 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, n int) []string {
- if n <= 0 {
- n = len(s)
+ l := utf8.RuneCountInString(s)
+ if n <= 0 || n > l {
+ n = l
}
a := make([]string, n)
var size, rune int
- na := 0
- for len(s) > 0 {
- if na+1 >= n {
- a[na] = s
- na++
- break
- }
- rune, size = utf8.DecodeRuneInString(s)
- s = s[size:]
- a[na] = string(rune)
- na++
+ i, cur := 0, 0
+ for ; i+1 < n; i++ {
+ rune, size = utf8.DecodeRuneInString(s[cur:])
+ a[i] = string(rune)
+ cur += size
}
- return a[0:na]
+ // add the rest
+ a[i] = s[cur:]
+ return a
}
// Count counts the number of non-overlapping instances of sep in s.
@@ -39,11 +36,21 @@ func Count(s, sep string) int {
return utf8.RuneCountInString(s) + 1
}
c := sep[0]
+ l := len(sep)
n := 0
- for i := 0; i+len(sep) <= len(s); i++ {
- if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
+ if l == 1 {
+ // special case worth making fast
+ for i := 0; i < len(s); i++ {
+ if s[i] == c {
+ n++
+ }
+ }
+ return n
+ }
+ for i := 0; i+l <= len(s); i++ {
+ if s[i] == c && s[i:i+l] == sep {
n++
- i += len(sep) - 1
+ i += l - 1
}
}
return n