diff options
author | Kyle Consalus <consalus@gmail.com> | 2010-04-19 16:36:50 -0700 |
---|---|---|
committer | Kyle Consalus <consalus@gmail.com> | 2010-04-19 16:36:50 -0700 |
commit | 033e5a85f33a9012df036756951549bd96887d4a (patch) | |
tree | 646fb9886445aa1a35b2aefdb46041cb781f6bd0 /src/pkg/strings/strings.go | |
parent | 5b8efb07ea9b77daa2cb81d361da2a2635f52c12 (diff) | |
download | golang-033e5a85f33a9012df036756951549bd96887d4a.tar.gz |
Added strings.FieldsFunc, a generalization of strings.Fields in style of the strings.Trim*Func functions.
R=golang-dev, r
CC=golang-dev
http://codereview.appspot.com/824051
Committer: Rob Pike <r@golang.org>
Diffstat (limited to 'src/pkg/strings/strings.go')
-rw-r--r-- | src/pkg/strings/strings.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 0a9f64ca0..90417f811 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -172,12 +172,19 @@ 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 { + return FieldsFunc(s, unicode.IsSpace) +} + +// FieldsFunc splits the string s at each run of Unicode code points c satifying f(c) +// and returns an array of slices of s. If no code points in s satisfy f(c), an empty slice +// is returned. +func FieldsFunc(s string, f func(int) bool) []string { // First count the fields. n := 0 inField := false for _, rune := range s { wasInField := inField - inField = !unicode.IsSpace(rune) + inField = !f(rune) if inField && !wasInField { n++ } @@ -188,7 +195,7 @@ func Fields(s string) []string { na := 0 fieldStart := -1 // Set to -1 when looking for start of field. for i, rune := range s { - if unicode.IsSpace(rune) { + if f(rune) { if fieldStart >= 0 { a[na] = s[fieldStart:i] na++ |