ct_regex_internal/haystack/
bytes.rs1use std::ops::Range;
2
3use crate::haystack::{Haystack, HaystackSlice, IntoHaystack, OwnedHaystackable};
4
5#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for ByteStack<'a> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "ByteStack",
"inner", &self.inner, "index", &&self.index)
}
}Debug, #[automatically_derived]
impl<'a> ::core::clone::Clone for ByteStack<'a> {
#[inline]
fn clone(&self) -> ByteStack<'a> {
ByteStack {
inner: ::core::clone::Clone::clone(&self.inner),
index: ::core::clone::Clone::clone(&self.index),
}
}
}Clone, #[automatically_derived]
impl<'a> ::core::cmp::PartialEq for ByteStack<'a> {
#[inline]
fn eq(&self, other: &ByteStack<'a>) -> bool {
self.inner == other.inner && self.index == other.index
}
}PartialEq, #[automatically_derived]
impl<'a> ::core::cmp::Eq for ByteStack<'a> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<&'a [u8]>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl<'a> ::core::hash::Hash for ByteStack<'a> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.inner, state);
::core::hash::Hash::hash(&self.index, state)
}
}Hash)]
8pub struct ByteStack<'a> {
9 inner: &'a [u8],
10 index: usize,
11}
12
13impl<'a> Iterator for ByteStack<'a> {
14 type Item = u8;
15
16 fn next(&mut self) -> Option<Self::Item> {
17 let byte = self.inner.get(self.index).copied();
18
19 if byte.is_some() {
20 self.index += 1;
21 }
22
23 byte
24 }
25}
26
27impl<'a> Haystack<'a> for ByteStack<'a> {
28 type Slice = &'a [u8];
29
30 fn item(&self) -> Option<Self::Item> {
31 self.inner.get(self.index).copied()
32 }
33
34 fn prev_item(&self) -> Option<Self::Item> {
35 self.inner.get(self.index.checked_sub(1)?).copied()
36 }
37
38 fn index(&self) -> usize {
39 self.index
40 }
41
42 fn inner_slice(&self) -> Self::Slice {
43 self.inner
44 }
45
46 fn remainder_as_slice(&self) -> Self::Slice {
47 &self.inner[self.index..]
48 }
49
50 fn go_to(&mut self, index: usize) {
51 self.index = index;
52 }
53}
54
55impl<'a> HaystackSlice<'a> for &'a [u8] {
56 type Item = u8;
57
58 fn slice_with(&self, range: Range<usize>) -> Self {
59 &self[range]
60 }
61
62 fn as_bytes(&self) -> &[u8] {
63 self
64 }
65}
66
67impl<'a> IntoHaystack<'a, ByteStack<'a>> for &'a [u8] {
68 fn into_haystack(self) -> ByteStack<'a> {
69 ByteStack {
70 inner: self,
71 index: 0,
72 }
73 }
74}
75
76impl<'a> IntoHaystack<'a, ByteStack<'a>> for &'a Vec<u8> {
77 fn into_haystack(self) -> ByteStack<'a> {
78 ByteStack {
79 inner: self,
80 index: 0,
81 }
82 }
83}
84
85impl OwnedHaystackable<u8> for Vec<u8> {
86 type Hay<'a> = ByteStack<'a>;
87
88 fn replace_range<'a>(
89 &mut self,
90 range: Range<usize>,
91 with: <Self::Hay<'a> as Haystack<'a>>::Slice
92 ) where Self: 'a {
93 self.splice(range, with.iter().cloned());
94 }
95
96 fn as_haystack<'a>(&'a self) -> Self::Hay<'a> {
97 self.into_haystack()
98 }
99
100 fn as_slice<'a>(&'a self) -> <Self::Hay<'a> as Haystack<'a>>::Slice {
101 self
102 }
103
104 fn len(&self) -> usize {
105 self.len()
106 }
107}