Properly support ranges on hex dump

This commit is contained in:
Intege-rs
2025-09-13 22:27:32 -04:00
parent 5a9a852133
commit b475d40910

View File

@@ -7,13 +7,12 @@ const SEP: &str = " | ";
pub struct HexDump<'s, T: Scannable + ?Sized, R: RangeBounds<usize>>(pub &'s T, pub R); pub struct HexDump<'s, T: Scannable + ?Sized, R: RangeBounds<usize>>(pub &'s T, pub R);
pub fn hex< pub fn hex<
's,
T: Scannable + ?Sized, T: Scannable + ?Sized,
R: RangeBounds<usize> R: RangeBounds<usize>
>( >(
data: &'s T, data: &T,
range:R range:R
) -> HexDump<'s, T, R> { ) -> HexDump<T, R> {
HexDump(data, range) HexDump(data, range)
} }
@@ -48,6 +47,7 @@ impl<'s, T: Scannable + ?Sized, R: RangeBounds<usize>> Display for HexDump<'s, T
// if there is nothing to print then just return... // if there is nothing to print then just return...
// this also prevents the ilog below from crashing :) // this also prevents the ilog below from crashing :)
if end == 0 || start > end { if end == 0 || start > end {
write!(f,"<empty>");
return Ok(()) return Ok(())
} }
@@ -56,6 +56,10 @@ impl<'s, T: Scannable + ?Sized, R: RangeBounds<usize>> Display for HexDump<'s, T
for (mut addr, chunk) in ChunkIter::new(self.0, start) { for (mut addr, chunk) in ChunkIter::new(self.0, start) {
for chunk in chunk.chunks(16) { for chunk in chunk.chunks(16) {
if addr >= end { return Ok(()) }
let chunk = &chunk[..(end - addr).min(chunk.len())];
//╶───╴Column╶────────────────────────────────╴ //╶───╴Column╶────────────────────────────────╴
write!(f, "{:0digits$X}{SEP}", addr, digits = digits)?; write!(f, "{:0digits$X}{SEP}", addr, digits = digits)?;
@@ -85,9 +89,6 @@ impl<'s, T: Scannable + ?Sized, R: RangeBounds<usize>> Display for HexDump<'s, T
} }
} }
let mut last_addr = 0usize;
let fchunk = self.0.chunk_at(0);
Ok(()) Ok(())
} }
} }