Skip to content

Commit ac254fb

Browse files
committed
Auto merge of #41859 - froydnj:align-float-parts, r=sfackler
fix confusion about parts required for float formatting The documentation for flt2dec doesn't match up with the actual implementation, so fix the documentation to align with reality. Presumably due to the mismatch, the formatting code for floats in std::fmt can use correspondingly shorter arrays in some places, so fix those places up as well. Fixes #41304.
2 parents 386b0b9 + b185844 commit ac254fb

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/libcore/fmt/float.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
2121
{
2222
unsafe {
2323
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
24-
let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
24+
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
2525
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
2626
*num, sign, precision,
2727
false, &mut buf, &mut parts);
@@ -39,7 +39,7 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
3939
unsafe {
4040
// enough for f32 and f64
4141
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
42-
let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
42+
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
4343
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
4444
*num, sign, 0, false, &mut buf, &mut parts);
4545
fmt.pad_formatted_parts(&formatted)
@@ -75,7 +75,7 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
7575
{
7676
unsafe {
7777
let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
78-
let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
78+
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
7979
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
8080
*num, sign, precision,
8181
upper, &mut buf, &mut parts);
@@ -94,7 +94,7 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
9494
unsafe {
9595
// enough for f32 and f64
9696
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
97-
let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
97+
let mut parts: [flt2dec::Part; 6] = mem::uninitialized();
9898
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
9999
*num, sign, (0, 0), upper,
100100
&mut buf, &mut parts);

src/libcore/num/flt2dec/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ fn determine_sign(sign: Sign, decoded: &FullDecoded, negative: bool) -> &'static
410410
/// it will only print given digits and nothing else.
411411
///
412412
/// The byte buffer should be at least `MAX_SIG_DIGITS` bytes long.
413-
/// There should be at least 5 parts available, due to the worst case like
414-
/// `[+][0.][0000][45][0000]` with `frac_digits = 10`.
413+
/// There should be at least 4 parts available, due to the worst case like
414+
/// `[+][0.][0000][2][0000]` with `frac_digits = 10`.
415415
pub fn to_shortest_str<'a, T, F>(mut format_shortest: F, v: T,
416416
sign: Sign, frac_digits: usize, _upper: bool,
417417
buf: &'a mut [u8], parts: &'a mut [Part<'a>]) -> Formatted<'a>
@@ -465,8 +465,8 @@ pub fn to_shortest_str<'a, T, F>(mut format_shortest: F, v: T,
465465
/// cannot be in this range, avoiding any confusion.
466466
///
467467
/// The byte buffer should be at least `MAX_SIG_DIGITS` bytes long.
468-
/// There should be at least 7 parts available, due to the worst case like
469-
/// `[+][1][.][2345][e][-][67]`.
468+
/// There should be at least 6 parts available, due to the worst case like
469+
/// `[+][1][.][2345][e][-][6]`.
470470
pub fn to_shortest_exp_str<'a, T, F>(mut format_shortest: F, v: T,
471471
sign: Sign, dec_bounds: (i16, i16), upper: bool,
472472
buf: &'a mut [u8], parts: &'a mut [Part<'a>]) -> Formatted<'a>
@@ -544,8 +544,8 @@ fn estimate_max_buf_len(exp: i16) -> usize {
544544
/// The byte buffer should be at least `ndigits` bytes long unless `ndigits` is
545545
/// so large that only the fixed number of digits will be ever written.
546546
/// (The tipping point for `f64` is about 800, so 1000 bytes should be enough.)
547-
/// There should be at least 7 parts available, due to the worst case like
548-
/// `[+][1][.][2345][e][-][67]`.
547+
/// There should be at least 6 parts available, due to the worst case like
548+
/// `[+][1][.][2345][e][-][6]`.
549549
pub fn to_exact_exp_str<'a, T, F>(mut format_exact: F, v: T,
550550
sign: Sign, ndigits: usize, upper: bool,
551551
buf: &'a mut [u8], parts: &'a mut [Part<'a>]) -> Formatted<'a>
@@ -600,8 +600,8 @@ pub fn to_exact_exp_str<'a, T, F>(mut format_exact: F, v: T,
600600
/// The byte buffer should be enough for the output unless `frac_digits` is
601601
/// so large that only the fixed number of digits will be ever written.
602602
/// (The tipping point for `f64` is about 800, and 1000 bytes should be enough.)
603-
/// There should be at least 5 parts available, due to the worst case like
604-
/// `[+][0.][0000][45][0000]` with `frac_digits = 10`.
603+
/// There should be at least 4 parts available, due to the worst case like
604+
/// `[+][0.][0000][2][0000]` with `frac_digits = 10`.
605605
pub fn to_exact_fixed_str<'a, T, F>(mut format_exact: F, v: T,
606606
sign: Sign, frac_digits: usize, _upper: bool,
607607
buf: &'a mut [u8], parts: &'a mut [Part<'a>]) -> Formatted<'a>

0 commit comments

Comments
 (0)