hex_fmt/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
//! # Formatting and shortening byte slices as hexadecimal strings
//!
//! This crate provides wrappers for byte slices and lists of byte slices that implement the
//! standard formatting traits and print the bytes as a hexadecimal string. It respects the
//! alignment, width and precision parameters and applies padding and shortening.
//!
//! ```
//! # use hex_fmt::{HexFmt, HexList};
//! let bytes: &[u8] = &[0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f];
//!
//! assert_eq!("0a1b2c3d4e5f", &format!("{}", HexFmt(bytes)));
//!
//! // By default the full slice is printed. Change the width to apply padding or shortening.
//! assert_eq!("0a..5f", &format!("{:6}", HexFmt(bytes)));
//! assert_eq!("0a1b2c3d4e5f", &format!("{:12}", HexFmt(bytes)));
//! assert_eq!(" 0a1b2c3d4e5f ", &format!("{:16}", HexFmt(bytes)));
//!
//! // The default alignment is centered. Use `<` or `>` to align left or right.
//! assert_eq!("0a1b..", &format!("{:<6}", HexFmt(bytes)));
//! assert_eq!("0a1b2c3d4e5f ", &format!("{:<16}", HexFmt(bytes)));
//! assert_eq!("..4e5f", &format!("{:>6}", HexFmt(bytes)));
//! assert_eq!(" 0a1b2c3d4e5f", &format!("{:>16}", HexFmt(bytes)));
//!
//! // Use e.g. `4.8` to set the minimum width to 4 and the maximum to 8.
//! assert_eq!(" 12 ", &format!("{:4.8}", HexFmt([0x12])));
//! assert_eq!("123456", &format!("{:4.8}", HexFmt([0x12, 0x34, 0x56])));
//! assert_eq!("123..89a", &format!("{:4.8}", HexFmt([0x12, 0x34, 0x56, 0x78, 0x9a])));
//!
//! // If you prefer uppercase, use `X`.
//! assert_eq!("0A1B2C3D4E5F", &format!("{:X}", HexFmt(bytes)));
//!
//! // All of the above can be combined.
//! assert_eq!("0A1B2C..", &format!("{:<4.8X}", HexFmt(bytes)));
//!
//! // With `HexList`, the parameters are applied to each entry.
//! let list = &[[0x0a; 3], [0x1b; 3], [0x2c; 3]];
//! assert_eq!("[0A.., 1B.., 2C..]", &format!("{:<4X}", HexList(list)));
//! ```
#![cfg_attr(not(test), no_std)]
use core::fmt::{Alignment, Debug, Display, Formatter, LowerHex, Result, UpperHex, Write};
const ELLIPSIS: &str = "..";
/// Wrapper for a byte array, whose `Debug`, `Display` and `LowerHex` implementations output
/// shortened hexadecimal strings.
pub struct HexFmt<T>(pub T);
impl<T: AsRef<[u8]>> Debug for HexFmt<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
LowerHex::fmt(self, f)
}
}
impl<T: AsRef<[u8]>> Display for HexFmt<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
LowerHex::fmt(self, f)
}
}
impl<T: AsRef<[u8]>> LowerHex for HexFmt<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Lowercase::fmt(self.0.as_ref(), f)
}
}
impl<T: AsRef<[u8]>> UpperHex for HexFmt<T> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
Uppercase::fmt(self.0.as_ref(), f)
}
}
/// Wrapper for a list of byte arrays, whose `Debug`, `Display` and `LowerHex` implementations
/// output shortened hexadecimal strings.
pub struct HexList<T>(pub T);
impl<T> Debug for HexList<T>
where
T: Clone + IntoIterator,
T::Item: AsRef<[u8]>,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
LowerHex::fmt(self, f)
}
}
impl<T> Display for HexList<T>
where
T: Clone + IntoIterator,
T::Item: AsRef<[u8]>,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
LowerHex::fmt(self, f)
}
}
impl<T> LowerHex for HexList<T>
where
T: Clone + IntoIterator,
T::Item: AsRef<[u8]>,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
let entries = self.0.clone().into_iter().map(HexFmt);
f.debug_list().entries(entries).finish()
}
}
impl<T> UpperHex for HexList<T>
where
T: Clone + IntoIterator,
T::Item: AsRef<[u8]>,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
let mut iter = self.0.clone().into_iter();
write!(f, "[")?;
if let Some(item) = iter.next() {
UpperHex::fmt(&HexFmt(item), f)?;
}
for item in iter {
write!(f, ", ")?;
UpperHex::fmt(&HexFmt(item), f)?;
}
write!(f, "]")
}
}
trait Case {
fn fmt_byte(f: &mut Formatter, byte: u8) -> Result;
fn fmt_digit(f: &mut Formatter, digit: u8) -> Result;
#[inline]
fn fmt(bytes: &[u8], f: &mut Formatter) -> Result {
let min_width = f.width().unwrap_or(0);
let max_width = f
.precision()
.or_else(|| f.width())
.unwrap_or_else(usize::max_value);
let align = f.align().unwrap_or(Alignment::Center);
// If the array is short enough, don't shorten it.
if 2 * bytes.len() <= max_width {
let fill = f.fill();
let missing = min_width.saturating_sub(2 * bytes.len());
let (left, right) = match align {
Alignment::Left => (0, missing),
Alignment::Right => (missing, 0),
Alignment::Center => (missing / 2, missing - missing / 2),
};
for _ in 0..left {
f.write_char(fill)?;
}
for byte in bytes {
Self::fmt_byte(f, *byte)?;
}
for _ in 0..right {
f.write_char(fill)?;
}
return Ok(());
}
// If the bytes don't fit and the ellipsis fills the maximum width, print only that.
if max_width <= ELLIPSIS.len() {
return write!(f, "{:.*}", max_width, ELLIPSIS);
}
// Compute the number of hex digits to display left and right of the ellipsis.
let digits = max_width.saturating_sub(ELLIPSIS.len());
let (left, right) = match align {
Alignment::Left => (digits, 0),
Alignment::Right => (0, digits),
Alignment::Center => (digits - digits / 2, digits / 2),
};
// Print the bytes on the left.
for byte in &bytes[..(left / 2)] {
Self::fmt_byte(f, *byte)?;
}
// If odd, print only the first hex digit of the next byte.
if left & 1 == 1 {
Self::fmt_digit(f, bytes[left / 2] >> 4)?;
}
// Print the ellipsis.
f.write_str(ELLIPSIS)?;
// If `right` is odd, print the second hex digit of a byte.
if right & 1 == 1 {
Self::fmt_digit(f, bytes[(bytes.len() - right / 2 - 1)] & 0x0f)?;
}
// Print the remaining bytes on the right.
for byte in &bytes[(bytes.len() - right / 2)..] {
Self::fmt_byte(f, *byte)?;
}
Ok(())
}
}
#[derive(Clone, Copy)]
struct Uppercase;
impl Case for Uppercase {
#[inline]
fn fmt_byte(f: &mut Formatter, byte: u8) -> Result {
write!(f, "{:02X}", byte)
}
#[inline]
fn fmt_digit(f: &mut Formatter, digit: u8) -> Result {
write!(f, "{:1X}", digit)
}
}
#[derive(Clone, Copy)]
struct Lowercase;
impl Case for Lowercase {
#[inline]
fn fmt_byte(f: &mut Formatter, byte: u8) -> Result {
write!(f, "{:02x}", byte)
}
#[inline]
fn fmt_digit(f: &mut Formatter, digit: u8) -> Result {
write!(f, "{:1x}", digit)
}
}
#[cfg(test)]
mod tests {
use super::HexFmt;
#[test]
fn test_fmt() {
assert_eq!("", &format!("{:.0}", HexFmt(&[0x01])));
assert_eq!(".", &format!("{:.1}", HexFmt(&[0x01])));
assert_eq!("01", &format!("{:.2}", HexFmt(&[0x01])));
assert_eq!("..", &format!("{:.2}", HexFmt(&[0x01, 0x23])));
}
}