Replies: 2 comments
-
I'm not sure how to convert it into UTF-8 or Rune types, but I found keeping them together in one string works out in my cases. I'm using this code to convert a string into an array while keeping unicode characters together. Hope this helps in the short term. fn string_to_array(s string) []string {
mut rune_list := []string{}
mut rune_builder := []byte{}
mut num_bytes := 0
// https://stackoverflow.com/a/33349765 - Simple table of utf8 hex ranges
for ch in s {
if ch >= 0xC0 && ch <= 0xDF {
num_bytes = 2
} else if ch >= 0xE0 && ch <= 0xEF {
num_bytes = 3
} else if ch >= 0xF0 && ch <= 0xF7 {
num_bytes = 4
}
if num_bytes > 0 { // we found or are inside of a utf8 character
rune_builder << ch
num_bytes--
if num_bytes == 0 {
rune_list << rune_builder.bytestr() // Convert list of bytes to string
rune_builder = []
}
} else {
rune_list << ch.str()
}
}
return rune_list
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
See string_from_wide/1 and string_from_wide2/2 in vlib/builtin/utf8.c.v . |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
as title said, how to decode two bytes Unicode to v's UTF-8?
Thanks a lot.
Beta Was this translation helpful? Give feedback.
All reactions