Skip to content

Commit

Permalink
builtin.string: fix is_upper and is_lower with numbers (#21357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Apr 26, 2024
1 parent 6c113cf commit c7af2c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,9 @@ 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[0].is_digit() {
return false
}
for i in 0 .. s.len {
if s[i] >= `A` && s[i] <= `Z` {
return false
Expand Down Expand Up @@ -1555,6 +1558,9 @@ 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[0].is_digit() {
return false
}
for i in 0 .. s.len {
if s[i] >= `a` && s[i] <= `z` {
return false
Expand Down
6 changes: 6 additions & 0 deletions vlib/builtin/string_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,9 @@ fn test_lower() {
assert s.to_lower() == 'hi'
assert 'aloha!'[0] == `a`
assert 'aloha!'[5] == `!`
s = '123'
assert !s.is_lower()
assert s.to_lower() == '123'
}

fn test_upper() {
Expand All @@ -1033,6 +1036,9 @@ fn test_upper() {
s = 'HI'
assert s.is_upper()
assert s.to_upper() == 'HI'
s = '123'
assert !s.is_upper()
assert s.to_upper() == '123'
}

fn test_capitalize() {
Expand Down

0 comments on commit c7af2c2

Please sign in to comment.