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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use std;
use std::borrow::{Borrow, ToOwned};
use std::cmp::Ordering;
use std::ffi::OsString;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Deref, DerefMut, RangeBounds};
use std::path::PathBuf;
use std::string::FromUtf8Error;
use std::vec::Drain;
use str::RawStr;

/// A `String` with unchecked contents.
///
/// It is basically a `Vec<u8>`, to be interpreted as string.
/// Unlike `String`, there are no guarantees about the contents being valid UTF-8.
/// Unlike `Vec<u8>`, its Display and Debug implementations show a string, not
/// an array of numbers.
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawString {
	inner: Vec<u8>,
}

impl RawString {
	#[inline]
	pub fn new() -> Self {
		RawString { inner: Vec::new() }
	}

	#[inline]
	pub fn with_capacity(capacity: usize) -> Self {
		RawString {
			inner: Vec::with_capacity(capacity),
		}
	}

	#[inline]
	pub fn from_bytes(bytes: Vec<u8>) -> Self {
		RawString { inner: bytes }
	}

	#[inline]
	pub fn from_string(bytes: String) -> Self {
		Self::from_bytes(bytes.into_bytes())
	}

	#[inline]
	pub fn into_bytes(self) -> Vec<u8> {
		self.inner
	}

	#[inline]
	pub fn capacity(&self) -> usize {
		self.inner.capacity()
	}

	#[inline]
	pub fn reserve(&mut self, additional: usize) {
		self.inner.reserve(additional)
	}

	#[inline]
	pub fn reserve_exact(&mut self, additional: usize) {
		self.inner.reserve_exact(additional)
	}

	#[inline]
	pub fn shrink_to_fit(&mut self) {
		self.inner.shrink_to_fit()
	}

	/* Unstable feature:
	#[inline]
	pub fn shrink_to(&mut self, min_capacity: usize) {
		self.inner.shrink_to(min_capacity)
	}
	*/

	#[inline]
	pub fn clear(&mut self) {
		self.inner.clear()
	}

	#[inline]
	pub fn truncate(&mut self, new_len: usize) {
		self.inner.truncate(new_len)
	}

	#[inline]
	pub fn pop(&mut self) -> Option<u8> {
		self.inner.pop()
	}

	#[inline]
	pub fn remove(&mut self, idx: usize) -> u8 {
		self.inner.remove(idx)
	}

	#[inline]
	pub fn retain<F: FnMut(u8) -> bool>(&mut self, mut f: F) {
		self.inner.retain(|x| f(*x))
	}

	#[inline]
	pub fn insert(&mut self, idx: usize, b: u8) {
		self.inner.insert(idx, b)
	}

	#[inline]
	pub fn insert_str<T: AsRef<RawStr>>(&mut self, idx: usize, s: T) {
		self.inner.splice(idx..idx, s.as_ref().bytes());
	}

	#[inline]
	pub fn split_off(&mut self, at: usize) -> RawString {
		RawString::from_bytes(self.inner.split_off(at))
	}

	#[inline]
	pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<u8> {
		self.inner.drain(range)
	}

	#[inline]
	pub fn replace_range<R: RangeBounds<usize>, T: AsRef<RawStr>>(
		&mut self,
		range: R,
		replace_with: T,
	) {
		self.inner.splice(range, replace_with.as_ref().bytes());
	}

	#[inline]
	pub fn into_boxed_raw_str(self) -> Box<RawStr> {
		let raw = Box::into_raw(self.inner.into_boxed_slice()) as *mut RawStr;
		unsafe { Box::from_raw(raw) }
	}

	#[inline]
	pub fn push(&mut self, b: u8) {
		self.inner.push(b)
	}

	#[inline]
	pub fn push_str<T: AsRef<RawStr>>(&mut self, s: T) {
		self.inner.extend_from_slice(s.as_ref().as_bytes())
	}

	#[inline]
	pub fn as_mut_bytes(&mut self) -> &mut Vec<u8> {
		&mut self.inner
	}

	#[inline]
	pub fn to_string(self) -> Result<String, FromUtf8Error> {
		String::from_utf8(self.into_bytes())
	}

	/// Convert to an OsString.
	///
	/// On Unix, it never fails.
	/// On other platforms, it must be encoded as UTF-8.
	///
	/// A never-failing version for Unix only is available as
	/// [`unix::RawStringExt::into_osstring`](struct.RawString.html#method.into_osstring).
	#[inline]
	pub fn to_osstring(self) -> Result<OsString, FromUtf8Error> {
		self.to_osstring_()
	}

	/// Convert to a PathBuf.
	///
	/// On Unix, it never fails.
	/// On other platforms, it must be encoded as UTF-8.
	///
	/// A never-failing version for Unix only is available as
	/// [`unix::RawStringExt::into_pathbuf`](struct.RawString.html#method.into_pathbuf).
	#[inline]
	pub fn to_pathbuf(self) -> Result<PathBuf, FromUtf8Error> {
		Ok(PathBuf::from(self.to_osstring()?))
	}

