Skip to content

Commit 15a9e67

Browse files
committed
optional offset added to displayed address prefix
1 parent 9ef5b54 commit 15a9e67

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/pretty_hex.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub struct HexConfig {
6262
pub chunk: usize,
6363
/// Maximum bytes to print.
6464
pub max_bytes: usize,
65+
/// Offset added to displayed address prefix
66+
pub display_offset: usize,
6567
}
6668

6769
/// Default configuration with `title`, `ascii`, 16 source bytes `width` grouped to 4 separate
@@ -75,6 +77,7 @@ impl Default for HexConfig {
7577
group: 4,
7678
chunk: 1,
7779
max_bytes: usize::MAX,
80+
display_offset: 0,
7881
}
7982
}
8083
}
@@ -136,7 +139,7 @@ where
136139
let lines_len = lines.len();
137140
for (i, row) in lines.enumerate() {
138141
if cfg.width > 0 {
139-
write!(writer, "{:04x}: ", i * cfg.width)?;
142+
write!(writer, "{:04x}: ", i * cfg.width + cfg.display_offset)?;
140143
}
141144
for (i, x) in row.as_ref().iter().enumerate() {
142145
write!(writer, "{}{:02x}", cfg.delimiter(i), x)?;

tests/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fn test_config() {
5757
group: 0,
5858
chunk: 0,
5959
max_bytes: usize::MAX,
60+
display_offset: 0
6061
};
6162
assert!(config_hex(&vec![], cfg).is_empty());
6263
assert_eq!("2425262728", config_hex(&"$%&'(", cfg));
@@ -95,6 +96,18 @@ fn test_config() {
9596
0010: 89d5 cf90 2367 4b48 dbb1 bc35 bfee"
9697
);
9798

99+
assert_eq!(
100+
config_hex(&v,
101+
HexConfig {
102+
ascii: false,
103+
display_offset: 0x200,
104+
..cfg
105+
}
106+
),
107+
"0200: 6b 4e 1a c3 af 03 d2 1e 7e 73 ba c8 bd 84 0f 83\n\
108+
0210: 89 d5 cf 90 23 67 4b 48 db b1 bc 35 bf ee"
109+
);
110+
98111
let v: Vec<u8> = (0..21).collect();
99112
let want = r##"Length: 21 (0x15) bytes
100113
0000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
@@ -111,6 +124,7 @@ fn test_config() {
111124
group: 2,
112125
chunk: 3,
113126
max_bytes: usize::MAX,
127+
display_offset: 0
114128
}),
115129
"0000: 000102 030405 060708 090a ...........\n\
116130
000b: 0b0c .."
@@ -126,6 +140,7 @@ fn test_config() {
126140
group: 3,
127141
chunk: 3,
128142
max_bytes: usize::MAX,
143+
display_offset: 0
129144
}
130145
),
131146
"0000: 000102 030405 060708 090a0b 0c0d0e 0f ................\n\

0 commit comments

Comments
 (0)