1
0
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:
WoodyAtHome 2022-05-17 07:01:03 +02:00 committed by GitHub
parent d10f83ce15
commit 02c8a6057c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -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(`~`))
}

View File

@ -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)]
}