2
0
mirror of synced 2025-02-23 14:58:12 +00:00

bind: correctly lowercasing all caps names

Fixes golang/go#13031

Change-Id: I6cae8e303d0d929e26c44cbdf1c243e98dabb766
Reviewed-on: https://go-review.googlesource.com/16270
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah (Hana) Kim 2015-10-23 12:01:49 -04:00 committed by Hyang-Ah Hana Kim
parent b7734b1851
commit 8a59fd3f45
2 changed files with 34 additions and 2 deletions

View File

@ -212,3 +212,23 @@ func TestCustomPrefix(t *testing.T) {
}
}
}
func TestLowerFirst(t *testing.T) {
testCases := []struct {
in, want string
}{
{"", ""},
{"Hello", "hello"},
{"HelloGopher", "helloGopher"},
{"hello", "hello"},
{"ID", "id"},
{"IDOrName", "idOrName"},
{"ΓειαΣας", "γειαΣας"},
}
for _, tc := range testCases {
if got := lowerFirst(tc.in); got != tc.want {
t.Errorf("lowerFirst(%q) = %q; want %q", tc.in, got, tc.want)
}
}
}

View File

@ -1006,6 +1006,18 @@ func lowerFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
var conv []rune
for len(s) > 0 {
r, n := utf8.DecodeRuneInString(s)
if !unicode.IsUpper(r) {
if l := len(conv); l > 1 {
conv[l-1] = unicode.ToUpper(conv[l-1])
}
return string(conv) + s
}
conv = append(conv, unicode.ToLower(r))
s = s[n:]
}
return string(conv)
}