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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#;
8
9 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
10 pub(craterust-analyzer cargo shell-quote 0.7.2 crate/) enum Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char# {
11 Bellrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Bell#,
12 Backspacerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backspace#,
13 Escaperust-analyzer cargo shell-quote 0.7.2 ascii/Char#Escape#,
14 FormFeedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#FormFeed#,
15 NewLinerust-analyzer cargo shell-quote 0.7.2 ascii/Char#NewLine#,
16 CarriageReturnrust-analyzer cargo shell-quote 0.7.2 ascii/Char#CarriageReturn#,
17 HorizontalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#HorizontalTab#,
18 VerticalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#VerticalTab#,
19 Controlrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Control#(u8),
20 Backslashrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backslash#,
21 SingleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#SingleQuote#,
22 DoubleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#DoubleQuote#,
23 Deleterust-analyzer cargo shell-quote 0.7.2 ascii/Char#Delete#,
24 PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(u8),
25 Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(u8),
26 Extendedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Extended#(u8),
27 }
28
29 impl Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char# {
30 pub fn fromrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]from().<T: Borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#<u8>>(ch: T) -> Selfrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char] {
31 let ch = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().ch.borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core borrow/Borrow#borrow().();
32 use Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::*;
33 match ch {
34 // ASCII control characters that frequently have dedicated backslash
35 // sequences when quoted.
36 BELrust-analyzer cargo shell-quote 0.7.2 ascii/BEL. => Bellrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Bell#,
37 BSrust-analyzer cargo shell-quote 0.7.2 ascii/BS. => Backspacerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backspace#,
38 ESCrust-analyzer cargo shell-quote 0.7.2 ascii/ESC. => Escaperust-analyzer cargo shell-quote 0.7.2 ascii/Char#Escape#,
39 FFrust-analyzer cargo shell-quote 0.7.2 ascii/FF. => FormFeedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#FormFeed#,
40 LFrust-analyzer cargo shell-quote 0.7.2 ascii/LF. => NewLinerust-analyzer cargo shell-quote 0.7.2 ascii/Char#NewLine#,
41 CRrust-analyzer cargo shell-quote 0.7.2 ascii/CR. => CarriageReturnrust-analyzer cargo shell-quote 0.7.2 ascii/Char#CarriageReturn#,
42 TABrust-analyzer cargo shell-quote 0.7.2 ascii/TAB. => HorizontalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#HorizontalTab#,
43 VTrust-analyzer cargo shell-quote 0.7.2 ascii/VT. => VerticalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#VerticalTab#,
44
45 // ASCII control characters, the rest.
46 0x00..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0x06 | 0x0E..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0x1A | 0x1C..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0x1F => Controlrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Control#(ch),
47
48 // ASCII printable characters that can have dedicated backslash
49 // sequences when quoted or otherwise need some special treatment.
50 b'\\' => Backslashrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backslash#,
51 b'\'' => SingleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#SingleQuote#,
52 b'\"' => DoubleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#DoubleQuote#,
53 DELrust-analyzer cargo shell-quote 0.7.2 ascii/DEL. => Deleterust-analyzer cargo shell-quote 0.7.2 ascii/Char#Delete#,
54
55 // ASCII printable letters, numbers, and "safe" punctuation.
56 b'a'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'z' | b'A'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'Z' | b'0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'9' => PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(ch),
57 b',' | b'.' | b'/' | b'_' | b'-' => PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(ch),
58
59 // ASCII punctuation which can have significance in the shell.
60 b'|' | b'&' | b';' | b'(' | b')' | b'<' | b'>' => Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch),
61 b' ' | b'?' | b'[' | b']' | b'{' | b'}' | b'`' => Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch),
62 b'~' | b'!' | b'$' | b'@' | b'+' | b'=' | b'*' => Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch),
63 b'%' | b'#' | b':' | b'^' => Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch),
64
65 // ASCII extended characters, or high bytes.
66 0x80..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xff => Extendedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Extended#(ch),
67 }
68 }
69
70 #[inline]
71 pub fn is_inertrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]is_inert().(&self) -> bool {
72 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(_))
73 }
74
75 #[inline]
76 #[cfg(feature = "sh")]
77 pub fn coderust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]code().(&self) -> u8 {
78 use Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::*;
79 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
80 Bellrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Bell# => BELrust-analyzer cargo shell-quote 0.7.2 ascii/BEL.,
81 Backspacerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backspace# => BSrust-analyzer cargo shell-quote 0.7.2 ascii/BS.,
82 Escaperust-analyzer cargo shell-quote 0.7.2 ascii/Char#Escape# => ESCrust-analyzer cargo shell-quote 0.7.2 ascii/ESC.,
83 FormFeedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#FormFeed# => FFrust-analyzer cargo shell-quote 0.7.2 ascii/FF.,
84 NewLinerust-analyzer cargo shell-quote 0.7.2 ascii/Char#NewLine# => LFrust-analyzer cargo shell-quote 0.7.2 ascii/LF.,
85 CarriageReturnrust-analyzer cargo shell-quote 0.7.2 ascii/Char#CarriageReturn# => CRrust-analyzer cargo shell-quote 0.7.2 ascii/CR.,
86 HorizontalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#HorizontalTab# => TABrust-analyzer cargo shell-quote 0.7.2 ascii/TAB.,
87 VerticalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#VerticalTab# => VTrust-analyzer cargo shell-quote 0.7.2 ascii/VT.,
88 Controlrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Control#(ch) => ch,
89 Backslashrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backslash# => b'\\',
90 SingleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#SingleQuote# => b'\'',
91 DoubleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#DoubleQuote# => b'"',
92 Deleterust-analyzer cargo shell-quote 0.7.2 ascii/Char#Delete# => DELrust-analyzer cargo shell-quote 0.7.2 ascii/DEL.,
93 PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(ch) => ch,
94 Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch) => ch,
95 Extendedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Extended#(ch) => ch,
96 }
97 }
98 }
99
100 const BELrust-analyzer cargo shell-quote 0.7.2 ascii/BEL.: u8 = 0x07; // -> \a
101 const BSrust-analyzer cargo shell-quote 0.7.2 ascii/BS.: u8 = 0x08; // -> \b
102 const TABrust-analyzer cargo shell-quote 0.7.2 ascii/TAB.: u8 = 0x09; // -> \t
103 const LFrust-analyzer cargo shell-quote 0.7.2 ascii/LF.: u8 = 0x0A; // -> \n
104 const VTrust-analyzer cargo shell-quote 0.7.2 ascii/VT.: u8 = 0x0B; // -> \v
105 const FFrust-analyzer cargo shell-quote 0.7.2 ascii/FF.: u8 = 0x0C; // -> \f
106 const CRrust-analyzer cargo shell-quote 0.7.2 ascii/CR.: u8 = 0x0D; // -> \r
107 const ESCrust-analyzer cargo shell-quote 0.7.2 ascii/ESC.: u8 = 0x1B; // -> \e
108 const DELrust-analyzer cargo shell-quote 0.7.2 ascii/DEL.: u8 = 0x7F;
109
110 #[cfg(test)]
111 mod testsrust-analyzer cargo shell-quote 0.7.2 ascii/tests/ {
112 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
113 #[cfg(feature = "sh")]
114 fn test_coderust-analyzer cargo shell-quote 0.7.2 ascii/tests/test_code().() {
115 for ch in u8::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MIN...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX. {
116 let char = superrust-analyzer cargo shell-quote 0.7.2 ascii/::Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::fromrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]from().(ch);
117 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ch, char.coderust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]code().());
118 }
119 }
120 }