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

builtin,strconv: fix and optimize utf8 and formatting functions (#9874)

This commit is contained in:
penguindark
2021-04-25 16:57:21 +02:00
committed by GitHub
parent 3c8d2bbaec
commit bfe0a7887f
4 changed files with 37 additions and 16 deletions

View File

@@ -103,16 +103,12 @@ fn utf8_len(c byte) int {
}
// Calculate string length for in number of codepoints
fn utf8_str_len(s string) int {
pub fn utf8_str_len(s string) int {
mut l := 0
for i := 0; i < s.len; i++ {
mut i := 0
for i < s.len {
l++
c := unsafe { s.str[i] }
if (c & (1 << 7)) != 0 {
for t := byte(1 << 6); (c & t) != 0; t >>= 1 {
i++
}
}
i += ((0xe5000000 >> ((unsafe { s.str[i] } >> 3) & 0x1e)) & 3) + 1
}
return l
}
@@ -124,17 +120,16 @@ pub fn utf8_str_visible_length(s string) int {
mut l := 0
mut ul := 1
for i := 0; i < s.len; i += ul {
ul = 1
c := unsafe { s.str[i] }
if (c & (1 << 7)) != 0 {
for t := byte(1 << 6); (c & t) != 0; t >>= 1 {
ul++
}
}
ul = ((0xe5000000 >> ((unsafe { s.str[i] } >> 3) & 0x1e)) & 3) + 1
if i + ul > s.len { // incomplete UTF-8 sequence
return l
}
l++
// avoid the match if not needed
if ul == 1 {
continue
}
// recognize combining characters and wide characters
match ul {
2 {