ct_regex_internal/haystack/
bytes.rs1use std::ops::Range;
2
3use crate::haystack::{HaystackIter, 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> HaystackIter<'a> for ByteStack<'a> {
28 type Slice = &'a [u8];
29
30 fn current_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 current_index(&self) -> usize {
39 self.index
40 }
41
42 fn whole_slice(&self) -> Self::Slice {
43 self.inner
44 }
45
46 fn remainder_as_slice(&self) -> Self::Slice {
47 &self.inner[self.index..]
49 }
50
51 fn go_to(&mut self, index: usize) {
52 self.index = index;
53 }
54}
55
56impl<'a> HaystackSlice<'a> for &'a [u8] {
57 type Item = u8;
58
59 fn slice_with(&self, range: Range<usize>) -> Self {
60 &self[range]
61 }
62}
63
64impl<'a> IntoHaystack<'a, ByteStack<'a>> for &'a [u8] {
65 fn into_haystack(self) -> ByteStack<'a> {
66 ByteStack {
67 inner: self,
68 index: 0,
69 }
70 }
71}
72
73impl<'a> IntoHaystack<'a, ByteStack<'a>> for &'a Vec<u8> {
74 fn into_haystack(self) -> ByteStack<'a> {
75 ByteStack {
76 inner: self,
77 index: 0,
78 }
79 }
80}
81
82impl OwnedHaystackable<u8> for Vec<u8> {
83 type Hay<'a> = ByteStack<'a>;
84
85 fn replace_range<'a>(
86 &mut self,
87 range: Range<usize>,
88 with: <Self::Hay<'a> as HaystackIter<'a>>::Slice
89 ) where Self: 'a {
90 self.splice(range, with.iter().cloned());
91 }
92
93 fn as_haystack<'a>(&'a self) -> Self::Hay<'a> {
94 self.into_haystack()
95 }
96
97 fn as_slice<'a>(&'a self) -> <Self::Hay<'a> as HaystackIter<'a>>::Slice {
98 self
99 }
100
101 fn len(&self) -> usize {
102 self.len()
103 }
104}