Skip to content

Commit

Permalink
Fixes the Squeezing bug where identical lines that are not a single r…
Browse files Browse the repository at this point in the history
…epeating byte were not squeezed (see #186)
  • Loading branch information
Taraxtix committed Mar 3, 2024
1 parent d8a26da commit 711230f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


## Bugfixes

- Now also squeezes identical lines that are not a single repeating byte, see #186 (@Taraxtix)

## Other

Expand Down
59 changes: 29 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub struct Printer<'a, Writer: Write> {
display_offset: u64,
/// The number of panels to draw.
panels: u64,
squeeze_byte: usize,
last_line: Vec<u8>,
/// The number of octets per group.
group_size: u8,
/// The number of digits used to write the base.
Expand Down Expand Up @@ -332,7 +332,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
},
display_offset: 0,
panels,
squeeze_byte: 0x00,
last_line: vec![],
group_size,
base_digits: match base {
Base::Binary => 8,
Expand Down Expand Up @@ -631,21 +631,18 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
if is_empty {
self.print_header()?;
}

// squeeze is active, check if the line is the same
// skip print if still squeezed, otherwise print and deactivate squeeze
if matches!(self.squeezer, Squeezer::Print | Squeezer::Delete) {
if self
.line_buf
.chunks_exact(std::mem::size_of::<usize>())
.all(|w| usize::from_ne_bytes(w.try_into().unwrap()) == self.squeeze_byte)
{
if self.squeezer == Squeezer::Delete {
self.idx += 8 * self.panels;
continue;
}
} else {
self.squeezer = Squeezer::Ignore;

if self.line_buf == self.last_line {
match self.squeezer {
Squeezer::Delete => {self.idx += 8 * self.panels;
continue;}
Squeezer::Ignore => self.squeezer = Squeezer::Print,
Squeezer::Print | Squeezer::Disabled => (),
}
}else{
match self.squeezer {
Squeezer::Delete | Squeezer::Print => self.squeezer = Squeezer::Ignore,
Squeezer::Ignore | Squeezer::Disabled => (),
}
}

Expand All @@ -670,19 +667,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
self.squeezer = Squeezer::Delete;
}

// repeat the first byte in the line until it's a usize
// compare that usize with each usize chunk in the line
// if they are all the same, change squeezer to print
let repeat_byte = (self.line_buf[0] as usize) * (usize::MAX / 255);
if !matches!(self.squeezer, Squeezer::Disabled | Squeezer::Delete)
&& self
.line_buf
.chunks_exact(std::mem::size_of::<usize>())
.all(|w| usize::from_ne_bytes(w.try_into().unwrap()) == repeat_byte)
{
self.squeezer = Squeezer::Print;
self.squeeze_byte = repeat_byte;
};
self.last_line = self.line_buf.clone();
};

// special ending
Expand Down Expand Up @@ -867,6 +852,20 @@ mod tests {
assert_print_all_output(input, expected_string);
}

#[test]
fn squeeze_non_repeating() {
let input = io::Cursor::new(b"\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00");
let expected_string = "\
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 00 01 00 01 00 01 00 01 ┊ 00 01 00 01 00 01 00 01 │⋄•⋄•⋄•⋄•┊⋄•⋄•⋄•⋄•│
│* │ ┊ │ ┊ │
│00000020│ 00 ┊ │⋄ ┊ │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
"
.to_owned();
assert_print_all_output(input, expected_string);
}

#[test]
fn squeeze_nonzero() {
let input = io::Cursor::new(b"000000000000000000000000000000000");
Expand Down

0 comments on commit 711230f

Please sign in to comment.