lexical_util/ascii.rs
1//! Utilities for working with ASCII characters.
2
3/// Determine if a character is a valid ASCII character for float grammar.
4pub const fn is_valid_ascii(c: u8) -> bool {
5 // Below 0x20 is mostly control characters, with no representation.
6 // 0x7F is a control character, DEL, so don't include it.
7 // We also want the few visual characters below 0x20:
8 // 0x09 - Horizontal Tab
9 // 0x0A - Newline
10 // 0x0B - Vertical Tab (Deprecated)
11 // 0x0C - Form Feed (Deprecated)
12 // 0x0D - Carriage Return
13 (c >= 0x09 && c <= 0x0d) || (c >= 0x20 && c < 0x7F)
14}
15
16/// Determine if a slice is all valid ASCII characters for float grammar.
17/// Modified to be used in a const fn, since for loops and iter don't work.
18pub const fn is_valid_ascii_slice(slc: &[u8]) -> bool {
19 let mut index = 0;
20 while index < slc.len() {
21 if !is_valid_ascii(slc[index]) {
22 return false;
23 }
24 index += 1;
25 }
26 true
27}
28
29/// Determine if a character is a valid ASCII letter.
30pub const fn is_valid_letter(c: u8) -> bool {
31 (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a)
32}
33
34/// Determine if a slice is all valid ASCII letters.
35/// Modified to be used in a const fn, since for loops and iter don't work.
36pub const fn is_valid_letter_slice(slc: &[u8]) -> bool {
37 let mut index = 0;
38 while index < slc.len() {
39 if !is_valid_letter(slc[index]) {
40 return false;
41 }
42 index += 1;
43 }
44 true
45}