strconv: fix format_fl()/format_es() (fix #13210) (#22244)

This commit is contained in:
yuyi 2024-09-18 21:54:09 +08:00 committed by GitHub
parent 13fdc724a7
commit fca336c895
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 8 deletions

View file

@ -34,8 +34,6 @@ pub fn format_str_sb(s string, p BF_param, mut sb strings.Builder) {
}
}
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
const digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
@ -328,8 +326,8 @@ pub fn format_fl(f f64, p BF_param) string {
tmp.free()
}
mut buf := [max_size_f64_char]u8{} // write temp float buffer in stack
mut out := [max_size_f64_char]u8{} // out buffer
mut buf := []u8{len: p.len0 + 32} // write temp float buffer
mut out := []u8{len: p.len0 + 32} // out buffer
mut buf_i := 0 // index temporary string
mut out_i := 0 // index output string
@ -399,8 +397,8 @@ pub fn format_es(f f64, p BF_param) string {
tmp.free()
}
mut buf := [max_size_f64_char]u8{} // write temp float buffer in stack
mut out := [max_size_f64_char]u8{} // out buffer
mut buf := []u8{len: p.len0 + 32} // write temp float buffer
mut out := []u8{len: p.len0 + 32} // out buffer
mut buf_i := 0 // index temporary string
mut out_i := 0 // index output string

View file

@ -30,8 +30,6 @@ pub fn format_str_sb(s string, p BF_param, mut sb strings.Builder) {
}
}
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
const digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'

View file

@ -0,0 +1,4 @@
0.10
0.10
0.10
0.100

View file

@ -0,0 +1,7 @@
fn main() {
n := 0.1
println('${n:36.2f}')
println('${n:37.2f}')
println('${n:38.2f}')
println('${n:70.3f}')
}