Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore newlines when writing Bstrs #161

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 84 additions & 17 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,24 +353,91 @@ impl fmt::Display for DebugBuffer {
}
}

fn format_bytes(data: &[u8], f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn format_bytes(data: &[u8], f: &mut impl fmt::Write) -> fmt::Result {
#![allow(clippy::assertions_on_constants)]
const MIN_OVERFLOW: usize = 8192;
const MAX_START: usize = 2048;
const MAX_END: usize = 2048;
const MAX_PRINTED: usize = MAX_START + MAX_END;
assert!(MAX_PRINTED < MIN_OVERFLOW);

if data.len() >= MIN_OVERFLOW {
write!(
f,
"<{} bytes total>{:?}...<{} bytes omitted>...{:?}",
data.len(),
data[..MAX_START].as_bstr(),
data.len() - MAX_PRINTED,
data[data.len() - MAX_END..].as_bstr(),
)

const LINES_MIN_OVERFLOW: usize = 40;
const LINES_MAX_START: usize = 10;
const LINES_MAX_END: usize = 10;
const LINES_MAX_PRINTED: usize = LINES_MAX_START + LINES_MAX_END;
epage marked this conversation as resolved.
Show resolved Hide resolved
assert!(LINES_MAX_PRINTED < LINES_MIN_OVERFLOW);

const BYTES_MIN_OVERFLOW: usize = 8192;
const BYTES_MAX_START: usize = 2048;
const BYTES_MAX_END: usize = 2048;
const BYTES_MAX_PRINTED: usize = BYTES_MAX_START + BYTES_MAX_END;
assert!(BYTES_MAX_PRINTED < BYTES_MIN_OVERFLOW);

let lines_total = data.as_bstr().lines().count();

if lines_total >= LINES_MIN_OVERFLOW {
epage marked this conversation as resolved.
Show resolved Hide resolved
let lines_omitted = lines_total - LINES_MAX_PRINTED;
let start_lines = data.as_bstr().lines().take(LINES_MAX_START);
let end_lines = data.as_bstr().lines().skip(LINES_MAX_START + lines_omitted);
write!(f, "<{} lines total>", lines_total)?;
write_debug_bstrs(f, start_lines)?;
write!(f, "\n<{} lines omitted>\n", lines_omitted)?;
write_debug_bstrs(f, end_lines)
} else if data.len() >= BYTES_MIN_OVERFLOW {
write!(f, "<{} bytes total>", data.len())?;
write_debug_bstrs(f, data[..BYTES_MAX_START].lines())?;
write!(f, "<{} bytes omitted>", data.len() - BYTES_MAX_PRINTED)?;
write_debug_bstrs(f, data[data.len() - BYTES_MAX_END..].lines())
} else {
write!(f, "{:?}", data.as_bstr())
write_debug_bstrs(f, data.lines())
}
}

fn write_debug_bstrs<'a>(
f: &mut impl std::fmt::Write,
lines: impl Iterator<Item = &'a [u8]>,
) -> std::fmt::Result {
let mut newline_needed = false;
write!(f, "\"")?;
for line in lines {
if newline_needed {
writeln!(f)?;
}
epage marked this conversation as resolved.
Show resolved Hide resolved
let s = format!("{:?}", line.as_bstr());
write!(f, "{}", &s[1..s.len() - 1])?;
newline_needed = true;
}
write!(f, "\"")
}

#[cfg(test)]
mod test {
#[test]
fn format_bytes() {
let mut s = String::new();
for i in 0..40 {
s.push_str(&format!("{}\n", i));
}
let mut buf = String::new();
super::format_bytes(s.as_bytes(), &mut buf).unwrap();
assert_eq!(
r#"<40 lines total>"0
1
epage marked this conversation as resolved.
Show resolved Hide resolved
2
3
4
5
6
7
8
9"
<20 lines omitted>
"30
31
32
33
34
35
36
37
38
39""#,
buf
);
}
}