Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin.string: enforce fasely values for numerical values when using is_upper and is_lower #21357

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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