Skip to content

Commit 36882b3

Browse files
committed
Add f formats to ifmt!
Currently the work just the same as the old `extfmt` versions
1 parent 27b4d10 commit 36882b3

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

src/libstd/fmt/mod.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ The current mapping of types to traits is:
122122
* `s` => String
123123
* `p` => Pointer
124124
* `t` => Binary
125+
* `f` => Float
125126
126127
What this means is that any type of argument which implements the
127128
`std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
@@ -377,6 +378,8 @@ pub trait String { fn fmt(&Self, &mut Formatter); }
377378
pub trait Poly { fn fmt(&Self, &mut Formatter); }
378379
#[allow(missing_doc)]
379380
pub trait Pointer { fn fmt(&Self, &mut Formatter); }
381+
#[allow(missing_doc)]
382+
pub trait Float { fn fmt(&Self, &mut Formatter); }
380383

381384
/// The sprintf function takes a precompiled format string and a list of
382385
/// arguments, to return the resulting formatted string.
@@ -549,7 +552,20 @@ impl<'self> Formatter<'self> {
549552
// Helper methods used for padding and processing formatting arguments that
550553
// all formatting traits can use.
551554

552-
/// TODO: dox
555+
/// Performs the correct padding for an integer which has already been
556+
/// emitted into a byte-array. The byte-array should *not* contain the sign
557+
/// for the integer, that will be added by this method.
558+
///
559+
/// # Arguments
560+
///
561+
/// * s - the byte array that the number has been formatted into
562+
/// * alternate_prefix - if the '#' character (FlagAlternate) is
563+
/// provided, this is the prefix to put in front of the number.
564+
/// Currently this is 0x/0o/0b/etc.
565+
/// * positive - whether the original integer was positive or not.
566+
///
567+
/// This function will correctly account for the flags provided as well as
568+
/// the minimum width. It will not take precision into account.
553569
pub fn pad_integral(&mut self, s: &[u8], alternate_prefix: &str,
554570
positive: bool) {
555571
use fmt::parse::{FlagAlternate, FlagSignPlus, FlagSignAwareZeroPad};
@@ -791,6 +807,22 @@ integer!(i16, u16)
791807
integer!(i32, u32)
792808
integer!(i64, u64)
793809

810+
macro_rules! floating(($ty:ident) => {
811+
impl Float for $ty {
812+
fn fmt(f: &$ty, fmt: &mut Formatter) {
813+
// XXX: this shouldn't perform an allocation
814+
let s = match fmt.precision {
815+
Some(i) => ::$ty::to_str_exact(f.abs(), i),
816+
None => ::$ty::to_str_digits(f.abs(), 6)
817+
};
818+
fmt.pad_integral(s.as_bytes(), "", *f >= 0.0);
819+
}
820+
}
821+
})
822+
floating!(float)
823+
floating!(f32)
824+
floating!(f64)
825+
794826
impl<T> Poly for T {
795827
fn fmt(t: &T, f: &mut Formatter) {
796828
match (f.width, f.precision) {

src/libsyntax/ext/ifmt.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -637,16 +637,17 @@ impl Context {
637637
Known(tyname) => {
638638
let fmt_trait = match tyname.as_slice() {
639639
"?" => "Poly",
640-
"d" | "i" => "Signed",
641-
"u" => "Unsigned",
642640
"b" => "Bool",
643641
"c" => "Char",
642+
"d" | "i" => "Signed",
643+
"f" => "Float",
644644
"o" => "Octal",
645-
"x" => "LowerHex",
646-
"X" => "UpperHex",
647-
"s" => "String",
648645
"p" => "Pointer",
646+
"s" => "String",
649647
"t" => "Binary",
648+
"u" => "Unsigned",
649+
"x" => "LowerHex",
650+
"X" => "UpperHex",
650651
_ => {
651652
self.ecx.span_err(sp, fmt!("unknown format trait \
652653
`%s`", tyname));

src/test/run-pass/ifmt.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,20 @@ pub fn main() {
172172
t!(ifmt!("{:#o}", -1u8), "0o377");
173173

174174
// Signed combinations
175-
t!(ifmt!("{:+5d}", 1), ~" +1");
176-
t!(ifmt!("{:+5d}", -1), ~" -1");
177-
t!(ifmt!("{:05d}", 1), ~"00001");
178-
t!(ifmt!("{:05d}", -1), ~"-0001");
179-
t!(ifmt!("{:+05d}", 1), ~"+0001");
180-
t!(ifmt!("{:+05d}", -1), ~"-0001");
175+
t!(ifmt!("{:+5d}", 1), " +1");
176+
t!(ifmt!("{:+5d}", -1), " -1");
177+
t!(ifmt!("{:05d}", 1), "00001");
178+
t!(ifmt!("{:05d}", -1), "-0001");
179+
t!(ifmt!("{:+05d}", 1), "+0001");
180+
t!(ifmt!("{:+05d}", -1), "-0001");
181+
182+
// Some float stuff
183+
t!(ifmt!("{:f}", 1.0f), "1");
184+
t!(ifmt!("{:f}", 1.0f32), "1");
185+
t!(ifmt!("{:f}", 1.0f64), "1");
186+
t!(ifmt!("{:.3f}", 1.0f), "1.000");
187+
t!(ifmt!("{:10.3f}", 1.0f), " 1.000");
188+
t!(ifmt!("{:+10.3f}", 1.0f), " +1.000");
189+
t!(ifmt!("{:+10.3f}", -1.0f), " -1.000");
181190
}
182191

0 commit comments

Comments
 (0)