UcFirst accounts for multi-byte leading characters

This commit is contained in:
Matt 2021-11-08 22:36:17 -05:00
parent 342f99b0fa
commit 12032df45d
3 changed files with 29 additions and 13 deletions

View File

@ -59,8 +59,8 @@ func replaceStr(input, search, replace, types string) string {
} }
func ucfirst(val string) string { func ucfirst(val string) string {
for i, v := range val { for _, v := range val {
return string(unicode.ToUpper(v)) + val[i+1:] return string(unicode.ToUpper(v)) + val[len(string(v)):]
} }
return "" return ""
} }

View File

@ -10,7 +10,7 @@ import (
"unicode" "unicode"
) )
// input is struct that holds input form user and result // input is struct that holds input from user and result
type input struct { type input struct {
Input string Input string
Result string Result string

View File

@ -276,18 +276,34 @@ func TestInput_TeaseEmpty(t *testing.T) {
} }
func TestInput_UcFirst(t *testing.T) { func TestInput_UcFirst(t *testing.T) {
str := New("this is test") tests := []struct {
against := "This is test" name string
if val := str.UcFirst(); val != against { arg string
t.Errorf("Expected: to be %s but got: %s", against, val) want string
}{
{
name: "leading lowercase",
arg: "test input",
want: "Test input",
},
{
name: "empty string",
arg: "",
want: "",
},
{
name: "multi-byte leading character",
arg: "δδδ",
want: "Δδδ",
},
} }
}
func TestInput_EmptyUcFirst(t *testing.T) { for _, tt := range tests {
str := New("") t.Run(tt.name, func(t *testing.T) {
against := "" if got := New(tt.arg).UcFirst(); got != tt.want {
if val := str.UcFirst(); val != against { t.Errorf("UcFirst(%v) = %v, want %v", tt.arg, got, tt.want)
t.Errorf("Expected: to be %s but got: %s", against, val) }
})
} }
} }