Created
December 7, 2019 21:36
-
-
Save thomcc/8b308c30ddc35eb07646b4bb2ade480f to your computer and use it in GitHub Desktop.
init.evcxr hex/binary printing "prelude"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// note: in my actual init.evcxr, this is all on one line, but I've rustfmted it for convenience | |
// the basic idea is that i just can call `some_expr_returning_integer.hex()` or | |
// `some_expr_returning_integer.bin()` to get a pretty-printed hex or binary version. zeroes are | |
// filled in, and every 4 or 8 (for hex/binary respectively) digits has a `_` separator. | |
// It works on anything that is Into<i128>. for things that aren't into<i128> but that | |
// i find myself needing to format, (specifically u128, f32, and f64), you need | |
// to do .hexx() or .binx(). this could be fixed but this file is a pain to maintain (it's one line) | |
// so... yeah. | |
// ``` | |
// >> 30.hex() | |
// 0x0000_001e | |
// >> 30.bin() | |
// 0b00000000_00000000_00000000_00011110 | |
// >> 30u8.hex() | |
// 0x001e | |
// >> i128::max_value().hex() | |
// 0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffff | |
// >> u128::max_value().hexx() | |
// 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff | |
// >> 1.0f32.hexx() | |
// 0x3f80_0000 | |
// >> 1.0f64.hexx() | |
// 0x3ff0_0000_0000_0000 | |
// >> 1.0f32.binx() | |
// 0b00111111_10000000_00000000_00000000 | |
// >> (-30).hex() | |
// 0xffff_ffe2 | |
// ``` | |
pub trait EvcxrNumFmtHelp { | |
fn hex(self) -> HexF; | |
fn bin(self) -> BinF; | |
} | |
impl<I: Into<i128>> EvcxrNumFmtHelp for I { | |
fn hex(self) -> HexF { | |
HexF(self.into() as u128, core::mem::size_of::<I>()) | |
} | |
fn bin(self) -> BinF { | |
BinF(self.into() as u128, core::mem::size_of::<I>()) | |
} | |
} | |
pub trait EvcxrNumFmtHelpHack { | |
fn hexx(self) -> HexF; | |
fn binx(self) -> BinF; | |
} | |
impl EvcxrNumFmtHelpHack for u128 { | |
fn hexx(self) -> HexF { | |
HexF(self, 16) | |
} | |
fn binx(self) -> BinF { | |
BinF(self, 16) | |
} | |
} | |
impl EvcxrNumFmtHelpHack for f64 { | |
fn hexx(self) -> HexF { | |
HexF(self.to_bits() as u128, 8) | |
} | |
fn binx(self) -> BinF { | |
BinF(self.to_bits() as u128, 8) | |
} | |
} | |
impl EvcxrNumFmtHelpHack for f32 { | |
fn hexx(self) -> HexF { | |
HexF(self.to_bits() as u128, 4) | |
} | |
fn binx(self) -> BinF { | |
BinF(self.to_bits() as u128, 4) | |
} | |
} | |
pub struct BinF(pub u128, pub usize); | |
impl core::fmt::Debug for BinF { | |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | |
f.write_str("0b")?; | |
for i in (0..self.1).rev() { | |
if i != self.1 - 1 { | |
f.write_str("_")?; | |
} | |
write!(f, "{:08b}", ((self.0 >> (i * 8)) & 0xff))?; | |
} | |
Ok(()) | |
} | |
} | |
pub struct HexF(pub u128, pub usize); | |
impl core::fmt::Debug for HexF { | |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | |
f.write_str("0x")?; | |
let fields = (self.1 + 1) / 2; | |
for i in (0..fields).rev() { | |
if i != fields - 1 { | |
f.write_str("_")?; | |
} | |
write!(f, "{:04x}", ((self.0 >> (i * 16)) & 0xffff))?; | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment