Skip to content

Commit 25b107f

Browse files
committed
Add LowerExp 'e' and UpperExp 'E' format traits/specifiers
1 parent 2b4bd07 commit 25b107f

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

src/libstd/fmt/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ The current mapping of types to traits is:
147147
* `p` ⇒ `Pointer`
148148
* `t` ⇒ `Binary`
149149
* `f` ⇒ `Float`
150+
* `e` ⇒ `LowerExp`
151+
* `E` ⇒ `UpperExp`
150152
* *nothing* ⇒ `Default`
151153
152154
What this means is that any type of argument which implements the
@@ -578,6 +580,12 @@ pub trait Pointer { fn fmt(&Self, &mut Formatter); }
578580
/// Format trait for the `f` character
579581
#[allow(missing_doc)]
580582
pub trait Float { fn fmt(&Self, &mut Formatter); }
583+
/// Format trait for the `e` character
584+
#[allow(missing_doc)]
585+
pub trait LowerExp { fn fmt(&Self, &mut Formatter); }
586+
/// Format trait for the `E` character
587+
#[allow(missing_doc)]
588+
pub trait UpperExp { fn fmt(&Self, &mut Formatter); }
581589

582590
/// The `write` function takes an output stream, a precompiled format string,
583591
/// and a list of arguments. The arguments will be formatted according to the
@@ -1085,6 +1093,28 @@ macro_rules! floating(($ty:ident) => {
10851093
fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
10861094
}
10871095
}
1096+
1097+
impl LowerExp for $ty {
1098+
fn fmt(f: &$ty, fmt: &mut Formatter) {
1099+
// XXX: this shouldn't perform an allocation
1100+
let s = match fmt.precision {
1101+
Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, false),
1102+
None => ::$ty::to_str_exp_digits(f.abs(), 6, false)
1103+
};
1104+
fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
1105+
}
1106+
}
1107+
1108+
impl UpperExp for $ty {
1109+
fn fmt(f: &$ty, fmt: &mut Formatter) {
1110+
// XXX: this shouldn't perform an allocation
1111+
let s = match fmt.precision {
1112+
Some(i) => ::$ty::to_str_exp_exact(f.abs(), i, true),
1113+
None => ::$ty::to_str_exp_digits(f.abs(), 6, true)
1114+
};
1115+
fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
1116+
}
1117+
}
10881118
})
10891119
floating!(f32)
10901120
floating!(f64)

src/libstd/num/f32.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,40 @@ pub fn to_str_digits(num: f32, dig: uint) -> ~str {
783783
r
784784
}
785785

786+
///
787+
/// Converts a float to a string using the exponential notation with exactly the number of
788+
/// provided digits after the decimal point in the significand
789+
///
790+
/// # Arguments
791+
///
792+
/// * num - The float value
793+
/// * digits - The number of digits after the decimal point
794+
/// * upper - Use `E` instead of `e` for the exponent sign
795+
///
796+
#[inline]
797+
pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> ~str {
798+
let (r, _) = strconv::float_to_str_common(
799+
num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
800+
r
801+
}
802+
803+
///
804+
/// Converts a float to a string using the exponential notation with the maximum number of
805+
/// digits after the decimal point in the significand
806+
///
807+
/// # Arguments
808+
///
809+
/// * num - The float value
810+
/// * digits - The number of digits after the decimal point
811+
/// * upper - Use `E` instead of `e` for the exponent sign
812+
///
813+
#[inline]
814+
pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> ~str {
815+
let (r, _) = strconv::float_to_str_common(
816+
num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
817+
r
818+
}
819+
786820
impl to_str::ToStr for f32 {
787821
#[inline]
788822
fn to_str(&self) -> ~str { to_str_digits(*self, 8) }

src/libstd/num/f64.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,40 @@ pub fn to_str_digits(num: f64, dig: uint) -> ~str {
785785
r
786786
}
787787

788+
///
789+
/// Converts a float to a string using the exponential notation with exactly the number of
790+
/// provided digits after the decimal point in the significand
791+
///
792+
/// # Arguments
793+
///
794+
/// * num - The float value
795+
/// * digits - The number of digits after the decimal point
796+
/// * upper - Use `E` instead of `e` for the exponent sign
797+
///
798+
#[inline]
799+
pub fn to_str_exp_exact(num: f64, dig: uint, upper: bool) -> ~str {
800+
let (r, _) = strconv::float_to_str_common(
801+
num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
802+
r
803+
}
804+
805+
///
806+
/// Converts a float to a string using the exponential notation with the maximum number of
807+
/// digits after the decimal point in the significand
808+
///
809+
/// # Arguments
810+
///
811+
/// * num - The float value
812+
/// * digits - The number of digits after the decimal point
813+
/// * upper - Use `E` instead of `e` for the exponent sign
814+
///
815+
#[inline]
816+
pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> ~str {
817+
let (r, _) = strconv::float_to_str_common(
818+
num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
819+
r
820+
}
821+
788822
impl to_str::ToStr for f64 {
789823
#[inline]
790824
fn to_str(&self) -> ~str { to_str_digits(*self, 8) }

src/libsyntax/ext/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ impl<'a> Context<'a> {
687687
"b" => "Bool",
688688
"c" => "Char",
689689
"d" | "i" => "Signed",
690+
"e" => "LowerExp",
691+
"E" => "UpperExp",
690692
"f" => "Float",
691693
"o" => "Octal",
692694
"p" => "Pointer",

0 commit comments

Comments
 (0)