ctb_formats_eite/util/
ascii.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! ASCII helpers
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29pub fn ascii_is_digit(b: u8) -> bool {
30    b.is_ascii_digit()
31}
32pub fn ascii_is_alpha(b: u8) -> bool {
33    b.is_ascii_alphabetic()
34}
35pub fn ascii_is_alphanum(b: u8) -> bool {
36    b.is_ascii_alphanumeric()
37}
38pub fn ascii_is_letter_lower(b: u8) -> bool {
39    (char::from(b)).is_ascii_lowercase()
40}
41pub fn ascii_is_letter(b: u8) -> bool {
42    (char::from(b)).is_ascii_alphabetic()
43}
44
45/// True if value is a valid ASCII byte (0..=127).
46pub fn is_ascii_byte(b: u8) -> bool {
47    b <= 0x7F
48}
49
50/// Printable ASCII (32–126 inclusive)
51pub fn ascii_is_printable(b: u8) -> bool {
52    (32..=126).contains(&b)
53}
54
55/// Space (0x20)
56pub fn ascii_is_space(b: u8) -> bool {
57    b == 0x20
58}
59
60/// Newline (LF=10 or CR=13)
61pub fn ascii_is_newline(b: u8) -> bool {
62    b == 10 || b == 13
63}
64
65/// Uppercase A–Z
66pub fn ascii_is_letter_upper(b: u8) -> bool {
67    b.is_ascii_uppercase()
68}
69
70/// Letter (uppercase or lowercase)
71pub fn ascii_is_letter_2(b: u8) -> bool {
72    ascii_is_letter(b)
73}
74
75/// Alphanumeric (A–Z a–z 0–9)
76pub fn ascii_is_alphanum_2(b: u8) -> bool {
77    ascii_is_alphanum(b)
78}
79
80/// Convenience CRLF sequence.
81pub fn crlf() -> Vec<u8> {
82    vec![13, 10]
83}
84
85#[cfg(test)]
86#[allow(
87    clippy::panic,
88    clippy::expect_used,
89    clippy::unwrap_used,
90    clippy::unwrap_in_result,
91    clippy::panic_in_result_fn,
92    clippy::indexing_slicing,
93    clippy::arithmetic_side_effects,
94    reason = "Standard repository test boilerplate"
95)]
96mod tests {
97    use super::*;
98
99    #[crate::ctb_test]
100    fn test_ascii_helpers() {
101        assert!(ascii_is_digit(b'5'));
102        assert!(ascii_is_alphanum(b'A'));
103        assert!(ascii_is_letter_lower(b'z'));
104    }
105}