Skip to content

Commit

Permalink
move HexDisplay to the traits module
Browse files Browse the repository at this point in the history
remove the util module
  • Loading branch information
Geal committed Aug 13, 2021
1 parent 5637d0b commit 9ddbc00
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 79 deletions.
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,9 @@ pub mod lib {
pub use self::bits::*;
pub use self::internal::*;
pub use self::traits::*;
pub use self::util::*;

pub use self::str::*;

#[macro_use]
mod util;

#[macro_use]
pub mod error;
Expand Down
78 changes: 78 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,84 @@ impl<I> ErrorConvert<error::VerboseError<(I, usize)>> for error::VerboseError<I>
}
}

#[cfg(feature = "std")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "std")))]
/// Helper trait to show a byte slice as a hex dump
pub trait HexDisplay {
/// Converts the value of `self` to a hex dump, returning the owned
/// `String`.
fn to_hex(&self, chunk_size: usize) -> String;

/// Converts the value of `self` to a hex dump beginning at `from` address, returning the owned
/// `String`.
fn to_hex_from(&self, chunk_size: usize, from: usize) -> String;
}

#[cfg(feature = "std")]
static CHARS: &[u8] = b"0123456789abcdef";

#[cfg(feature = "std")]
impl HexDisplay for [u8] {
#[allow(unused_variables)]
fn to_hex(&self, chunk_size: usize) -> String {
self.to_hex_from(chunk_size, 0)
}

#[allow(unused_variables)]
fn to_hex_from(&self, chunk_size: usize, from: usize) -> String {
let mut v = Vec::with_capacity(self.len() * 3);
let mut i = from;
for chunk in self.chunks(chunk_size) {
let s = format!("{:08x}", i);
for &ch in s.as_bytes().iter() {
v.push(ch);
}
v.push(b'\t');

i += chunk_size;

for &byte in chunk {
v.push(CHARS[(byte >> 4) as usize]);
v.push(CHARS[(byte & 0xf) as usize]);
v.push(b' ');
}
if chunk_size > chunk.len() {
for j in 0..(chunk_size - chunk.len()) {
v.push(b' ');
v.push(b' ');
v.push(b' ');
}
}
v.push(b'\t');

for &byte in chunk {
if (byte >= 32 && byte <= 126) || byte >= 128 {
v.push(byte);
} else {
v.push(b'.');
}
}
v.push(b'\n');
}

String::from_utf8_lossy(&v[..]).into_owned()
}
}

#[cfg(feature = "std")]
impl HexDisplay for str {
#[allow(unused_variables)]
fn to_hex(&self, chunk_size: usize) -> String {
self.to_hex_from(chunk_size, 0)
}

#[allow(unused_variables)]
fn to_hex_from(&self, chunk_size: usize, from: usize) -> String {
self.as_bytes().to_hex_from(chunk_size, from)
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
76 changes: 0 additions & 76 deletions src/util.rs

This file was deleted.

0 comments on commit 9ddbc00

Please sign in to comment.