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

builtin: add string.runes() (#10611)

This commit is contained in:
Daniel Däschle
2021-06-30 08:17:38 +02:00
committed by GitHub
parent 1e896c7020
commit 6838030ab5
3 changed files with 27 additions and 1 deletions

View File

@@ -68,6 +68,21 @@ pub fn vstrlen(s &byte) int {
return unsafe { C.strlen(&char(s)) }
}
pub fn (s string) runes() []rune {
mut runes := []rune{cap: s.len}
for i := 0; i < s.len; i++ {
char_len := utf8_char_len(unsafe { s.str[i] })
if char_len > 1 {
mut r := unsafe { s[i..i + char_len] }
runes << r.utf32_code()
i += char_len - 1
} else {
runes << unsafe { s.str[i] }
}
}
return runes
}
// tos converts a C string to a V string.
// String data is reused, not copied.
[unsafe]