string: fix capitalize

pull/3792/head
yuyi 2020-02-20 18:33:38 +08:00 committed by GitHub
parent d51019dd77
commit 8be07194c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 0 deletions

View File

@ -752,6 +752,9 @@ pub fn (s string) to_upper() string {
} }
pub fn (s string) capitalize() string { pub fn (s string) capitalize() string {
if s.len == 0 {
return ''
}
sl := s.to_lower() sl := s.to_lower()
cap := sl[0].str().to_upper() + sl.right(1) cap := sl[0].str().to_upper() + sl.right(1)
return cap return cap

View File

@ -503,6 +503,8 @@ fn test_capitalize() {
assert s.capitalize() == 'Test' assert s.capitalize() == 'Test'
s = 'i am ray' s = 'i am ray'
assert s.capitalize() == 'I am ray' assert s.capitalize() == 'I am ray'
s = ''
assert s.capitalize() == ''
} }
fn test_title() { fn test_title() {