| 1 | macro_rules! fmtint { |
| 2 | ($($t:ident)*) => ($( |
| 3 | #[allow(unused_comparisons)] |
| 4 | impl<'a, 'b> Formatter<'a, 'b> { |
| 5 | pub fn $t(&mut self, x: $t) -> Result<()> { |
| 6 | self.set_default_align(Alignment::Right); |
| 7 | |
| 8 | let ty = match self.ty() { |
| 9 | None => ' ', |
| 10 | Some(c) => c, |
| 11 | }; |
| 12 | |
| 13 | if !self.is_int_type() { |
| 14 | let mut msg = String::new(); |
| 15 | write!(msg, "Unknown format code {:?} for type", ty).unwrap(); |
| 16 | return Err(FmtError::TypeError(msg)); |
| 17 | } |
| 18 | |
| 19 | if self.precision() != None { |
| 20 | return Err(FmtError::TypeError("precision not allowed for integers".to_string())); |
| 21 | } |
| 22 | |
| 23 | if self.thousands() { |
| 24 | return Err(FmtError::Invalid("thousands specifier not yet supported".to_string())); |
| 25 | } |
| 26 | |
| 27 | if self.fill() == '0' && self.align() == Alignment::Right { |
| 28 | return Err(FmtError::Invalid("sign aware 0 padding not yet supported".to_string())); |
| 29 | } |
| 30 | |
| 31 | let mut s = String::new(); |
| 32 | |
| 33 | if x >= 0 && self.sign_plus() { |
| 34 | self.write_str("+").unwrap(); |
| 35 | } |
| 36 | |
| 37 | if self.alternate() { |
| 38 | match ty { |
| 39 | 'b' => self.write_str("0b").unwrap(), |
| 40 | 'o' => self.write_str("0o").unwrap(), |
| 41 | 'x' | 'X' => self.write_str("0x").unwrap(), |
| 42 | _ => { |
| 43 | let mut msg = String::new(); |
| 44 | write!(msg, "alternate ('#') cannot be used with type {:?}", ty).unwrap(); |
| 45 | return Err(FmtError::Invalid(msg)); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | match ty { |
| 51 | ' ' => write!(s, "{}", x).unwrap(), |
| 52 | 'b' => write!(s, "{:b}", x).unwrap(), |
| 53 | 'o' => write!(s, "{:o}", x).unwrap(), |
| 54 | 'x' => write!(s, "{:x}", x).unwrap(), |
| 55 | 'X' => write!(s, "{:X}", x).unwrap(), |
| 56 | _ => unreachable!(), |
| 57 | } |
| 58 | |
| 59 | self.str_unchecked(s.as_str()) |
| 60 | } |
| 61 | })*) |
| 62 | } |
| 63 | |
| 64 | macro_rules! fmtfloat { |
| 65 | ($($t:ident)*) => ($( |
| 66 | impl<'a, 'b> Formatter<'a, 'b> { |
| 67 | pub fn $t(&mut self, x: $t) -> Result<()> { |
| 68 | self.set_default_align(Alignment::Right); |
| 69 | |
| 70 | let ty = match self.ty() { |
| 71 | None => 'f', |
| 72 | Some(c) => c, |
| 73 | }; |
| 74 | |
| 75 | if !self.is_float_type() { |
| 76 | let mut msg = String::new(); |
| 77 | write!(msg, "Unknown format code {:?} for type", ty).unwrap(); |
| 78 | return Err(FmtError::TypeError(msg)); |
| 79 | } |
| 80 | |
| 81 | if self.alternate() { |
| 82 | return Err(FmtError::TypeError("Alternate form (#) not allowed for floats".to_string())); |
| 83 | } |
| 84 | |
| 85 | if self.thousands() { |
| 86 | return Err(FmtError::Invalid("thousands specifier not yet supported".to_string())); |
| 87 | } |
| 88 | |
| 89 | if self.fill() == '0' && self.align() == Alignment::Right { |
| 90 | return Err(FmtError::Invalid("sign aware 0 padding not yet supported".to_string())); |
| 91 | } |
| 92 | |
| 93 | let mut s = String::new(); |
| 94 | |
| 95 | if x >= (0 as $t) && self.sign_plus() { |
| 96 | self.write_str("+").unwrap(); |
| 97 | } |
| 98 | |
| 99 | match self.precision() { |
| 100 | None => { |
| 101 | match ty { |
| 102 | 'f' => write!(s, "{}", x).unwrap(), |
| 103 | 'e' => write!(s, "{:e}", x).unwrap(), |
| 104 | 'E' => write!(s, "{:E}", x).unwrap(), |
| 105 | _ => unreachable!(), |
| 106 | } |
| 107 | } |
| 108 | Some(p) => { |
| 109 | match ty { |
| 110 | 'f' => write!(s, "{:.*}", p, x).unwrap(), |
| 111 | 'e' => write!(s, "{:.*e}", p, x).unwrap(), |
| 112 | 'E' => write!(s, "{:.*E}", p, x).unwrap(), |
| 113 | _ => unreachable!(), |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | let prev_prec = self.precision(); |
| 119 | self.set_precision(None); |
| 120 | let out = self.str_unchecked(s.as_str()); |
| 121 | self.set_precision(prev_prec); |
| 122 | out |
| 123 | } |
| 124 | })*) |
| 125 | } |