japaric / ufmt

a smaller, faster and panic-free alternative to core::fmt
Apache License 2.0
352 stars 40 forks source link

Added debug_hex feature. #26

Closed hnj2 closed 2 years ago

hnj2 commented 3 years ago

This feature will cause the uDebug implementation of for integer primitives to print hexadecimal representations of those integers.

uwriteln!(s, "{}", 0xdeadbeef_u32)    // prints: 3735928559
uwriteln!(s, "{:?}", 0xdeadbeef_u32)  // prints: deadbeef
uwriteln!(s, "{:#?}", 0xdeadbeef_u32) // prints: 0xdeadbeef

uwriteln!(s, "{}", 0xdeadbeef_u32 as i32)    // prints: -559038737
uwriteln!(s, "{:?}", 0xdeadbeef_u32 as i32)  // prints: deadbeef
uwriteln!(s, "{:#?}", 0xdeadbeef_u32 as i32) // prints: 0xdeadbeef

Advantages:

In various cases this can be useful:

Drawbacks:

Alternatives:

mutantbob commented 2 years ago

It would be better if hex encoding were triggered by the {:x} format string rather than replacing {} . https://doc.rust-lang.org/std/fmt/#formatting-traits

mutantbob commented 2 years ago

https://github.com/japaric/ufmt/pull/35 adds support for {:x}

japaric commented 2 years ago

thanks for the PR. I agree with mutantbob and prefer the alternative of having this on a separate trait rather than as a Cargo feature as that allows one to use both decimal and hexadecimal formatting in the crate based on the format string.