1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: enable string index error handling s[i] or {...} (#10670)

This commit is contained in:
Uwe Krüger
2021-07-05 20:00:30 +02:00
committed by GitHub
parent 9e61321d4c
commit a1f123bd42
3 changed files with 85 additions and 5 deletions

View File

@ -1263,6 +1263,17 @@ fn (s string) at(idx int) byte {
}
}
// version of `at()` that is used in `a[i] or {`
// return an error when the index is out of range
fn (s string) at_with_check(idx int) ?byte {
if idx < 0 || idx >= s.len {
return error('string index out of range')
}
unsafe {
return s.str[idx]
}
}
// is_space returns `true` if the byte is a white space character.
// The following list is considered white space characters: ` `, `\t`, `\n`, `\v`, `\f`, `\r`, 0x85, 0xa0
// Example: assert byte(` `).is_space() == true