use super::*;
pub fn hex_line_count(bytes: &[u8]) -> usize {
bytes.len().div_ceil(HEX_BYTES_PER_LINE).max(1)
}
pub fn hex_line(offset: usize, bytes: &[u8]) -> String {
hex_row(offset, bytes, HEX_BYTES_PER_LINE, HEX_OFFSET_DIGITS)
}
pub fn hex_row(offset: usize, bytes: &[u8], per_line: usize, digits: usize) -> String {
let mut text = String::with_capacity(hex_row_width(per_line, digits));
if digits > 0 {
text.push_str(&format!("{offset:0digits$x} "));
}
for index in 0..per_line {
if digits > 0 && index == per_line / 2 {
text.push(' ');
}
match bytes.get(index) {
Some(byte) => text.push_str(&format!("{byte:02x} ")),
None => text.push_str(" "),
}
}
text.push_str(" |");
for byte in bytes {
text.push(if byte.is_ascii_graphic() || *byte == b' ' { *byte as char } else { '.' });
}
text.push('|');
text
}
pub const fn hex_row_width(per_line: usize, digits: usize) -> usize {
let offset = if digits > 0 { digits + 2 + 1 } else { 0 };
offset + per_line * 3 + 2 + per_line + 1
}