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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std;
use std::char::REPLACEMENT_CHARACTER;
use std::cmp::Ordering;
use std::convert::AsRef;
use std::ffi::OsStr;
use std::fmt::{Debug, Display, Formatter, Write};
use std::mem::transmute;
use std::ops::{Index, IndexMut};
use std::path::Path;
use std::str::{from_utf8, Utf8Error};

mod index;
mod utf8chunks;

pub use self::index::{RawStrIndex, RawStrIndexOutput};
pub use self::utf8chunks::{Utf8Chunk, Utf8ChunksIter};

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

impl RawStr {
	#[inline]
	pub fn from<S: AsRef<RawStr> + ?Sized>(s: &S) -> &Self {
		s.as_ref()
	}

	#[inline]
	pub fn from_bytes(bytes: &[u8]) -> &Self {
		unsafe { transmute::<&[u8], &Self>(bytes) }
	}

	#[inline]
	pub fn from_str(bytes: &str) -> &Self {
		Self::from_bytes(bytes.as_bytes())
	}

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

	#[inline]
	pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self {
		unsafe { transmute::<&mut [u8], &mut Self>(bytes) }
	}

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

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

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

	#[inline]
	pub fn as_ptr(&self) -> *const u8 {
		self.inner.as_ptr()
	}

	#[inline]
	pub fn first(&self) -> Option<u8> {
		self.inner.first().map(|&x| x)
	}

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

	#[inline]
	pub fn last(&self) -> Option<u8> {
		self.inner.last().map(|&x| x)
	}

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

	#[inline]
	pub fn split_first(&self) -> Option<(u8, &RawStr)> {
		self.inner
			.split_first()
			.map(|(&a, b)| (a, RawStr::from_bytes(b)))
	}

	#[inline]
	pub fn split_first_mut(&mut self) -> Option<(&mut u8, &mut RawStr)> {
		self.inner
			.split_first_mut()
			.map(|(a, b)| (a, RawStr::from_bytes_mut(b)))
	}

	#[inline]
	pub fn split_last(&self) -> Option<(u8, &RawStr)> {
		self.inner
			.split_last()
			.map(|(&a, b)| (a, RawStr::from_bytes(b)))
	}

	#[inline]
	pub fn split_last_mut(&mut self) -> Option<(&mut u8, &mut RawStr)> {
		self.inner
			.split_last_mut()
			.map(|(a, b)| (a, RawStr::from_bytes_mut(b)))
	}

	#[inline]
	pub fn split_at(&self, mid: usize) -> (&RawStr, &RawStr) {
		let (a, b) = self.inner.split_at(mid);
		(RawStr::from_bytes(a), RawStr::from_bytes(b))
	}

	#[inline]
	pub fn split_at_mut(&mut self, mid: usize) -> (&mut RawStr, &mut RawStr) {
		let (a, b) = self.inner.split_at_mut(mid);
		(RawStr::from_bytes_mut(a), RawStr::from_bytes_mut(b))
	}

	#[inline]
	pub fn contains_byte(&self, x: u8) -> bool {
		self.inner.contains(&x)
	}

	#[inline]
	pub fn starts_with<T: AsRef<RawStr>>(&self, x: T) -> bool {
		self.inner.starts_with(x.as_ref().as_bytes())
	}

	#[inline]
	pub fn ends_with<T: AsRef<RawStr>>(&self, x: T) -> bool {
		self.inner.ends_with(x.as_ref().as_bytes())
	}

	#[inline]
	pub fn get<I: RawStrIndex>(&self, index: I) -> Option<&I::Output> {
		index.get(self)
	}

	#[inline]
	pub fn get_mut<I: RawStrIndex>(&mut self, index: I) -> Option<&mut I::Output> {
		index.get_mut(self)
	}

	#[inline]
	pub unsafe fn get_unchecked<I: RawStrIndex>(&self, index: I) -> &I::Output {
		index.get_unchecked(self)
	}

