use core::fmt;
pub struct EncodedBytes<'a>(pub &'a [u8]);
#[cfg(feature = "alloc")]
pub struct EncodedBytesOwned(pub alloc::vec::Vec<u8>);
impl<'a> EncodedBytes<'a> {
pub fn as_bytes(&self) -> &[u8] {
self.0
}
#[cfg(feature = "alloc")]
pub fn hex_dump(&self) -> alloc::string::String {
hex_dump_bytes(self.0)
}
}
impl<'a> fmt::Display for EncodedBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, byte) in self.0.iter().enumerate() {
if i > 0 {
write!(f, " ")?;
}
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl<'a> fmt::LowerHex for EncodedBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl<'a> fmt::UpperHex for EncodedBytes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in self.0 {
write!(f, "{:02X}", byte)?;
}
Ok(())
}
}
#[cfg(feature = "alloc")]
impl EncodedBytesOwned {
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn hex_dump(&self) -> alloc::string::String {
hex_dump_bytes(&self.0)
}
}
#[cfg(feature = "alloc")]
impl fmt::Display for EncodedBytesOwned {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
EncodedBytes(&self.0).fmt(f)
}
}
#[cfg(feature = "alloc")]
impl fmt::LowerHex for EncodedBytesOwned {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(&EncodedBytes(&self.0), f)
}
}
#[cfg(feature = "alloc")]
impl fmt::UpperHex for EncodedBytesOwned {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(&EncodedBytes(&self.0), f)
}
}
#[cfg(feature = "alloc")]
pub fn hex_dump_bytes(data: &[u8]) -> alloc::string::String {
use alloc::string::String;
let mut out = String::new();
for (chunk_idx, chunk) in data.chunks(16).enumerate() {
let addr = chunk_idx * 16;
let mut hex_part = String::new();
for (i, byte) in chunk.iter().enumerate() {
if i > 0 && i % 2 == 0 {
hex_part.push(' ');
}
let hi = (byte >> 4) as usize;
let lo = (byte & 0xf) as usize;
const HEX: &[u8] = b"0123456789abcdef";
hex_part.push(HEX[hi] as char);
hex_part.push(HEX[lo] as char);
}
while hex_part.len() < 39 {
hex_part.push(' ');
}
let ascii: String = chunk
.iter()
.map(|&b| {
if b.is_ascii_graphic() || b == b' ' {
b as char
} else {
'.'
}
})
.collect();
if !out.is_empty() {
out.push('\n');
}
out.push_str(&alloc::format!("{:08x}: {} {}", addr, hex_part, ascii));
}
out
}