Skip to content

Commit 6c88a8c

Browse files
committed
value: write out bitstrings with len > 4 as hex rather than binary
1 parent d2da3a0 commit 6c88a8c

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src/value.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,25 @@ impl fmt::Display for Value {
786786
// First, write any bitstrings out
787787
for tmr in &Tmr::TWO_TWO_N {
788788
if value.ty.tmr() == *tmr {
789-
for bit in value.iter_padded() {
790-
f.write_str(if bit { "1" } else { "0" })?;
789+
if value.ty.bit_width() < 4 {
790+
f.write_str("0b")?;
791+
for bit in value.iter_padded() {
792+
f.write_str(if bit { "1" } else { "0" })?;
793+
}
794+
} else {
795+
f.write_str("0x")?;
796+
// Annoyingly `array_chunks` is unstable so we have to do it manually
797+
// https://github.com/rust-lang/rust/issues/100450
798+
let mut iter = value.iter_padded();
799+
while let (Some(a), Some(b), Some(c), Some(d)) =
800+
(iter.next(), iter.next(), iter.next(), iter.next())
801+
{
802+
let n = (u8::from(a) << 3)
803+
+ (u8::from(b) << 2)
804+
+ (u8::from(c) << 1)
805+
+ u8::from(d);
806+
write!(f, "{:x}", n)?;
807+
}
791808
}
792809
continue 'main_loop;
793810
}
@@ -1106,9 +1123,9 @@ mod tests {
11061123
fn value_display() {
11071124
// Only test a couple values becasue we probably want to change this
11081125
// at some point and will have to redo this test.
1109-
assert_eq!(Value::u1(0).to_string(), "0",);
1110-
assert_eq!(Value::u1(1).to_string(), "1",);
1111-
assert_eq!(Value::u4(6).to_string(), "0110",);
1126+
assert_eq!(Value::u1(0).to_string(), "0b0",);
1127+
assert_eq!(Value::u1(1).to_string(), "0b1",);
1128+
assert_eq!(Value::u4(6).to_string(), "0x6",);
11121129
}
11131130

11141131
#[test]

0 commit comments

Comments
 (0)