use super::StringExtT;
#[derive(Debug, Clone)]
pub enum HexStr<'s, const N: usize, const P: bool = false, const U: bool = false> {
Owned(Vec<u8>),
Borrowed(&'s [u8]),
}
impl<'s, const N: usize, const P: bool, const U: bool> HexStr<'s, N, P, U> {
#[inline]
pub const fn new(value: &'s [u8]) -> Self {
Self::Borrowed(value)
}
#[inline]
pub const fn new_owned(value: Vec<u8>) -> Self {
Self::Owned(value)
}
#[inline]
pub fn set_with_prefix<const NP: bool>(self) -> HexStr<'s, N, NP, U> {
match self {
Self::Borrowed(value) => HexStr::Borrowed(value),
Self::Owned(value) => HexStr::Owned(value),
}
}
#[inline]
pub fn set_uppercase<const NU: bool>(self) -> HexStr<'s, N, P, NU> {
match self {
Self::Borrowed(value) => HexStr::Borrowed(value),
Self::Owned(value) => HexStr::Owned(value),
}
}
#[inline]
fn encode(&self, string: &mut Vec<u8>) {
let mut buffer = [0; N];
for (idx, &i) in (0..N).rev().zip(
match self {
Self::Borrowed(value) => value,
Self::Owned(value) => value.as_slice(),
}
.iter()
.rev(),
) {
buffer[idx] = i
}
if U {
string.extend(
const_hex::Buffer::<N, P>::new()
.const_format_upper(&buffer)
.as_bytes(),
);
} else {
string.extend(
const_hex::Buffer::<N, P>::new()
.const_format(&buffer)
.as_bytes(),
);
}
}
}
impl<const N: usize, const P: bool, const U: bool> StringExtT for HexStr<'_, N, P, U> {
fn push_to_string(self, string: &mut Vec<u8>) {
self.encode(string);
}
}
impl<const N: usize, const P: bool> StringExtT for const_hex::Buffer<N, P> {
fn push_to_string(self, string: &mut Vec<u8>) {
string.extend(self.as_bytes())
}
}
#[cfg(test)]
mod test {
use crate::string::{HexStr, StringExtT};
#[test]
fn test() {
assert_eq!(
HexStr::<0>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]).to_string_ext(),
""
);
assert_eq!(
HexStr::<7>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]).to_string_ext(),
"01020304050607"
);
assert_eq!(
HexStr::<8>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]).to_string_ext(),
"0001020304050607"
);
assert_eq!(
HexStr::<9>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]).to_string_ext(),
"000001020304050607"
);
}
#[test]
fn test_with_prefix() {
assert_eq!(
HexStr::<0>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
.set_with_prefix::<true>()
.to_string_ext(),
"0x"
);
assert_eq!(
HexStr::<7>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
.set_with_prefix::<true>()
.to_string_ext(),
"0x01020304050607"
);
assert_eq!(
HexStr::<8>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
.set_with_prefix::<true>()
.to_string_ext(),
"0x0001020304050607"
);
assert_eq!(
HexStr::<9>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
.set_with_prefix::<true>()
.to_string_ext(),
"0x000001020304050607"
);
}
#[test]
fn test_uppcase() {
assert_eq!(
HexStr::<7>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xa7])
.set_with_prefix::<true>()
.set_uppercase::<true>()
.to_string_ext(),
"0x010203040506A7"
);
assert_eq!(
HexStr::<8>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xa7])
.set_with_prefix::<true>()
.set_uppercase::<true>()
.to_string_ext(),
"0x00010203040506A7"
);
assert_eq!(
HexStr::<9>::new(&[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xa7])
.set_with_prefix::<true>()
.set_uppercase::<true>()
.to_string_ext(),
"0x0000010203040506A7"
);
}
}