Skip to content

Commit

Permalink
builtin: add empty string verification for the new string .is_oct() e…
Browse files Browse the repository at this point in the history
…tc methods, suggested on PR #20540 (#20564)
  • Loading branch information
viniciusfdasilva committed Jan 17, 2024
1 parent afd74ad commit 3569635
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions vlib/builtin/string.v
Expand Up @@ -1791,6 +1791,10 @@ fn (s string) at_with_check(idx int) ?u8 {
pub fn (str string) is_oct() bool {
mut i := 0

if str.len == 0 {
return false
}

if str[i] == `0` {
i++
} else if str[i] == `-` || str[i] == `+` {
Expand Down Expand Up @@ -1830,6 +1834,10 @@ pub fn (str string) is_oct() bool {
pub fn (str string) is_bin() bool {
mut i := 0

if str.len == 0 {
return false
}

if str[i] == `0` {
i++
} else if str[i] == `-` || str[i] == `+` {
Expand Down Expand Up @@ -1869,6 +1877,10 @@ pub fn (str string) is_bin() bool {
pub fn (str string) is_hex() bool {
mut i := 0

if str.len == 0 {
return false
}

if str[i] == `0` {
i++
} else if str[i] == `-` || str[i] == `+` {
Expand Down Expand Up @@ -1909,6 +1921,10 @@ pub fn (str string) is_hex() bool {
pub fn (str string) is_int() bool {
mut i := 0

if str.len == 0 {
return false
}

if (str[i] != `-` && str[i] != `+`) && (!str[i].is_digit()) {
return false
} else {
Expand Down
4 changes: 4 additions & 0 deletions vlib/builtin/string_test.v
Expand Up @@ -426,6 +426,7 @@ fn test_rsplit_once() ? {
}

fn test_is_bin() {
assert ''.is_bin() == false
assert '0b1'.is_bin() == true
assert '0b0'.is_bin() == true
assert '0b'.is_bin() == false
Expand Down Expand Up @@ -455,6 +456,7 @@ fn test_is_bin() {
}

fn test_is_oct() {
assert ''.is_oct() == false
assert '0o0'.is_oct() == true
assert '0o1'.is_oct() == true
assert '0o2'.is_oct() == true
Expand Down Expand Up @@ -505,6 +507,7 @@ fn test_is_oct() {
}

fn test_is_hex() {
assert ''.is_hex() == false
assert '-324'.is_hex() == false
assert '-0'.is_hex() == false
assert '0x1'.is_hex() == true
Expand All @@ -527,6 +530,7 @@ fn test_is_hex() {
}

fn test_is_int() {
assert ''.is_int() == false
assert '-324'.is_int() == true
assert '234'.is_int() == true
assert '-0'.is_int() == true
Expand Down

0 comments on commit 3569635

Please sign in to comment.