Skip to content

Commit d2da3a0

Browse files
committed
value: write out power-of-two length bitstrings compactly in Display
1 parent 1ca2498 commit d2da3a0

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

src/value.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use crate::dag::{Dag, DagLike};
99
use crate::types::{CompleteBound, Final};
10-
use crate::BitIter;
10+
use crate::{BitIter, Tmr};
1111

1212
use crate::{BitCollector, EarlyEndOfStreamError};
1313
use core::{cmp, fmt, iter};
@@ -769,7 +769,7 @@ impl fmt::Display for Value {
769769
// a unit is displayed simply as 0 or 1.
770770
stack.push(S::Disp(self.as_ref()));
771771

772-
while let Some(next) = stack.pop() {
772+
'main_loop: while let Some(next) = stack.pop() {
773773
let value = match next {
774774
S::Disp(ref value) | S::DispUnlessUnit(ref value) => value,
775775
S::DispCh(ch) => {
@@ -782,20 +782,33 @@ impl fmt::Display for Value {
782782
if !matches!(next, S::DispUnlessUnit(..)) {
783783
f.write_str("ε")?;
784784
}
785-
} else if let Some(l_value) = value.as_left() {
786-
f.write_str("0")?;
787-
stack.push(S::DispUnlessUnit(l_value));
788-
} else if let Some(r_value) = value.as_right() {
789-
f.write_str("1")?;
790-
stack.push(S::DispUnlessUnit(r_value));
791-
} else if let Some((l_value, r_value)) = value.as_product() {
792-
stack.push(S::DispCh(')'));
793-
stack.push(S::Disp(r_value));
794-
stack.push(S::DispCh(','));
795-
stack.push(S::Disp(l_value));
796-
stack.push(S::DispCh('('));
797785
} else {
798-
unreachable!()
786+
// First, write any bitstrings out
787+
for tmr in &Tmr::TWO_TWO_N {
788+
if value.ty.tmr() == *tmr {
789+
for bit in value.iter_padded() {
790+
f.write_str(if bit { "1" } else { "0" })?;
791+
}
792+
continue 'main_loop;
793+
}
794+
}
795+
796+
// If we don't have a bitstring, then write out the explicit value.
797+
if let Some(l_value) = value.as_left() {
798+
f.write_str("0")?;
799+
stack.push(S::DispUnlessUnit(l_value));
800+
} else if let Some(r_value) = value.as_right() {
801+
f.write_str("1")?;
802+
stack.push(S::DispUnlessUnit(r_value));
803+
} else if let Some((l_value, r_value)) = value.as_product() {
804+
stack.push(S::DispCh(')'));
805+
stack.push(S::Disp(r_value));
806+
stack.push(S::DispCh(','));
807+
stack.push(S::Disp(l_value));
808+
stack.push(S::DispCh('('));
809+
} else {
810+
unreachable!()
811+
}
799812
}
800813
}
801814
Ok(())
@@ -1095,7 +1108,7 @@ mod tests {
10951108
// at some point and will have to redo this test.
10961109
assert_eq!(Value::u1(0).to_string(), "0",);
10971110
assert_eq!(Value::u1(1).to_string(), "1",);
1098-
assert_eq!(Value::u4(6).to_string(), "((0,1),(1,0))",);
1111+
assert_eq!(Value::u4(6).to_string(), "0110",);
10991112
}
11001113

11011114
#[test]

0 commit comments

Comments
 (0)