	#[inline]
	pub unsafe fn get_unchecked_mut<I: RawStrIndex>(&mut self, index: I) -> &mut I::Output {
		index.get_unchecked_mut(self)
	}

	#[inline]
	pub unsafe fn slice_unchecked(&self, begin: usize, end: usize) -> &RawStr {
		self.get_unchecked(begin..end)
	}

	#[inline]
	pub unsafe fn slice_mut_unchecked(&mut self, begin: usize, end: usize) -> &mut RawStr {
		self.get_unchecked_mut(begin..end)
	}

	#[inline]
	pub fn bytes(&self) -> std::iter::Cloned<std::slice::Iter<u8>> {
		self.inner.iter().cloned()
	}

	#[inline]
	pub fn bytes_mut(&mut self) -> std::slice::IterMut<u8> {
		self.inner.iter_mut()
	}

	/// Iterate over chunks of valid UTF-8.
	///
	/// The iterator iterates over the chunks of valid UTF-8 separated by any
	/// broken characters, which could be replaced by the unicode replacement
	/// character.
	#[inline]
	pub fn utf8_chunks(&self) -> Utf8ChunksIter {
		Utf8ChunksIter { bytes: &self.inner }
	}

	// Things that could be added:
	//   pub fn lines(&self) -> Lines
	//   pub fn split_whitespace(&self) -> SplitWhitespace
	//   pub fn trim
	//   pub fn trim_left
	//   pub fn trim_right
	//
	//  RawPattern and:
	//   pub fn contains<'a, P: RawPattern<'a>>(&'a self, pat: P) -> bool
	//   pub fn starts_with<'a, P: RawPattern<'a>>(&'a self, pat: P) -> bool
	//   pub fn ends_with<'a, P: RawPattern<'a>>(&'a self, pat: P) -> bool
	//   pub fn find<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Option<usize>
	//   pub fn rfind<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Option<usize>
	//   pub fn split<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Split<'a, P>
	//   pub fn rsplit<'a, P: RawPattern<'a>>(&'a self, pat: P) -> RSplit<'a, P>
	//   pub fn split_terminator<'a, P: RawPattern<'a>>(&'a self, pat: P) -> SplitTerminator<'a, P>
	//   pub fn rsplit_terminator<'a, P: RawPattern<'a>>(&'a self, pat: P) -> RSplitTerminator<'a, P>
	//   pub fn splitn<'a, P: RawPattern<'a>>(&'a self, n: usize, pat: P) -> Split<'a, P>
	//   pub fn rsplitn<'a, P: RawPattern<'a>>(&'a self, n: usize, pat: P) -> RSplit<'a, P>
	//   pub fn matches<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Matches<'a, P>
	//   pub fn rmatches<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Matches<'a, P>
	//   pub fn match_indices<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Matches<'a, P>
	//   pub fn rmatch_indices<'a, P: RawPattern<'a>>(&'a self, pat: P) -> Matches<'a, P>
	//   pub fn trim_matches <RawPattern>
	//   pub fn trim_left_matches <RawPattern>
	//   pub fn trim_right_matches <RawPattern>
	//   pub fn replace (RawPattern -> AsRef<RawStr>) -> RawString
	//   pub fn replace_n (RawPattern -> AsRef<RawStr>, n) -> RawString
	//
	//   pub fn is_utf8_char_boundary(&self, index: usize) -> bool
	//   pub fn utf8_chars() -> Utf8Chars
	//   pub fn utf8_char_indices() -> Utf8CharIndices
	//   pub fn encode_utf16(&self) -> EncodeUtf16

	#[inline]
	pub fn to_str(&self) -> Result<&str, Utf8Error> {
		from_utf8(self.as_bytes())
	}

	/// Convert to an OsStr.
	///
	/// 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::RawStrExt::as_osstr`](struct.RawStr.html#method.as_osstr).
	#[inline]
	pub fn to_osstr(&self) -> Result<&OsStr, Utf8Error> {
		self.to_osstr_()
	}

