1
0
mirror of https://github.com/golang/go synced 2024-11-17 00:54:49 -07:00

strings: clarify usage of Title and ToTitle

This is intended to help clear up confusion around the usage of the
Title and ToTitle functions. It includes a link to define title case
to distinguish it from upper case. It also includes an additional
example for the ToTitle function to showcase the difference in behavior
between it and the Title function.

Fixes #33302

Change-Id: I44e62962fb04d0d22966a39eda3a2d16de7a2291
Reviewed-on: https://go-review.googlesource.com/c/go/+/187825
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Andrew Todd 2019-07-27 16:20:49 -07:00 committed by Rob Pike
parent 01d137262a
commit fbb819ebc4
2 changed files with 14 additions and 4 deletions

View File

@ -247,14 +247,23 @@ func ExampleSplitAfterN() {
} }
func ExampleTitle() { func ExampleTitle() {
// Compare this example to the ToTitle example.
fmt.Println(strings.Title("her royal highness")) fmt.Println(strings.Title("her royal highness"))
// Output: Her Royal Highness fmt.Println(strings.Title("loud noises"))
fmt.Println(strings.Title("хлеб"))
// Output:
// Her Royal Highness
// Loud Noises
// Хлеб
} }
func ExampleToTitle() { func ExampleToTitle() {
// Compare this example to the Title example.
fmt.Println(strings.ToTitle("her royal highness"))
fmt.Println(strings.ToTitle("loud noises")) fmt.Println(strings.ToTitle("loud noises"))
fmt.Println(strings.ToTitle("хлеб")) fmt.Println(strings.ToTitle("хлеб"))
// Output: // Output:
// HER ROYAL HIGHNESS
// LOUD NOISES // LOUD NOISES
// ХЛЕБ // ХЛЕБ
} }

View File

@ -610,7 +610,8 @@ func ToLower(s string) string {
return Map(unicode.ToLower, s) return Map(unicode.ToLower, s)
} }
// ToTitle returns a copy of the string s with all Unicode letters mapped to their title case. // ToTitle returns a copy of the string s with all Unicode letters mapped to
// their Unicode title case.
func ToTitle(s string) string { return Map(unicode.ToTitle, s) } func ToTitle(s string) string { return Map(unicode.ToTitle, s) }
// ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their // ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their
@ -626,7 +627,7 @@ func ToLowerSpecial(c unicode.SpecialCase, s string) string {
} }
// ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their // ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their
// title case, giving priority to the special casing rules. // Unicode title case, giving priority to the special casing rules.
func ToTitleSpecial(c unicode.SpecialCase, s string) string { func ToTitleSpecial(c unicode.SpecialCase, s string) string {
return Map(c.ToTitle, s) return Map(c.ToTitle, s)
} }
@ -707,7 +708,7 @@ func isSeparator(r rune) bool {
} }
// Title returns a copy of the string s with all Unicode letters that begin words // Title returns a copy of the string s with all Unicode letters that begin words
// mapped to their title case. // mapped to their Unicode title case.
// //
// BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly. // BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
func Title(s string) string { func Title(s string) string {