summaryrefslogtreecommitdiff
path: root/src/pkg/strings/strings.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2010-03-29 13:39:16 -0700
committerRob Pike <r@golang.org>2010-03-29 13:39:16 -0700
commit306a0d39e0ac64921a50ba5b93222743ba81de70 (patch)
tree3a9c7cc6b3208aa8a3f62fc98701cc1c31319ab3 /src/pkg/strings/strings.go
parent71d0f3ddab299518c9e9955f6ff806a18afa000d (diff)
downloadgolang-306a0d39e0ac64921a50ba5b93222743ba81de70.tar.gz
strings.FIelds: slight simplification.
R=rsc CC=golang-dev http://codereview.appspot.com/833042
Diffstat (limited to 'src/pkg/strings/strings.go')
-rw-r--r--src/pkg/strings/strings.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go
index 1ceaeefbd..24aac10e9 100644
--- a/src/pkg/strings/strings.go
+++ b/src/pkg/strings/strings.go
@@ -161,6 +161,7 @@ func SplitAfter(s, sep string, n int) []string {
// Fields splits the string s around each instance of one or more consecutive white space
// characters, returning an array of substrings of s or an empty list if s contains only white space.
func Fields(s string) []string {
+ // First count the fields.
n := 0
inField := false
for _, rune := range s {
@@ -171,9 +172,10 @@ func Fields(s string) []string {
}
}
+ // Now create them.
a := make([]string, n)
na := 0
- fieldStart := -1
+ fieldStart := -1 // Set to -1 when looking for start of field.
for i, rune := range s {
if unicode.IsSpace(rune) {
if fieldStart >= 0 {
@@ -185,11 +187,10 @@ func Fields(s string) []string {
fieldStart = i
}
}
- if fieldStart != -1 {
+ if fieldStart != -1 { // Last field might end at EOF.
a[na] = s[fieldStart:]
- na++
}
- return a[0:na]
+ return a
}
// Join concatenates the elements of a to create a single string. The separator string