@@ -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
126127What 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); }
377378pub trait Poly { fn fmt ( & Self , & mut Formatter ) ; }
378379#[ allow( missing_doc) ]
379380pub 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)
791807integer ! ( i32 , u32 )
792808integer ! ( 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+
794826impl < T > Poly for T {
795827 fn fmt ( t : & T , f : & mut Formatter ) {
796828 match ( f. width , f. precision ) {
0 commit comments