wiwi/parser/
input.rs

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
use crate::prelude_std::*;

pub trait Input: Sized {
	type ConstSize<const N: usize>: Sized;
	type ConstSizeOwned<const N: usize>: Sized + 'static;

	fn len(&self) -> usize;

	#[inline]
	fn is_empty(&self) -> bool {
		self.len() == 0
	}

	#[inline]
	fn starts_with<N>(&self, needle: &N) -> bool
	where
		N: Needle<Self>
	{
		needle.input_starts_with(self)
	}

	fn take_first(&self, i: usize) -> Option<(Self, Self)>;
	fn take_first_const<const N: usize>(&self) -> Option<(Self::ConstSize<N>, Self)>;
	fn take_first_const_owned<const N: usize>(&self) -> Option<(Self::ConstSizeOwned<N>, Self)>;
}

pub trait Needle<I>
where
	I: Input
{
	fn len(&self) -> usize;

	#[inline]
	fn is_empty(&self) -> bool {
		self.len() == 0
	}

	fn input_starts_with(&self, input: &I) -> bool;
}

impl<'h> Input for &'h [u8] {
	type ConstSize<const N: usize> = &'h [u8; N];
	type ConstSizeOwned<const N: usize> = [u8; N];

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

	#[inline]
	fn take_first(&self, i: usize) -> Option<(&'h [u8], &'h [u8])> {
		self.split_at_checked(i)
	}

	#[inline]
	fn take_first_const<const N: usize>(&self) -> Option<(Self::ConstSize<N>, Self)> {
		self.split_at_checked(N).map(|(output, remaining_input)| (
			// SAFETY: ptr derived from `output`, which is a slice of len `N`
			unsafe { &*output.as_ptr().cast() },
			remaining_input
		))
	}

	#[inline]
	fn take_first_const_owned<const N: usize>(&self) -> Option<(Self::ConstSizeOwned<N>, Self)> {
		self.take_first_const().map(|(output, remaining_input)| (*output, remaining_input))
	}
}

impl<'h> Needle<&'h [u8]> for &[u8] {
	#[inline]
	fn len(&self) -> usize {
		(**self).len()
	}

	#[inline]
	fn input_starts_with(&self, input: &&'h [u8]) -> bool {
		input.starts_with(self)
	}
}