ctoolbox/formats/eite/util/
ascii.rs

1// ---------------
2// ASCII helpers
3// ---------------
4
5pub fn ascii_is_digit(b: u8) -> bool {
6    b.is_ascii_digit()
7}
8pub fn ascii_is_alpha(b: u8) -> bool {
9    b.is_ascii_alphabetic()
10}
11pub fn ascii_is_alphanum(b: u8) -> bool {
12    b.is_ascii_alphanumeric()
13}
14pub fn ascii_is_letter_lower(b: u8) -> bool {
15    (char::from(b)).is_ascii_lowercase()
16}
17pub fn ascii_is_letter(b: u8) -> bool {
18    (char::from(b)).is_ascii_alphabetic()
19}
20
21/// True if value is a valid ASCII byte (0..=127).
22pub fn is_ascii_byte(b: u8) -> bool {
23    b <= 0x7F
24}
25
26/// Printable ASCII (32–126 inclusive)
27pub fn ascii_is_printable(b: u8) -> bool {
28    (32..=126).contains(&b)
29}
30
31/// Space (0x20)
32pub fn ascii_is_space(b: u8) -> bool {
33    b == 0x20
34}
35
36/// Newline (LF=10 or CR=13)
37pub fn ascii_is_newline(b: u8) -> bool {
38    b == 10 || b == 13
39}
40
41/// Uppercase A–Z
42pub fn ascii_is_letter_upper(b: u8) -> bool {
43    b.is_ascii_uppercase()
44}
45
46/// Letter (uppercase or lowercase)
47pub fn ascii_is_letter_2(b: u8) -> bool {
48    ascii_is_letter(b)
49}
50
51/// Alphanumeric (A–Z a–z 0–9)
52pub fn ascii_is_alphanum_2(b: u8) -> bool {
53    ascii_is_alphanum(b)
54}
55
56/// Convenience CRLF sequence.
57pub fn crlf() -> Vec<u8> {
58    vec![13, 10]
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[crate::ctb_test]
66    fn test_ascii_helpers() {
67        assert!(ascii_is_digit(b'5'));
68        assert!(ascii_is_alphanum(b'A'));
69        assert!(ascii_is_letter_lower(b'z'));
70    }
71}