From 15a9e678c648076e3f88fff0272b6f9a3f410e7e Mon Sep 17 00:00:00 2001 From: matcap Date: Sun, 16 Jul 2023 16:16:25 +0200 Subject: [PATCH 1/3] optional offset added to displayed address prefix --- src/pretty_hex.rs | 5 ++++- tests/tests.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/pretty_hex.rs b/src/pretty_hex.rs index 1337812..97f6f5a 100644 --- a/src/pretty_hex.rs +++ b/src/pretty_hex.rs @@ -62,6 +62,8 @@ pub struct HexConfig { pub chunk: usize, /// Maximum bytes to print. pub max_bytes: usize, + /// Offset added to displayed address prefix + pub display_offset: usize, } /// Default configuration with `title`, `ascii`, 16 source bytes `width` grouped to 4 separate @@ -75,6 +77,7 @@ impl Default for HexConfig { group: 4, chunk: 1, max_bytes: usize::MAX, + display_offset: 0, } } } @@ -136,7 +139,7 @@ where let lines_len = lines.len(); for (i, row) in lines.enumerate() { if cfg.width > 0 { - write!(writer, "{:04x}: ", i * cfg.width)?; + write!(writer, "{:04x}: ", i * cfg.width + cfg.display_offset)?; } for (i, x) in row.as_ref().iter().enumerate() { write!(writer, "{}{:02x}", cfg.delimiter(i), x)?; diff --git a/tests/tests.rs b/tests/tests.rs index 4d5b5d0..3d01907 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -57,6 +57,7 @@ fn test_config() { group: 0, chunk: 0, max_bytes: usize::MAX, + display_offset: 0 }; assert!(config_hex(&vec![], cfg).is_empty()); assert_eq!("2425262728", config_hex(&"$%&'(", cfg)); @@ -95,6 +96,18 @@ fn test_config() { 0010: 89d5 cf90 2367 4b48 dbb1 bc35 bfee" ); + assert_eq!( + config_hex(&v, + HexConfig { + ascii: false, + display_offset: 0x200, + ..cfg + } + ), + "0200: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83\n\ + 0210: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee" + ); + let v: Vec = (0..21).collect(); let want = r##"Length: 21 (0x15) bytes 0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................ @@ -111,6 +124,7 @@ fn test_config() { group: 2, chunk: 3, max_bytes: usize::MAX, + display_offset: 0 }), "0000: 000102 030405 060708 090a ...........\n\ 000b: 0b0c .." @@ -126,6 +140,7 @@ fn test_config() { group: 3, chunk: 3, max_bytes: usize::MAX, + display_offset: 0 } ), "0000: 000102 030405 060708 090a0b 0c0d0e 0f ................\n\ From 78e0aad90b72f51fdcda4f1d29a698567b94fd8b Mon Sep 17 00:00:00 2001 From: matcap Date: Sun, 16 Jul 2023 17:35:12 +0200 Subject: [PATCH 2/3] variable address prefix width --- src/pretty_hex.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/pretty_hex.rs b/src/pretty_hex.rs index 97f6f5a..fc284c2 100644 --- a/src/pretty_hex.rs +++ b/src/pretty_hex.rs @@ -112,6 +112,18 @@ impl HexConfig { const NON_ASCII: char = '.'; + +type AddressWriter = dyn Fn(&mut dyn fmt::Write, usize) -> fmt::Result; + +fn get_address_writer(max_addr: usize) -> &'static AddressWriter{ + match max_addr { + 0x0000..=0xffff => &|w: &mut dyn fmt::Write, a| write!(w, "{:04x}: ", a), + 0x010000..=0xffffff => &|w: &mut dyn fmt::Write, a| write!(w, "{:06x}: ", a), + 0x01000000..=0xffffffff => &|w: &mut dyn fmt::Write, a| write!(w, "{:08x}: ", a), + _ => &|w: &mut dyn fmt::Write, a| write!(w, "{:016x}: ", a) + } +} + /// Write hex dump in specified format. pub fn hex_write(writer: &mut W, source: &T, cfg: HexConfig) -> fmt::Result where @@ -136,10 +148,15 @@ where } else { source.len() }); + let lines_len = lines.len(); + + let max_address = source.len() - cfg.width + cfg.display_offset; + let write_address = get_address_writer(max_address); + for (i, row) in lines.enumerate() { if cfg.width > 0 { - write!(writer, "{:04x}: ", i * cfg.width + cfg.display_offset)?; + write_address(writer, i * cfg.width + cfg.display_offset)?; } for (i, x) in row.as_ref().iter().enumerate() { write!(writer, "{}{:02x}", cfg.delimiter(i), x)?; From 4d7902036881a5d93286c72fffc3a7b10e16644d Mon Sep 17 00:00:00 2001 From: matcap Date: Sun, 23 Jul 2023 20:00:01 +0200 Subject: [PATCH 3/3] check for underflow when calculating maximum address prefix --- src/pretty_hex.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pretty_hex.rs b/src/pretty_hex.rs index fc284c2..28a2db0 100644 --- a/src/pretty_hex.rs +++ b/src/pretty_hex.rs @@ -151,7 +151,11 @@ where let lines_len = lines.len(); - let max_address = source.len() - cfg.width + cfg.display_offset; + let max_address = if source.len() <= cfg.width { + source.len() + cfg.display_offset + } else { + source.len() - cfg.width + cfg.display_offset + }; let write_address = get_address_writer(max_address); for (i, row) in lines.enumerate() {