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:
@ -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
|
||||
|
Reference in New Issue
Block a user