mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Added strings.FieldsFunc, a generalization of strings.Fields in style of the strings.Trim*Func functions.
R=golang-dev, r CC=golang-dev https://golang.org/cl/824051
This commit is contained in:
parent
47c4416b7e
commit
ad2c5a4984
2 changed files with 25 additions and 2 deletions
|
|
@ -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
|
// 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.
|
// characters, returning an array of substrings of s or an empty list if s contains only white space.
|
||||||
func Fields(s string) []string {
|
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.
|
// First count the fields.
|
||||||
n := 0
|
n := 0
|
||||||
inField := false
|
inField := false
|
||||||
for _, rune := range s {
|
for _, rune := range s {
|
||||||
wasInField := inField
|
wasInField := inField
|
||||||
inField = !unicode.IsSpace(rune)
|
inField = !f(rune)
|
||||||
if inField && !wasInField {
|
if inField && !wasInField {
|
||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
|
|
@ -188,7 +195,7 @@ func Fields(s string) []string {
|
||||||
na := 0
|
na := 0
|
||||||
fieldStart := -1 // Set to -1 when looking for start of field.
|
fieldStart := -1 // Set to -1 when looking for start of field.
|
||||||
for i, rune := range s {
|
for i, rune := range s {
|
||||||
if unicode.IsSpace(rune) {
|
if f(rune) {
|
||||||
if fieldStart >= 0 {
|
if fieldStart >= 0 {
|
||||||
a[na] = s[fieldStart:i]
|
a[na] = s[fieldStart:i]
|
||||||
na++
|
na++
|
||||||
|
|
|
||||||
|
|
@ -222,6 +222,22 @@ func TestFields(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFieldsFunc(t *testing.T) {
|
||||||
|
pred := func(c int) bool { return c == 'X' }
|
||||||
|
var fieldsFuncTests = []FieldsTest{
|
||||||
|
FieldsTest{"", []string{}},
|
||||||
|
FieldsTest{"XX", []string{}},
|
||||||
|
FieldsTest{"XXhiXXX", []string{"hi"}},
|
||||||
|
FieldsTest{"aXXbXXXcX", []string{"a", "b", "c"}},
|
||||||
|
}
|
||||||
|
for _, tt := range fieldsFuncTests {
|
||||||
|
a := FieldsFunc(tt.s, pred)
|
||||||
|
if !eq(a, tt.a) {
|
||||||
|
t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Test case for any function which accepts and returns a single string.
|
// Test case for any function which accepts and returns a single string.
|
||||||
type StringTest struct {
|
type StringTest struct {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue