use std::{
ascii,
fmt::{self, Write},
};
#[repr(transparent)]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct FourCharCode(u32);
impl fmt::Debug for FourCharCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"")?;
for ch in self
.into_chars()
.iter()
.flat_map(|&b| ascii::escape_default(b))
{
f.write_char(ch as char)?;
}
write!(f, "\"")
}
}
impl FourCharCode {
#[inline]
pub const fn from_int(int: u32) -> Self {
Self(int)
}
#[inline]
pub const fn from_chars(chars: [u8; 4]) -> Self {
Self(u32::from_be_bytes(chars))
}
#[inline]
pub const fn into_int(self) -> u32 {
self.0
}
#[inline]
pub const fn into_chars(self) -> [u8; 4] {
self.0.to_be_bytes()
}
#[inline]
pub const fn is_ascii(&self) -> bool {
const NON_ASCII: u32 = u32::from_be_bytes([128; 4]);
self.0 & NON_ASCII == 0
}
#[inline]
pub const fn is_ascii_graphic(&self) -> bool {
matches!(
self.into_chars(),
[b'!'..=b'~', b'!'..=b'~', b'!'..=b'~', b'!'..=b'~'],
)
}
}