Debian Codegraph @

src/bash.rs

raw symbols SCIP
1 #![cfg(feature = "bash")]
2
3 use craterust-analyzer cargo shell-quote 0.7.2 crate/::{Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#, QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#};
4
5 /// Quote byte strings for use with Bash, the GNU Bourne-Again Shell.
6 ///
7 /// # Compatibility
8 ///
9 /// Quoted/escaped strings produced by [`Bash`] work in both Bash and Z Shell.
10 ///
11 /// # ⚠️ Warning
12 ///
13 /// It is _possible_ to encode NUL in a Bash string, but Bash appears to then
14 /// truncate the rest of the string after that point **or** sometimes it filters
15 /// the NUL out. It's not yet clear to me when/why each behaviour is chosen.
16 ///
17 /// If you're quoting UTF-8 content this may not be a problem since there is
18 /// only one code point – the null character itself – that will ever produce a
19 /// NUL byte. To avoid this problem entirely, consider using [Modified
20 /// UTF-8][modified-utf-8] so that the NUL byte can never appear in a valid byte
21 /// stream.
22 ///
23 /// [modified-utf-8]: https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
24 ///
25 /// # Notes
26 ///
27 /// From bash(1):
28 ///
29 /// Words of the form $'string' are treated specially. The word expands to
30 /// string, with backslash-escaped characters replaced as specified by the
31 /// ANSI C standard. Backslash escape sequences, if present, are decoded as
32 /// follows:
33 ///
34 /// ```text
35 /// \a alert (bell)
36 /// \b backspace
37 /// \e an escape character
38 /// \f form feed
39 /// \n new line
40 /// \r carriage return
41 /// \t horizontal tab
42 /// \v vertical tab
43 /// \\ backslash
44 /// \' single quote
45 /// \nnn the eight-bit character whose value is the
46 /// octal value nnn (one to three digits)
47 /// \xHH the eight-bit character whose value is the
48 /// hexadecimal value HH (one or two hex digits)
49 /// \cx a control-x character
50 /// ```
51 ///
52 /// Bash allows, in newer versions, for non-ASCII Unicode characters with
53 /// `\uHHHH` and `\UXXXXXXXX` syntax inside these [ANSI C quoted
54 /// strings][ansi-c-quoting], but we avoid this and work only with bytes. Part
55 /// of the problem is that it's not clear how Bash then works with these
56 /// strings. Does it encode these characters into bytes according to the user's
57 /// current locale? Are strings in Bash now natively Unicode?
58 ///
59 /// For now it's up to the caller to figure out encoding. A significant use case
60 /// for this code is to quote filenames into scripts, and on *nix variants I
61 /// understand that filenames are essentially arrays of bytes, even if the OS
62 /// adds some normalisation and case-insensitivity on top.
63 ///
64 /// [ansi-c-quoting]:
65 /// https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
66 ///
67 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
68 pub struct Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash#;
69
70 // ----------------------------------------------------------------------------
71
72 impl QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> for Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash# {
73 fn quote_intorust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<Vec<u8>>`]quote_into().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S, out: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
74 Selfrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<Vec<u8>>`]::quote_into_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_into_vec().(s, out);
75 }
76 }
77
78 impl QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash# {
79 fn quote_intorust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<String>`]quote_into().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S, out: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
80 Selfrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<String>`]::quote_into_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_into_vec().(s, unsafe { out.as_mut_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_mut_vec().() })
81 }
82 }
83
84 #[cfg(unix)]
85 impl QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#> for Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash# {
86 fn quote_intorust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<std::ffi::OsString>`]quote_into().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S, out: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#) {
87 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStringExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStringExt#;
88 let s = Selfrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<std::ffi::OsString>`]::quote_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_vec().(s);
89 let s = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#::from_vecrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsString][OsStringExt]from_vec().(s);
90 out.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString]push().(s);
91 }
92 }
93
94 #[cfg(feature = "bstr")]
95 impl QuoteIntorust-analyzer cargo shell-quote 0.7.2 QuoteInto#<bstrrust-analyzer cargo bstr 1.13.1 crate/::BStringrust-analyzer cargo bstr 1.13.1 bstring/BString#> for Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash# {
96 fn quote_intorust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<bstr::BString>`]quote_into().<'q, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'q>>>(s: S, out: &mut bstrrust-analyzer cargo bstr 1.13.1 crate/::BStringrust-analyzer cargo bstr 1.13.1 bstring/BString#) {
97 let s = Selfrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash][`QuoteInto<bstr::BString>`]::quote_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_vec().(s);
98 out.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(s);
99 }
100 }
101
102 // ----------------------------------------------------------------------------
103
104 impl Bashrust-analyzer cargo shell-quote 0.7.2 bash/Bash# {
105 /// Quote a string of bytes into a new `Vec<u8>`.
106 ///
107 /// This will return one of the following:
108 /// - The string as-is, if no escaping is necessary.
109 /// - An [ANSI-C escaped string][ansi-c-quoting], like `$'foo\nbar'`.
110 ///
111 /// See [`quote_into_vec`][`Self::quote_into_vec`] for a variant that
112 /// extends an existing `Vec` instead of allocating a new one.
113 ///
114 /// # Examples
115 ///
116 /// ```
117 /// # use shell_quote::Bash;
118 /// assert_eq!(Bash::quote_vec("foobar"), b"foobar");
119 /// assert_eq!(Bash::quote_vec("foo bar"), b"$'foo bar'");
120 /// ```
121 ///
122 /// [ansi-c-quoting]:
123 /// https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
124 ///
125 pub fn quote_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_vec().<'a, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a>>>(s: S) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> {
126 // Here, previously, in the `Escape` cases, an optimisation
127 // precalculated the required capacity of the output `Vec` to avoid
128 // reallocations later on, but benchmarks showed that it was slower. It
129 // _may_ have lowered maximum RAM required, but that was not measured.
130 match s.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() {
131 Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(bytes) => match bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_prepare().(bytes) {
132 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Empty# => vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b'\'', b'\''],
133 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Inert# => bytes.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
134 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Escape#(esc) => {
135 let mut sout = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
136 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_chars().(esc, &mut sout);
137 sout
138 }
139 },
140 Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Textrust-analyzer cargo shell-quote 0.7.2 Quotable#Text#(text) => match textrust-analyzer cargo shell-quote 0.7.2 bash/text/::escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/text/escape_prepare().(text) {
141 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Empty# => vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b'\'', b'\''],
142 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Inert# => text.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
143 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Escape#(esc) => {
144 let mut sout = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
145 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/text/escape_chars().(esc, &mut sout);
146 sout
147 }
148 },
149 }
150 }
151
152 /// Quote a string of bytes into an existing `Vec<u8>`.
153 ///
154 /// See [`quote_vec`][`Self::quote_vec`] for more details.
155 ///
156 /// # Examples
157 ///
158 /// ```
159 /// # use shell_quote::Bash;
160 /// let mut buf = Vec::with_capacity(128);
161 /// Bash::quote_into_vec("foobar", &mut buf);
162 /// buf.push(b' '); // Add a space.
163 /// Bash::quote_into_vec("foo bar", &mut buf);
164 /// assert_eq!(buf, b"foobar $'foo bar'");
165 /// ```
166 ///
167 pub fn quote_into_vecrust-analyzer cargo shell-quote 0.7.2 bash/impl#[Bash]quote_into_vec().<'a, S: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#<'a>>>(s: S, sout: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
168 // Here, previously, in the `Escape` cases, an optimisation
169 // precalculated the required capacity of the output `Vec` to avoid
170 // reallocations later on, but benchmarks showed that it was slower. It
171 // _may_ have lowered maximum RAM required, but that was not measured.
172 match s.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() {
173 Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Bytesrust-analyzer cargo shell-quote 0.7.2 Quotable#Bytes#(bytes) => match bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_prepare().(bytes) {
174 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Empty# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"''"),
175 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Inert# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(bytes),
176 bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Escape#(esc) => bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/::escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_chars().(esc, sout),
177 },
178 Quotablerust-analyzer cargo shell-quote 0.7.2 Quotable#::Textrust-analyzer cargo shell-quote 0.7.2 Quotable#Text#(text) => match textrust-analyzer cargo shell-quote 0.7.2 bash/text/::escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/text/escape_prepare().(text) {
179 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Empty# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"''"),
180 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Inert# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(text.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()),
181 textrust-analyzer cargo shell-quote 0.7.2 bash/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Escape#(esc) => textrust-analyzer cargo shell-quote 0.7.2 bash/text/::escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/text/escape_chars().(esc, sout),
182 },
183 }
184 }
185 }
186
187 // ----------------------------------------------------------------------------
188
189 mod bytesrust-analyzer cargo shell-quote 0.7.2 bash/bytes/ {
190 use superrust-analyzer cargo shell-quote 0.7.2 bash/::u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().;
191 use craterust-analyzer cargo shell-quote 0.7.2 crate/::asciirust-analyzer cargo shell-quote 0.7.2 ascii/::Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#;
192
193 pub enum Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared# {
194 Emptyrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Empty#,
195 Inertrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Inert#,
196 Escaperust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Escape#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#>),
197 }
198
199 pub fn escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_prepare().(sin: &[u8]) -> Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared# {
200 let esc: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = sin.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::fromrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
201 // An optimisation: if the string is not empty and contains only "safe"
202 // characters we can avoid further work.
203 if esc.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
204 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Empty#
205 } else if esc.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::is_inertrust-analyzer cargo shell-quote 0.7.2 ascii/impl#[Char]is_inert().) {
206 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Inert#
207 } else {
208 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/bytes/Prepared#Escape#(esc)
209 }
210 }
211
212 pub fn escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/bytes/escape_chars().(esc: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#>, sout: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
213 // Push a Bash-style $'...' quoted string into `sout`.
214 sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"$'");
215 for mode in esc {
216 use Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::*;
217 match mode {
218 Bellrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Bell# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\a"),
219 Backspacerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backspace# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\b"),
220 Escaperust-analyzer cargo shell-quote 0.7.2 ascii/Char#Escape# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\e"),
221 FormFeedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#FormFeed# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\f"),
222 NewLinerust-analyzer cargo shell-quote 0.7.2 ascii/Char#NewLine# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\n"),
223 CarriageReturnrust-analyzer cargo shell-quote 0.7.2 ascii/Char#CarriageReturn# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\r"),
224 HorizontalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#HorizontalTab# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\t"),
225 VerticalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#VerticalTab# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\v"),
226 Controlrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Control#(ch) => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().(ch)),
227 Backslashrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backslash# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\\\"),
228 SingleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#SingleQuote# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\'"),
229 DoubleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#DoubleQuote# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\""),
230 Deleterust-analyzer cargo shell-quote 0.7.2 ascii/Char#Delete# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\x7F"),
231 PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(ch) => sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ch),
232 Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch) => sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ch),
233 Extendedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Extended#(ch) => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().(ch)),
234 }
235 }
236 sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\'');
237 }
238 }
239
240 // ----------------------------------------------------------------------------
241
242 mod textrust-analyzer cargo shell-quote 0.7.2 bash/text/ {
243 use superrust-analyzer cargo shell-quote 0.7.2 bash/::u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().;
244 use craterust-analyzer cargo shell-quote 0.7.2 crate/::utf8rust-analyzer cargo shell-quote 0.7.2 utf8/::Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#;
245
246 pub enum Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared# {
247 Emptyrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Empty#,
248 Inertrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Inert#,
249 Escaperust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Escape#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#>),
250 }
251
252 pub fn escape_preparerust-analyzer cargo shell-quote 0.7.2 bash/text/escape_prepare().(sin: &str) -> Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared# {
253 let esc: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = sin.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::fromrust-analyzer cargo shell-quote 0.7.2 utf8/impl#[Char]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
254 // An optimisation: if the string is not empty and contains only "safe"
255 // characters we can avoid further work.
256 if esc.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
257 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Empty#
258 } else if esc.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::is_inertrust-analyzer cargo shell-quote 0.7.2 utf8/impl#[Char]is_inert().) {
259 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Inert#
260 } else {
261 Preparedrust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 bash/text/Prepared#Escape#(esc)
262 }
263 }
264
265 pub fn escape_charsrust-analyzer cargo shell-quote 0.7.2 bash/text/escape_chars().(esc: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#>, sout: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) {
266 // Push a Bash-style $'...' quoted string into `sout`.
267 sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"$'");
268 let buf = &mut [0u8; 4];
269 for mode in esc {
270 use Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::*;
271 match mode {
272 Bellrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Bell# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\a"),
273 Backspacerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backspace# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\b"),
274 Escaperust-analyzer cargo shell-quote 0.7.2 utf8/Char#Escape# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\e"),
275 FormFeedrust-analyzer cargo shell-quote 0.7.2 utf8/Char#FormFeed# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\f"),
276 NewLinerust-analyzer cargo shell-quote 0.7.2 utf8/Char#NewLine# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\n"),
277 CarriageReturnrust-analyzer cargo shell-quote 0.7.2 utf8/Char#CarriageReturn# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\r"),
278 HorizontalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#HorizontalTab# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\t"),
279 VerticalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#VerticalTab# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\v"),
280 Controlrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Control#(ch) => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().(ch)),
281 Backslashrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backslash# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\\\"),
282 SingleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#SingleQuote# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\'"),
283 DoubleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#DoubleQuote# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\""),
284 Deleterust-analyzer cargo shell-quote 0.7.2 utf8/Char#Delete# => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(b"\\x7F"),
285 PrintableInertrust-analyzer cargo shell-quote 0.7.2 utf8/Char#PrintableInert#(ch) => sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ch),
286 Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ch) => sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ch),
287 Utf8rust-analyzer cargo shell-quote 0.7.2 utf8/Char#Utf8#(ch) => sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(ch.encode_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]encode_utf8().(buf).as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()),
288 }
289 }
290 sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\'');
291 }
292 }
293
294 // ----------------------------------------------------------------------------
295
296 /// Escape a byte as a 4-byte hex escape sequence.
297 ///
298 /// The `\\xHH` format (backslash, a literal "x", two hex characters) is
299 /// understood by many shells.
300 #[inline]
301 fn u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().(ch: u8) -> [u8; 4] {
302 const HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 bash/HEX_DIGITS.: &[u8] = b"0123456789ABCDEF";
303 [
304 b'\\',
305 b'x',
306 HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 bash/HEX_DIGITS.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().(ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr().4) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
307 HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 bash/HEX_DIGITS.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().(ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0xF) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
308 ]
309 }
310
311 #[cfg(test)]
312 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
313 fn test_u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/test_u8_to_hex_escape().() {
314 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. {
315 let expected = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\\x{ch:02X}");
316 let observed = u8_to_hex_escaperust-analyzer cargo shell-quote 0.7.2 bash/u8_to_hex_escape().(ch);
317 let observed = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(&observed).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
318 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(observed, &expected);
319 }
320 }