Skip to content

Commit

Permalink
strconv: fix format_fl()/format_es() (fix #13210) (#22244)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Sep 18, 2024
1 parent 13fdc72 commit fca336c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
10 changes: 4 additions & 6 deletions vlib/strconv/format_mem.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions vlib/strconv/format_mem.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 4 additions & 0 deletions vlib/v/slow_tests/inout/printing_interpolating_float.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.10
0.10
0.10
0.100
7 changes: 7 additions & 0 deletions vlib/v/slow_tests/inout/printing_interpolating_float.vv
Original file line number Diff line number Diff line change
@@ -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}')
}

0 comments on commit fca336c

Please sign in to comment.