Skip to content

Commit

Permalink
util: Align ASCII section in hex print
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Schaefer <[email protected]>
  • Loading branch information
JohnAZoidberg committed Oct 28, 2023
1 parent 055cec1 commit 2893618
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions framework_lib/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,25 @@ fn print_ascii_buffer(buffer: &[u8], newline: bool) {
///
/// Example
///
/// print_multiline_buffer(&[0xa0, 0x00, 0x00, 0x36, 0x62, 0x6e, 0x03, 0x00, 0xc5, 0x11, 0x80, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0x2000)
/// print_multiline_buffer(&[0xa0, 0x00, 0x00, 0x36, 0x62, 0x6e, 0x03, 0x00, 0xc5, 0x11, 0x80, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 0x2000)
/// Output:
/// 00000000: a000 0036 626e 0300 c511 8035 0000 0000 ...6bn.....5....
/// 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
/// 00002000: a000 0036 626e 0300 c511 8035 0000 0000 ...6bn.....5....
/// 00002010: 0000 0000 0000 0000 0000 0000 0000 00 ................
pub fn print_multiline_buffer(buffer: &[u8], offset: usize) {
let chunk_size = 16;
for (i, chunk) in buffer.chunks(chunk_size).enumerate() {
print!("{:08x}:", offset + i * chunk_size);
print_chunk(chunk, false);

// Make sure ASCII section aligns, even if less than 16 byte chunks
if chunk.len() < 16 {
let byte_padding = 16 - chunk.len();
let space_padding = byte_padding / 2;
let padding = byte_padding * 2 + space_padding;
print!("{}", " ".repeat(padding));
}
print!(" ");

print_ascii_buffer(chunk, true);
}
}
Expand Down

0 comments on commit 2893618

Please sign in to comment.