pub static WHITESPACE: [bool; 256] = {
let mut t = [false; 256];
t[0x09] = true; t[0x0A] = true; t[0x0D] = true; t[0x20] = true; t
};
pub static IDENT_START: [bool; 256] = make_ident_start();
pub static IDENT_CONT: [bool; 256] = make_ident_cont();
pub static ALPHA_UNDERSCORE: [bool; 256] = make_ident_start();
pub static DIGIT: [bool; 256] = make_digit();
pub static HEX_DIGIT: [bool; 256] = make_hex_digit();
const fn make_ident_start() -> [bool; 256] {
let mut t = [false; 256];
let mut i = b'A';
while i <= b'Z' { t[i as usize] = true; i += 1; }
i = b'a';
while i <= b'z' { t[i as usize] = true; i += 1; }
t[b'_' as usize] = true;
t
}
const fn make_ident_cont() -> [bool; 256] {
let mut t = make_ident_start();
let mut i = b'0';
while i <= b'9' { t[i as usize] = true; i += 1; }
t
}
const fn make_digit() -> [bool; 256] {
let mut t = [false; 256];
let mut i = b'0';
while i <= b'9' { t[i as usize] = true; i += 1; }
t
}
const fn make_hex_digit() -> [bool; 256] {
let mut t = [false; 256];
let mut i = b'0';
while i <= b'9' { t[i as usize] = true; i += 1; }
i = b'a';
while i <= b'f' { t[i as usize] = true; i += 1; }
i = b'A';
while i <= b'F' { t[i as usize] = true; i += 1; }
t
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ident_start_covers_alpha_and_underscore() {
for b in b'A'..=b'Z' { assert!(IDENT_START[b as usize], "missing {}", b as char); }
for b in b'a'..=b'z' { assert!(IDENT_START[b as usize], "missing {}", b as char); }
assert!(IDENT_START[b'_' as usize]);
assert!(!IDENT_START[b'0' as usize]);
assert!(!IDENT_START[b'-' as usize]);
assert!(!IDENT_START[b'@' as usize]);
}
#[test]
fn ident_cont_includes_digits_but_not_hyphen() {
for b in b'0'..=b'9' { assert!(IDENT_CONT[b as usize]); }
assert!(IDENT_CONT[b'a' as usize]);
assert!(IDENT_CONT[b'_' as usize]);
assert!(!IDENT_CONT[b'-' as usize]);
assert!(!IDENT_CONT[b'.' as usize]);
}
#[test]
fn digit_table_only_decimal() {
for b in b'0'..=b'9' { assert!(DIGIT[b as usize]); }
assert!(!DIGIT[b'a' as usize]);
assert!(!DIGIT[b'A' as usize]);
assert!(!DIGIT[b' ' as usize]);
}
#[test]
fn hex_digit_covers_all_forms() {
for b in b'0'..=b'9' { assert!(HEX_DIGIT[b as usize]); }
for b in b'a'..=b'f' { assert!(HEX_DIGIT[b as usize]); }
for b in b'A'..=b'F' { assert!(HEX_DIGIT[b as usize]); }
assert!(!HEX_DIGIT[b'g' as usize]);
assert!(!HEX_DIGIT[b'G' as usize]);
assert!(!HEX_DIGIT[b'x' as usize]);
}
#[test]
fn whitespace_table_exactly_four_bytes() {
assert!(WHITESPACE[b' ' as usize]);
assert!(WHITESPACE[b'\t' as usize]);
assert!(WHITESPACE[b'\n' as usize]);
assert!(WHITESPACE[b'\r' as usize]);
assert!(!WHITESPACE[b'a' as usize]);
assert!(!WHITESPACE[0x0B_usize]); }
#[test]
fn alpha_underscore_is_alias_for_ident_start() {
for i in 0..256_usize {
assert_eq!(ALPHA_UNDERSCORE[i], IDENT_START[i]);
}
}
}