diff options
Diffstat (limited to 'src/strings')
| -rw-r--r-- | src/strings/strings.go | 28 | ||||
| -rw-r--r-- | src/strings/strings_test.go | 15 |
2 files changed, 42 insertions, 1 deletions
diff --git a/src/strings/strings.go b/src/strings/strings.go index 73bba9278c..8520f8a732 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -571,7 +571,33 @@ func ToUpper(s string) string { } // ToLower returns a copy of the string s with all Unicode letters mapped to their lower case. -func ToLower(s string) string { return Map(unicode.ToLower, s) } +func ToLower(s string) string { + isASCII, hasUpper := true, false + for i := 0; i < len(s); i++ { + c := s[i] + if c >= utf8.RuneSelf { + isASCII = false + break + } + hasUpper = hasUpper || (c >= 'A' && c <= 'Z') + } + + if isASCII { // optimize for ASCII-only strings. + if !hasUpper { + return s + } + b := make([]byte, len(s)) + for i := 0; i < len(s); i++ { + c := s[i] + if c >= 'A' && c <= 'Z' { + c += 'a' - 'A' + } + b[i] = c + } + return string(b) + } + return Map(unicode.ToLower, s) +} // ToTitle returns a copy of the string s with all Unicode letters mapped to their title case. func ToTitle(s string) string { return Map(unicode.ToTitle, s) } diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index b185e7eec8..289dd92d51 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -532,6 +532,8 @@ var lowerTests = []StringTest{ {"abc", "abc"}, {"AbC123", "abc123"}, {"azAZ09_", "azaz09_"}, + {"longStrinGwitHmixofsmaLLandcAps", "longstringwithmixofsmallandcaps"}, + {"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", "long\u0250string\u0250with\u0250nonascii\u0250chars"}, {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char } @@ -664,6 +666,19 @@ func BenchmarkToUpper(b *testing.B) { } } +func BenchmarkToLower(b *testing.B) { + for _, tc := range lowerTests { + b.Run(tc.in, func(b *testing.B) { + for i := 0; i < b.N; i++ { + actual := ToLower(tc.in) + if actual != tc.out { + b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out) + } + } + }) + } +} + func BenchmarkMapNoChanges(b *testing.B) { identity := func(r rune) rune { return r |
