Debian Codegraph @

src/ascii.rs

raw symbols SCIP
1 #![cfg(any(feature = "bash", feature = "fish", feature = "sh"))]
2
3 //! Scanner for ASCII control codes, shell metacharacters, printable characters,
4 //! and extended codes, i.e. classify each byte in a stream according to where
5 //! it appears in extended ASCII.
6
7 use std::borrow::Borrow;
8
9 #[derive(PartialEq)]
10 pub(crate) enum Char {
11 Bell,
12 Backspace,
13 Escape,
14 FormFeed,
15 NewLine,
16 CarriageReturn,
17 HorizontalTab,
18 VerticalTab,
19 Control(u8),
20 Backslash,
21 SingleQuote,
22 DoubleQuote,
23 Delete,
24 PrintableInert(u8),
25 Printable(u8),
26 Extended(u8),
27 }
28
29 impl Char {
30 pub fn from<T: Borrow<u8>>(ch: T) -> Self {
31 let ch = *ch.borrow();
32 use Char::*;
33 match ch {
34 // ASCII control characters that frequently have dedicated backslash
35 // sequences when quoted.
36 BEL => Bell,
37 BS => Backspace,
38 ESC => Escape,
39 FF => FormFeed,
40 LF => NewLine,
41 CR => CarriageReturn,
42 TAB => HorizontalTab,
43 VT => VerticalTab,
44
45 // ASCII control characters, the rest.
46 0x00..=0x06 | 0x0E..=0x1A | 0x1C..=0x1F => Control(ch),
47
48 // ASCII printable characters that can have dedicated backslash
49 // sequences when quoted or otherwise need some special treatment.
50 b'\\' => Backslash,
51 b'\'' => SingleQuote,
52 b'\"' => DoubleQuote,
53 DEL => Delete,
54
55 // ASCII printable letters, numbers, and "safe" punctuation.
56 b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => PrintableInert(ch),
57 b',' | b'.' | b'/' | b'_' | b'-' => PrintableInert(ch),
58
59 // ASCII punctuation which can have significance in the shell.
60 b'|' | b'&' | b';' | b'(' | b')' | b'<' | b'>' => Printable(ch),
61 b' ' | b'?' | b'[' | b']' | b'{' | b'}' | b'`' => Printable(ch),
62 b'~' | b'!' | b'$' | b'@' | b'+' | b'=' | b'*' => Printable(ch),
63 b'%' | b'#' | b':' | b'^' => Printable(ch),
64
65 // ASCII extended characters, or high bytes.
66 0x80..=0xff => Extended(ch),
67 }
68 }
69
70 #[inline]
71 pub fn is_inert(&self) -> bool {
72 matches!(self, Char::PrintableInert(_))
73 }
74
75 #[inline]
76 #[cfg(feature = "sh")]
77 pub fn code(&self) -> u8 {
78 use Char::*;
79 match *self {
80 Bell => BEL,
81 Backspace => BS,
82 Escape => ESC,
83 FormFeed => FF,
84 NewLine => LF,
85 CarriageReturn => CR,
86 HorizontalTab => TAB,
87 VerticalTab => VT,
88 Control(ch) => ch,
89 Backslash => b'\\',
90 SingleQuote => b'\'',
91 DoubleQuote => b'"',
92 Delete => DEL,
93 PrintableInert(ch) => ch,
94 Printable(ch) => ch,
95 Extended(ch) => ch,
96 }
97 }
98 }
99
100 const BEL: u8 = 0x07; // -> \a
101 const BS: u8 = 0x08; // -> \b
102 const TAB: u8 = 0x09; // -> \t
103 const LF: u8 = 0x0A; // -> \n
104 const VT: u8 = 0x0B; // -> \v
105 const FF: u8 = 0x0C; // -> \f
106 const CR: u8 = 0x0D; // -> \r
107 const ESC: u8 = 0x1B; // -> \e
108 const DEL: u8 = 0x7F;
109
110 #[cfg(test)]
111 mod tests {
112 #[test]
113 #[cfg(feature = "sh")]
114 fn test_code() {
115 for ch in u8::MIN..=u8::MAX {
116 let char = super::Char::from(ch);
117 assert_eq!(ch, char.code());
118 }
119 }
120 }