| 1 | #![cfg(any(feature = "bash", feature = "fish"))] |
| 2 | |
| 3 | //! Scanner for control codes, shell metacharacters, printable characters, and |
| 4 | //! UTF-8 sequences, i.e. classify each byte in a stream according to where it |
| 5 | //! appears in UTF-8. |
| 6 | |
| 7 | #[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:)] |
| 8 | pub(craterust-analyzer cargo shell-quote 0.7.2 crate/) enum Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char# { |
| 9 | Bellrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Bell#, |
| 10 | Backspacerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backspace#, |
| 11 | Escaperust-analyzer cargo shell-quote 0.7.2 utf8/Char#Escape#, |
| 12 | FormFeedrust-analyzer cargo shell-quote 0.7.2 utf8/Char#FormFeed#, |
| 13 | NewLinerust-analyzer cargo shell-quote 0.7.2 utf8/Char#NewLine#, |
| 14 | CarriageReturnrust-analyzer cargo shell-quote 0.7.2 utf8/Char#CarriageReturn#, |
| 15 | HorizontalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#HorizontalTab#, |
| 16 | VerticalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#VerticalTab#, |
| 17 | Controlrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Control#(u8), |
| 18 | Backslashrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backslash#, |
| 19 | SingleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#SingleQuote#, |
| 20 | DoubleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#DoubleQuote#, |
| 21 | Deleterust-analyzer cargo shell-quote 0.7.2 utf8/Char#Delete#, |
| 22 | PrintableInertrust-analyzer cargo shell-quote 0.7.2 utf8/Char#PrintableInert#(u8), |
| 23 | Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(u8), |
| 24 | Utf8rust-analyzer cargo shell-quote 0.7.2 utf8/Char#Utf8#(char), |
| 25 | } |
| 26 | |
| 27 | impl Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char# { |
| 28 | pub fn fromrust-analyzer cargo shell-quote 0.7.2 utf8/impl#[Char]from().(ch: char) -> Selfrust-analyzer cargo shell-quote 0.7.2 utf8/impl#[Char] { |
| 29 | let ascii: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<u8, _> = ch.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().(); |
| 30 | use Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::*; |
| 31 | match ascii { |
| 32 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ascii) => match ascii { |
| 33 | // ASCII control characters that frequently have dedicated backslash |
| 34 | // sequences when quoted. |
| 35 | BELrust-analyzer cargo shell-quote 0.7.2 utf8/BEL. => Bellrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Bell#, |
| 36 | BSrust-analyzer cargo shell-quote 0.7.2 utf8/BS. => Backspacerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backspace#, |
| 37 | ESCrust-analyzer cargo shell-quote 0.7.2 utf8/ESC. => Escaperust-analyzer cargo shell-quote 0.7.2 utf8/Char#Escape#, |
| 38 | FFrust-analyzer cargo shell-quote 0.7.2 utf8/FF. => FormFeedrust-analyzer cargo shell-quote 0.7.2 utf8/Char#FormFeed#, |
| 39 | LFrust-analyzer cargo shell-quote 0.7.2 utf8/LF. => NewLinerust-analyzer cargo shell-quote 0.7.2 utf8/Char#NewLine#, |
| 40 | CRrust-analyzer cargo shell-quote 0.7.2 utf8/CR. => CarriageReturnrust-analyzer cargo shell-quote 0.7.2 utf8/Char#CarriageReturn#, |
| 41 | TABrust-analyzer cargo shell-quote 0.7.2 utf8/TAB. => HorizontalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#HorizontalTab#, |
| 42 | VTrust-analyzer cargo shell-quote 0.7.2 utf8/VT. => VerticalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#VerticalTab#, |
| 43 | |
| 44 | // ASCII control characters, the rest. |
| 45 | 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 utf8/Char#Control#(ascii), |
| 46 | |
| 47 | // ASCII printable characters that can have dedicated backslash |
| 48 | // sequences when quoted or otherwise need some special treatment. |
| 49 | b'\\' => Backslashrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backslash#, |
| 50 | b'\'' => SingleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#SingleQuote#, |
| 51 | b'\"' => DoubleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#DoubleQuote#, |
| 52 | DELrust-analyzer cargo shell-quote 0.7.2 utf8/DEL. => Deleterust-analyzer cargo shell-quote 0.7.2 utf8/Char#Delete#, |
| 53 | |
| 54 | // ASCII printable letters, numbers, and "safe" punctuation. |
| 55 | 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 utf8/Char#PrintableInert#(ascii), |
| 56 | b',' | b'.' | b'/' | b'_' | b'-' => PrintableInertrust-analyzer cargo shell-quote 0.7.2 utf8/Char#PrintableInert#(ascii), |
| 57 | |
| 58 | // ASCII punctuation which can have significance in the shell. |
| 59 | b'|' | b'&' | b';' | b'(' | b')' | b'<' | b'>' => Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ascii), |
| 60 | b' ' | b'?' | b'[' | b']' | b'{' | b'}' | b'`' => Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ascii), |
| 61 | b'~' | b'!' | b'$' | b'@' | b'+' | b'=' | b'*' => Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ascii), |
| 62 | b'%' | b'#' | b':' | b'^' => Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ascii), |
| 63 | |
| 64 | // UTF-8 sequences. |
| 65 | 0x80..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xff => Utf8rust-analyzer cargo shell-quote 0.7.2 utf8/Char#Utf8#(ch), |
| 66 | }, |
| 67 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Utf8rust-analyzer cargo shell-quote 0.7.2 utf8/Char#Utf8#(ch), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[inline] |
| 72 | pub fn is_inertrust-analyzer cargo shell-quote 0.7.2 utf8/impl#[Char]is_inert().(&self) -> bool { |
| 73 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::PrintableInertrust-analyzer cargo shell-quote 0.7.2 utf8/Char#PrintableInert#(_)) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const BELrust-analyzer cargo shell-quote 0.7.2 utf8/BEL.: u8 = 0x07; // -> \a |
| 78 | const BSrust-analyzer cargo shell-quote 0.7.2 utf8/BS.: u8 = 0x08; // -> \b |
| 79 | const TABrust-analyzer cargo shell-quote 0.7.2 utf8/TAB.: u8 = 0x09; // -> \t |
| 80 | const LFrust-analyzer cargo shell-quote 0.7.2 utf8/LF.: u8 = 0x0A; // -> \n |
| 81 | const VTrust-analyzer cargo shell-quote 0.7.2 utf8/VT.: u8 = 0x0B; // -> \v |
| 82 | const FFrust-analyzer cargo shell-quote 0.7.2 utf8/FF.: u8 = 0x0C; // -> \f |
| 83 | const CRrust-analyzer cargo shell-quote 0.7.2 utf8/CR.: u8 = 0x0D; // -> \r |
| 84 | const ESCrust-analyzer cargo shell-quote 0.7.2 utf8/ESC.: u8 = 0x1B; // -> \e |
| 85 | const DELrust-analyzer cargo shell-quote 0.7.2 utf8/DEL.: u8 = 0x7F; |