| 1 | //! Efficiently split lines by whitespace, while handling the backslash |
| 2 | //! escape sequences in Rust-like string format. |
| 3 | //! |
| 4 | //! For example, if you had a string like: |
| 5 | //! ```text |
| 6 | //! One\ two three\ four |
| 7 | //! ``` |
| 8 | //! Naïve splitting on whitespace would produce four outputs: |
| 9 | //! * `One\` |
| 10 | //! * `two` |
| 11 | //! * `three\` |
| 12 | //! * `four` |
| 13 | //! |
| 14 | //! This crate will instead produce two strings: |
| 15 | //! * `One two` |
| 16 | //! * `three four` |
| 17 | //! |
| 18 | //! This crate also handles special escape sequences like "\n", which represents a newline. |
| 19 | //! Specifically, the escape sequences are: |
| 20 | //! |
| 21 | //! * `\a` |
| 22 | //! * `\b` |
| 23 | //! * `\t` |
| 24 | //! * `\n` |
| 25 | //! * `\v` |
| 26 | //! * `\f` |
| 27 | //! * `\r` |
| 28 | //! * `\\` |
| 29 | //! |
| 30 | //! If the backslash character is found, but the successive character is not special, |
| 31 | //! then the backslash is disregarded and the successive character is included verbatim. |
| 32 | |
| 33 | 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/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 34 | |
| 35 | /// Splits once |
| 36 | /// |
| 37 | /// Returns a tuple of the first "word" up until the first unescaped whitespace character, |
| 38 | /// and then every after the whitespace characters. |
| 39 | pub fn split_one_bytesrust-analyzer cargo escape_string 0.1.2 split_one_bytes().<'a>(bytes: &'a [u8]) |
| 40 | -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, [u8]>, &'a [u8])> |
| 41 | { |
| 42 | let mut start = 0usize; |
| 43 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b) = bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(start) |
| 44 | { |
| 45 | if b.is_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_whitespace().() |
| 46 | { start+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; } |
| 47 | else |
| 48 | { break; } |
| 49 | } |
| 50 | |
| 51 | let mut owned: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 52 | |
| 53 | let mut position = start; |
| 54 | |
| 55 | while position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 56 | { |
| 57 | if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().position]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'\\' |
| 58 | { |
| 59 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().owned.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() |
| 60 | { |
| 61 | owned = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#position]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`][ToOwned]to_owned().()); |
| 62 | } |
| 63 | let b = owned.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 64 | position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 65 | match bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(position) |
| 66 | { |
| 67 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 68 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'a') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\x07'), |
| 69 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'b') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\x08'), |
| 70 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b't') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\t'), |
| 71 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'n') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\n'), |
| 72 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'v') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\x0b'), |
| 73 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'f') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\x0c'), |
| 74 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'r') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\r'), |
| 75 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b' ') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b' '), |
| 76 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'\\') => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\\'), |
| 77 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(a) => b.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().a), |
| 78 | } |
| 79 | position+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 80 | } |
| 81 | else if bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().position]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..is_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_whitespace().() |
| 82 | { |
| 83 | break; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(o) = owned.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() |
| 88 | { o.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().( bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().position]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). ); } |
| 89 | position rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | let mut after = position; |
| 94 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b) = bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(after) |
| 95 | { |
| 96 | if b.is_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_whitespace().() |
| 97 | { after+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; } |
| 98 | else |
| 99 | { break; } |
| 100 | } |
| 101 | |
| 102 | let after = &bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().after..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 103 | |
| 104 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(owned) = owned |
| 105 | { |
| 106 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#( (Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(owned), after) ) |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#( (Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#position]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().), after) ) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// Split some text by unescaped whitespace. |
| 115 | /// |
| 116 | /// find the first unescaped whitespace in `text`, return |
| 117 | /// a tuple of the text before the whitespace and the text after |
| 118 | /// the whitespace. |
| 119 | /// |
| 120 | /// Ignores prefixed whitespace and discards whitespace between |
| 121 | /// the first portion and the text after the whitespace. |
| 122 | /// |
| 123 | /// Returns None if there was an escape character and then nothing |
| 124 | /// |
| 125 | /// Does not look at the following text at all. |
| 126 | pub fn split_onerust-analyzer cargo escape_string 0.1.2 split_one().<'a>(text: &'a str) |
| 127 | -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>, &'a str)> |
| 128 | { |
| 129 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((one, remainder)) = split_one_bytesrust-analyzer cargo escape_string 0.1.2 split_one_bytes().(text.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) |
| 130 | { |
| 131 | let one_text; |
| 132 | match one |
| 133 | { |
| 134 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(b) => |
| 135 | one_text = unsafe { Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#( 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_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(b) ) }, |
| 136 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(b) => |
| 137 | one_text = unsafe { Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#( Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_uncheckedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_unchecked().(b) ) }, |
| 138 | } |
| 139 | let remainder = unsafe { 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_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(remainder) }; |
| 140 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((one_text, remainder)) |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Produce all the words as a vector |
| 149 | pub fn splitrust-analyzer cargo escape_string 0.1.2 split().<'a>(mut text: &'a str) |
| 150 | -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str>>> |
| 151 | { |
| 152 | let mut res = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!(); |
| 153 | |
| 154 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().text.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() |
| 155 | { |
| 156 | let s = split_onerust-analyzer cargo escape_string 0.1.2 split_one().(text); |
| 157 | if s.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; } |
| 158 | let s = s.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 159 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().( s.0 ); |
| 160 | text = s.1; |
| 161 | } |
| 162 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(res) |
| 163 | } |
| 164 | |
| 165 | /// Converts text with all the special characters escape with a backslash |
| 166 | pub fn escaperust-analyzer cargo escape_string 0.1.2 escape().<'a>(text: &'a str) |
| 167 | -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str> |
| 168 | { |
| 169 | let bytes = text.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(); |
| 170 | |
| 171 | let mut owned = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 172 | |
| 173 | for pos in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 174 | { |
| 175 | let special = |
| 176 | match bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 177 | { |
| 178 | 0x07 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'a'), |
| 179 | 0x08 => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'b'), |
| 180 | b'\t' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b't'), |
| 181 | b'\n' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'n'), |
| 182 | 0x0b => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'v'), |
| 183 | 0x0c => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'f'), |
| 184 | b'\r' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'r'), |
| 185 | b' ' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b' '), |
| 186 | b'\\' => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'\\'), |
| 187 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 188 | }; |
| 189 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(s) = special |
| 190 | { |
| 191 | if owned.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() |
| 192 | { |
| 193 | owned = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`][ToOwned]to_owned().()); |
| 194 | } |
| 195 | owned.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'\\'); |
| 196 | owned.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(s); |
| 197 | } |
| 198 | else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(owned) = owned.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() |
| 199 | { |
| 200 | owned.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().( bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(owned) = owned |
| 205 | { |
| 206 | unsafe { Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_uncheckedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_unchecked().(owned)) } |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | unsafe { Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(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_utf8_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8_unchecked().(bytes)) } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[cfg(test)] |
| 215 | mod testsrust-analyzer cargo escape_string 0.1.2 tests/ |
| 216 | { |
| 217 | use ::split_onerust-analyzer cargo escape_string 0.1.2 split_one().; |
| 218 | use ::splitrust-analyzer cargo escape_string 0.1.2 split().; |
| 219 | use ::escaperust-analyzer cargo escape_string 0.1.2 escape().; |
| 220 | |
| 221 | fn checkrust-analyzer cargo escape_string 0.1.2 tests/check().(text: &str, one: &str, two: &str) |
| 222 | { |
| 223 | let a = split_onerust-analyzer cargo escape_string 0.1.2 split_one().(text); |
| 224 | let a = a.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 225 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(a.0, one); |
| 226 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(a.1, two); |
| 227 | } |
| 228 | |
| 229 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 230 | fn failurerust-analyzer cargo escape_string 0.1.2 tests/failure().() |
| 231 | { |
| 232 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(split_onerust-analyzer cargo escape_string 0.1.2 split_one().("abc\\"), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); |
| 233 | } |
| 234 | |
| 235 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 236 | fn finerust-analyzer cargo escape_string 0.1.2 tests/fine().() |
| 237 | { |
| 238 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\\\", "abc\\", ""); |
| 239 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("1525824000000 520893", "1525824000000", "520893"); |
| 240 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\\\ def", "abc\\", "def"); |
| 241 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\\\\\\\ def", "abc\\\\", "def"); |
| 242 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\\\\\\\ def", "abc\\\\", "def"); |
| 243 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\ def ghi", "abc def", "ghi"); |
| 244 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("abc\\ def ghi", "abc def", "ghi"); |
| 245 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().("", "", ""); |
| 246 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().(" ", "", ""); |
| 247 | } |
| 248 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 249 | fn splittingrust-analyzer cargo escape_string 0.1.2 tests/splitting().() |
| 250 | { |
| 251 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}",splitrust-analyzer cargo escape_string 0.1.2 split().("abc\\\\")), "Some([\"abc\\\\\"])"); |
| 252 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}",splitrust-analyzer cargo escape_string 0.1.2 split().("abc def")), "Some([\"abc\", \"def\"])"); |
| 253 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}",splitrust-analyzer cargo escape_string 0.1.2 split().("abc\\ def")), "Some([\"abc def\"])"); |
| 254 | } |
| 255 | |
| 256 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 257 | fn escapingrust-analyzer cargo escape_string 0.1.2 tests/escaping().() |
| 258 | { |
| 259 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo escape_string 0.1.2 escape().("abc\ndef"), "abc\\ndef"); |
| 260 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(escaperust-analyzer cargo escape_string 0.1.2 escape().("abc\n def"), "abc\\n\\ def"); |
| 261 | } |
| 262 | |
| 263 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 264 | fn round_triprust-analyzer cargo escape_string 0.1.2 tests/round_trip().() |
| 265 | { |
| 266 | checkrust-analyzer cargo escape_string 0.1.2 tests/check().(&escaperust-analyzer cargo escape_string 0.1.2 escape().("ads\nasd"), "ads\nasd", ""); |
| 267 | } |
| 268 | } |