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 {
for i, v := range val {
return string(unicode.ToUpper(v)) + val[i+1:]
for _, v := range val {
return string(unicode.ToUpper(v)) + val[len(string(v)):]
}
return ""
}

View File

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

View File

@ -276,18 +276,34 @@ func TestInput_TeaseEmpty(t *testing.T) {
}
func TestInput_UcFirst(t *testing.T) {
str := New("this is test")
against := "This is test"
if val := str.UcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
tests := []struct {
name string
arg string
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) {
str := New("")
against := ""
if val := str.UcFirst(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.arg).UcFirst(); got != tt.want {
t.Errorf("UcFirst(%v) = %v, want %v", tt.arg, got, tt.want)
}
})
}
}