Merge pull request #57 from gavofyork/uint_decimal_display

uint fmt debug/display changed to output decimal representation
This commit is contained in:
Gav Wood 2016-01-16 13:33:41 +01:00
commit 1d06c05a9f

View File

@ -595,16 +595,30 @@ macro_rules! construct_uint {
impl fmt::Debug for $name { impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &$name(ref data) = self; fmt::Display::fmt(self, f)
try!(write!(f, "0x"));
for ch in data.iter().rev() {
try!(write!(f, "{:02x}", ch));
}
Ok(())
} }
} }
impl fmt::Display for $name { 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &$name(ref data) = self; let &$name(ref data) = self;
try!(write!(f, "0x")); 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("10").unwrap(), U256::from(10u64));
assert_eq!(U256::from_dec_str("1024").unwrap(), U256::from(1024u64)); 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");
}
} }