1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pub fn is_permitted_printable_char(c: char) -> bool {
let x = c as u32;
let is_above_space = x >= 0x20; let is_below_tilde = x <= 0x7E; let is_tab = x == 0x09; (is_above_space && is_below_tilde) || is_tab
}
pub fn is_permitted_newline_char(c: char) -> bool {
let x = c as u32;
x == 0x0A
}
pub fn is_permitted_char(c: char) -> bool {
is_permitted_printable_char(c) || is_permitted_newline_char(c)
}
#[cfg(test)]
mod tests {
#[test]
fn test_permitted_characters() {
let mut good_chars = (0x20..=0x7E).collect::<Vec<u8>>();
good_chars.push(0x0A); good_chars.push(0x09); for c in good_chars {
assert!(super::is_permitted_char(c as char));
}
}
#[test]
fn test_forbidden_characters() {
let mut bad_chars = (0x0..0x09).collect::<Vec<u8>>();
bad_chars.append(&mut (0x0B..=0x1F).collect::<Vec<u8>>());
bad_chars.push(0x7F);
for c in bad_chars {
assert!(!super::is_permitted_char(c as char));
}
}
}