1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
15 // Don't inline this so callers don't use the stack space this function
16 // requires unless they have to.
18 fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
19 sign: flt2dec::Sign, precision: usize) -> Result
20 where T: flt2dec::DecodableFloat
23 let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
24 let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
25 let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
26 *num, sign, precision,
27 false, &mut buf, &mut parts);
28 fmt.pad_formatted_parts(&formatted)
32 // Don't inline this so callers that call both this and the above won't wind
33 // up using the combined stack space of both functions in some cases.
35 fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
36 num: &T, sign: flt2dec::Sign) -> Result
37 where T: flt2dec::DecodableFloat
40 // enough for f32 and f64
41 let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
42 let mut parts: [flt2dec::Part; 5] = mem::uninitialized();
43 let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
44 *num, sign, 0, false, &mut buf, &mut parts);
45 fmt.pad_formatted_parts(&formatted)
49 // Common code of floating point Debug and Display.
50 fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
51 where T: flt2dec::DecodableFloat
53 let force_sign = fmt.sign_plus();
54 let sign = match (force_sign, negative_zero) {
55 (false, false) => flt2dec::Sign::Minus,
56 (false, true) => flt2dec::Sign::MinusRaw,
57 (true, false) => flt2dec::Sign::MinusPlus,
58 (true, true) => flt2dec::Sign::MinusPlusRaw,
61 if let Some(precision) = fmt.precision {
62 float_to_decimal_common_exact(fmt, num, sign, precision)
64 float_to_decimal_common_shortest(fmt, num, sign)
68 // Don't inline this so callers don't use the stack space this function
69 // requires unless they have to.
71 fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
72 sign: flt2dec::Sign, precision: usize,
73 upper: bool) -> Result
74 where T: flt2dec::DecodableFloat
77 let mut buf: [u8; 1024] = mem::uninitialized(); // enough for f32 and f64
78 let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
79 let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
80 *num, sign, precision,
81 upper, &mut buf, &mut parts);
82 fmt.pad_formatted_parts(&formatted)
86 // Don't inline this so callers that call both this and the above won't wind
87 // up using the combined stack space of both functions in some cases.
89 fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
90 num: &T, sign: flt2dec::Sign,
91 upper: bool) -> Result
92 where T: flt2dec::DecodableFloat
95 // enough for f32 and f64
96 let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
97 let mut parts: [flt2dec::Part; 7] = mem::uninitialized();
98 let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
99 *num, sign, (0, 0), upper,
100 &mut buf, &mut parts);
101 fmt.pad_formatted_parts(&formatted)
105 // Common code of floating point LowerExp and UpperExp.
106 fn float_to_exponential_common<T>(fmt: &mut Formatter, num: &T, upper: bool) -> Result
107 where T: flt2dec::DecodableFloat
109 let force_sign = fmt.sign_plus();
110 let sign = match force_sign {
111 false => flt2dec::Sign::Minus,
112 true => flt2dec::Sign::MinusPlus,
115 if let Some(precision) = fmt.precision {
116 // 1 integral digit + `precision` fractional digits = `precision + 1` total digits
117 float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
119 float_to_exponential_common_shortest(fmt, num, sign, upper)
123 macro_rules! floating {
125 #[stable(feature = "rust1", since = "1.0.0")]
127 fn fmt(&self, fmt: &mut Formatter) -> Result {
128 float_to_decimal_common(fmt, self, true)
132 #[stable(feature = "rust1", since = "1.0.0")]
133 impl Display for $ty {
134 fn fmt(&self, fmt: &mut Formatter) -> Result {
135 float_to_decimal_common(fmt, self, false)
139 #[stable(feature = "rust1", since = "1.0.0")]
140 impl LowerExp for $ty {
141 fn fmt(&self, fmt: &mut Formatter) -> Result {
142 float_to_exponential_common(fmt, self, false)
146 #[stable(feature = "rust1", since = "1.0.0")]
147 impl UpperExp for $ty {
148 fn fmt(&self, fmt: &mut Formatter) -> Result {
149 float_to_exponential_common(fmt, self, true)