bytes, strings: add new function Fields

R=rsc, r, phf
CC=golang-dev
https://golang.org/cl/170046
This commit is contained in:
Andrey Mirtchovski 2009-12-15 21:09:55 -08:00 committed by Russ Cox
parent d5bcf7bf41
commit 7f501c06f7
4 changed files with 132 additions and 0 deletions

View file

@ -254,6 +254,36 @@ func TestSplitAfter(t *testing.T) {
}
}
type FieldsTest struct {
s string
a []string
}
var fieldstests = []FieldsTest{
FieldsTest{"", []string{}},
FieldsTest{" ", []string{}},
FieldsTest{" \t ", []string{}},
FieldsTest{" abc ", []string{"abc"}},
FieldsTest{"1 2 3 4", []string{"1", "2", "3", "4"}},
FieldsTest{"1 2 3 4", []string{"1", "2", "3", "4"}},
FieldsTest{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
FieldsTest{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
FieldsTest{"\u2000\u2001\u2002", []string{}},
FieldsTest{"\n™\t™\n", []string{"™", "™"}},
FieldsTest{faces, []string{faces}},
}
func TestFields(t *testing.T) {
for _, tt := range fieldstests {
a := Fields(strings.Bytes(tt.s))
result := arrayOfString(a)
if !eq(result, tt.a) {
t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
continue
}
}
}
// Test case for any function which accepts and returns a byte array.
// For ease of creation, we write the byte arrays as strings.
type StringTest struct {