From 8be07194c7717253aaf16daa9c614ce1f0994b2f Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 20 Feb 2020 18:33:38 +0800 Subject: [PATCH] string: fix capitalize --- vlib/builtin/string.v | 3 +++ vlib/builtin/string_test.v | 2 ++ 2 files changed, 5 insertions(+) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index b024f13b3f..dea073067e 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -752,6 +752,9 @@ pub fn (s string) to_upper() string { } pub fn (s string) capitalize() string { + if s.len == 0 { + return '' + } sl := s.to_lower() cap := sl[0].str().to_upper() + sl.right(1) return cap diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index fae933c60c..b558f123cc 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -503,6 +503,8 @@ fn test_capitalize() { assert s.capitalize() == 'Test' s = 'i am ray' assert s.capitalize() == 'I am ray' + s = '' + assert s.capitalize() == '' } fn test_title() {