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

pull/9876/head
penguindark 2021-04-25 16:57:21 +02:00 committed by GitHub
parent 3c8d2bbaec
commit bfe0a7887f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {

View File

@ -94,6 +94,14 @@ pub fn (d Dec32) get_string_32(neg bool, i_n_digit int, i_pad_digit int) string
x++
}
// no decimal digits needed, end here
if i_n_digit == 0 {
unsafe {
buf[i]=0
return tos(byteptr(&buf[0]), i)
}
}
if out_len >= 1 {
buf[y - x] = `.`
x++

View File

@ -110,6 +110,14 @@ fn (d Dec64) get_string_64(neg bool, i_n_digit int, i_pad_digit int) string {
x++
}
// no decimal digits needed, end here
if i_n_digit == 0 {
unsafe {
buf[i]=0
return tos(byteptr(&buf[0]), i)
}
}
if out_len >= 1 {
buf[y - x] = `.`
x++

View File

@ -29,7 +29,7 @@ enum Char_parse_state {
reset_params
}
enum Align_text {
pub enum Align_text {
right = 0
left
center
@ -176,6 +176,12 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
i++
}
}
// no more digits needed, stop here
if dec_digit <= 0 {
return unsafe { tos(res.data, dot_res_sp) }
}
//println("r_i-d_pos: ${r_i - d_pos}")
if dot_res_sp >= 0 {
if (r_i - dot_res_sp) > dec_digit {
@ -204,6 +210,7 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
*/
pub struct BF_param {
pub mut:
pad_ch byte = byte(` `) // padding char
len0 int = -1 // default len for whole the number or string
len1 int = 6 // number of decimal digits, if needed
@ -214,7 +221,10 @@ pub struct BF_param {
}
pub fn format_str(s string, p BF_param) string {
dif := p.len0 - s.len
if p.len0 <= 0 {
return s
}
dif := p.len0 - utf8_str_visible_length(s)
if dif <= 0 {
return s
}