pub(crate) const C_TCHAR: u8 = 1;
pub(crate) const C_QDTEXT: u8 = 2;
pub(crate) const C_ESCAPABLE: u8 = 4;
pub(crate) const C_OWS: u8 = 8;
pub(crate) const C_ATTR: u8 = 16;
static TABLE: [u8; 128] = build_table();
pub(crate) fn char_classes(b: u8) -> u8 {
*TABLE.get(usize::from(b)).unwrap_or(&0)
}
const fn build_table() -> [u8; 128] {
let mut table = [0u8; 128];
let mut i = 0;
while i < 128 {
let b = i as u8;
let mut classes = 0;
if is_tchar(b) {
classes |= C_TCHAR;
}
if is_qdtext(b) {
classes |= C_QDTEXT;
}
if is_escapable(b) {
classes |= C_ESCAPABLE;
}
if is_ows(b) {
classes |= C_OWS;
}
if is_attr(b) {
classes |= C_ATTR;
}
table[i] = classes;
i += 1;
}
table
}
const fn is_tchar(b: u8) -> bool {
matches!(b,
b'!'
| b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
| b'0'..=b'9'
| b'a'..=b'z'
| b'A'..=b'Z')
}
const fn is_qdtext(b: u8) -> bool {
matches!(b, b'\t' | b' ' | 0x21 | 0x23..=0x5B | 0x5D..=0x7E)
}
const fn is_escapable(b: u8) -> bool {
matches!(b, b'\t' | b' ' | 0x21..=0x7E | 0x80..=0xFF)
}
const fn is_attr(b: u8) -> bool {
matches!(b,
b'a'..=b'z'
| b'A'..=b'Z'
| b'0'..=b'9'
| b'!'
| b'#'
| b'$'
| b'&'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~')
}
const fn is_ows(b: u8) -> bool {
matches!(b, b' ' | b'\t')
}