1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Efficiently split lines by whitespace, while handling the backslash
//! escape sequences in Rust-like string format.
//!
//! For example, if you had a string like:
//! ```text
//!   One\ two three\ four
//! ```
//! Naïve splitting on whitespace would produce four outputs:
//! * `One\`
//! * `two`
//! * `three\`
//! * `four`
//!
//! This crate will instead produce two strings:
//! * `One two`
//! * `three four`
//!
//! This crate also handles special escape sequences like "\n", which represents a newline.
//! Specifically, the escape sequences are:
//!
//! * `\a`
//! * `\b`
//! * `\t`
//! * `\n`
//! * `\v`
//! * `\f`
//! * `\r`
//! * `\\`
//!
//! If the backslash character is found, but the successive character is not special,
//! then the backslash is disregarded and the successive character is included verbatim.

use std::borrow::Cow;

/// Splits once
///
/// Returns a tuple of the first "word" up until the first unescaped whitespace character,
/// and then every after the whitespace characters.
pub fn split_one_bytes<'a>(bytes: &'a [u8])
	-> Option<(Cow<'a, [u8]>, &'a [u8])>
{
	let mut start = 0usize;
	while let Some(b) = bytes.get(start)
	{
		if b.is_ascii_whitespace()
			{ start+=1; }
		else
			{ break; }
	}

	let mut owned: Option<Vec<u8>> = None;

	let mut position = start;

	while position < bytes.len()
	{
		if bytes[position] == b'\\'
		{
			if !owned.is_some()
			{
				owned = Some(bytes[start..position].to_owned());
			}
			let b = owned.as_mut().unwrap();
			position += 1;
			match bytes.get(position)
			{
				None => return None,
				Some(b'a') => b.push(b'\x07'),
				Some(b'b') => b.push(b'\x08'),
				Some(b't') => b.push(b'\t'),
				Some(b'n') => b.push(b'\n'),
				Some(b'v') => b.push(b'\x0b'),
				Some(b'f') => b.push(b'\x0c'),
				Some(b'r') => b.push(b'\r'),
				Some(b' ') => b.push(b' '),
				Some(b'\\') => b.push(b'\\'),
				Some(a) => b.push(*a),
			}
			position+=1;
		}
		else if bytes[position].is_ascii_whitespace()
		{
			break;
		}
		else
		{
			if let Some(o) = owned.as_mut()
				{ o.push( bytes[position] ); }
			position += 1;
		}
	}

	let mut after = position;
	while let Some(b) = bytes.get(after)
	{
		if b.is_ascii_whitespace()
			{ after+=1; }
		else
			{ break; }
	}

	let after = &bytes[after..];

	if let Some(owned) = owned
	{
		Some( (Cow::Owned(owned), after) )
	}
	else
	{
		Some( (Cow::Borrowed(&bytes[start..position]), after) )
	}
}

/// Split some text by unescaped whitespace.
///
/// find the first unescaped whitespace in `text`, return
/// a tuple of the text before the whitespace and the text after
/// the whitespace.
///
/// Ignores prefixed whitespace and discards whitespace between
/// the first portion and the text after the whitespace.
///
/// Returns None if there was an escape character and then nothing
///
/// Does not look at the following text at all.
pub fn split_one<'a>(text: &'a str)
	-> Option<(Cow<'a, str>, &'a str)>
{
	if let Some((one, remainder)) = split_one_bytes(text.as_bytes())
	{
		let one_text;
		match one
		{
			Cow::Borrowed(b) =>
				one_text = unsafe { Cow::Borrowed( std::str::from_utf8_unchecked(b) ) },
			Cow::Owned(b) =>
				one_text = unsafe { Cow::Owned( String::from_utf8_unchecked(b) ) },
		}
		let remainder = unsafe { std::str::from_utf8_unchecked(remainder) };
		Some((one_text, remainder))
	}
	else
	{
		None
	}
}

/// Produce all the words as a vector
pub fn split<'a>(mut text: &'a str)
	-> Option<Vec<Cow<'a, str>>>
{
	let mut res = vec!();

	while !text.is_empty()
	{
		let s = split_one(text);
		if s.is_none() { return None; }
		let s = s.unwrap();
		res.push( s.0 );
		text = s.1;
	}
	Some(res)
}

/// Converts text with all the special characters escape with a backslash
pub fn escape<'a>(text: &'a str)
	-> Cow<'a, str>
{
	let bytes = text.as_bytes();

	let mut owned = None;

	for pos in 0..bytes.len()
	{
		let special =
			match bytes[pos]
			{
				0x07 => Some(b'a'),
				0x08 => Some(b'b'),
				b'\t' => Some(b't'),
				b'\n' => Some(b'n'),
				0x0b => Some(b'v'),
				0x0c => Some(b'f'),
				b'\r' => Some(b'r'),
				b' ' => Some(b' '),
				b'\\' => Some(b'\\'),
				_ => None,
			};
		if let Some(s) = special
		{
			if owned.is_none()
			{
				owned = Some(bytes[0..pos].to_owned());
			}
			owned.as_mut().unwrap().push(b'\\');
			owned.as_mut().unwrap().push(s);
		}
		else if let Some(owned) = owned.as_mut()
		{
			owned.push( bytes[pos] );
		}
	}

	if let Some(owned) = owned
	{
		unsafe { Cow::Owned(String::from_utf8_unchecked(owned)) }
	}
	else
	{
		unsafe { Cow::Borrowed(std::str::from_utf8_unchecked(bytes)) }
	}
}

#[cfg(test)]
mod tests
{
	use ::split_one;
	use ::split;
	use ::escape;

	fn check(text: &str, one: &str, two: &str)
	{
		let a = split_one(text);
		let a = a.unwrap();
		assert_eq!(a.0, one);
		assert_eq!(a.1, two);
	}

	#[test]
	fn failure()
	{
		assert_eq!(split_one("abc\\"), None);
	}

	#[test]
	fn fine()
	{
		check("abc\\\\", "abc\\", "");
		check("1525824000000 520893", "1525824000000", "520893");
		check("abc\\\\ def", "abc\\", "def");
		check("abc\\\\\\\\ def", "abc\\\\", "def");
		check("abc\\\\\\\\    def", "abc\\\\", "def");
		check("abc\\ def   ghi", "abc def", "ghi");
		check("abc\\ def   ghi", "abc def", "ghi");
		check("", "", "");
		check(" ", "", "");
	}
	#[test]
	fn splitting()
	{
		assert_eq!(format!("{:?}",split("abc\\\\")), "Some([\"abc\\\\\"])");
		assert_eq!(format!("{:?}",split("abc def")), "Some([\"abc\", \"def\"])");
		assert_eq!(format!("{:?}",split("abc\\ def")), "Some([\"abc def\"])");
	}

	#[test]
	fn escaping()
	{
		assert_eq!(escape("abc\ndef"), "abc\\ndef");
		assert_eq!(escape("abc\n def"), "abc\\n\\ def");
	}

	#[test]
	fn round_trip()
	{
		check(&escape("ads\nasd"), "ads\nasd", "");
	}
}