Skip to content

Commit

Permalink
Merge remote-tracking branch 'Mododo/bitstring_tail'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 17, 2023
2 parents ab6aefa + 562d562 commit afb7270
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ impl std::fmt::Display for Bitstring<'_> {

if let Some(mut last_byte) = last_byte {
let tag = if rem % 4 != 0 { "_" } else { "" };
if rem <= 4 {
let rem = 1 + (rem >> 2) as usize;
if rem == 1 {
last_byte >>= 4;
}
ok!(write!(f, "{last_byte:x}{tag}"));
ok!(write!(f, "{last_byte:0rem$x}{tag}"));
}

Ok(())
Expand Down Expand Up @@ -290,3 +291,52 @@ pub(crate) fn debug_struct_field2_finish(
builder.field(name2, value2);
builder.finish()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bitstring_zero_char_with_completion_tag() {
assert_eq!(
format!(
"{}",
Bitstring {
bytes: &[0b_0100_0000],
bit_len: 2
}
),
format!("{:x}_", 0b_0110)
);
assert_eq!(
format!(
"{}",
Bitstring {
bytes: &[0b_0000_1000],
bit_len: 5
}
),
format!("{:x}{:x}_", 0b_0000, 0b_1100)
);
assert_eq!(
format!(
"{}",
Bitstring {
bytes: &[0b_0000_1000, 0b_0100_0000],
bit_len: 8 + 2
}
),
format!("{:x}{:x}{:x}_", 0b_0000, 0b_1000, 0b_0110)
);
assert_eq!(
format!(
"{}",
Bitstring {
bytes: &[0b_0100_0000, 0b_0000_1000],
bit_len: 8 + 5
}
),
format!("{:x}{:x}{:x}{:x}_", 0b_0100, 0b_0000, 0b_0000, 0b_1100)
);
}
}

0 comments on commit afb7270

Please sign in to comment.