strings: add Title

strings.ToTitle converts all characters to title case, which for consistency with the
other To* functions it should continue to do.  This CL adds string.Title, which
does a proper title-casing of the string.
A similar function for package bytes will follow once this is settled.
Fixes #933.

R=rsc
CC=golang-dev
https://golang.org/cl/1869042
This commit is contained in:
Rob Pike 2010-07-20 00:03:59 -07:00
parent f6b93ab432
commit 8684a08989
2 changed files with 68 additions and 0 deletions

View file

@ -741,3 +741,25 @@ func TestReplace(t *testing.T) {
}
}
}
type TitleTest struct {
in, out string
}
var TitleTests = []TitleTest{
TitleTest{"", ""},
TitleTest{"a", "A"},
TitleTest{" aaa aaa aaa ", " Aaa Aaa Aaa "},
TitleTest{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
TitleTest{"123a456", "123a456"},
TitleTest{"double-blind", "Double-Blind"},
TitleTest{"ÿøû", "Ÿøû"},
}
func TestTitle(t *testing.T) {
for _, tt := range TitleTests {
if s := Title(tt.in); s != tt.out {
t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
}
}
}