| 1 | #![cfg(feature = "fish")] |
| 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 fish. |
| 6 | /// |
| 7 | /// # ⚠️ Warning |
| 8 | /// |
| 9 | /// Prior to version 3.6.2, fish did not correctly handle some Unicode code |
| 10 | /// points encoded as UTF-8. From the [version 3.6.2 release notes][]: |
| 11 | /// |
| 12 | /// > fish uses certain Unicode non-characters internally for marking wildcards |
| 13 | /// > and expansions. It incorrectly allowed these markers to be read on command |
| 14 | /// > substitution output, rather than transforming them into a safe internal |
| 15 | /// > representation. |
| 16 | /// |
| 17 | /// [version 3.6.2 release notes]: |
| 18 | /// https://github.com/fish-shell/fish-shell/releases/tag/3.6.2 |
| 19 | /// |
| 20 | /// At present this crate has **no workaround** for this issue. Please use fish |
| 21 | /// 3.6.2 or later. |
| 22 | /// |
| 23 | /// # Notes |
| 24 | /// |
| 25 | /// The documentation on [quoting][] and [escaping characters][] in fish is |
| 26 | /// confusing at first, especially when coming from a Bourne-like shell, but |
| 27 | /// essentially we have to be able to move and and out of a quoted string |
| 28 | /// context. For example, the escape sequence `\t` for a tab _must_ be outside |
| 29 | /// of quotes, single or double, to be recognised as a tab character by fish: |
| 30 | /// |
| 31 | /// ```fish |
| 32 | /// echo 'foo'\t'bar' |
| 33 | /// ``` |
| 34 | /// |
| 35 | /// This emphasises the importance of using the correct quoting module for the |
| 36 | /// target shell. |
| 37 | /// |
| 38 | /// [quoting]: https://fishshell.com/docs/current/language.html#quotes |
| 39 | /// [escaping characters]: |
| 40 | /// https://fishshell.com/docs/current/language.html#escaping-characters |
| 41 | #[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:)] |
| 42 | pub struct Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish#; |
| 43 | |
| 44 | 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 Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish# { |
| 45 | fn quote_intorust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`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>) { |
| 46 | Selfrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`QuoteInto<Vec<u8>>`]::quote_into_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]quote_into_vec().(s, out); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | 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 Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish# { |
| 51 | fn quote_intorust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`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#) { |
| 52 | Selfrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`QuoteInto<String>`]::quote_into_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]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().() }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[cfg(unix)] |
| 57 | 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 Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish# { |
| 58 | fn quote_intorust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`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#) { |
| 59 | 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#; |
| 60 | let s = Selfrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`QuoteInto<std::ffi::OsString>`]::quote_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]quote_vec().(s); |
| 61 | 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); |
| 62 | out.pushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString]push().(s); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[cfg(feature = "bstr")] |
| 67 | 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 Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish# { |
| 68 | fn quote_intorust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`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#) { |
| 69 | let s = Selfrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish][`QuoteInto<bstr::BString>`]::quote_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]quote_vec().(s); |
| 70 | out.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(s); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl Fishrust-analyzer cargo shell-quote 0.7.2 fish/Fish# { |
| 75 | /// Quote a string of bytes into a new `Vec<u8>`. |
| 76 | /// |
| 77 | /// This will return one of the following: |
| 78 | /// - The string as-is, if no escaping is necessary. |
| 79 | /// - An escaped string, like `'foo \'bar'`, `\a'ABC'` |
| 80 | /// |
| 81 | /// See [`quote_into_vec`][`Self::quote_into_vec`] for a variant that |
| 82 | /// extends an existing `Vec` instead of allocating a new one. |
| 83 | /// |
| 84 | /// # Examples |
| 85 | /// |
| 86 | /// ``` |
| 87 | /// # use shell_quote::Fish; |
| 88 | /// assert_eq!(Fish::quote_vec("foobar"), b"foobar"); |
| 89 | /// assert_eq!(Fish::quote_vec("foo 'bar"), b"foo' \\'bar'"); |
| 90 | /// ``` |
| 91 | pub fn quote_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]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> { |
| 92 | match s.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() { |
| 93 | 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 fish/bytes/::escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/bytes/escape_prepare().(bytes) { |
| 94 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Empty# => vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b'\'', b'\''], |
| 95 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Inert# => bytes.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 96 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Escape#(esc) => { |
| 97 | 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().(); |
| 98 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/bytes/escape_chars().(esc, &mut sout); |
| 99 | sout |
| 100 | } |
| 101 | }, |
| 102 | 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 fish/text/::escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/text/escape_prepare().(text) { |
| 103 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Empty# => vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![b'\'', b'\''], |
| 104 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Inert# => text.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 105 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Escape#(esc) => { |
| 106 | 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().(); |
| 107 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/text/escape_chars().(esc, &mut sout); |
| 108 | sout |
| 109 | } |
| 110 | }, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// Quote a string of bytes into an existing `Vec<u8>`. |
| 115 | /// |
| 116 | /// See [`quote_vec`][`Self::quote_vec`] for more details. |
| 117 | /// |
| 118 | /// # Examples |
| 119 | /// |
| 120 | /// ``` |
| 121 | /// # use shell_quote::Fish; |
| 122 | /// let mut buf = Vec::with_capacity(128); |
| 123 | /// Fish::quote_into_vec("foobar", &mut buf); |
| 124 | /// buf.push(b' '); // Add a space. |
| 125 | /// Fish::quote_into_vec("foo 'bar", &mut buf); |
| 126 | /// assert_eq!(buf, b"foobar foo' \\'bar'"); |
| 127 | /// ``` |
| 128 | /// |
| 129 | pub fn quote_into_vecrust-analyzer cargo shell-quote 0.7.2 fish/impl#[Fish]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>) { |
| 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 fish/bytes/::escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/bytes/escape_prepare().(bytes) { |
| 132 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/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"''"), |
| 133 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/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), |
| 134 | bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Escape#(esc) => bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/::escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/bytes/escape_chars().(esc, sout), |
| 135 | }, |
| 136 | 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 fish/text/::escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/text/escape_prepare().(text) { |
| 137 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/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"''"), |
| 138 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/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().()), |
| 139 | textrust-analyzer cargo shell-quote 0.7.2 fish/text/::Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Escape#(esc) => textrust-analyzer cargo shell-quote 0.7.2 fish/text/::escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/text/escape_chars().(esc, sout), |
| 140 | }, |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | |
| 147 | mod bytesrust-analyzer cargo shell-quote 0.7.2 fish/bytes/ { |
| 148 | use superrust-analyzer cargo shell-quote 0.7.2 fish/::u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().; |
| 149 | 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#; |
| 150 | |
| 151 | pub enum Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared# { |
| 152 | Emptyrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Empty#, |
| 153 | Inertrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Inert#, |
| 154 | Escaperust-analyzer cargo shell-quote 0.7.2 fish/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#>), |
| 155 | } |
| 156 | |
| 157 | pub fn escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/bytes/escape_prepare().(sin: &[u8]) -> Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared# { |
| 158 | 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().(); |
| 159 | // An optimisation: if the string is not empty and contains only "safe" |
| 160 | // characters we can avoid further work. |
| 161 | if esc.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 162 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Empty# |
| 163 | } 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().) { |
| 164 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Inert# |
| 165 | } else { |
| 166 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/bytes/Prepared#Escape#(esc) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | pub fn escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/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>) { |
| 171 | #[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:)] |
| 172 | enum QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle# { |
| 173 | Insiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Inside#, |
| 174 | Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, |
| 175 | Whateverrust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Whatever#, |
| 176 | } |
| 177 | use QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#::*; |
| 178 | |
| 179 | let mut inside_quotes_now = false; |
| 180 | let mut push_literal = |style: QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#, literal: &[u8]| { |
| 181 | match (inside_quotes_now, style) { |
| 182 | (true, Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#) => { |
| 183 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 184 | inside_quotes_now = false; |
| 185 | } |
| 186 | (false, Insiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Inside#) => { |
| 187 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 188 | inside_quotes_now = true; |
| 189 | } |
| 190 | _ => (), |
| 191 | } |
| 192 | sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(literal); |
| 193 | }; |
| 194 | for mode in esc { |
| 195 | use Charrust-analyzer cargo shell-quote 0.7.2 ascii/Char#::*; |
| 196 | match mode { |
| 197 | Bellrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Bell# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\a"), |
| 198 | Backspacerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backspace# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\b"), |
| 199 | Escaperust-analyzer cargo shell-quote 0.7.2 ascii/Char#Escape# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\e"), |
| 200 | FormFeedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#FormFeed# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\f"), |
| 201 | NewLinerust-analyzer cargo shell-quote 0.7.2 ascii/Char#NewLine# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\n"), |
| 202 | CarriageReturnrust-analyzer cargo shell-quote 0.7.2 ascii/Char#CarriageReturn# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\r"), |
| 203 | HorizontalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#HorizontalTab# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\t"), |
| 204 | VerticalTabrust-analyzer cargo shell-quote 0.7.2 ascii/Char#VerticalTab# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\v"), |
| 205 | Controlrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Control#(ch) => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, &u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().(ch)), |
| 206 | Backslashrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Backslash# => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Whatever#, b"\\\\"), |
| 207 | SingleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#SingleQuote# => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Whatever#, b"\\'"), |
| 208 | DoubleQuoterust-analyzer cargo shell-quote 0.7.2 ascii/Char#DoubleQuote# => push_literal(Insiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Inside#, b"\""), |
| 209 | Deleterust-analyzer cargo shell-quote 0.7.2 ascii/Char#Delete# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, b"\\X7F"), |
| 210 | PrintableInertrust-analyzer cargo shell-quote 0.7.2 ascii/Char#PrintableInert#(ch) => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Whatever#, &ch.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]to_le_bytes().()), |
| 211 | Printablerust-analyzer cargo shell-quote 0.7.2 ascii/Char#Printable#(ch) => push_literal(Insiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Inside#, &ch.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]to_le_bytes().()), |
| 212 | Extendedrust-analyzer cargo shell-quote 0.7.2 ascii/Char#Extended#(ch) => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/bytes/QuoteStyle#Outside#, &u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().(ch)), |
| 213 | } |
| 214 | } |
| 215 | if inside_quotes_now { |
| 216 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // ---------------------------------------------------------------------------- |
| 222 | |
| 223 | mod textrust-analyzer cargo shell-quote 0.7.2 fish/text/ { |
| 224 | use superrust-analyzer cargo shell-quote 0.7.2 fish/::u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().; |
| 225 | 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#; |
| 226 | |
| 227 | pub enum Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared# { |
| 228 | Emptyrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Empty#, |
| 229 | Inertrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Inert#, |
| 230 | Escaperust-analyzer cargo shell-quote 0.7.2 fish/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#>), |
| 231 | } |
| 232 | |
| 233 | pub fn escape_preparerust-analyzer cargo shell-quote 0.7.2 fish/text/escape_prepare().(sin: &str) -> Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared# { |
| 234 | 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().(); |
| 235 | // An optimisation: if the string is not empty and contains only "safe" |
| 236 | // characters we can avoid further work. |
| 237 | if esc.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 238 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Emptyrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Empty# |
| 239 | } 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().) { |
| 240 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Inertrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Inert# |
| 241 | } else { |
| 242 | Preparedrust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#::Escaperust-analyzer cargo shell-quote 0.7.2 fish/text/Prepared#Escape#(esc) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | pub fn escape_charsrust-analyzer cargo shell-quote 0.7.2 fish/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>) { |
| 247 | #[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:)] |
| 248 | enum QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle# { |
| 249 | Insiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Inside#, |
| 250 | Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, |
| 251 | Whateverrust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Whatever#, |
| 252 | } |
| 253 | use QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#::*; |
| 254 | |
| 255 | let mut inside_quotes_now = false; |
| 256 | let mut push_literal = |style: QuoteStylerust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#, literal: &[u8]| { |
| 257 | match (inside_quotes_now, style) { |
| 258 | (true, Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#) => { |
| 259 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 260 | inside_quotes_now = false; |
| 261 | } |
| 262 | (false, Insiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Inside#) => { |
| 263 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 264 | inside_quotes_now = true; |
| 265 | } |
| 266 | _ => (), |
| 267 | } |
| 268 | sout.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(literal); |
| 269 | }; |
| 270 | let buf = &mut [0u8; 4]; |
| 271 | for mode in esc { |
| 272 | use Charrust-analyzer cargo shell-quote 0.7.2 utf8/Char#::*; |
| 273 | match mode { |
| 274 | Bellrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Bell# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\a"), |
| 275 | Backspacerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backspace# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\b"), |
| 276 | Escaperust-analyzer cargo shell-quote 0.7.2 utf8/Char#Escape# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\e"), |
| 277 | FormFeedrust-analyzer cargo shell-quote 0.7.2 utf8/Char#FormFeed# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\f"), |
| 278 | NewLinerust-analyzer cargo shell-quote 0.7.2 utf8/Char#NewLine# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\n"), |
| 279 | CarriageReturnrust-analyzer cargo shell-quote 0.7.2 utf8/Char#CarriageReturn# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\r"), |
| 280 | HorizontalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#HorizontalTab# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\t"), |
| 281 | VerticalTabrust-analyzer cargo shell-quote 0.7.2 utf8/Char#VerticalTab# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\v"), |
| 282 | Controlrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Control#(ch) => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, &u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().(ch)), |
| 283 | Backslashrust-analyzer cargo shell-quote 0.7.2 utf8/Char#Backslash# => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Whatever#, b"\\\\"), |
| 284 | SingleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#SingleQuote# => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Whatever#, b"\\'"), |
| 285 | DoubleQuoterust-analyzer cargo shell-quote 0.7.2 utf8/Char#DoubleQuote# => push_literal(Insiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Inside#, b"\""), |
| 286 | Deleterust-analyzer cargo shell-quote 0.7.2 utf8/Char#Delete# => push_literal(Outsiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Outside#, b"\\X7F"), |
| 287 | PrintableInertrust-analyzer cargo shell-quote 0.7.2 utf8/Char#PrintableInert#(ch) => push_literal(Whateverrust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Whatever#, &ch.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]to_le_bytes().()), |
| 288 | Printablerust-analyzer cargo shell-quote 0.7.2 utf8/Char#Printable#(ch) => push_literal(Insiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Inside#, &ch.to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]to_le_bytes().()), |
| 289 | Utf8rust-analyzer cargo shell-quote 0.7.2 utf8/Char#Utf8#(char) => push_literal(Insiderust-analyzer cargo shell-quote 0.7.2 fish/text/QuoteStyle#Inside#, char.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().()), |
| 290 | } |
| 291 | } |
| 292 | if inside_quotes_now { |
| 293 | sout.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\''); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // ---------------------------------------------------------------------------- |
| 299 | |
| 300 | /// Escape a byte as a 4-byte hex escape sequence _with uppercase "X"_. |
| 301 | /// |
| 302 | /// The `\\XHH` format (backslash, a literal "X", two hex characters) is |
| 303 | /// understood by fish. The `\\xHH` format is _also_ understood, but until fish |
| 304 | /// 3.6.0 it had a weirdness. From the [release notes][]: |
| 305 | /// |
| 306 | /// > The `\\x` and `\\X` escape syntax is now equivalent. `\\xAB` previously |
| 307 | /// > behaved the same as `\\XAB`, except that it would error if the value “AB” |
| 308 | /// > was larger than “7f” (127 in decimal, the highest ASCII value). |
| 309 | /// |
| 310 | /// [release notes]: https://github.com/fish-shell/fish-shell/releases/tag/3.6.0 |
| 311 | /// |
| 312 | #[inline] |
| 313 | fn u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().(ch: u8) -> [u8; 4] { |
| 314 | const HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 fish/HEX_DIGITS.: &[u8] = b"0123456789ABCDEF"; |
| 315 | [ |
| 316 | b'\\', |
| 317 | b'X', |
| 318 | HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 fish/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()., |
| 319 | HEX_DIGITSrust-analyzer cargo shell-quote 0.7.2 fish/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()., |
| 320 | ] |
| 321 | } |
| 322 | |
| 323 | #[cfg(test)] |
| 324 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 325 | fn test_u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/test_u8_to_hex_escape_uppercase_x().() { |
| 326 | 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. { |
| 327 | let expected = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\\X{ch:02X}"); |
| 328 | let observed = u8_to_hex_escape_uppercase_xrust-analyzer cargo shell-quote 0.7.2 fish/u8_to_hex_escape_uppercase_x().(ch); |
| 329 | 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().(); |
| 330 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(observed, &expected); |
| 331 | } |
| 332 | } |