	/// Convert to a Path.
	///
	/// 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::RawStrExt::as_path`](struct.RawStr.html#method.as_path).
	#[inline]
	pub fn to_path(&self) -> Result<&Path, Utf8Error> {
		Ok(Path::new(self.to_osstr()?))
	}

	#[cfg(unix)]
	#[inline]
	fn to_osstr_(&self) -> Result<&OsStr, Utf8Error> {
		use std::os::unix::ffi::OsStrExt;
		Ok(OsStr::from_bytes(self.as_bytes()))
	}

	#[cfg(not(unix))]
	#[inline]
	fn to_osstr_(&self) -> Result<&OsStr, Utf8Error> {
		Ok(OsStr::new(self.to_str()?))
	}

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

	#[inline]
	pub fn eq_ignore_ascii_case(&self, other: &RawStr) -> bool {
		self.inner.eq_ignore_ascii_case(&other.inner)
	}

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

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

// AsRef {{{

impl AsRef<RawStr> for RawStr {
	#[inline]
	fn as_ref(&self) -> &RawStr {
		self
	}
}

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

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

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

// }}}

// Default {{{

impl<'a> Default for &'a RawStr {
	#[inline]
	fn default() -> Self {
		RawStr::from_bytes(&[])
	}
}

impl<'a> Default for &'a mut RawStr {
	#[inline]
	fn default() -> Self {
		RawStr::from_bytes_mut(&mut [])
	}
}

// }}}

// Index {{{

impl<I: RawStrIndex> Index<I> for RawStr {
	type Output = I::Output;
	#[inline]
	fn index(&self, index: I) -> &I::Output {
		index.index(self)
	}
}

impl<I: RawStrIndex> IndexMut<I> for RawStr {
	#[inline]
	fn index_mut(&mut self, index: I) -> &mut I::Output {
		index.index_mut(self)
	}
}

// }}}

// IntoIterator {{{

impl<'a> IntoIterator for &'a RawStr {
	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 RawStr {
	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 str> for &'a RawStr {
	#[inline]
	fn from(src: &'a str) -> &'a RawStr {
		RawStr::from_str(src)
	}
}

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

// }}}

// Display {{{

impl Display for RawStr {
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		for Utf8Chunk { valid, broken } in self.utf8_chunks() {
			f.write_str(valid)?;
			if !broken.is_empty() {
				f.write_char(REPLACEMENT_CHARACTER)?;
			}
		}
		Ok(())
	}
}

// }}}

// Debug {{{

fn write_escaped_str(f: &mut std::fmt::Formatter, s: &str) -> std::fmt::Result {
	let mut written = 0;
	for (i, c) in s.char_indices() {
		let e = c.escape_debug();
		if e.len() != 1 {
			f.write_str(&s[written..i])?;
			for c in e {
				f.write_char(c)?;
			}
			written = i + c.len_utf8();
		}
	}
	f.write_str(&s[written..])
}

impl Debug for RawStr {
	fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
		f.write_char('"')?;
		for Utf8Chunk { valid, broken } in self.utf8_chunks() {
			write_escaped_str(f, valid)?;
			for &b in broken {
				write!(f, "\\x{:02x}", b)?;
			}
		}
		f.write_char('"')
	}
}

// }}}

// {{{ PartialEq / PartialOrd

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

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

// }}}

// Tests {{{

#[test]
fn test_display() {
	let a = RawStr::from("1\" μs / °C");
	assert_eq!(&format!("{}", a), "1\" μs / °C");

	let b = RawStr::from_bytes(b"1 \xFF \xce\xbcs / \xc2\xb0C");
	assert_eq!(&format!("{}", b), "1 \u{FFFD} μs / °C");
}

#[test]
fn test_debug() {
	let a: &RawStr = RawStr::from("1\" μs / °C");
	assert_eq!(&format!("{:?}", a), "\"1\\\" μs / °C\"");

	let b: &RawStr = RawStr::from_bytes(b"1 \xFF \xce\xbcs / \xc2\xb0C");
	assert_eq!(&format!("{:?}", b), "\"1 \\xff μs / °C\"");
}

// }}}