mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin.string: new fn (s string) is_ascii() bool (#14418)
This commit is contained in:
parent
d10f83ce15
commit
02c8a6057c
@ -2002,3 +2002,8 @@ pub fn (name string) match_glob(pattern string) bool {
|
||||
// Matched all of `pattern` to all of `name`
|
||||
return true
|
||||
}
|
||||
|
||||
// is_ascii returns true if all characters belong to the US-ASCII set ([` `..`~`])
|
||||
pub fn (s string) is_ascii() bool {
|
||||
return !s.bytes().any(it < u8(` `) || it > u8(`~`))
|
||||
}
|
||||
|
@ -989,6 +989,16 @@ fn test_string_f32() {
|
||||
assert '-123.456'.f32() - (-123.456) <= f32_epsilon
|
||||
}
|
||||
|
||||
fn test_string_is_ascii() {
|
||||
assert ''.is_ascii() == true
|
||||
assert ' '.is_ascii() == true
|
||||
assert '~~'.is_ascii() == true
|
||||
assert ' Az~'.is_ascii() == true
|
||||
assert ' Aö~'.is_ascii() == false
|
||||
assert '👋'.is_ascii() == false
|
||||
assert 'a👋bc'.is_ascii() == false
|
||||
}
|
||||
|
||||
fn test_string_with_zero_byte_escape() {
|
||||
assert '\x00'.bytes() == [u8(0)]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user