| 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 std::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_bytes<'a>(bytes: &'a [u8]) |
| 40 | -> Option<(Cow<'a, [u8]>, &'a [u8])> |
| 41 | { |
| 42 | let mut start = 0usize; |
| 43 | while let Some(b) = bytes.get(start) |
| 44 | { |
| 45 | if b.is_ascii_whitespace() |
| 46 | { start+=1; } |
| 47 | else |
| 48 | { break; } |
| 49 | } |
| 50 | |
| 51 | let mut owned: Option<Vec<u8>> = None; |
| 52 | |
| 53 | let mut position = start; |
| 54 | |
| 55 | while position < bytes.len() |
| 56 | { |
| 57 | if bytes[position] == b'\\' |
| 58 | { |
| 59 | if !owned.is_some() |
| 60 | { |
| 61 | owned = Some(bytes[start..position].to_owned()); |
| 62 | } |
| 63 | let b = owned.as_mut().unwrap(); |
| 64 | position += 1; |
| 65 | match bytes.get(position) |
| 66 | { |
| 67 | None => return None, |
| 68 | Some(b'a') => b.push(b'\x07'), |
| 69 | Some(b'b') => b.push(b'\x08'), |
| 70 | Some(b't') => b.push(b'\t'), |
| 71 | Some(b'n') => b.push(b'\n'), |
| 72 | Some(b'v') => b.push(b'\x0b'), |
| 73 | Some(b'f') => b.push(b'\x0c'), |
| 74 | Some(b'r') => b.push(b'\r'), |
| 75 | Some(b' ') => b.push(b' '), |
| 76 | Some(b'\\') => b.push(b'\\'), |
| 77 | Some(a) => b.push(*a), |
| 78 | } |
| 79 | position+=1; |
| 80 | } |
| 81 | else if bytes[position].is_ascii_whitespace() |
| 82 | { |
| 83 | break; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | if let Some(o) = owned.as_mut() |
| 88 | { o.push( bytes[position] ); } |
| 89 | position += 1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | let mut after = position; |
| 94 | while let Some(b) = bytes.get(after) |
| 95 | { |
| 96 | if b.is_ascii_whitespace() |
| 97 | { after+=1; } |
| 98 | else |
| 99 | { break; } |
| 100 | } |
| 101 | |
| 102 | let after = &bytes[after..]; |
| 103 | |
| 104 | if let Some(owned) = owned |
| 105 | { |
| 106 | Some( (Cow::Owned(owned), after) ) |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | Some( (Cow::Borrowed(&bytes[start..position]), 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_one<'a>(text: &'a str) |
| 127 | -> Option<(Cow<'a, str>, &'a str)> |
| 128 | { |
| 129 | if let Some((one, remainder)) = split_one_bytes(text.as_bytes()) |
| 130 | { |
| 131 | let one_text; |
| 132 | match one |
| 133 | { |
| 134 | Cow::Borrowed(b) => |
| 135 | one_text = unsafe { Cow::Borrowed( std::str::from_utf8_unchecked(b) ) }, |
| 136 | Cow::Owned(b) => |
| 137 | one_text = unsafe { Cow::Owned( String::from_utf8_unchecked(b) ) }, |
| 138 | } |
| 139 | let remainder = unsafe { std::str::from_utf8_unchecked(remainder) }; |
| 140 | Some((one_text, remainder)) |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | None |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Produce all the words as a vector |
| 149 | pub fn split<'a>(mut text: &'a str) |
| 150 | -> Option<Vec<Cow<'a, str>>> |
| 151 | { |
| 152 | let mut res = vec!(); |
| 153 | |
| 154 | while !text.is_empty() |
| 155 | { |
| 156 | let s = split_one(text); |
| 157 | if s.is_none() { return None; } |
| 158 | let s = s.unwrap(); |
| 159 | res.push( s.0 ); |
| 160 | text = s.1; |
| 161 | } |
| 162 | Some(res) |
| 163 | } |
| 164 | |
| 165 | /// Converts text with all the special characters escape with a backslash |
| 166 | pub fn escape<'a>(text: &'a str) |
| 167 | -> Cow<'a, str> |
| 168 | { |
| 169 | let bytes = text.as_bytes(); |
| 170 | |
| 171 | let mut owned = None; |
| 172 | |
| 173 | for pos in 0..bytes.len() |
| 174 | { |
| 175 | let special = |
| 176 | match bytes[pos] |
| 177 | { |
| 178 | 0x07 => Some(b'a'), |
| 179 | 0x08 => Some(b'b'), |
| 180 | b'\t' => Some(b't'), |
| 181 | b'\n' => Some(b'n'), |
| 182 | 0x0b => Some(b'v'), |
| 183 | 0x0c => Some(b'f'), |
| 184 | b'\r' => Some(b'r'), |
| 185 | b' ' => Some(b' '), |
| 186 | b'\\' => Some(b'\\'), |
| 187 | _ => None, |
| 188 | }; |
| 189 | if let Some(s) = special |
| 190 | { |
| 191 | if owned.is_none() |
| 192 | { |
| 193 | owned = Some(bytes[0..pos].to_owned()); |
| 194 | } |
| 195 | owned.as_mut().unwrap().push(b'\\'); |
| 196 | owned.as_mut().unwrap().push(s); |
| 197 | } |
| 198 | else if let Some(owned) = owned.as_mut() |
| 199 | { |
| 200 | owned.push( bytes[pos] ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if let Some(owned) = owned |
| 205 | { |
| 206 | unsafe { Cow::Owned(String::from_utf8_unchecked(owned)) } |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | unsafe { Cow::Borrowed(std::str::from_utf8_unchecked(bytes)) } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[cfg(test)] |
| 215 | mod tests |
| 216 | { |
| 217 | use ::split_one; |
| 218 | use ::split; |
| 219 | use ::escape; |
| 220 | |
| 221 | fn check(text: &str, one: &str, two: &str) |
| 222 | { |
| 223 | let a = split_one(text); |
| 224 | let a = a.unwrap(); |
| 225 | assert_eq!(a.0, one); |
| 226 | assert_eq!(a.1, two); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn failure() |
| 231 | { |
| 232 | assert_eq!(split_one("abc\\"), None); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn fine() |
| 237 | { |
| 238 | check("abc\\\\", "abc\\", ""); |
| 239 | check("1525824000000 520893", "1525824000000", "520893"); |
| 240 | check("abc\\\\ def", "abc\\", "def"); |
| 241 | check("abc\\\\\\\\ def", "abc\\\\", "def"); |
| 242 | check("abc\\\\\\\\ def", "abc\\\\", "def"); |
| 243 | check("abc\\ def ghi", "abc def", "ghi"); |
| 244 | check("abc\\ def ghi", "abc def", "ghi"); |
| 245 | check("", "", ""); |
| 246 | check(" ", "", ""); |
| 247 | } |
| 248 | #[test] |
| 249 | fn splitting() |
| 250 | { |
| 251 | assert_eq!(format!("{:?}",split("abc\\\\")), "Some([\"abc\\\\\"])"); |
| 252 | assert_eq!(format!("{:?}",split("abc def")), "Some([\"abc\", \"def\"])"); |
| 253 | assert_eq!(format!("{:?}",split("abc\\ def")), "Some([\"abc def\"])"); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn escaping() |
| 258 | { |
| 259 | assert_eq!(escape("abc\ndef"), "abc\\ndef"); |
| 260 | assert_eq!(escape("abc\n def"), "abc\\n\\ def"); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn round_trip() |
| 265 | { |
| 266 | check(&escape("ads\nasd"), "ads\nasd", ""); |
| 267 | } |
| 268 | } |