Skip to content

Commit 711230f

Browse files
committed
Fixes the Squeezing bug where identical lines that are not a single repeating byte were not squeezed (see sharkdp#186)
1 parent d8a26da commit 711230f

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

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

99
## Other
1010

src/lib.rs

+29-30
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub struct Printer<'a, Writer: Write> {
281281
display_offset: u64,
282282
/// The number of panels to draw.
283283
panels: u64,
284-
squeeze_byte: usize,
284+
last_line: Vec<u8>,
285285
/// The number of octets per group.
286286
group_size: u8,
287287
/// The number of digits used to write the base.
@@ -332,7 +332,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
332332
},
333333
display_offset: 0,
334334
panels,
335-
squeeze_byte: 0x00,
335+
last_line: vec![],
336336
group_size,
337337
base_digits: match base {
338338
Base::Binary => 8,
@@ -631,21 +631,18 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
631631
if is_empty {
632632
self.print_header()?;
633633
}
634-
635-
// squeeze is active, check if the line is the same
636-
// skip print if still squeezed, otherwise print and deactivate squeeze
637-
if matches!(self.squeezer, Squeezer::Print | Squeezer::Delete) {
638-
if self
639-
.line_buf
640-
.chunks_exact(std::mem::size_of::<usize>())
641-
.all(|w| usize::from_ne_bytes(w.try_into().unwrap()) == self.squeeze_byte)
642-
{
643-
if self.squeezer == Squeezer::Delete {
644-
self.idx += 8 * self.panels;
645-
continue;
646-
}
647-
} else {
648-
self.squeezer = Squeezer::Ignore;
634+
635+
if self.line_buf == self.last_line {
636+
match self.squeezer {
637+
Squeezer::Delete => {self.idx += 8 * self.panels;
638+
continue;}
639+
Squeezer::Ignore => self.squeezer = Squeezer::Print,
640+
Squeezer::Print | Squeezer::Disabled => (),
641+
}
642+
}else{
643+
match self.squeezer {
644+
Squeezer::Delete | Squeezer::Print => self.squeezer = Squeezer::Ignore,
645+
Squeezer::Ignore | Squeezer::Disabled => (),
649646
}
650647
}
651648

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

673-
// repeat the first byte in the line until it's a usize
674-
// compare that usize with each usize chunk in the line
675-
// if they are all the same, change squeezer to print
676-
let repeat_byte = (self.line_buf[0] as usize) * (usize::MAX / 255);
677-
if !matches!(self.squeezer, Squeezer::Disabled | Squeezer::Delete)
678-
&& self
679-
.line_buf
680-
.chunks_exact(std::mem::size_of::<usize>())
681-
.all(|w| usize::from_ne_bytes(w.try_into().unwrap()) == repeat_byte)
682-
{
683-
self.squeezer = Squeezer::Print;
684-
self.squeeze_byte = repeat_byte;
685-
};
670+
self.last_line = self.line_buf.clone();
686671
};
687672

688673
// special ending
@@ -867,6 +852,20 @@ mod tests {
867852
assert_print_all_output(input, expected_string);
868853
}
869854

855+
#[test]
856+
fn squeeze_non_repeating() {
857+
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");
858+
let expected_string = "\
859+
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
860+
│00000000│ 00 01 00 01 00 01 00 01 ┊ 00 01 00 01 00 01 00 01 │⋄•⋄•⋄•⋄•┊⋄•⋄•⋄•⋄•│
861+
│* │ ┊ │ ┊ │
862+
│00000020│ 00 ┊ │⋄ ┊ │
863+
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
864+
"
865+
.to_owned();
866+
assert_print_all_output(input, expected_string);
867+
}
868+
870869
#[test]
871870
fn squeeze_nonzero() {
872871
let input = io::Cursor::new(b"000000000000000000000000000000000");

0 commit comments

Comments
 (0)