2020-10-07 10:06:52 +02:00
|
|
|
module strconv
|
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
printf/sprintf V implementation
|
|
|
|
|
|
|
|
Copyright (c) 2020 Dario Deledda. All rights reserved.
|
|
|
|
Use of this source code is governed by an MIT license
|
|
|
|
that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
This file contains the printf/sprintf functions
|
|
|
|
|
|
|
|
*/
|
2020-07-16 19:01:22 +02:00
|
|
|
|
2020-04-26 09:23:10 +02:00
|
|
|
import strings
|
|
|
|
|
2021-04-25 16:57:21 +02:00
|
|
|
pub enum Align_text {
|
2020-04-26 16:26:33 +02:00
|
|
|
right = 0
|
|
|
|
left
|
2020-04-26 09:23:10 +02:00
|
|
|
center
|
|
|
|
}
|
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Float conversion utility
|
|
|
|
|
|
|
|
*/
|
2021-05-24 04:20:45 +02:00
|
|
|
const(
|
|
|
|
// rounding value
|
|
|
|
dec_round = [
|
|
|
|
f64(0.5),
|
|
|
|
0.05,
|
|
|
|
0.005,
|
|
|
|
0.0005,
|
|
|
|
0.00005,
|
|
|
|
0.000005,
|
|
|
|
0.0000005,
|
|
|
|
0.00000005,
|
|
|
|
0.000000005,
|
|
|
|
0.0000000005,
|
|
|
|
0.00000000005,
|
|
|
|
0.000000000005,
|
|
|
|
0.0000000000005,
|
|
|
|
0.00000000000005,
|
|
|
|
0.000000000000005,
|
|
|
|
0.0000000000000005,
|
|
|
|
0.00000000000000005,
|
|
|
|
0.000000000000000005,
|
|
|
|
0.0000000000000000005,
|
|
|
|
0.00000000000000000005,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2020-04-26 09:23:10 +02:00
|
|
|
const(
|
|
|
|
// rounding value
|
|
|
|
dec_round = [
|
|
|
|
f64(0.44),
|
|
|
|
0.044,
|
|
|
|
0.0044,
|
|
|
|
0.00044,
|
|
|
|
0.000044,
|
|
|
|
0.0000044,
|
|
|
|
0.00000044,
|
|
|
|
0.000000044,
|
|
|
|
0.0000000044,
|
|
|
|
0.00000000044,
|
|
|
|
0.000000000044,
|
|
|
|
0.0000000000044,
|
|
|
|
0.00000000000044,
|
|
|
|
0.000000000000044,
|
|
|
|
0.0000000000000044,
|
|
|
|
0.00000000000000044,
|
|
|
|
0.000000000000000044,
|
|
|
|
0.0000000000000000044,
|
|
|
|
0.00000000000000000044,
|
|
|
|
0.000000000000000000044,
|
2020-06-12 08:57:38 +02:00
|
|
|
]
|
2020-04-26 09:23:10 +02:00
|
|
|
)
|
2021-05-24 04:20:45 +02:00
|
|
|
*/
|
2020-04-26 09:23:10 +02:00
|
|
|
// max float 1.797693134862315708145274237317043567981e+308
|
|
|
|
|
2020-06-07 23:04:23 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Single format functions
|
|
|
|
|
|
|
|
*/
|
2020-06-03 00:30:46 +02:00
|
|
|
pub struct BF_param {
|
2021-04-25 16:57:21 +02:00
|
|
|
pub mut:
|
2020-08-27 06:46:18 +02:00
|
|
|
pad_ch byte = byte(` `) // padding char
|
2020-04-26 09:23:10 +02:00
|
|
|
len0 int = -1 // default len for whole the number or string
|
|
|
|
len1 int = 6 // number of decimal digits, if needed
|
2020-04-26 16:26:33 +02:00
|
|
|
positive bool = true // mandatory: the sign of the number passed
|
2020-09-09 15:34:41 +02:00
|
|
|
sign_flag bool // flag for print sign as prefix in padding
|
2020-04-26 09:23:10 +02:00
|
|
|
allign Align_text = .right // alignment of the string
|
2020-09-09 15:34:41 +02:00
|
|
|
rm_tail_zero bool // remove the tail zeros from floats
|
2020-04-26 09:23:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_str(s string, p BF_param) string {
|
2021-04-25 16:57:21 +02:00
|
|
|
if p.len0 <= 0 {
|
2021-05-05 03:12:18 +02:00
|
|
|
return s.clone()
|
2021-04-25 16:57:21 +02:00
|
|
|
}
|
|
|
|
dif := p.len0 - utf8_str_visible_length(s)
|
2020-04-26 09:23:10 +02:00
|
|
|
if dif <= 0 {
|
2021-05-05 03:12:18 +02:00
|
|
|
return s.clone()
|
2020-04-26 09:23:10 +02:00
|
|
|
}
|
|
|
|
mut res := strings.new_builder(s.len + dif)
|
|
|
|
if p.allign == .right {
|
|
|
|
for i1 :=0; i1 < dif; i1++ {
|
2020-04-26 16:26:33 +02:00
|
|
|
res.write_b(p.pad_ch)
|
2020-04-26 09:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-22 12:18:11 +01:00
|
|
|
res.write_string(s)
|
2020-04-26 09:23:10 +02:00
|
|
|
if p.allign == .left {
|
|
|
|
for i1 :=0; i1 < dif; i1++ {
|
2020-04-26 16:26:33 +02:00
|
|
|
res.write_b(p.pad_ch)
|
2020-04-26 09:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res.str()
|
|
|
|
}
|