diff options
author | iximeow <me@iximeow.net> | 2019-03-29 18:53:46 -0700 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2020-01-12 16:26:39 -0800 |
commit | 0b7536284cbae6e085eeeda602512449a42ca22a (patch) | |
tree | d43ca192e64a13398253a7c5941019fe53947e88 | |
parent | c960b06863024087437644524894f247e8a4b448 (diff) |
add some display logic into arch
-rw-r--r-- | src/display.rs | 115 | ||||
-rw-r--r-- | src/lib.rs | 1 |
2 files changed, 116 insertions, 0 deletions
diff --git a/src/display.rs b/src/display.rs new file mode 100644 index 0000000..7dcdfb5 --- /dev/null +++ b/src/display.rs @@ -0,0 +1,115 @@ +pub enum NumberStyleHint { + Signed, + HexSigned, + SignedWithSign, + HexSignedWithSign, + SignedWithSignSplit, + HexSignedWithSignSplit, + Unsigned, + HexUnsigned, + UnsignedWithSign, + HexUnsignedWithSign +} + +pub fn format_number_i32(i: i32, hint: NumberStyleHint) -> String { + match hint { + NumberStyleHint::Signed => { + format!("{}", i) + }, + NumberStyleHint::HexSigned => { + if i == std::i32::MIN { + format!("-0x7fffffff") + } else if i < 0 { + format!("-{:#x}", -i) + } else { + format!("{:#x}", i) + } + }, + NumberStyleHint::Unsigned => { + format!("{}", i as u32) + }, + NumberStyleHint::HexUnsigned => { + format!("{:#x}", i as u32) + }, + NumberStyleHint::SignedWithSignSplit => { + if i == std::i32::MIN { + format!("- 2147483647") + } else if i < 0 { + format!("- {}", -i) + } else { + format!("+ {}", i) + } + } + NumberStyleHint::HexSignedWithSignSplit => { + if i == std::i32::MIN { + format!("- 0x7fffffff") + } else if i < 0 { + format!("- {:#x}", -i) + } else { + format!("+ {:#x}", i) + } + }, + NumberStyleHint::HexSignedWithSign => { + if i == std::i32::MIN { + format!("-0x7fffffff") + } else if i < 0 { + format!("-{:#x}", -i) + } else { + format!("+{:#x}", i) + } + }, + NumberStyleHint::SignedWithSign => { + format!("{:+}", i) + } + NumberStyleHint::HexUnsignedWithSign => { + format!("{:+#x}", i as u32) + }, + NumberStyleHint::UnsignedWithSign => { + format!("{:+}", i as u32) + } + } +} + +pub fn signed_i8_hex(imm: i8) -> String { + let (sign, imm) = if imm == std::i8::MIN { + (false, imm) + } else if imm < 0 { + (true, -imm) + } else { + (false, imm) + }; + format!("{}{:#x}", sign, imm) +} + +pub fn signed_i16_hex(imm: i16) -> String { + let (sign, imm) = if imm == std::i16::MIN { + (false, imm) + } else if imm < 0 { + (true, -imm) + } else { + (false, imm) + }; + format!("{}{:#x}", sign, imm) +} + +pub fn signed_i32_hex(imm: i32) -> String { + let (sign, imm) = if imm == std::i32::MIN { + (false, imm) + } else if imm < 0 { + (true, -imm) + } else { + (false, imm) + }; + format!("{}{:#x}", sign, imm) +} + +pub fn signed_i64_hex(imm: i64) -> String { + let (sign, imm) = if imm == std::i64::MIN { + (false, imm) + } else if imm < 0 { + (true, -imm) + } else { + (false, imm) + }; + format!("{}{:#x}", sign, imm) +} @@ -14,6 +14,7 @@ use num_traits::{Bounded, WrappingAdd, WrappingSub, CheckedAdd, CheckedSub}; use termion::color; +pub mod display; // This is pretty wonk.. pub trait AddressDisplay { fn stringy(&self) -> String; |