builtin.string: fix is_upper and is_lower with numbers (#21357)

This commit is contained in:
Swastik Baranwal 2024-04-26 18:50:21 +05:30 committed by GitHub
parent 6c113cf777
commit c7af2c21d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View File

@ -1524,6 +1524,9 @@ pub fn (s string) to_lower() string {
// Example: assert 'hello developer'.is_lower() == true // Example: assert 'hello developer'.is_lower() == true
@[direct_array_access] @[direct_array_access]
pub fn (s string) is_lower() bool { pub fn (s string) is_lower() bool {
if s[0].is_digit() {
return false
}
for i in 0 .. s.len { for i in 0 .. s.len {
if s[i] >= `A` && s[i] <= `Z` { if s[i] >= `A` && s[i] <= `Z` {
return false return false
@ -1555,6 +1558,9 @@ pub fn (s string) to_upper() string {
// Example: assert 'HELLO V'.is_upper() == true // Example: assert 'HELLO V'.is_upper() == true
@[direct_array_access] @[direct_array_access]
pub fn (s string) is_upper() bool { pub fn (s string) is_upper() bool {
if s[0].is_digit() {
return false
}
for i in 0 .. s.len { for i in 0 .. s.len {
if s[i] >= `a` && s[i] <= `z` { if s[i] >= `a` && s[i] <= `z` {
return false return false

View File

@ -1013,6 +1013,9 @@ fn test_lower() {
assert s.to_lower() == 'hi' assert s.to_lower() == 'hi'
assert 'aloha!'[0] == `a` assert 'aloha!'[0] == `a`
assert 'aloha!'[5] == `!` assert 'aloha!'[5] == `!`
s = '123'
assert !s.is_lower()
assert s.to_lower() == '123'
} }
fn test_upper() { fn test_upper() {
@ -1033,6 +1036,9 @@ fn test_upper() {
s = 'HI' s = 'HI'
assert s.is_upper() assert s.is_upper()
assert s.to_upper() == 'HI' assert s.to_upper() == 'HI'
s = '123'
assert !s.is_upper()
assert s.to_upper() == '123'
} }
fn test_capitalize() { fn test_capitalize() {