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

encoding.utf8: add support for indexing a utf8 str (#9670)

This commit is contained in:
ChAoS_UnItY
2021-04-11 14:04:18 +08:00
committed by GitHub
parent a2a18ef92c
commit acb58d4923
2 changed files with 38 additions and 0 deletions

View File

@ -87,6 +87,27 @@ pub fn get_uchar(s string, index int) int {
}
// raw_index - get the raw chracter from the string by the given index value.
// example: '我是V Lang'.raw_index(1) => '是'
pub fn raw_index(s string, index int) string {
mut r := []rune{}
for i := 0; i < s.len; i++ {
b := s[i]
ch_len := ((0xe5000000>>((b>>3) & 0x1e)) & 3)
r << if ch_len > 0 {
i += ch_len
rune(get_uchar(s,i-ch_len))
} else {
rune(b)
}
}
return r[index].str()
}
/*
Conversion functions