#[inline]
pub const fn is_whitespace(b: u8) -> bool {
matches!(b, 0x00 | 0x09 | 0x0A | 0x0C | 0x0D | 0x20)
}
#[inline]
pub const fn is_delimiter(b: u8) -> bool {
matches!(
b,
b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%'
)
}
#[inline]
pub const fn is_regular(b: u8) -> bool {
!is_whitespace(b) && !is_delimiter(b)
}
#[inline]
pub const fn is_eol(b: u8) -> bool {
matches!(b, 0x0A | 0x0D)
}
#[inline]
pub const fn is_numeric_char(b: u8) -> bool {
b.is_ascii_digit() || matches!(b, b'+' | b'-' | b'.')
}
#[inline]
pub const fn hex_value(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}
#[inline]
pub const fn octal_value(b: u8) -> Option<u8> {
match b {
b'0'..=b'7' => Some(b - b'0'),
_ => None,
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn classes_partition_byte_space() {
for b in 0..=255u8 {
let classes = [is_whitespace(b), is_delimiter(b), is_regular(b)]
.iter()
.filter(|&&c| c)
.count();
assert_eq!(classes, 1, "byte 0x{b:02x} must be in exactly one class");
}
}
#[test]
fn spec_examples() {
for b in [0x00, 0x09, 0x0A, 0x0C, 0x0D, 0x20] {
assert!(is_whitespace(b));
}
for b in b"()<>[]{}/%" {
assert!(is_delimiter(*b));
}
for b in b"AZaz09#'\"*!" {
assert!(is_regular(*b));
}
}
#[test]
fn hex_and_octal() {
assert_eq!(hex_value(b'0'), Some(0));
assert_eq!(hex_value(b'a'), Some(10));
assert_eq!(hex_value(b'F'), Some(15));
assert_eq!(hex_value(b'g'), None);
assert_eq!(octal_value(b'7'), Some(7));
assert_eq!(octal_value(b'8'), None);
}
}