Skip to content

Commit

Permalink
builtin: fix empty string lower / upper assert (#21358)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 26, 2024
1 parent 712a912 commit 5329a0a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ pub fn (s string) to_lower() string {
// Example: assert 'hello developer'.is_lower() == true
@[direct_array_access]
pub fn (s string) is_lower() bool {
if s.len > 0 && s[0].is_digit() {
if s == '' || s[0].is_digit() {
return false
}
for i in 0 .. s.len {
Expand Down Expand Up @@ -1558,7 +1558,7 @@ pub fn (s string) to_upper() string {
// Example: assert 'HELLO V'.is_upper() == true
@[direct_array_access]
pub fn (s string) is_upper() bool {
if s.len > 0 && s[0].is_digit() {
if s == '' || s[0].is_digit() {
return false
}
for i in 0 .. s.len {
Expand Down
4 changes: 4 additions & 0 deletions vlib/builtin/string_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,8 @@ fn test_lower() {
s = '123'
assert !s.is_lower()
assert s.to_lower() == '123'
s = ''
assert !s.is_lower()
}

fn test_upper() {
Expand All @@ -1039,6 +1041,8 @@ fn test_upper() {
s = '123'
assert !s.is_upper()
assert s.to_upper() == '123'
s = ''
assert !s.is_upper()
}

fn test_capitalize() {
Expand Down

0 comments on commit 5329a0a

Please sign in to comment.