mirror of
https://github.com/vlang/v.git
synced 2025-09-11 08:25:42 -04:00
builtin: add string.is_identifier() (#24350)
This commit is contained in:
parent
146c766dc9
commit
57b815f96d
@ -2768,7 +2768,7 @@ pub fn (name string) match_glob(pattern string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// is_ascii returns true if all characters belong to the US-ASCII set ([` `..`~`])
|
// is_ascii returns true if all characters belong to the US-ASCII set ([` `..`~`])
|
||||||
@[inline]
|
@[direct_array_access; inline]
|
||||||
pub fn (s string) is_ascii() bool {
|
pub fn (s string) is_ascii() bool {
|
||||||
for i := 0; i < s.len; i++ {
|
for i := 0; i < s.len; i++ {
|
||||||
if s[i] < u8(` `) || s[i] > u8(`~`) {
|
if s[i] < u8(` `) || s[i] > u8(`~`) {
|
||||||
@ -2778,6 +2778,24 @@ pub fn (s string) is_ascii() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// is_identifier checks if a string is a valid identifier (starts with letter/underscore, followed by letters, digits, or underscores)
|
||||||
|
@[direct_array_access]
|
||||||
|
pub fn (s string) is_identifier() bool {
|
||||||
|
if s.len == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !(s[0].is_letter() || s[0] == `_`) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := 1; i < s.len; i++ {
|
||||||
|
c := s[i]
|
||||||
|
if !(c.is_letter() || c.is_digit() || c == `_`) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// camel_to_snake convert string from camelCase to snake_case
|
// camel_to_snake convert string from camelCase to snake_case
|
||||||
// Example: assert 'Abcd'.camel_to_snake() == 'abcd'
|
// Example: assert 'Abcd'.camel_to_snake() == 'abcd'
|
||||||
// Example: assert 'aaBB'.camel_to_snake() == 'aa_bb'
|
// Example: assert 'aaBB'.camel_to_snake() == 'aa_bb'
|
||||||
|
@ -1553,6 +1553,20 @@ fn test_string_is_ascii() {
|
|||||||
assert 'a👋bc'.is_ascii() == false
|
assert 'a👋bc'.is_ascii() == false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_string_is_identifier() {
|
||||||
|
assert ''.is_identifier() == false
|
||||||
|
assert ' '.is_identifier() == false
|
||||||
|
assert '~~'.is_identifier() == false
|
||||||
|
assert '_Az~'.is_identifier() == false
|
||||||
|
assert '_Aö~'.is_identifier() == false
|
||||||
|
assert '👋'.is_identifier() == false
|
||||||
|
assert 'a👋bc'.is_identifier() == false
|
||||||
|
assert '9'.is_identifier() == false
|
||||||
|
assert '_9'.is_identifier() == true
|
||||||
|
assert 'a 9'.is_identifier() == false
|
||||||
|
assert 't'.is_identifier() == true
|
||||||
|
}
|
||||||
|
|
||||||
fn test_string_with_zero_byte_escape() {
|
fn test_string_with_zero_byte_escape() {
|
||||||
assert '\x00'.bytes() == [u8(0)]
|
assert '\x00'.bytes() == [u8(0)]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user