Merge pull request #37 from yangby-cryptape/hash-lowerhex-alternate

Prefix '0x' for lower hex with alternate flag.
This commit is contained in:
Marek Kotewicz 2018-07-05 11:48:19 +02:00 committed by GitHub
commit 05da6f2d0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View File

@ -133,8 +133,7 @@ macro_rules! construct_hash {
impl ::core::fmt::Debug for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "0x")?;
::core::fmt::LowerHex::fmt(self, f)
write!(f, "{:#x}", self)
}
}
@ -154,6 +153,9 @@ macro_rules! construct_hash {
impl ::core::fmt::LowerHex for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
if f.alternate() {
write!(f, "0x");
}
for i in &self.0[..] {
write!(f, "{:02x}", i)?;
}

View File

@ -7,6 +7,7 @@ fn should_format_and_debug_correctly() {
assert_eq!(format!("{}", hash), format!("0x{}", display));
assert_eq!(format!("{:?}", hash), format!("0x{}", hex));
assert_eq!(format!("{:x}", hash), hex);
assert_eq!(format!("{:#x}", hash), format!("0x{}", hex));
};
test(0x1, "00000000000000000000000000000001", "0000…0001");