bitvec_nom2/
input.rs

1use bitvec::prelude::*;
2use core::iter::Enumerate;
3use core::ops::{Index, Range, RangeFrom, RangeFull, RangeTo};
4use nom::error::{ErrorKind, ParseError};
5use nom::*;
6/*use crate::lib::std::slice::Iter;
7use crate::lib::std::str::from_utf8;
8use crate::lib::std::str::CharIndices;
9use crate::lib::std::str::Chars;*/
10
11use crate::BSlice;
12
13impl<'a, T, O> InputLength for BSlice<'a, T, O>
14where
15    O: BitOrder,
16    T: 'a + BitStore,
17{
18    #[inline]
19    fn input_len(&self) -> usize {
20        self.0.len()
21    }
22}
23
24impl<'a, 'b, T, O> InputLength for &'b BSlice<'a, T, O>
25where
26    O: BitOrder,
27    T: 'a + BitStore,
28{
29    #[inline]
30    fn input_len(&self) -> usize {
31        self.0.len()
32    }
33}
34
35impl<'a, T, O> Offset for BSlice<'a, T, O>
36where
37    O: BitOrder,
38    T: BitStore,
39{
40    #[inline(always)]
41    fn offset(&self, second: &Self) -> usize {
42        unsafe { second.0.as_bitptr().offset_from(self.0.as_bitptr()) as usize }
43    }
44}
45
46impl<'a, O> AsBytes for BSlice<'a, u8, O>
47where
48    O: BitOrder,
49{
50    #[inline(always)]
51    fn as_bytes(&self) -> &[u8] {
52        let domain = self.0.domain();
53        let region = domain
54            .region()
55            .expect("expected memory region from bit slice");
56
57        region.1
58    }
59}
60
61/*
62macro_rules! as_bytes_array_impls {
63    ($($N:expr)+) => {
64      $(
65        impl<'a, O> AsBytes for &'a BArray<O, [u8; $N]>
66        where O: BitOrder {
67          #[inline(always)]
68          fn as_bytes(&self) -> &[u8] {
69            self.0.as_raw_slice()
70          }
71        }
72
73        impl<O> AsBytes for BArray<O, [u8; $N]>
74        where O: BitOrder {
75          #[inline(always)]
76          fn as_bytes(&self) -> &[u8] {
77            self.0.as_raw_slice()
78          }
79        }
80      )+
81    };
82  }
83
84
85as_bytes_array_impls! {
86    0  1  2  3  4  5  6  7  8  9
87    10 11 12 13 14 15 16 17 18 19
88    20 21 22 23 24 25 26 27 28 29
89    30 31 32
90}*/
91
92impl<'a, T, O> InputIter for BSlice<'a, T, O>
93where
94    O: BitOrder,
95    T: 'a + BitStore,
96{
97    type Item = bool;
98    type Iter = Enumerate<Self::IterElem>;
99    type IterElem = alloc::boxed::Box<dyn Iterator<Item = bool> + 'a>;
100
101    #[inline]
102    fn iter_indices(&self) -> Self::Iter {
103        self.iter_elements().enumerate()
104    }
105
106    #[inline]
107    fn iter_elements(&self) -> Self::IterElem {
108        alloc::boxed::Box::from(self.0.iter().by_vals())
109    }
110
111    #[inline]
112    fn position<P>(&self, predicate: P) -> Option<usize>
113    where
114        P: Fn(Self::Item) -> bool,
115    {
116        self.iter_elements().position(predicate)
117    }
118
119    #[inline]
120    fn slice_index(&self, count: usize) -> Result<usize, Needed> {
121        if self.0.len() >= count {
122            Ok(count)
123        } else {
124            Err(Needed::new(count - self.0.len()))
125        }
126    }
127}
128
129impl<'a, T, O> InputTake for BSlice<'a, T, O>
130where
131    O: BitOrder,
132    T: 'a + BitStore,
133{
134    #[inline]
135    fn take(&self, count: usize) -> Self {
136        BSlice(&self.0[..count])
137    }
138
139    #[inline]
140    fn take_split(&self, count: usize) -> (Self, Self) {
141        let (a, b) = self.0.split_at(count);
142        (BSlice(b), BSlice(a))
143    }
144}
145
146/*
147impl<'a, 'b, T, O> InputTake for &'b BSlice<'a, T, O>
148where
149    O: BitOrder,
150    T: 'a + BitStore,
151{
152    #[inline]
153    fn take(&self, count: usize) -> Self {
154        &BSlice(&self.0[..count])
155    }
156
157    #[inline]
158    fn take_split(&self, count: usize) -> (Self, Self) {
159        let (a, b) = self.0.split_at(count);
160        (&BSlice(b), &BSlice(a))
161    }
162}
163*/
164
165impl<'a, T, O> InputTakeAtPosition for BSlice<'a, T, O>
166where
167    O: BitOrder,
168    T: 'a + BitStore,
169{
170    type Item = bool;
171
172    fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E>
173    where
174        P: Fn(Self::Item) -> bool,
175    {
176        self.0
177            .iter()
178            .by_vals()
179            .position(predicate)
180            .map(|i| {
181                let (a, b) = self.0.split_at(i);
182                (BSlice(a), BSlice(b))
183            })
184            .ok_or_else(|| Err::Incomplete(Needed::new(1)))
185    }
186
187    fn split_at_position1<P, E: ParseError<Self>>(
188        &self,
189        predicate: P,
190        e: ErrorKind,
191    ) -> IResult<Self, Self, E>
192    where
193        P: Fn(Self::Item) -> bool,
194    {
195        match self.0.iter().by_vals().position(predicate) {
196            Some(0) => {
197                let s = BSlice(self.0.split_at(0).1);
198                Err(Err::Error(E::from_error_kind(s, e)))
199            }
200            Some(i) => Ok({
201                let (a, b) = self.0.split_at(i);
202                (BSlice(a), BSlice(b))
203            }),
204            None => Err(Err::Incomplete(Needed::new(1))),
205        }
206    }
207
208    fn split_at_position_complete<P, E: ParseError<Self>>(
209        &self,
210        predicate: P,
211    ) -> IResult<Self, Self, E>
212    where
213        P: Fn(Self::Item) -> bool,
214    {
215        self.0
216            .iter()
217            .position(|b| predicate(*b))
218            .map(|i| {
219                let (a, b) = self.0.split_at(i);
220                (BSlice(a), BSlice(b))
221            })
222            .or_else(|| {
223                let s = BSlice(self.0.split_at(0).1);
224                Some((s, BSlice(BitSlice::empty())))
225            })
226            .ok_or_else(|| unreachable!())
227    }
228
229    fn split_at_position1_complete<P, E: ParseError<Self>>(
230        &self,
231        predicate: P,
232        e: ErrorKind,
233    ) -> IResult<Self, Self, E>
234    where
235        P: Fn(Self::Item) -> bool,
236    {
237        match self.0.iter().by_vals().position(predicate) {
238            Some(0) => {
239                let s = BSlice(self.0.split_at(0).1);
240                Err(Err::Error(E::from_error_kind(s, e)))
241            }
242            Some(i) => Ok({
243                let (a, b) = self.0.split_at(i);
244                (BSlice(a), BSlice(b))
245            }),
246            None => {
247                if self.0.is_empty() {
248                    let s = BSlice(self.0.split_at(0).1);
249                    Err(Err::Error(E::from_error_kind(s, e)))
250                } else {
251                    let s = BSlice(self.0.split_at(0).1);
252                    Ok((s, BSlice(BitSlice::empty())))
253                }
254            }
255        }
256    }
257}
258
259impl<'a, 'b, O1, O2, T1, T2> Compare<BSlice<'b, T2, O2>> for BSlice<'a, T1, O1>
260where
261    O1: BitOrder,
262    O2: BitOrder,
263    T1: 'a + BitStore,
264    T2: 'a + BitStore,
265{
266    #[inline]
267    fn compare(&self, other: BSlice<'b, T2, O2>) -> CompareResult {
268        match self.0.iter().zip(other.0.iter()).position(|(a, b)| a != b) {
269            Some(_) => CompareResult::Error,
270            None => {
271                if self.0.len() >= other.0.len() {
272                    CompareResult::Ok
273                } else {
274                    CompareResult::Incomplete
275                }
276            }
277        }
278    }
279
280    #[inline(always)]
281    fn compare_no_case(&self, other: BSlice<'b, T2, O2>) -> CompareResult {
282        self.compare(other)
283    }
284}
285
286impl<'a, T, O> FindToken<bool> for BSlice<'a, T, O>
287where
288    O: BitOrder,
289    T: 'a + BitStore,
290{
291    fn find_token(&self, token: bool) -> bool {
292        self.0.iter().any(|i| i == token)
293    }
294}
295
296impl<'a, T, O> FindToken<(usize, bool)> for BSlice<'a, T, O>
297where
298    O: BitOrder,
299    T: 'a + BitStore,
300{
301    fn find_token(&self, token: (usize, bool)) -> bool {
302        self.0.iter().enumerate().any(|(i, t)| (i, *t) == token)
303    }
304}
305
306impl<'a, 'b, O1, O2, T1, T2> FindSubstring<BSlice<'b, T2, O2>> for BSlice<'a, T1, O1>
307where
308    O1: BitOrder,
309    O2: BitOrder,
310    T1: 'a + BitStore,
311    T2: 'b + BitStore,
312{
313    fn find_substring(&self, substr: BSlice<T2, O2>) -> Option<usize> {
314        if substr.0.len() > self.0.len() {
315            return None;
316        }
317
318        if substr.0.is_empty() {
319            return Some(0);
320        }
321
322        self.0
323            .windows(substr.0.len())
324            .position(|window| window == substr.0)
325    }
326}
327
328macro_rules! impl_fn_slice {
329    ( $ty:ty ) => {
330        fn slice(&self, range: $ty) -> Self {
331            BSlice(&self.0[range])
332        }
333    };
334}
335
336macro_rules! slice_range_impl {
337    ( BSlice, $ty:ty ) => {
338        impl<'a, T, O> Slice<$ty> for BSlice<'a, T, O>
339        where
340            O: BitOrder,
341            T: BitStore,
342        {
343            impl_fn_slice!($ty);
344        }
345    };
346}
347
348macro_rules! slice_ranges_impl {
349    ( BSlice ) => {
350        slice_range_impl! {BSlice, Range<usize>}
351        slice_range_impl! {BSlice, RangeTo<usize>}
352        slice_range_impl! {BSlice, RangeFrom<usize>}
353        slice_range_impl! {BSlice, RangeFull}
354    };
355}
356
357slice_ranges_impl! {BSlice}
358
359#[cfg(feature = "alloc")]
360impl<'a, T, O> ExtendInto for BSlice<'a, T, O>
361where
362    O: BitOrder,
363    T: BitStore,
364{
365    type Item = bool;
366    type Extender = BitVec<T, O>;
367
368    #[inline]
369    fn new_builder(&self) -> BitVec<T, O> {
370        BitVec::new()
371    }
372
373    #[inline]
374    fn extend_into(&self, acc: &mut Self::Extender) {
375        acc.extend(self.0.iter());
376    }
377}
378
379impl<'a, T, O> Index<usize> for BSlice<'a, T, O>
380where
381    O: BitOrder,
382    T: BitStore,
383{
384    type Output = bool;
385
386    fn index(&self, index: usize) -> &Self::Output {
387        &self.0[index]
388    }
389}
390
391/*
392impl<'a, T, O> Index<RangeFrom<usize>> for BSlice<'a, T, O>
393where
394    O: BitOrder,
395    T: BitStore,
396{
397    type Output = Self;
398
399    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
400        &BSlice(&self.0[index])
401    }
402}*/