From d41ad82aa93853744d0f3b115f350b42cf6b1230 Mon Sep 17 00:00:00 2001 From: debris Date: Sat, 16 Jan 2016 00:38:41 +0100 Subject: [PATCH] uint fmt debug/display is changed to output decimal representation --- src/uint.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/uint.rs b/src/uint.rs index 038c85458..32cbd2cf1 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -595,16 +595,30 @@ macro_rules! construct_uint { impl fmt::Debug for $name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let &$name(ref data) = self; - try!(write!(f, "0x")); - for ch in data.iter().rev() { - try!(write!(f, "{:02x}", ch)); - } - Ok(()) + fmt::Display::fmt(self, f) } } impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if *self == $name::zero() { + return write!(f, "0"); + } + + let mut s = String::new(); + let mut current = *self; + let ten = $name::from(10); + + while current != $name::zero() { + s = format!("{}{}", (current % ten).low_u32(), s); + current = current / ten; + } + + write!(f, "{}", s) + } + } + + impl fmt::LowerHex for $name { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let &$name(ref data) = self; try!(write!(f, "0x")); @@ -1052,5 +1066,16 @@ mod tests { assert_eq!(U256::from_dec_str("10").unwrap(), U256::from(10u64)); assert_eq!(U256::from_dec_str("1024").unwrap(), U256::from(1024u64)); } + + #[test] + fn display_uint() { + let s = "12345678987654321023456789"; + assert_eq!(format!("{}", U256::from_dec_str(s).unwrap()), s); + } + + #[test] + fn display_uint_zero() { + assert_eq!(format!("{}", U256::from(0)), "0"); + } }