	#[cfg(unix)]
	#[inline]
	fn to_osstring_(self) -> Result<OsString, FromUtf8Error> {
		use std::os::unix::ffi::OsStringExt;
		Ok(OsString::from_vec(self.into_bytes()))
	}

	#[cfg(not(unix))]
	#[inline]
	fn to_osstring_(self) -> Result<OsString, FromUtf8Error> {
		Ok(OsString::from(self.to_string()?))
	}
}

// Deref / DerefMut {{{

impl Deref for RawString {
	type Target = RawStr;
	#[inline]
	fn deref(&self) -> &RawStr {
		RawStr::from_bytes(&self.inner)
	}
}

impl DerefMut for RawString {
	#[inline]
	fn deref_mut(&mut self) -> &mut RawStr {
		RawStr::from_bytes_mut(&mut self.inner)
	}
}

// }}}

// Borrow / ToOwned {{{

impl Borrow<RawStr> for RawString {
	#[inline]
	fn borrow(&self) -> &RawStr {
		RawStr::from_bytes(&self.inner)
	}
}

impl ToOwned for RawStr {
	type Owned = RawString;
	#[inline]
	fn to_owned(&self) -> RawString {
		RawString::from_bytes(self.as_bytes().to_owned())
	}
}

// }}}

// AsRef {{{

impl AsRef<RawStr> for RawString {
	#[inline]
	fn as_ref(&self) -> &RawStr {
		RawStr::from_bytes(&self.inner)
	}
}

impl AsRef<[u8]> for RawString {
	#[inline]
	fn as_ref(&self) -> &[u8] {
		&self.inner
	}
}

// }}}

// {{{ IntoIterator

impl IntoIterator for RawString {
	type Item = u8;
	type IntoIter = std::vec::IntoIter<u8>;
	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		self.inner.into_iter()
	}
}

impl<'a> IntoIterator for &'a RawString {
	type Item = u8;
	type IntoIter = std::iter::Cloned<std::slice::Iter<'a, u8>>;
	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		self.bytes()
	}
}

impl<'a> IntoIterator for &'a mut RawString {
	type Item = &'a mut u8;
	type IntoIter = std::slice::IterMut<'a, u8>;
	#[inline]
	fn into_iter(self) -> Self::IntoIter {
		self.bytes_mut()
	}
}

// }}}

// From {{{

impl<'a> From<&'a RawStr> for RawString {
	#[inline]
	fn from(src: &'a RawStr) -> RawString {
		RawString::from_bytes(src.as_bytes().to_owned())
	}
}

impl<'a> From<&'a str> for RawString {
	#[inline]
	fn from(src: &'a str) -> RawString {
		RawString::from_bytes(src.as_bytes().to_owned())
	}
}

impl<'a> From<&'a [u8]> for RawString {
	#[inline]
	fn from(src: &'a [u8]) -> RawString {
		RawString::from_bytes(src.to_owned())
	}
}

impl From<String> for RawString {
	#[inline]
	fn from(src: String) -> RawString {
		RawString::from_bytes(src.into_bytes())
	}
}

impl From<Vec<u8>> for RawString {
	#[inline]
	fn from(src: Vec<u8>) -> RawString {
		RawString::from_bytes(src)
	}
}

// }}}

// Display / Debug {{{

impl Display for RawString {
	#[inline]
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		Display::fmt(self.deref(), f)
	}
}

impl Debug for RawString {
	#[inline]
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		Debug::fmt(self.deref(), f)
	}
}

// }}}

// {{{ PartialEq / PartialOrd

macro_rules! impl_ord {
	($t:ty) => {
		impl PartialEq<$t> for RawString {
			#[inline]
			fn eq(&self, other: &$t) -> bool {
				<RawStr as PartialEq>::eq(self, other.as_ref())
			}
		}
		impl PartialEq<RawString> for $t {
			#[inline]
			fn eq(&self, other: &RawString) -> bool {
				<RawStr as PartialEq>::eq(self.as_ref(), other)
			}
		}
		impl PartialOrd<$t> for RawString {
			#[inline]
			fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
				<RawStr as PartialOrd>::partial_cmp(self, other.as_ref())
			}
		}
		impl PartialOrd<RawString> for $t {
			#[inline]
			fn partial_cmp(&self, other: &RawString) -> Option<Ordering> {
				<RawStr as PartialOrd>::partial_cmp(self.as_ref(), other)
			}
		}
	};
}

impl_ord!(RawStr);
impl_ord!(str);
impl_ord!([u8]);
impl_ord!(&RawStr);
impl_ord!(&str);
impl_ord!(&[u8]);

// }}}