2021-11-14 20:06:58 +01:00
|
|
|
module strconv
|
|
|
|
|
|
|
|
import strings
|
|
|
|
|
|
|
|
pub fn format_str_sb(s string, p BF_param, mut sb strings.Builder) {
|
|
|
|
if p.len0 <= 0 {
|
|
|
|
sb.write_string(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dif := p.len0 - utf8_str_visible_length(s)
|
|
|
|
|
|
|
|
if dif <= 0 {
|
|
|
|
sb.write_string(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.allign == .right {
|
|
|
|
for i1 := 0; i1 < dif; i1++ {
|
2022-04-15 13:58:56 +02:00
|
|
|
sb.write_u8(p.pad_ch)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.write_string(s)
|
|
|
|
|
|
|
|
if p.allign == .left {
|
|
|
|
for i1 := 0; i1 < dif; i1++ {
|
2022-04-15 13:58:56 +02:00
|
|
|
sb.write_u8(p.pad_ch)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
max_size_f64_char = 32 // the f64 max representation is -36,028,797,018,963,968e1023, 21 chars, 32 is faster for the memory manger
|
|
|
|
// digit pairs in reverse order
|
|
|
|
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
|
|
|
)
|
|
|
|
|
|
|
|
// format_dec_sb format a u64
|
|
|
|
[direct_array_access]
|
|
|
|
pub fn format_dec_sb(d u64, p BF_param, mut res strings.Builder) {
|
|
|
|
mut n_char := dec_digits(d)
|
|
|
|
sign_len := if !p.positive || p.sign_flag { 1 } else { 0 }
|
|
|
|
number_len := sign_len + n_char
|
|
|
|
dif := p.len0 - number_len
|
|
|
|
mut sign_written := false
|
|
|
|
|
|
|
|
if p.allign == .right {
|
|
|
|
if p.pad_ch == `0` {
|
|
|
|
if p.positive {
|
|
|
|
if p.sign_flag {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(`+`)
|
2021-11-14 20:06:58 +01:00
|
|
|
sign_written = true
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(`-`)
|
2021-11-14 20:06:58 +01:00
|
|
|
sign_written = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// write the pad chars
|
|
|
|
for i1 := 0; i1 < dif; i1++ {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(p.pad_ch)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sign_written {
|
|
|
|
// no pad char, write the sign before the number
|
|
|
|
if p.positive {
|
|
|
|
if p.sign_flag {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(`+`)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(`-`)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy version
|
|
|
|
// max u64 18446744073709551615 => 20 byte
|
2022-04-15 17:25:45 +02:00
|
|
|
mut buf := [32]u8{}
|
2021-11-14 20:06:58 +01:00
|
|
|
mut i := 20
|
|
|
|
mut d1 := d
|
|
|
|
for i >= (21 - n_char) {
|
2022-04-15 13:58:56 +02:00
|
|
|
buf[i] = u8(d1 % 10) + `0`
|
2021-11-14 20:06:58 +01:00
|
|
|
|
|
|
|
d1 = d1 / 10
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:13:44 +01:00
|
|
|
for _ in 0 .. n_char {
|
2021-11-14 20:06:58 +01:00
|
|
|
i++
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(buf[i])
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
|
|
|
|
//===========================================
|
|
|
|
|
|
|
|
if p.allign == .left {
|
|
|
|
for i1 := 0; i1 < dif; i1++ {
|
2022-04-15 13:58:56 +02:00
|
|
|
res.write_u8(p.pad_ch)
|
2021-11-14 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|