2019-11-24 01:43:32 -08:00
|
|
|
use crate::fmt::{Debug, Display, Formatter, LowerExp, Result, UpperExp};
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::mem::MaybeUninit;
|
|
|
|
use crate::num::flt2dec;
|
2017-02-07 20:58:34 +11:00
|
|
|
|
2017-04-13 15:49:37 -04:00
|
|
|
// Don't inline this so callers don't use the stack space this function
|
|
|
|
// requires unless they have to.
|
|
|
|
#[inline(never)]
|
2019-11-24 01:43:32 -08:00
|
|
|
fn float_to_decimal_common_exact<T>(
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
num: &T,
|
|
|
|
sign: flt2dec::Sign,
|
|
|
|
precision: usize,
|
|
|
|
) -> Result
|
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-04-13 15:49:37 -04:00
|
|
|
{
|
2020-08-30 14:43:52 +02:00
|
|
|
// SAFETY: Possible undefined behavior, see FIXME(#76092)
|
2017-04-14 16:25:49 -04:00
|
|
|
unsafe {
|
2019-03-18 22:45:02 +01:00
|
|
|
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
|
2019-04-19 01:37:12 +02:00
|
|
|
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
|
2020-08-30 14:43:52 +02:00
|
|
|
// FIXME(#76092): This is calling `assume_init_mut` on an uninitialized
|
2019-07-17 09:51:58 +02:00
|
|
|
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
|
2018-11-27 16:12:08 +01:00
|
|
|
// we decided whether that is valid or not.
|
2019-07-17 09:51:58 +02:00
|
|
|
// We can do this only because we are libstd and coupled to the compiler.
|
|
|
|
// (FWIW, using `freeze` would not be enough; `flt2dec::Part` is an enum!)
|
2019-11-24 01:43:32 -08:00
|
|
|
let formatted = flt2dec::to_exact_fixed_str(
|
|
|
|
flt2dec::strategy::grisu::format_exact,
|
|
|
|
*num,
|
|
|
|
sign,
|
|
|
|
precision,
|
2020-08-29 02:13:02 +02:00
|
|
|
buf.assume_init_mut(),
|
|
|
|
parts.assume_init_mut(),
|
2019-11-24 01:43:32 -08:00
|
|
|
);
|
2017-04-14 16:25:49 -04:00
|
|
|
fmt.pad_formatted_parts(&formatted)
|
|
|
|
}
|
2017-04-13 15:49:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't inline this so callers that call both this and the above won't wind
|
|
|
|
// up using the combined stack space of both functions in some cases.
|
|
|
|
#[inline(never)]
|
2019-11-24 01:43:32 -08:00
|
|
|
fn float_to_decimal_common_shortest<T>(
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
num: &T,
|
|
|
|
sign: flt2dec::Sign,
|
|
|
|
precision: usize,
|
|
|
|
) -> Result
|
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-04-13 15:49:37 -04:00
|
|
|
{
|
2020-08-30 14:43:52 +02:00
|
|
|
// SAFETY: Possible undefined behavior, see FIXME(#76092)
|
2017-04-14 16:25:49 -04:00
|
|
|
unsafe {
|
|
|
|
// enough for f32 and f64
|
2019-03-18 22:45:02 +01:00
|
|
|
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
|
2019-04-19 01:37:12 +02:00
|
|
|
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
|
2020-08-30 14:43:52 +02:00
|
|
|
// FIXME(#76092)
|
2019-11-24 01:43:32 -08:00
|
|
|
let formatted = flt2dec::to_shortest_str(
|
|
|
|
flt2dec::strategy::grisu::format_shortest,
|
|
|
|
*num,
|
|
|
|
sign,
|
|
|
|
precision,
|
2020-08-29 02:13:02 +02:00
|
|
|
buf.assume_init_mut(),
|
|
|
|
parts.assume_init_mut(),
|
2019-11-24 01:43:32 -08:00
|
|
|
);
|
2017-04-14 16:25:49 -04:00
|
|
|
fmt.pad_formatted_parts(&formatted)
|
|
|
|
}
|
2017-04-13 15:49:37 -04:00
|
|
|
}
|
|
|
|
|
2017-02-07 20:58:34 +11:00
|
|
|
// Common code of floating point Debug and Display.
|
2019-11-24 01:43:32 -08:00
|
|
|
fn float_to_decimal_common<T>(
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
num: &T,
|
|
|
|
negative_zero: bool,
|
|
|
|
min_precision: usize,
|
|
|
|
) -> Result
|
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-02-07 20:58:34 +11:00
|
|
|
{
|
|
|
|
let force_sign = fmt.sign_plus();
|
|
|
|
let sign = match (force_sign, negative_zero) {
|
|
|
|
(false, false) => flt2dec::Sign::Minus,
|
2019-11-24 01:43:32 -08:00
|
|
|
(false, true) => flt2dec::Sign::MinusRaw,
|
|
|
|
(true, false) => flt2dec::Sign::MinusPlus,
|
|
|
|
(true, true) => flt2dec::Sign::MinusPlusRaw,
|
2017-02-07 20:58:34 +11:00
|
|
|
};
|
|
|
|
|
2017-04-13 15:49:37 -04:00
|
|
|
if let Some(precision) = fmt.precision {
|
|
|
|
float_to_decimal_common_exact(fmt, num, sign, precision)
|
|
|
|
} else {
|
2017-12-18 23:16:00 +00:00
|
|
|
float_to_decimal_common_shortest(fmt, num, sign, min_precision)
|
2017-04-13 15:49:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't inline this so callers don't use the stack space this function
|
|
|
|
// requires unless they have to.
|
|
|
|
#[inline(never)]
|
2019-11-24 01:43:32 -08:00
|
|
|
fn float_to_exponential_common_exact<T>(
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
num: &T,
|
|
|
|
sign: flt2dec::Sign,
|
|
|
|
precision: usize,
|
|
|
|
upper: bool,
|
|
|
|
) -> Result
|
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-04-13 15:49:37 -04:00
|
|
|
{
|
2020-08-30 14:43:52 +02:00
|
|
|
// SAFETY: Possible undefined behavior, see FIXME(#76092)
|
2017-04-14 16:25:49 -04:00
|
|
|
unsafe {
|
2019-03-18 22:45:02 +01:00
|
|
|
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
|
2019-04-19 01:37:12 +02:00
|
|
|
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
|
2020-08-30 14:43:52 +02:00
|
|
|
// FIXME(#76092)
|
2019-11-24 01:43:32 -08:00
|
|
|
let formatted = flt2dec::to_exact_exp_str(
|
|
|
|
flt2dec::strategy::grisu::format_exact,
|
|
|
|
*num,
|
|
|
|
sign,
|
|
|
|
precision,
|
|
|
|
upper,
|
2020-08-29 02:13:02 +02:00
|
|
|
buf.assume_init_mut(),
|
|
|
|
parts.assume_init_mut(),
|
2019-11-24 01:43:32 -08:00
|
|
|
);
|
2017-04-14 16:25:49 -04:00
|
|
|
fmt.pad_formatted_parts(&formatted)
|
|
|
|
}
|
2017-04-13 15:49:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't inline this so callers that call both this and the above won't wind
|
|
|
|
// up using the combined stack space of both functions in some cases.
|
|
|
|
#[inline(never)]
|
2019-11-24 01:43:32 -08:00
|
|
|
fn float_to_exponential_common_shortest<T>(
|
|
|
|
fmt: &mut Formatter<'_>,
|
|
|
|
num: &T,
|
|
|
|
sign: flt2dec::Sign,
|
|
|
|
upper: bool,
|
|
|
|
) -> Result
|
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-04-13 15:49:37 -04:00
|
|
|
{
|
2020-08-30 14:43:52 +02:00
|
|
|
// SAFETY: Possible undefined behavior, see FIXME(#76092)
|
2017-04-14 16:25:49 -04:00
|
|
|
unsafe {
|
|
|
|
// enough for f32 and f64
|
2019-03-18 22:45:02 +01:00
|
|
|
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
|
2019-04-19 01:37:12 +02:00
|
|
|
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
|
2020-08-30 14:43:52 +02:00
|
|
|
// FIXME(#76092)
|
2019-11-24 01:43:32 -08:00
|
|
|
let formatted = flt2dec::to_shortest_exp_str(
|
|
|
|
flt2dec::strategy::grisu::format_shortest,
|
|
|
|
*num,
|
|
|
|
sign,
|
|
|
|
(0, 0),
|
|
|
|
upper,
|
2020-08-29 02:13:02 +02:00
|
|
|
buf.assume_init_mut(),
|
|
|
|
parts.assume_init_mut(),
|
2019-11-24 01:43:32 -08:00
|
|
|
);
|
2017-04-14 16:25:49 -04:00
|
|
|
fmt.pad_formatted_parts(&formatted)
|
|
|
|
}
|
2017-02-07 20:58:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common code of floating point LowerExp and UpperExp.
|
2019-04-19 01:37:12 +02:00
|
|
|
fn float_to_exponential_common<T>(fmt: &mut Formatter<'_>, num: &T, upper: bool) -> Result
|
2019-11-24 01:43:32 -08:00
|
|
|
where
|
|
|
|
T: flt2dec::DecodableFloat,
|
2017-02-07 20:58:34 +11:00
|
|
|
{
|
|
|
|
let force_sign = fmt.sign_plus();
|
|
|
|
let sign = match force_sign {
|
|
|
|
false => flt2dec::Sign::Minus,
|
2019-11-24 01:43:32 -08:00
|
|
|
true => flt2dec::Sign::MinusPlus,
|
2017-02-07 20:58:34 +11:00
|
|
|
};
|
|
|
|
|
2017-04-13 15:49:37 -04:00
|
|
|
if let Some(precision) = fmt.precision {
|
2017-02-07 20:58:34 +11:00
|
|
|
// 1 integral digit + `precision` fractional digits = `precision + 1` total digits
|
2017-04-13 15:49:37 -04:00
|
|
|
float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
|
2017-02-07 20:58:34 +11:00
|
|
|
} else {
|
2017-04-13 15:49:37 -04:00
|
|
|
float_to_exponential_common_shortest(fmt, num, sign, upper)
|
|
|
|
}
|
2017-02-07 20:58:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! floating {
|
2019-11-24 01:43:32 -08:00
|
|
|
($ty:ident) => {
|
2017-02-07 20:58:34 +11:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Debug for $ty {
|
2019-04-19 01:37:12 +02:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
|
2017-12-18 23:16:00 +00:00
|
|
|
float_to_decimal_common(fmt, self, true, 1)
|
2017-02-07 20:58:34 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Display for $ty {
|
2019-04-19 01:37:12 +02:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
|
2017-12-18 23:16:00 +00:00
|
|
|
float_to_decimal_common(fmt, self, false, 0)
|
2017-02-07 20:58:34 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl LowerExp for $ty {
|
2019-04-19 01:37:12 +02:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
|
2017-02-07 20:58:34 +11:00
|
|
|
float_to_exponential_common(fmt, self, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl UpperExp for $ty {
|
2019-04-19 01:37:12 +02:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
|
2017-02-07 20:58:34 +11:00
|
|
|
float_to_exponential_common(fmt, self, true)
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 01:43:32 -08:00
|
|
|
};
|
2017-02-07 20:58:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
floating! { f32 }
|
|
|
|
floating! { f64 }
|