1use core::hash::BuildHasher;
13use core::num::NonZeroUsize;
14
15use crate::ascii::Caseless as AsciiCaseless;
16#[cfg(feature = "unstable-recover")]
17#[cfg(feature = "std")]
18use crate::error::FromRecoverableError;
19use crate::error::Needed;
20use crate::lib::std::iter::{Cloned, Enumerate};
21use crate::lib::std::slice::Iter;
22use crate::lib::std::str::from_utf8;
23use crate::lib::std::str::CharIndices;
24use crate::lib::std::str::FromStr;
25
26#[allow(unused_imports)]
27#[cfg(any(feature = "unstable-doc", feature = "unstable-recover"))]
28use crate::error::ErrMode;
29
30#[cfg(feature = "alloc")]
31use crate::lib::std::collections::BTreeMap;
32#[cfg(feature = "alloc")]
33use crate::lib::std::collections::BTreeSet;
34#[cfg(feature = "std")]
35use crate::lib::std::collections::HashMap;
36#[cfg(feature = "std")]
37use crate::lib::std::collections::HashSet;
38#[cfg(feature = "alloc")]
39use crate::lib::std::string::String;
40#[cfg(feature = "alloc")]
41use crate::lib::std::vec::Vec;
42
43mod impls;
44#[cfg(test)]
45mod tests;
46
47pub type Str<'i> = &'i str;
49
50#[allow(clippy::derived_hash_with_manual_eq)]
52#[derive(Hash)]
53#[repr(transparent)]
54pub struct Bytes([u8]);
55
56impl Bytes {
57 #[inline]
59 pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &Self {
60 Self::from_bytes(bytes.as_ref())
61 }
62
63 #[inline]
64 fn from_bytes(slice: &[u8]) -> &Self {
65 unsafe { crate::lib::std::mem::transmute(slice) }
66 }
67
68 #[inline]
69 fn as_bytes(&self) -> &[u8] {
70 &self.0
71 }
72}
73
74#[allow(clippy::derived_hash_with_manual_eq)]
76#[derive(Hash)]
77#[repr(transparent)]
78pub struct BStr([u8]);
79
80impl BStr {
81 #[inline]
83 pub fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &Self {
84 Self::from_bytes(bytes.as_ref())
85 }
86
87 #[inline]
88 fn from_bytes(slice: &[u8]) -> &Self {
89 unsafe { crate::lib::std::mem::transmute(slice) }
90 }
91
92 #[inline]
93 fn as_bytes(&self) -> &[u8] {
94 &self.0
95 }
96}
97
98#[deprecated(since = "0.6.23", note = "Replaced with `LocatingSlice`")]
100pub type Located<I> = LocatingSlice<I>;
101
102#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
115#[doc(alias = "LocatingSliceSpan")]
116#[doc(alias = "Located")]
117pub struct LocatingSlice<I> {
118 initial: I,
119 input: I,
120}
121
122impl<I> LocatingSlice<I>
123where
124 I: Clone + Offset,
125{
126 pub fn new(input: I) -> Self {
128 let initial = input.clone();
129 Self { initial, input }
130 }
131
132 #[inline]
133 fn location(&self) -> usize {
134 self.input.offset_from(&self.initial)
135 }
136}
137
138impl<I> LocatingSlice<I>
139where
140 I: Clone + Stream + Offset,
141{
142 #[doc(alias = "fseek")]
147 #[inline]
148 pub fn reset_to_start(&mut self) {
149 let start = self.initial.checkpoint();
150 self.input.reset(&start);
151 }
152}
153
154impl<I> AsRef<I> for LocatingSlice<I> {
155 #[inline(always)]
156 fn as_ref(&self) -> &I {
157 &self.input
158 }
159}
160
161impl<I> crate::lib::std::ops::Deref for LocatingSlice<I> {
162 type Target = I;
163
164 #[inline(always)]
165 fn deref(&self) -> &Self::Target {
166 &self.input
167 }
168}
169
170impl<I: crate::lib::std::fmt::Display> crate::lib::std::fmt::Display for LocatingSlice<I> {
171 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
172 self.input.fmt(f)
173 }
174}
175
176impl<I: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug for LocatingSlice<I> {
177 #[inline]
178 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
179 self.input.fmt(f)
180 }
181}
182
183#[cfg(feature = "unstable-recover")]
188#[derive(Clone)]
189#[cfg(feature = "std")]
190pub struct Recoverable<I, E>
191where
192 I: Stream,
193{
194 input: I,
195 errors: Vec<E>,
196 is_recoverable: bool,
197}
198
199#[cfg(feature = "unstable-recover")]
200#[cfg(feature = "std")]
201impl<I, E> Default for Recoverable<I, E>
202where
203 I: Default + Stream,
204{
205 #[inline]
206 fn default() -> Self {
207 Self::new(I::default())
208 }
209}
210
211#[cfg(feature = "unstable-recover")]
212#[cfg(feature = "std")]
213impl<I, E> Recoverable<I, E>
214where
215 I: Stream,
216{
217 #[inline]
219 pub fn new(input: I) -> Self {
220 Self {
221 input,
222 errors: Default::default(),
223 is_recoverable: true,
224 }
225 }
226
227 #[inline]
229 pub fn unrecoverable(input: I) -> Self {
230 Self {
231 input,
232 errors: Default::default(),
233 is_recoverable: false,
234 }
235 }
236
237 #[inline]
239 pub fn into_parts(self) -> (I, Vec<E>) {
240 (self.input, self.errors)
241 }
242}
243
244#[cfg(feature = "unstable-recover")]
245#[cfg(feature = "std")]
246impl<I, E> AsRef<I> for Recoverable<I, E>
247where
248 I: Stream,
249{
250 #[inline(always)]
251 fn as_ref(&self) -> &I {
252 &self.input
253 }
254}
255
256#[cfg(feature = "unstable-recover")]
257#[cfg(feature = "std")]
258impl<I, E> crate::lib::std::ops::Deref for Recoverable<I, E>
259where
260 I: Stream,
261{
262 type Target = I;
263
264 #[inline(always)]
265 fn deref(&self) -> &Self::Target {
266 &self.input
267 }
268}
269
270#[cfg(feature = "unstable-recover")]
271#[cfg(feature = "std")]
272impl<I: crate::lib::std::fmt::Display, E> crate::lib::std::fmt::Display for Recoverable<I, E>
273where
274 I: Stream,
275{
276 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
277 crate::lib::std::fmt::Display::fmt(&self.input, f)
278 }
279}
280
281#[cfg(feature = "unstable-recover")]
282#[cfg(feature = "std")]
283impl<I: Stream + crate::lib::std::fmt::Debug, E: crate::lib::std::fmt::Debug>
284 crate::lib::std::fmt::Debug for Recoverable<I, E>
285{
286 #[inline]
287 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
288 if f.alternate() {
289 self.input.fmt(f)
290 } else {
291 f.debug_struct("Recoverable")
292 .field("input", &self.input)
293 .field("errors", &self.errors)
294 .field("is_recoverable", &self.is_recoverable)
295 .finish()
296 }
297 }
298}
299
300#[derive(Clone, Copy, Default, Eq, PartialEq)]
339#[doc(alias = "LocatingSliceSpan")]
340pub struct Stateful<I, S> {
341 pub input: I,
343 pub state: S,
345}
346
347impl<I, S> AsRef<I> for Stateful<I, S> {
348 #[inline(always)]
349 fn as_ref(&self) -> &I {
350 &self.input
351 }
352}
353
354impl<I, S> crate::lib::std::ops::Deref for Stateful<I, S> {
355 type Target = I;
356
357 #[inline(always)]
358 fn deref(&self) -> &Self::Target {
359 self.as_ref()
360 }
361}
362
363impl<I: crate::lib::std::fmt::Display, S> crate::lib::std::fmt::Display for Stateful<I, S> {
364 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
365 self.input.fmt(f)
366 }
367}
368
369impl<I: crate::lib::std::fmt::Debug, S: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug
370 for Stateful<I, S>
371{
372 #[inline]
373 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
374 if f.alternate() {
375 self.input.fmt(f)
376 } else {
377 f.debug_struct("Stateful")
378 .field("input", &self.input)
379 .field("state", &self.state)
380 .finish()
381 }
382 }
383}
384
385#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
450pub struct Partial<I> {
451 input: I,
452 partial: bool,
453}
454
455impl<I> Partial<I>
456where
457 I: StreamIsPartial,
458{
459 #[inline]
461 pub fn new(input: I) -> Self {
462 debug_assert!(
463 !I::is_partial_supported(),
464 "`Partial` can only wrap complete sources"
465 );
466 let partial = true;
467 Self { input, partial }
468 }
469
470 #[inline(always)]
472 pub fn into_inner(self) -> I {
473 self.input
474 }
475}
476
477impl<I> Default for Partial<I>
478where
479 I: Default + StreamIsPartial,
480{
481 #[inline]
482 fn default() -> Self {
483 Self::new(I::default())
484 }
485}
486
487impl<I> crate::lib::std::ops::Deref for Partial<I> {
488 type Target = I;
489
490 #[inline(always)]
491 fn deref(&self) -> &Self::Target {
492 &self.input
493 }
494}
495
496impl<I: crate::lib::std::fmt::Display> crate::lib::std::fmt::Display for Partial<I> {
497 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
498 self.input.fmt(f)
499 }
500}
501
502impl<I: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug for Partial<I> {
503 #[inline]
504 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
505 if f.alternate() {
506 self.input.fmt(f)
507 } else {
508 f.debug_struct("Partial")
509 .field("input", &self.input)
510 .field("partial", &self.partial)
511 .finish()
512 }
513 }
514}
515
516pub trait SliceLen {
518 fn slice_len(&self) -> usize;
521}
522
523impl<S: SliceLen> SliceLen for AsciiCaseless<S> {
524 #[inline(always)]
525 fn slice_len(&self) -> usize {
526 self.0.slice_len()
527 }
528}
529
530impl<T> SliceLen for &[T] {
531 #[inline(always)]
532 fn slice_len(&self) -> usize {
533 self.len()
534 }
535}
536
537impl<T, const LEN: usize> SliceLen for [T; LEN] {
538 #[inline(always)]
539 fn slice_len(&self) -> usize {
540 self.len()
541 }
542}
543
544impl<T, const LEN: usize> SliceLen for &[T; LEN] {
545 #[inline(always)]
546 fn slice_len(&self) -> usize {
547 self.len()
548 }
549}
550
551impl SliceLen for &str {
552 #[inline(always)]
553 fn slice_len(&self) -> usize {
554 self.len()
555 }
556}
557
558impl SliceLen for u8 {
559 #[inline(always)]
560 fn slice_len(&self) -> usize {
561 1
562 }
563}
564
565impl SliceLen for char {
566 #[inline(always)]
567 fn slice_len(&self) -> usize {
568 self.len_utf8()
569 }
570}
571
572impl SliceLen for &Bytes {
573 #[inline(always)]
574 fn slice_len(&self) -> usize {
575 self.len()
576 }
577}
578
579impl SliceLen for &BStr {
580 #[inline(always)]
581 fn slice_len(&self) -> usize {
582 self.len()
583 }
584}
585
586impl<I> SliceLen for (I, usize, usize)
587where
588 I: SliceLen,
589{
590 #[inline(always)]
591 fn slice_len(&self) -> usize {
592 self.0.slice_len() * 8 + self.2 - self.1
593 }
594}
595
596impl<I> SliceLen for LocatingSlice<I>
597where
598 I: SliceLen,
599{
600 #[inline(always)]
601 fn slice_len(&self) -> usize {
602 self.input.slice_len()
603 }
604}
605
606#[cfg(feature = "unstable-recover")]
607#[cfg(feature = "std")]
608impl<I, E> SliceLen for Recoverable<I, E>
609where
610 I: SliceLen,
611 I: Stream,
612{
613 #[inline(always)]
614 fn slice_len(&self) -> usize {
615 self.input.slice_len()
616 }
617}
618
619impl<I, S> SliceLen for Stateful<I, S>
620where
621 I: SliceLen,
622{
623 #[inline(always)]
624 fn slice_len(&self) -> usize {
625 self.input.slice_len()
626 }
627}
628
629impl<I> SliceLen for Partial<I>
630where
631 I: SliceLen,
632{
633 #[inline(always)]
634 fn slice_len(&self) -> usize {
635 self.input.slice_len()
636 }
637}
638
639pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::Debug {
641 type Token: crate::lib::std::fmt::Debug;
645 type Slice: crate::lib::std::fmt::Debug;
649
650 type IterOffsets: Iterator<Item = (usize, Self::Token)>;
652
653 type Checkpoint: Offset + Clone + crate::lib::std::fmt::Debug;
655
656 fn iter_offsets(&self) -> Self::IterOffsets;
658
659 fn eof_offset(&self) -> usize;
661
662 fn next_token(&mut self) -> Option<Self::Token>;
664 #[inline(always)]
666 fn peek_token(&self) -> Option<(Self, Self::Token)>
667 where
668 Self: Clone,
669 {
670 let mut peek = self.clone();
671 let token = peek.next_token()?;
672 Some((peek, token))
673 }
674
675 fn offset_for<P>(&self, predicate: P) -> Option<usize>
677 where
678 P: Fn(Self::Token) -> bool;
679 fn offset_at(&self, tokens: usize) -> Result<usize, Needed>;
683 fn next_slice(&mut self, offset: usize) -> Self::Slice;
705 #[inline(always)]
707 fn peek_slice(&self, offset: usize) -> (Self, Self::Slice)
708 where
709 Self: Clone,
710 {
711 let mut peek = self.clone();
712 let slice = peek.next_slice(offset);
713 (peek, slice)
714 }
715
716 #[inline(always)]
718 fn finish(&mut self) -> Self::Slice {
719 self.next_slice(self.eof_offset())
720 }
721 #[inline(always)]
723 fn peek_finish(&self) -> (Self, Self::Slice)
724 where
725 Self: Clone,
726 {
727 let mut peek = self.clone();
728 let slice = peek.finish();
729 (peek, slice)
730 }
731
732 fn checkpoint(&self) -> Self::Checkpoint;
734 fn reset(&mut self, checkpoint: &Self::Checkpoint);
740
741 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug;
743}
744
745impl<'i, T> Stream for &'i [T]
746where
747 T: Clone + crate::lib::std::fmt::Debug,
748{
749 type Token = T;
750 type Slice = &'i [T];
751
752 type IterOffsets = Enumerate<Cloned<Iter<'i, T>>>;
753
754 type Checkpoint = Checkpoint<Self, Self>;
755
756 #[inline(always)]
757 fn iter_offsets(&self) -> Self::IterOffsets {
758 self.iter().cloned().enumerate()
759 }
760 #[inline(always)]
761 fn eof_offset(&self) -> usize {
762 self.len()
763 }
764
765 #[inline(always)]
766 fn next_token(&mut self) -> Option<Self::Token> {
767 let (token, next) = self.split_first()?;
768 *self = next;
769 Some(token.clone())
770 }
771
772 #[inline(always)]
773 fn offset_for<P>(&self, predicate: P) -> Option<usize>
774 where
775 P: Fn(Self::Token) -> bool,
776 {
777 self.iter().position(|b| predicate(b.clone()))
778 }
779 #[inline(always)]
780 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
781 if let Some(needed) = tokens.checked_sub(self.len()).and_then(NonZeroUsize::new) {
782 Err(Needed::Size(needed))
783 } else {
784 Ok(tokens)
785 }
786 }
787 #[inline(always)]
788 fn next_slice(&mut self, offset: usize) -> Self::Slice {
789 let (slice, next) = self.split_at(offset);
790 *self = next;
791 slice
792 }
793
794 #[inline(always)]
795 fn checkpoint(&self) -> Self::Checkpoint {
796 Checkpoint::<_, Self>::new(*self)
797 }
798 #[inline(always)]
799 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
800 *self = checkpoint.inner;
801 }
802
803 #[inline(always)]
804 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
805 self
806 }
807}
808
809impl<'i> Stream for &'i str {
810 type Token = char;
811 type Slice = &'i str;
812
813 type IterOffsets = CharIndices<'i>;
814
815 type Checkpoint = Checkpoint<Self, Self>;
816
817 #[inline(always)]
818 fn iter_offsets(&self) -> Self::IterOffsets {
819 self.char_indices()
820 }
821 #[inline(always)]
822 fn eof_offset(&self) -> usize {
823 self.len()
824 }
825
826 #[inline(always)]
827 fn next_token(&mut self) -> Option<Self::Token> {
828 let c = self.chars().next()?;
829 let offset = c.len();
830 *self = &self[offset..];
831 Some(c)
832 }
833
834 #[inline(always)]
835 fn offset_for<P>(&self, predicate: P) -> Option<usize>
836 where
837 P: Fn(Self::Token) -> bool,
838 {
839 for (o, c) in self.iter_offsets() {
840 if predicate(c) {
841 return Some(o);
842 }
843 }
844 None
845 }
846 #[inline]
847 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
848 let mut cnt = 0;
849 for (offset, _) in self.iter_offsets() {
850 if cnt == tokens {
851 return Ok(offset);
852 }
853 cnt += 1;
854 }
855
856 if cnt == tokens {
857 Ok(self.eof_offset())
858 } else {
859 Err(Needed::Unknown)
860 }
861 }
862 #[inline(always)]
863 fn next_slice(&mut self, offset: usize) -> Self::Slice {
864 let (slice, next) = self.split_at(offset);
865 *self = next;
866 slice
867 }
868
869 #[inline(always)]
870 fn checkpoint(&self) -> Self::Checkpoint {
871 Checkpoint::<_, Self>::new(*self)
872 }
873 #[inline(always)]
874 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
875 *self = checkpoint.inner;
876 }
877
878 #[inline(always)]
879 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
880 self
881 }
882}
883
884impl<'i> Stream for &'i Bytes {
885 type Token = u8;
886 type Slice = &'i [u8];
887
888 type IterOffsets = Enumerate<Cloned<Iter<'i, u8>>>;
889
890 type Checkpoint = Checkpoint<Self, Self>;
891
892 #[inline(always)]
893 fn iter_offsets(&self) -> Self::IterOffsets {
894 self.iter().cloned().enumerate()
895 }
896 #[inline(always)]
897 fn eof_offset(&self) -> usize {
898 self.len()
899 }
900
901 #[inline(always)]
902 fn next_token(&mut self) -> Option<Self::Token> {
903 if self.is_empty() {
904 None
905 } else {
906 let token = self[0];
907 *self = &self[1..];
908 Some(token)
909 }
910 }
911
912 #[inline(always)]
913 fn offset_for<P>(&self, predicate: P) -> Option<usize>
914 where
915 P: Fn(Self::Token) -> bool,
916 {
917 self.iter().position(|b| predicate(*b))
918 }
919 #[inline(always)]
920 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
921 if let Some(needed) = tokens.checked_sub(self.len()).and_then(NonZeroUsize::new) {
922 Err(Needed::Size(needed))
923 } else {
924 Ok(tokens)
925 }
926 }
927 #[inline(always)]
928 fn next_slice(&mut self, offset: usize) -> Self::Slice {
929 let (slice, next) = self.0.split_at(offset);
930 *self = Bytes::from_bytes(next);
931 slice
932 }
933
934 #[inline(always)]
935 fn checkpoint(&self) -> Self::Checkpoint {
936 Checkpoint::<_, Self>::new(*self)
937 }
938 #[inline(always)]
939 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
940 *self = checkpoint.inner;
941 }
942
943 #[inline(always)]
944 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
945 self
946 }
947}
948
949impl<'i> Stream for &'i BStr {
950 type Token = u8;
951 type Slice = &'i [u8];
952
953 type IterOffsets = Enumerate<Cloned<Iter<'i, u8>>>;
954
955 type Checkpoint = Checkpoint<Self, Self>;
956
957 #[inline(always)]
958 fn iter_offsets(&self) -> Self::IterOffsets {
959 self.iter().cloned().enumerate()
960 }
961 #[inline(always)]
962 fn eof_offset(&self) -> usize {
963 self.len()
964 }
965
966 #[inline(always)]
967 fn next_token(&mut self) -> Option<Self::Token> {
968 if self.is_empty() {
969 None
970 } else {
971 let token = self[0];
972 *self = &self[1..];
973 Some(token)
974 }
975 }
976
977 #[inline(always)]
978 fn offset_for<P>(&self, predicate: P) -> Option<usize>
979 where
980 P: Fn(Self::Token) -> bool,
981 {
982 self.iter().position(|b| predicate(*b))
983 }
984 #[inline(always)]
985 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
986 if let Some(needed) = tokens.checked_sub(self.len()).and_then(NonZeroUsize::new) {
987 Err(Needed::Size(needed))
988 } else {
989 Ok(tokens)
990 }
991 }
992 #[inline(always)]
993 fn next_slice(&mut self, offset: usize) -> Self::Slice {
994 let (slice, next) = self.0.split_at(offset);
995 *self = BStr::from_bytes(next);
996 slice
997 }
998
999 #[inline(always)]
1000 fn checkpoint(&self) -> Self::Checkpoint {
1001 Checkpoint::<_, Self>::new(*self)
1002 }
1003 #[inline(always)]
1004 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1005 *self = checkpoint.inner;
1006 }
1007
1008 #[inline(always)]
1009 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1010 self
1011 }
1012}
1013
1014impl<I> Stream for (I, usize)
1015where
1016 I: Stream<Token = u8> + Clone,
1017{
1018 type Token = bool;
1019 type Slice = (I::Slice, usize, usize);
1020
1021 type IterOffsets = BitOffsets<I>;
1022
1023 type Checkpoint = Checkpoint<(I::Checkpoint, usize), Self>;
1024
1025 #[inline(always)]
1026 fn iter_offsets(&self) -> Self::IterOffsets {
1027 BitOffsets {
1028 i: self.clone(),
1029 o: 0,
1030 }
1031 }
1032 #[inline(always)]
1033 fn eof_offset(&self) -> usize {
1034 let offset = self.0.eof_offset() * 8;
1035 if offset == 0 {
1036 0
1037 } else {
1038 offset - self.1
1039 }
1040 }
1041
1042 #[inline(always)]
1043 fn next_token(&mut self) -> Option<Self::Token> {
1044 next_bit(self)
1045 }
1046
1047 #[inline(always)]
1048 fn offset_for<P>(&self, predicate: P) -> Option<usize>
1049 where
1050 P: Fn(Self::Token) -> bool,
1051 {
1052 self.iter_offsets()
1053 .find_map(|(o, b)| predicate(b).then_some(o))
1054 }
1055 #[inline(always)]
1056 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
1057 if let Some(needed) = tokens
1058 .checked_sub(self.eof_offset())
1059 .and_then(NonZeroUsize::new)
1060 {
1061 Err(Needed::Size(needed))
1062 } else {
1063 Ok(tokens)
1064 }
1065 }
1066 #[inline(always)]
1067 fn next_slice(&mut self, offset: usize) -> Self::Slice {
1068 let byte_offset = (offset + self.1) / 8;
1069 let end_offset = (offset + self.1) % 8;
1070 let s = self.0.next_slice(byte_offset);
1071 let start_offset = self.1;
1072 self.1 = end_offset;
1073 (s, start_offset, end_offset)
1074 }
1075
1076 #[inline(always)]
1077 fn checkpoint(&self) -> Self::Checkpoint {
1078 Checkpoint::<_, Self>::new((self.0.checkpoint(), self.1))
1079 }
1080 #[inline(always)]
1081 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1082 self.0.reset(&checkpoint.inner.0);
1083 self.1 = checkpoint.inner.1;
1084 }
1085
1086 #[inline(always)]
1087 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1088 &self.0
1089 }
1090}
1091
1092pub struct BitOffsets<I> {
1094 i: (I, usize),
1095 o: usize,
1096}
1097
1098impl<I> Iterator for BitOffsets<I>
1099where
1100 I: Stream<Token = u8> + Clone,
1101{
1102 type Item = (usize, bool);
1103 fn next(&mut self) -> Option<Self::Item> {
1104 let b = next_bit(&mut self.i)?;
1105 let o = self.o;
1106
1107 self.o += 1;
1108
1109 Some((o, b))
1110 }
1111}
1112
1113fn next_bit<I>(i: &mut (I, usize)) -> Option<bool>
1114where
1115 I: Stream<Token = u8> + Clone,
1116{
1117 if i.eof_offset() == 0 {
1118 return None;
1119 }
1120 let offset = i.1;
1121
1122 let mut next_i = i.0.clone();
1123 let byte = next_i.next_token()?;
1124 let bit = (byte >> offset) & 0x1 == 0x1;
1125
1126 let next_offset = offset + 1;
1127 if next_offset == 8 {
1128 i.0 = next_i;
1129 i.1 = 0;
1130 Some(bit)
1131 } else {
1132 i.1 = next_offset;
1133 Some(bit)
1134 }
1135}
1136
1137impl<I: Stream> Stream for LocatingSlice<I> {
1138 type Token = <I as Stream>::Token;
1139 type Slice = <I as Stream>::Slice;
1140
1141 type IterOffsets = <I as Stream>::IterOffsets;
1142
1143 type Checkpoint = Checkpoint<I::Checkpoint, Self>;
1144
1145 #[inline(always)]
1146 fn iter_offsets(&self) -> Self::IterOffsets {
1147 self.input.iter_offsets()
1148 }
1149 #[inline(always)]
1150 fn eof_offset(&self) -> usize {
1151 self.input.eof_offset()
1152 }
1153
1154 #[inline(always)]
1155 fn next_token(&mut self) -> Option<Self::Token> {
1156 self.input.next_token()
1157 }
1158
1159 #[inline(always)]
1160 fn offset_for<P>(&self, predicate: P) -> Option<usize>
1161 where
1162 P: Fn(Self::Token) -> bool,
1163 {
1164 self.input.offset_for(predicate)
1165 }
1166 #[inline(always)]
1167 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
1168 self.input.offset_at(tokens)
1169 }
1170 #[inline(always)]
1171 fn next_slice(&mut self, offset: usize) -> Self::Slice {
1172 self.input.next_slice(offset)
1173 }
1174
1175 #[inline(always)]
1176 fn checkpoint(&self) -> Self::Checkpoint {
1177 Checkpoint::<_, Self>::new(self.input.checkpoint())
1178 }
1179 #[inline(always)]
1180 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1181 self.input.reset(&checkpoint.inner);
1182 }
1183
1184 #[inline(always)]
1185 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1186 &self.input
1187 }
1188}
1189
1190#[cfg(feature = "unstable-recover")]
1191#[cfg(feature = "std")]
1192impl<I, E: crate::lib::std::fmt::Debug> Stream for Recoverable<I, E>
1193where
1194 I: Stream,
1195{
1196 type Token = <I as Stream>::Token;
1197 type Slice = <I as Stream>::Slice;
1198
1199 type IterOffsets = <I as Stream>::IterOffsets;
1200
1201 type Checkpoint = Checkpoint<I::Checkpoint, Self>;
1202
1203 #[inline(always)]
1204 fn iter_offsets(&self) -> Self::IterOffsets {
1205 self.input.iter_offsets()
1206 }
1207 #[inline(always)]
1208 fn eof_offset(&self) -> usize {
1209 self.input.eof_offset()
1210 }
1211
1212 #[inline(always)]
1213 fn next_token(&mut self) -> Option<Self::Token> {
1214 self.input.next_token()
1215 }
1216
1217 #[inline(always)]
1218 fn offset_for<P>(&self, predicate: P) -> Option<usize>
1219 where
1220 P: Fn(Self::Token) -> bool,
1221 {
1222 self.input.offset_for(predicate)
1223 }
1224 #[inline(always)]
1225 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
1226 self.input.offset_at(tokens)
1227 }
1228 #[inline(always)]
1229 fn next_slice(&mut self, offset: usize) -> Self::Slice {
1230 self.input.next_slice(offset)
1231 }
1232
1233 #[inline(always)]
1234 fn checkpoint(&self) -> Self::Checkpoint {
1235 Checkpoint::<_, Self>::new(self.input.checkpoint())
1236 }
1237 #[inline(always)]
1238 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1239 self.input.reset(&checkpoint.inner);
1240 }
1241
1242 #[inline(always)]
1243 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1244 &self.input
1245 }
1246}
1247
1248impl<I: Stream, S: crate::lib::std::fmt::Debug> Stream for Stateful<I, S> {
1249 type Token = <I as Stream>::Token;
1250 type Slice = <I as Stream>::Slice;
1251
1252 type IterOffsets = <I as Stream>::IterOffsets;
1253
1254 type Checkpoint = Checkpoint<I::Checkpoint, Self>;
1255
1256 #[inline(always)]
1257 fn iter_offsets(&self) -> Self::IterOffsets {
1258 self.input.iter_offsets()
1259 }
1260 #[inline(always)]
1261 fn eof_offset(&self) -> usize {
1262 self.input.eof_offset()
1263 }
1264
1265 #[inline(always)]
1266 fn next_token(&mut self) -> Option<Self::Token> {
1267 self.input.next_token()
1268 }
1269
1270 #[inline(always)]
1271 fn offset_for<P>(&self, predicate: P) -> Option<usize>
1272 where
1273 P: Fn(Self::Token) -> bool,
1274 {
1275 self.input.offset_for(predicate)
1276 }
1277 #[inline(always)]
1278 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
1279 self.input.offset_at(tokens)
1280 }
1281 #[inline(always)]
1282 fn next_slice(&mut self, offset: usize) -> Self::Slice {
1283 self.input.next_slice(offset)
1284 }
1285
1286 #[inline(always)]
1287 fn checkpoint(&self) -> Self::Checkpoint {
1288 Checkpoint::<_, Self>::new(self.input.checkpoint())
1289 }
1290 #[inline(always)]
1291 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1292 self.input.reset(&checkpoint.inner);
1293 }
1294
1295 #[inline(always)]
1296 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1297 &self.input
1298 }
1299}
1300
1301impl<I: Stream> Stream for Partial<I> {
1302 type Token = <I as Stream>::Token;
1303 type Slice = <I as Stream>::Slice;
1304
1305 type IterOffsets = <I as Stream>::IterOffsets;
1306
1307 type Checkpoint = Checkpoint<I::Checkpoint, Self>;
1308
1309 #[inline(always)]
1310 fn iter_offsets(&self) -> Self::IterOffsets {
1311 self.input.iter_offsets()
1312 }
1313 #[inline(always)]
1314 fn eof_offset(&self) -> usize {
1315 self.input.eof_offset()
1316 }
1317
1318 #[inline(always)]
1319 fn next_token(&mut self) -> Option<Self::Token> {
1320 self.input.next_token()
1321 }
1322
1323 #[inline(always)]
1324 fn offset_for<P>(&self, predicate: P) -> Option<usize>
1325 where
1326 P: Fn(Self::Token) -> bool,
1327 {
1328 self.input.offset_for(predicate)
1329 }
1330 #[inline(always)]
1331 fn offset_at(&self, tokens: usize) -> Result<usize, Needed> {
1332 self.input.offset_at(tokens)
1333 }
1334 #[inline(always)]
1335 fn next_slice(&mut self, offset: usize) -> Self::Slice {
1336 self.input.next_slice(offset)
1337 }
1338
1339 #[inline(always)]
1340 fn checkpoint(&self) -> Self::Checkpoint {
1341 Checkpoint::<_, Self>::new(self.input.checkpoint())
1342 }
1343 #[inline(always)]
1344 fn reset(&mut self, checkpoint: &Self::Checkpoint) {
1345 self.input.reset(&checkpoint.inner);
1346 }
1347
1348 #[inline(always)]
1349 fn raw(&self) -> &dyn crate::lib::std::fmt::Debug {
1350 &self.input
1351 }
1352}
1353
1354pub trait Location {
1358 fn location(&self) -> usize;
1360}
1361
1362impl<I> Location for LocatingSlice<I>
1363where
1364 I: Clone + Offset,
1365{
1366 #[inline(always)]
1367 fn location(&self) -> usize {
1368 self.location()
1369 }
1370}
1371
1372#[cfg(feature = "unstable-recover")]
1373#[cfg(feature = "std")]
1374impl<I, E> Location for Recoverable<I, E>
1375where
1376 I: Location,
1377 I: Stream,
1378{
1379 #[inline(always)]
1380 fn location(&self) -> usize {
1381 self.input.location()
1382 }
1383}
1384
1385impl<I, S> Location for Stateful<I, S>
1386where
1387 I: Location,
1388{
1389 #[inline(always)]
1390 fn location(&self) -> usize {
1391 self.input.location()
1392 }
1393}
1394
1395impl<I> Location for Partial<I>
1396where
1397 I: Location,
1398{
1399 #[inline(always)]
1400 fn location(&self) -> usize {
1401 self.input.location()
1402 }
1403}
1404
1405#[cfg(feature = "unstable-recover")]
1409#[cfg(feature = "std")]
1410pub trait Recover<E>: Stream {
1411 fn record_err(
1416 &mut self,
1417 token_start: &Self::Checkpoint,
1418 err_start: &Self::Checkpoint,
1419 err: ErrMode<E>,
1420 ) -> Result<(), ErrMode<E>>;
1421
1422 fn is_recovery_supported() -> bool;
1424}
1425
1426#[cfg(feature = "unstable-recover")]
1427#[cfg(feature = "std")]
1428impl<'a, T, E> Recover<E> for &'a [T]
1429where
1430 &'a [T]: Stream,
1431{
1432 #[inline(always)]
1433 fn record_err(
1434 &mut self,
1435 _token_start: &Self::Checkpoint,
1436 _err_start: &Self::Checkpoint,
1437 err: ErrMode<E>,
1438 ) -> Result<(), ErrMode<E>> {
1439 Err(err)
1440 }
1441
1442 #[inline(always)]
1444 fn is_recovery_supported() -> bool {
1445 false
1446 }
1447}
1448
1449#[cfg(feature = "unstable-recover")]
1450#[cfg(feature = "std")]
1451impl<E> Recover<E> for &str {
1452 #[inline(always)]
1453 fn record_err(
1454 &mut self,
1455 _token_start: &Self::Checkpoint,
1456 _err_start: &Self::Checkpoint,
1457 err: ErrMode<E>,
1458 ) -> Result<(), ErrMode<E>> {
1459 Err(err)
1460 }
1461
1462 #[inline(always)]
1464 fn is_recovery_supported() -> bool {
1465 false
1466 }
1467}
1468
1469#[cfg(feature = "unstable-recover")]
1470#[cfg(feature = "std")]
1471impl<E> Recover<E> for &Bytes {
1472 #[inline(always)]
1473 fn record_err(
1474 &mut self,
1475 _token_start: &Self::Checkpoint,
1476 _err_start: &Self::Checkpoint,
1477 err: ErrMode<E>,
1478 ) -> Result<(), ErrMode<E>> {
1479 Err(err)
1480 }
1481
1482 #[inline(always)]
1484 fn is_recovery_supported() -> bool {
1485 false
1486 }
1487}
1488
1489#[cfg(feature = "unstable-recover")]
1490#[cfg(feature = "std")]
1491impl<E> Recover<E> for &BStr {
1492 #[inline(always)]
1493 fn record_err(
1494 &mut self,
1495 _token_start: &Self::Checkpoint,
1496 _err_start: &Self::Checkpoint,
1497 err: ErrMode<E>,
1498 ) -> Result<(), ErrMode<E>> {
1499 Err(err)
1500 }
1501
1502 #[inline(always)]
1504 fn is_recovery_supported() -> bool {
1505 false
1506 }
1507}
1508
1509#[cfg(feature = "unstable-recover")]
1510#[cfg(feature = "std")]
1511impl<I, E> Recover<E> for (I, usize)
1512where
1513 I: Recover<E>,
1514 I: Stream<Token = u8> + Clone,
1515{
1516 #[inline(always)]
1517 fn record_err(
1518 &mut self,
1519 _token_start: &Self::Checkpoint,
1520 _err_start: &Self::Checkpoint,
1521 err: ErrMode<E>,
1522 ) -> Result<(), ErrMode<E>> {
1523 Err(err)
1524 }
1525
1526 #[inline(always)]
1528 fn is_recovery_supported() -> bool {
1529 false
1530 }
1531}
1532
1533#[cfg(feature = "unstable-recover")]
1534#[cfg(feature = "std")]
1535impl<I, E> Recover<E> for LocatingSlice<I>
1536where
1537 I: Recover<E>,
1538 I: Stream,
1539{
1540 #[inline(always)]
1541 fn record_err(
1542 &mut self,
1543 _token_start: &Self::Checkpoint,
1544 _err_start: &Self::Checkpoint,
1545 err: ErrMode<E>,
1546 ) -> Result<(), ErrMode<E>> {
1547 Err(err)
1548 }
1549
1550 #[inline(always)]
1552 fn is_recovery_supported() -> bool {
1553 false
1554 }
1555}
1556
1557#[cfg(feature = "unstable-recover")]
1558#[cfg(feature = "std")]
1559impl<I, E, R> Recover<E> for Recoverable<I, R>
1560where
1561 I: Stream,
1562 R: FromRecoverableError<Self, E>,
1563 R: crate::lib::std::fmt::Debug,
1564{
1565 fn record_err(
1566 &mut self,
1567 token_start: &Self::Checkpoint,
1568 err_start: &Self::Checkpoint,
1569 err: ErrMode<E>,
1570 ) -> Result<(), ErrMode<E>> {
1571 if self.is_recoverable {
1572 match err {
1573 ErrMode::Incomplete(need) => Err(ErrMode::Incomplete(need)),
1574 ErrMode::Backtrack(err) | ErrMode::Cut(err) => {
1575 self.errors
1576 .push(R::from_recoverable_error(token_start, err_start, self, err));
1577 Ok(())
1578 }
1579 }
1580 } else {
1581 Err(err)
1582 }
1583 }
1584
1585 #[inline(always)]
1587 fn is_recovery_supported() -> bool {
1588 true
1589 }
1590}
1591
1592#[cfg(feature = "unstable-recover")]
1593#[cfg(feature = "std")]
1594impl<I, E, S> Recover<E> for Stateful<I, S>
1595where
1596 I: Recover<E>,
1597 I: Stream,
1598 S: Clone + crate::lib::std::fmt::Debug,
1599{
1600 #[inline(always)]
1601 fn record_err(
1602 &mut self,
1603 _token_start: &Self::Checkpoint,
1604 _err_start: &Self::Checkpoint,
1605 err: ErrMode<E>,
1606 ) -> Result<(), ErrMode<E>> {
1607 Err(err)
1608 }
1609
1610 #[inline(always)]
1612 fn is_recovery_supported() -> bool {
1613 false
1614 }
1615}
1616
1617#[cfg(feature = "unstable-recover")]
1618#[cfg(feature = "std")]
1619impl<I, E> Recover<E> for Partial<I>
1620where
1621 I: Recover<E>,
1622 I: Stream,
1623{
1624 #[inline(always)]
1625 fn record_err(
1626 &mut self,
1627 _token_start: &Self::Checkpoint,
1628 _err_start: &Self::Checkpoint,
1629 err: ErrMode<E>,
1630 ) -> Result<(), ErrMode<E>> {
1631 Err(err)
1632 }
1633
1634 #[inline(always)]
1636 fn is_recovery_supported() -> bool {
1637 false
1638 }
1639}
1640
1641pub trait StreamIsPartial: Sized {
1645 type PartialState;
1647
1648 #[must_use]
1650 fn complete(&mut self) -> Self::PartialState;
1651
1652 fn restore_partial(&mut self, state: Self::PartialState);
1654
1655 fn is_partial_supported() -> bool;
1657
1658 #[inline(always)]
1660 fn is_partial(&self) -> bool {
1661 Self::is_partial_supported()
1662 }
1663}
1664
1665impl<T> StreamIsPartial for &[T] {
1666 type PartialState = ();
1667
1668 #[inline]
1669 fn complete(&mut self) -> Self::PartialState {}
1670
1671 #[inline]
1672 fn restore_partial(&mut self, _state: Self::PartialState) {}
1673
1674 #[inline(always)]
1675 fn is_partial_supported() -> bool {
1676 false
1677 }
1678}
1679
1680impl StreamIsPartial for &str {
1681 type PartialState = ();
1682
1683 #[inline]
1684 fn complete(&mut self) -> Self::PartialState {
1685 }
1687
1688 #[inline]
1689 fn restore_partial(&mut self, _state: Self::PartialState) {}
1690
1691 #[inline(always)]
1692 fn is_partial_supported() -> bool {
1693 false
1694 }
1695}
1696
1697impl StreamIsPartial for &Bytes {
1698 type PartialState = ();
1699
1700 #[inline]
1701 fn complete(&mut self) -> Self::PartialState {
1702 }
1704
1705 #[inline]
1706 fn restore_partial(&mut self, _state: Self::PartialState) {}
1707
1708 #[inline(always)]
1709 fn is_partial_supported() -> bool {
1710 false
1711 }
1712}
1713
1714impl StreamIsPartial for &BStr {
1715 type PartialState = ();
1716
1717 #[inline]
1718 fn complete(&mut self) -> Self::PartialState {
1719 }
1721
1722 #[inline]
1723 fn restore_partial(&mut self, _state: Self::PartialState) {}
1724
1725 #[inline(always)]
1726 fn is_partial_supported() -> bool {
1727 false
1728 }
1729}
1730
1731impl<I> StreamIsPartial for (I, usize)
1732where
1733 I: StreamIsPartial,
1734{
1735 type PartialState = I::PartialState;
1736
1737 #[inline]
1738 fn complete(&mut self) -> Self::PartialState {
1739 self.0.complete()
1740 }
1741
1742 #[inline]
1743 fn restore_partial(&mut self, state: Self::PartialState) {
1744 self.0.restore_partial(state);
1745 }
1746
1747 #[inline(always)]
1748 fn is_partial_supported() -> bool {
1749 I::is_partial_supported()
1750 }
1751
1752 #[inline(always)]
1753 fn is_partial(&self) -> bool {
1754 self.0.is_partial()
1755 }
1756}
1757
1758impl<I> StreamIsPartial for LocatingSlice<I>
1759where
1760 I: StreamIsPartial,
1761{
1762 type PartialState = I::PartialState;
1763
1764 #[inline]
1765 fn complete(&mut self) -> Self::PartialState {
1766 self.input.complete()
1767 }
1768
1769 #[inline]
1770 fn restore_partial(&mut self, state: Self::PartialState) {
1771 self.input.restore_partial(state);
1772 }
1773
1774 #[inline(always)]
1775 fn is_partial_supported() -> bool {
1776 I::is_partial_supported()
1777 }
1778
1779 #[inline(always)]
1780 fn is_partial(&self) -> bool {
1781 self.input.is_partial()
1782 }
1783}
1784
1785#[cfg(feature = "unstable-recover")]
1786#[cfg(feature = "std")]
1787impl<I, E> StreamIsPartial for Recoverable<I, E>
1788where
1789 I: StreamIsPartial,
1790 I: Stream,
1791{
1792 type PartialState = I::PartialState;
1793
1794 #[inline]
1795 fn complete(&mut self) -> Self::PartialState {
1796 self.input.complete()
1797 }
1798
1799 #[inline]
1800 fn restore_partial(&mut self, state: Self::PartialState) {
1801 self.input.restore_partial(state);
1802 }
1803
1804 #[inline(always)]
1805 fn is_partial_supported() -> bool {
1806 I::is_partial_supported()
1807 }
1808
1809 #[inline(always)]
1810 fn is_partial(&self) -> bool {
1811 self.input.is_partial()
1812 }
1813}
1814
1815impl<I, S> StreamIsPartial for Stateful<I, S>
1816where
1817 I: StreamIsPartial,
1818{
1819 type PartialState = I::PartialState;
1820
1821 #[inline]
1822 fn complete(&mut self) -> Self::PartialState {
1823 self.input.complete()
1824 }
1825
1826 #[inline]
1827 fn restore_partial(&mut self, state: Self::PartialState) {
1828 self.input.restore_partial(state);
1829 }
1830
1831 #[inline(always)]
1832 fn is_partial_supported() -> bool {
1833 I::is_partial_supported()
1834 }
1835
1836 #[inline(always)]
1837 fn is_partial(&self) -> bool {
1838 self.input.is_partial()
1839 }
1840}
1841
1842impl<I> StreamIsPartial for Partial<I>
1843where
1844 I: StreamIsPartial,
1845{
1846 type PartialState = bool;
1847
1848 #[inline]
1849 fn complete(&mut self) -> Self::PartialState {
1850 core::mem::replace(&mut self.partial, false)
1851 }
1852
1853 #[inline]
1854 fn restore_partial(&mut self, state: Self::PartialState) {
1855 self.partial = state;
1856 }
1857
1858 #[inline(always)]
1859 fn is_partial_supported() -> bool {
1860 true
1861 }
1862
1863 #[inline(always)]
1864 fn is_partial(&self) -> bool {
1865 self.partial
1866 }
1867}
1868
1869pub trait Offset<Start = Self> {
1871 fn offset_from(&self, start: &Start) -> usize;
1880}
1881
1882impl<T> Offset for &[T] {
1883 #[inline]
1884 fn offset_from(&self, start: &Self) -> usize {
1885 let fst = (*start).as_ptr();
1886 let snd = (*self).as_ptr();
1887
1888 debug_assert!(
1889 fst <= snd,
1890 "`Offset::offset_from({snd:?}, {fst:?})` only accepts slices of `self`"
1891 );
1892 (snd as usize - fst as usize) / crate::lib::std::mem::size_of::<T>()
1893 }
1894}
1895
1896impl<'a, T> Offset<<&'a [T] as Stream>::Checkpoint> for &'a [T]
1897where
1898 T: Clone + crate::lib::std::fmt::Debug,
1899{
1900 #[inline(always)]
1901 fn offset_from(&self, other: &<&'a [T] as Stream>::Checkpoint) -> usize {
1902 self.checkpoint().offset_from(other)
1903 }
1904}
1905
1906impl Offset for &str {
1907 #[inline(always)]
1908 fn offset_from(&self, start: &Self) -> usize {
1909 self.as_bytes().offset_from(&start.as_bytes())
1910 }
1911}
1912
1913impl<'a> Offset<<&'a str as Stream>::Checkpoint> for &'a str {
1914 #[inline(always)]
1915 fn offset_from(&self, other: &<&'a str as Stream>::Checkpoint) -> usize {
1916 self.checkpoint().offset_from(other)
1917 }
1918}
1919
1920impl Offset for &Bytes {
1921 #[inline(always)]
1922 fn offset_from(&self, start: &Self) -> usize {
1923 self.as_bytes().offset_from(&start.as_bytes())
1924 }
1925}
1926
1927impl<'a> Offset<<&'a Bytes as Stream>::Checkpoint> for &'a Bytes {
1928 #[inline(always)]
1929 fn offset_from(&self, other: &<&'a Bytes as Stream>::Checkpoint) -> usize {
1930 self.checkpoint().offset_from(other)
1931 }
1932}
1933
1934impl Offset for &BStr {
1935 #[inline(always)]
1936 fn offset_from(&self, start: &Self) -> usize {
1937 self.as_bytes().offset_from(&start.as_bytes())
1938 }
1939}
1940
1941impl<'a> Offset<<&'a BStr as Stream>::Checkpoint> for &'a BStr {
1942 #[inline(always)]
1943 fn offset_from(&self, other: &<&'a BStr as Stream>::Checkpoint) -> usize {
1944 self.checkpoint().offset_from(other)
1945 }
1946}
1947
1948impl<I> Offset for (I, usize)
1949where
1950 I: Offset,
1951{
1952 #[inline(always)]
1953 fn offset_from(&self, start: &Self) -> usize {
1954 self.0.offset_from(&start.0) * 8 + self.1 - start.1
1955 }
1956}
1957
1958impl<I> Offset<<(I, usize) as Stream>::Checkpoint> for (I, usize)
1959where
1960 I: Stream<Token = u8> + Clone,
1961{
1962 #[inline(always)]
1963 fn offset_from(&self, other: &<(I, usize) as Stream>::Checkpoint) -> usize {
1964 self.checkpoint().offset_from(other)
1965 }
1966}
1967
1968impl<I> Offset for LocatingSlice<I>
1969where
1970 I: Stream,
1971{
1972 #[inline(always)]
1973 fn offset_from(&self, other: &Self) -> usize {
1974 self.offset_from(&other.checkpoint())
1975 }
1976}
1977
1978impl<I> Offset<<LocatingSlice<I> as Stream>::Checkpoint> for LocatingSlice<I>
1979where
1980 I: Stream,
1981{
1982 #[inline(always)]
1983 fn offset_from(&self, other: &<LocatingSlice<I> as Stream>::Checkpoint) -> usize {
1984 self.checkpoint().offset_from(other)
1985 }
1986}
1987
1988#[cfg(feature = "unstable-recover")]
1989#[cfg(feature = "std")]
1990impl<I, E> Offset for Recoverable<I, E>
1991where
1992 I: Stream,
1993 E: crate::lib::std::fmt::Debug,
1994{
1995 #[inline(always)]
1996 fn offset_from(&self, other: &Self) -> usize {
1997 self.offset_from(&other.checkpoint())
1998 }
1999}
2000
2001#[cfg(feature = "unstable-recover")]
2002#[cfg(feature = "std")]
2003impl<I, E> Offset<<Recoverable<I, E> as Stream>::Checkpoint> for Recoverable<I, E>
2004where
2005 I: Stream,
2006 E: crate::lib::std::fmt::Debug,
2007{
2008 #[inline(always)]
2009 fn offset_from(&self, other: &<Recoverable<I, E> as Stream>::Checkpoint) -> usize {
2010 self.checkpoint().offset_from(other)
2011 }
2012}
2013
2014impl<I, S> Offset for Stateful<I, S>
2015where
2016 I: Stream,
2017 S: Clone + crate::lib::std::fmt::Debug,
2018{
2019 #[inline(always)]
2020 fn offset_from(&self, start: &Self) -> usize {
2021 self.offset_from(&start.checkpoint())
2022 }
2023}
2024
2025impl<I, S> Offset<<Stateful<I, S> as Stream>::Checkpoint> for Stateful<I, S>
2026where
2027 I: Stream,
2028 S: crate::lib::std::fmt::Debug,
2029{
2030 #[inline(always)]
2031 fn offset_from(&self, other: &<Stateful<I, S> as Stream>::Checkpoint) -> usize {
2032 self.checkpoint().offset_from(other)
2033 }
2034}
2035
2036impl<I> Offset for Partial<I>
2037where
2038 I: Stream,
2039{
2040 #[inline(always)]
2041 fn offset_from(&self, start: &Self) -> usize {
2042 self.offset_from(&start.checkpoint())
2043 }
2044}
2045
2046impl<I> Offset<<Partial<I> as Stream>::Checkpoint> for Partial<I>
2047where
2048 I: Stream,
2049{
2050 #[inline(always)]
2051 fn offset_from(&self, other: &<Partial<I> as Stream>::Checkpoint) -> usize {
2052 self.checkpoint().offset_from(other)
2053 }
2054}
2055
2056impl<I, S> Offset for Checkpoint<I, S>
2057where
2058 I: Offset,
2059{
2060 #[inline(always)]
2061 fn offset_from(&self, start: &Self) -> usize {
2062 self.inner.offset_from(&start.inner)
2063 }
2064}
2065
2066pub trait AsBytes {
2068 fn as_bytes(&self) -> &[u8];
2070}
2071
2072impl AsBytes for &[u8] {
2073 #[inline(always)]
2074 fn as_bytes(&self) -> &[u8] {
2075 self
2076 }
2077}
2078
2079impl AsBytes for &Bytes {
2080 #[inline(always)]
2081 fn as_bytes(&self) -> &[u8] {
2082 (*self).as_bytes()
2083 }
2084}
2085
2086impl<I> AsBytes for LocatingSlice<I>
2087where
2088 I: AsBytes,
2089{
2090 #[inline(always)]
2091 fn as_bytes(&self) -> &[u8] {
2092 self.input.as_bytes()
2093 }
2094}
2095
2096#[cfg(feature = "unstable-recover")]
2097#[cfg(feature = "std")]
2098impl<I, E> AsBytes for Recoverable<I, E>
2099where
2100 I: Stream,
2101 I: AsBytes,
2102{
2103 #[inline(always)]
2104 fn as_bytes(&self) -> &[u8] {
2105 self.input.as_bytes()
2106 }
2107}
2108
2109impl<I, S> AsBytes for Stateful<I, S>
2110where
2111 I: AsBytes,
2112{
2113 #[inline(always)]
2114 fn as_bytes(&self) -> &[u8] {
2115 self.input.as_bytes()
2116 }
2117}
2118
2119impl<I> AsBytes for Partial<I>
2120where
2121 I: AsBytes,
2122{
2123 #[inline(always)]
2124 fn as_bytes(&self) -> &[u8] {
2125 self.input.as_bytes()
2126 }
2127}
2128
2129pub trait AsBStr {
2131 fn as_bstr(&self) -> &[u8];
2133}
2134
2135impl AsBStr for &[u8] {
2136 #[inline(always)]
2137 fn as_bstr(&self) -> &[u8] {
2138 self
2139 }
2140}
2141
2142impl AsBStr for &BStr {
2143 #[inline(always)]
2144 fn as_bstr(&self) -> &[u8] {
2145 (*self).as_bytes()
2146 }
2147}
2148
2149impl AsBStr for &str {
2150 #[inline(always)]
2151 fn as_bstr(&self) -> &[u8] {
2152 (*self).as_bytes()
2153 }
2154}
2155
2156impl<I> AsBStr for LocatingSlice<I>
2157where
2158 I: AsBStr,
2159{
2160 #[inline(always)]
2161 fn as_bstr(&self) -> &[u8] {
2162 self.input.as_bstr()
2163 }
2164}
2165
2166#[cfg(feature = "unstable-recover")]
2167#[cfg(feature = "std")]
2168impl<I, E> AsBStr for Recoverable<I, E>
2169where
2170 I: Stream,
2171 I: AsBStr,
2172{
2173 #[inline(always)]
2174 fn as_bstr(&self) -> &[u8] {
2175 self.input.as_bstr()
2176 }
2177}
2178
2179impl<I, S> AsBStr for Stateful<I, S>
2180where
2181 I: AsBStr,
2182{
2183 #[inline(always)]
2184 fn as_bstr(&self) -> &[u8] {
2185 self.input.as_bstr()
2186 }
2187}
2188
2189impl<I> AsBStr for Partial<I>
2190where
2191 I: AsBStr,
2192{
2193 #[inline(always)]
2194 fn as_bstr(&self) -> &[u8] {
2195 self.input.as_bstr()
2196 }
2197}
2198
2199#[derive(Debug, Eq, PartialEq)]
2201pub enum CompareResult {
2202 Ok(usize),
2208 Incomplete,
2210 Error,
2212}
2213
2214pub trait Compare<T> {
2216 fn compare(&self, t: T) -> CompareResult;
2218}
2219
2220impl<'b> Compare<&'b [u8]> for &[u8] {
2221 #[inline]
2222 fn compare(&self, t: &'b [u8]) -> CompareResult {
2223 if t.iter().zip(*self).any(|(a, b)| a != b) {
2224 CompareResult::Error
2225 } else if self.len() < t.slice_len() {
2226 CompareResult::Incomplete
2227 } else {
2228 CompareResult::Ok(t.slice_len())
2229 }
2230 }
2231}
2232
2233impl<'b> Compare<AsciiCaseless<&'b [u8]>> for &[u8] {
2234 #[inline]
2235 fn compare(&self, t: AsciiCaseless<&'b [u8]>) -> CompareResult {
2236 if t.0
2237 .iter()
2238 .zip(*self)
2239 .any(|(a, b)| !a.eq_ignore_ascii_case(b))
2240 {
2241 CompareResult::Error
2242 } else if self.len() < t.slice_len() {
2243 CompareResult::Incomplete
2244 } else {
2245 CompareResult::Ok(t.slice_len())
2246 }
2247 }
2248}
2249
2250impl<const LEN: usize> Compare<[u8; LEN]> for &[u8] {
2251 #[inline(always)]
2252 fn compare(&self, t: [u8; LEN]) -> CompareResult {
2253 self.compare(&t[..])
2254 }
2255}
2256
2257impl<const LEN: usize> Compare<AsciiCaseless<[u8; LEN]>> for &[u8] {
2258 #[inline(always)]
2259 fn compare(&self, t: AsciiCaseless<[u8; LEN]>) -> CompareResult {
2260 self.compare(AsciiCaseless(&t.0[..]))
2261 }
2262}
2263
2264impl<'b, const LEN: usize> Compare<&'b [u8; LEN]> for &[u8] {
2265 #[inline(always)]
2266 fn compare(&self, t: &'b [u8; LEN]) -> CompareResult {
2267 self.compare(&t[..])
2268 }
2269}
2270
2271impl<'b, const LEN: usize> Compare<AsciiCaseless<&'b [u8; LEN]>> for &[u8] {
2272 #[inline(always)]
2273 fn compare(&self, t: AsciiCaseless<&'b [u8; LEN]>) -> CompareResult {
2274 self.compare(AsciiCaseless(&t.0[..]))
2275 }
2276}
2277
2278impl<'b> Compare<&'b str> for &[u8] {
2279 #[inline(always)]
2280 fn compare(&self, t: &'b str) -> CompareResult {
2281 self.compare(t.as_bytes())
2282 }
2283}
2284
2285impl<'b> Compare<AsciiCaseless<&'b str>> for &[u8] {
2286 #[inline(always)]
2287 fn compare(&self, t: AsciiCaseless<&'b str>) -> CompareResult {
2288 self.compare(AsciiCaseless(t.0.as_bytes()))
2289 }
2290}
2291
2292impl Compare<u8> for &[u8] {
2293 #[inline]
2294 fn compare(&self, t: u8) -> CompareResult {
2295 match self.first().copied() {
2296 Some(c) if t == c => CompareResult::Ok(t.slice_len()),
2297 Some(_) => CompareResult::Error,
2298 None => CompareResult::Incomplete,
2299 }
2300 }
2301}
2302
2303impl Compare<AsciiCaseless<u8>> for &[u8] {
2304 #[inline]
2305 fn compare(&self, t: AsciiCaseless<u8>) -> CompareResult {
2306 match self.first() {
2307 Some(c) if t.0.eq_ignore_ascii_case(c) => CompareResult::Ok(t.slice_len()),
2308 Some(_) => CompareResult::Error,
2309 None => CompareResult::Incomplete,
2310 }
2311 }
2312}
2313
2314impl Compare<char> for &[u8] {
2315 #[inline(always)]
2316 fn compare(&self, t: char) -> CompareResult {
2317 self.compare(t.encode_utf8(&mut [0; 4]).as_bytes())
2318 }
2319}
2320
2321impl Compare<AsciiCaseless<char>> for &[u8] {
2322 #[inline(always)]
2323 fn compare(&self, t: AsciiCaseless<char>) -> CompareResult {
2324 self.compare(AsciiCaseless(t.0.encode_utf8(&mut [0; 4]).as_bytes()))
2325 }
2326}
2327
2328impl<'b> Compare<&'b str> for &str {
2329 #[inline(always)]
2330 fn compare(&self, t: &'b str) -> CompareResult {
2331 self.as_bytes().compare(t.as_bytes())
2332 }
2333}
2334
2335impl<'b> Compare<AsciiCaseless<&'b str>> for &str {
2336 #[inline(always)]
2337 fn compare(&self, t: AsciiCaseless<&'b str>) -> CompareResult {
2338 self.as_bytes().compare(t.as_bytes())
2339 }
2340}
2341
2342impl Compare<char> for &str {
2343 #[inline(always)]
2344 fn compare(&self, t: char) -> CompareResult {
2345 self.as_bytes().compare(t)
2346 }
2347}
2348
2349impl Compare<AsciiCaseless<char>> for &str {
2350 #[inline(always)]
2351 fn compare(&self, t: AsciiCaseless<char>) -> CompareResult {
2352 self.as_bytes().compare(t)
2353 }
2354}
2355
2356impl<'a, T> Compare<T> for &'a Bytes
2357where
2358 &'a [u8]: Compare<T>,
2359{
2360 #[inline(always)]
2361 fn compare(&self, t: T) -> CompareResult {
2362 let bytes = (*self).as_bytes();
2363 bytes.compare(t)
2364 }
2365}
2366
2367impl<'a, T> Compare<T> for &'a BStr
2368where
2369 &'a [u8]: Compare<T>,
2370{
2371 #[inline(always)]
2372 fn compare(&self, t: T) -> CompareResult {
2373 let bytes = (*self).as_bytes();
2374 bytes.compare(t)
2375 }
2376}
2377
2378impl<I, U> Compare<U> for LocatingSlice<I>
2379where
2380 I: Compare<U>,
2381{
2382 #[inline(always)]
2383 fn compare(&self, other: U) -> CompareResult {
2384 self.input.compare(other)
2385 }
2386}
2387
2388#[cfg(feature = "unstable-recover")]
2389#[cfg(feature = "std")]
2390impl<I, E, U> Compare<U> for Recoverable<I, E>
2391where
2392 I: Stream,
2393 I: Compare<U>,
2394{
2395 #[inline(always)]
2396 fn compare(&self, other: U) -> CompareResult {
2397 self.input.compare(other)
2398 }
2399}
2400
2401impl<I, S, U> Compare<U> for Stateful<I, S>
2402where
2403 I: Compare<U>,
2404{
2405 #[inline(always)]
2406 fn compare(&self, other: U) -> CompareResult {
2407 self.input.compare(other)
2408 }
2409}
2410
2411impl<I, T> Compare<T> for Partial<I>
2412where
2413 I: Compare<T>,
2414{
2415 #[inline(always)]
2416 fn compare(&self, t: T) -> CompareResult {
2417 self.input.compare(t)
2418 }
2419}
2420
2421pub trait FindSlice<T> {
2423 fn find_slice(&self, substr: T) -> Option<crate::lib::std::ops::Range<usize>>;
2425}
2426
2427impl<'s> FindSlice<&'s [u8]> for &[u8] {
2428 #[inline(always)]
2429 fn find_slice(&self, substr: &'s [u8]) -> Option<crate::lib::std::ops::Range<usize>> {
2430 memmem(self, substr)
2431 }
2432}
2433
2434impl<'s> FindSlice<(&'s [u8],)> for &[u8] {
2435 #[inline(always)]
2436 fn find_slice(&self, substr: (&'s [u8],)) -> Option<crate::lib::std::ops::Range<usize>> {
2437 memmem(self, substr.0)
2438 }
2439}
2440
2441impl<'s> FindSlice<(&'s [u8], &'s [u8])> for &[u8] {
2442 #[inline(always)]
2443 fn find_slice(
2444 &self,
2445 substr: (&'s [u8], &'s [u8]),
2446 ) -> Option<crate::lib::std::ops::Range<usize>> {
2447 memmem2(self, substr)
2448 }
2449}
2450
2451impl<'s> FindSlice<(&'s [u8], &'s [u8], &'s [u8])> for &[u8] {
2452 #[inline(always)]
2453 fn find_slice(
2454 &self,
2455 substr: (&'s [u8], &'s [u8], &'s [u8]),
2456 ) -> Option<crate::lib::std::ops::Range<usize>> {
2457 memmem3(self, substr)
2458 }
2459}
2460
2461impl FindSlice<char> for &[u8] {
2462 #[inline(always)]
2463 fn find_slice(&self, substr: char) -> Option<crate::lib::std::ops::Range<usize>> {
2464 let mut b = [0; 4];
2465 let substr = substr.encode_utf8(&mut b);
2466 self.find_slice(&*substr)
2467 }
2468}
2469
2470impl FindSlice<(char,)> for &[u8] {
2471 #[inline(always)]
2472 fn find_slice(&self, substr: (char,)) -> Option<crate::lib::std::ops::Range<usize>> {
2473 let mut b = [0; 4];
2474 let substr0 = substr.0.encode_utf8(&mut b);
2475 self.find_slice((&*substr0,))
2476 }
2477}
2478
2479impl FindSlice<(char, char)> for &[u8] {
2480 #[inline(always)]
2481 fn find_slice(&self, substr: (char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
2482 let mut b = [0; 4];
2483 let substr0 = substr.0.encode_utf8(&mut b);
2484 let mut b = [0; 4];
2485 let substr1 = substr.1.encode_utf8(&mut b);
2486 self.find_slice((&*substr0, &*substr1))
2487 }
2488}
2489
2490impl FindSlice<(char, char, char)> for &[u8] {
2491 #[inline(always)]
2492 fn find_slice(&self, substr: (char, char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
2493 let mut b = [0; 4];
2494 let substr0 = substr.0.encode_utf8(&mut b);
2495 let mut b = [0; 4];
2496 let substr1 = substr.1.encode_utf8(&mut b);
2497 let mut b = [0; 4];
2498 let substr2 = substr.2.encode_utf8(&mut b);
2499 self.find_slice((&*substr0, &*substr1, &*substr2))
2500 }
2501}
2502
2503impl FindSlice<u8> for &[u8] {
2504 #[inline(always)]
2505 fn find_slice(&self, substr: u8) -> Option<crate::lib::std::ops::Range<usize>> {
2506 memchr(substr, self).map(|i| i..i + 1)
2507 }
2508}
2509
2510impl FindSlice<(u8,)> for &[u8] {
2511 #[inline(always)]
2512 fn find_slice(&self, substr: (u8,)) -> Option<crate::lib::std::ops::Range<usize>> {
2513 memchr(substr.0, self).map(|i| i..i + 1)
2514 }
2515}
2516
2517impl FindSlice<(u8, u8)> for &[u8] {
2518 #[inline(always)]
2519 fn find_slice(&self, substr: (u8, u8)) -> Option<crate::lib::std::ops::Range<usize>> {
2520 memchr2(substr, self).map(|i| i..i + 1)
2521 }
2522}
2523
2524impl FindSlice<(u8, u8, u8)> for &[u8] {
2525 #[inline(always)]
2526 fn find_slice(&self, substr: (u8, u8, u8)) -> Option<crate::lib::std::ops::Range<usize>> {
2527 memchr3(substr, self).map(|i| i..i + 1)
2528 }
2529}
2530
2531impl<'s> FindSlice<&'s str> for &[u8] {
2532 #[inline(always)]
2533 fn find_slice(&self, substr: &'s str) -> Option<crate::lib::std::ops::Range<usize>> {
2534 self.find_slice(substr.as_bytes())
2535 }
2536}
2537
2538impl<'s> FindSlice<(&'s str,)> for &[u8] {
2539 #[inline(always)]
2540 fn find_slice(&self, substr: (&'s str,)) -> Option<crate::lib::std::ops::Range<usize>> {
2541 memmem(self, substr.0.as_bytes())
2542 }
2543}
2544
2545impl<'s> FindSlice<(&'s str, &'s str)> for &[u8] {
2546 #[inline(always)]
2547 fn find_slice(&self, substr: (&'s str, &'s str)) -> Option<crate::lib::std::ops::Range<usize>> {
2548 memmem2(self, (substr.0.as_bytes(), substr.1.as_bytes()))
2549 }
2550}
2551
2552impl<'s> FindSlice<(&'s str, &'s str, &'s str)> for &[u8] {
2553 #[inline(always)]
2554 fn find_slice(
2555 &self,
2556 substr: (&'s str, &'s str, &'s str),
2557 ) -> Option<crate::lib::std::ops::Range<usize>> {
2558 memmem3(
2559 self,
2560 (
2561 substr.0.as_bytes(),
2562 substr.1.as_bytes(),
2563 substr.2.as_bytes(),
2564 ),
2565 )
2566 }
2567}
2568
2569impl<'s> FindSlice<&'s str> for &str {
2570 #[inline(always)]
2571 fn find_slice(&self, substr: &'s str) -> Option<crate::lib::std::ops::Range<usize>> {
2572 self.as_bytes().find_slice(substr)
2573 }
2574}
2575
2576impl<'s> FindSlice<(&'s str,)> for &str {
2577 #[inline(always)]
2578 fn find_slice(&self, substr: (&'s str,)) -> Option<crate::lib::std::ops::Range<usize>> {
2579 self.as_bytes().find_slice(substr)
2580 }
2581}
2582
2583impl<'s> FindSlice<(&'s str, &'s str)> for &str {
2584 #[inline(always)]
2585 fn find_slice(&self, substr: (&'s str, &'s str)) -> Option<crate::lib::std::ops::Range<usize>> {
2586 self.as_bytes().find_slice(substr)
2587 }
2588}
2589
2590impl<'s> FindSlice<(&'s str, &'s str, &'s str)> for &str {
2591 #[inline(always)]
2592 fn find_slice(
2593 &self,
2594 substr: (&'s str, &'s str, &'s str),
2595 ) -> Option<crate::lib::std::ops::Range<usize>> {
2596 self.as_bytes().find_slice(substr)
2597 }
2598}
2599
2600impl FindSlice<char> for &str {
2601 #[inline(always)]
2602 fn find_slice(&self, substr: char) -> Option<crate::lib::std::ops::Range<usize>> {
2603 self.as_bytes().find_slice(substr)
2604 }
2605}
2606
2607impl FindSlice<(char,)> for &str {
2608 #[inline(always)]
2609 fn find_slice(&self, substr: (char,)) -> Option<crate::lib::std::ops::Range<usize>> {
2610 self.as_bytes().find_slice(substr)
2611 }
2612}
2613
2614impl FindSlice<(char, char)> for &str {
2615 #[inline(always)]
2616 fn find_slice(&self, substr: (char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
2617 self.as_bytes().find_slice(substr)
2618 }
2619}
2620
2621impl FindSlice<(char, char, char)> for &str {
2622 #[inline(always)]
2623 fn find_slice(&self, substr: (char, char, char)) -> Option<crate::lib::std::ops::Range<usize>> {
2624 self.as_bytes().find_slice(substr)
2625 }
2626}
2627
2628impl<'i, S> FindSlice<S> for &'i Bytes
2629where
2630 &'i [u8]: FindSlice<S>,
2631{
2632 #[inline(always)]
2633 fn find_slice(&self, substr: S) -> Option<crate::lib::std::ops::Range<usize>> {
2634 let bytes = (*self).as_bytes();
2635 let offset = bytes.find_slice(substr);
2636 offset
2637 }
2638}
2639
2640impl<'i, S> FindSlice<S> for &'i BStr
2641where
2642 &'i [u8]: FindSlice<S>,
2643{
2644 #[inline(always)]
2645 fn find_slice(&self, substr: S) -> Option<crate::lib::std::ops::Range<usize>> {
2646 let bytes = (*self).as_bytes();
2647 let offset = bytes.find_slice(substr);
2648 offset
2649 }
2650}
2651
2652impl<I, T> FindSlice<T> for LocatingSlice<I>
2653where
2654 I: FindSlice<T>,
2655{
2656 #[inline(always)]
2657 fn find_slice(&self, substr: T) -> Option<crate::lib::std::ops::Range<usize>> {
2658 self.input.find_slice(substr)
2659 }
2660}
2661
2662#[cfg(feature = "unstable-recover")]
2663#[cfg(feature = "std")]
2664impl<I, E, T> FindSlice<T> for Recoverable<I, E>
2665where
2666 I: Stream,
2667 I: FindSlice<T>,
2668{
2669 #[inline(always)]
2670 fn find_slice(&self, substr: T) -> Option<crate::lib::std::ops::Range<usize>> {
2671 self.input.find_slice(substr)
2672 }
2673}
2674
2675impl<I, S, T> FindSlice<T> for Stateful<I, S>
2676where
2677 I: FindSlice<T>,
2678{
2679 #[inline(always)]
2680 fn find_slice(&self, substr: T) -> Option<crate::lib::std::ops::Range<usize>> {
2681 self.input.find_slice(substr)
2682 }
2683}
2684
2685impl<I, T> FindSlice<T> for Partial<I>
2686where
2687 I: FindSlice<T>,
2688{
2689 #[inline(always)]
2690 fn find_slice(&self, substr: T) -> Option<crate::lib::std::ops::Range<usize>> {
2691 self.input.find_slice(substr)
2692 }
2693}
2694
2695pub trait ParseSlice<R> {
2697 fn parse_slice(&self) -> Option<R>;
2702}
2703
2704impl<R: FromStr> ParseSlice<R> for &[u8] {
2705 #[inline(always)]
2706 fn parse_slice(&self) -> Option<R> {
2707 from_utf8(self).ok().and_then(|s| s.parse().ok())
2708 }
2709}
2710
2711impl<R: FromStr> ParseSlice<R> for &str {
2712 #[inline(always)]
2713 fn parse_slice(&self) -> Option<R> {
2714 self.parse().ok()
2715 }
2716}
2717
2718pub trait UpdateSlice: Stream {
2720 fn update_slice(self, inner: Self::Slice) -> Self;
2722}
2723
2724impl<T> UpdateSlice for &[T]
2725where
2726 T: Clone + crate::lib::std::fmt::Debug,
2727{
2728 #[inline(always)]
2729 fn update_slice(self, inner: Self::Slice) -> Self {
2730 inner
2731 }
2732}
2733
2734impl UpdateSlice for &str {
2735 #[inline(always)]
2736 fn update_slice(self, inner: Self::Slice) -> Self {
2737 inner
2738 }
2739}
2740
2741impl UpdateSlice for &Bytes {
2742 #[inline(always)]
2743 fn update_slice(self, inner: Self::Slice) -> Self {
2744 Bytes::new(inner)
2745 }
2746}
2747
2748impl UpdateSlice for &BStr {
2749 #[inline(always)]
2750 fn update_slice(self, inner: Self::Slice) -> Self {
2751 BStr::new(inner)
2752 }
2753}
2754
2755impl<I> UpdateSlice for LocatingSlice<I>
2756where
2757 I: UpdateSlice,
2758{
2759 #[inline(always)]
2760 fn update_slice(mut self, inner: Self::Slice) -> Self {
2761 self.input = I::update_slice(self.input, inner);
2762 self
2763 }
2764}
2765
2766#[cfg(feature = "unstable-recover")]
2767#[cfg(feature = "std")]
2768impl<I, E> UpdateSlice for Recoverable<I, E>
2769where
2770 I: Stream,
2771 I: UpdateSlice,
2772 E: crate::lib::std::fmt::Debug,
2773{
2774 #[inline(always)]
2775 fn update_slice(mut self, inner: Self::Slice) -> Self {
2776 self.input = I::update_slice(self.input, inner);
2777 self
2778 }
2779}
2780
2781impl<I, S> UpdateSlice for Stateful<I, S>
2782where
2783 I: UpdateSlice,
2784 S: Clone + crate::lib::std::fmt::Debug,
2785{
2786 #[inline(always)]
2787 fn update_slice(mut self, inner: Self::Slice) -> Self {
2788 self.input = I::update_slice(self.input, inner);
2789 self
2790 }
2791}
2792
2793impl<I> UpdateSlice for Partial<I>
2794where
2795 I: UpdateSlice,
2796{
2797 #[inline(always)]
2798 fn update_slice(self, inner: Self::Slice) -> Self {
2799 Partial {
2800 input: I::update_slice(self.input, inner),
2801 partial: self.partial,
2802 }
2803 }
2804}
2805
2806pub struct Checkpoint<T, S> {
2808 inner: T,
2809 stream: core::marker::PhantomData<S>,
2810}
2811
2812impl<T, S> Checkpoint<T, S> {
2813 fn new(inner: T) -> Self {
2814 Self {
2815 inner,
2816 stream: Default::default(),
2817 }
2818 }
2819}
2820
2821impl<T: Copy, S> Copy for Checkpoint<T, S> {}
2822
2823impl<T: Clone, S> Clone for Checkpoint<T, S> {
2824 #[inline(always)]
2825 fn clone(&self) -> Self {
2826 Self {
2827 inner: self.inner.clone(),
2828 stream: Default::default(),
2829 }
2830 }
2831}
2832
2833impl<T: PartialOrd, S> PartialOrd for Checkpoint<T, S> {
2834 #[inline(always)]
2835 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
2836 self.inner.partial_cmp(&other.inner)
2837 }
2838}
2839
2840impl<T: Ord, S> Ord for Checkpoint<T, S> {
2841 #[inline(always)]
2842 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
2843 self.inner.cmp(&other.inner)
2844 }
2845}
2846
2847impl<T: PartialEq, S> PartialEq for Checkpoint<T, S> {
2848 #[inline(always)]
2849 fn eq(&self, other: &Self) -> bool {
2850 self.inner.eq(&other.inner)
2851 }
2852}
2853
2854impl<T: Eq, S> Eq for Checkpoint<T, S> {}
2855
2856impl<T: crate::lib::std::fmt::Debug, S> crate::lib::std::fmt::Debug for Checkpoint<T, S> {
2857 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
2858 self.inner.fmt(f)
2859 }
2860}
2861
2862#[derive(PartialEq, Eq)]
2891pub struct Range {
2892 pub(crate) start_inclusive: usize,
2893 pub(crate) end_inclusive: Option<usize>,
2894}
2895
2896impl Range {
2897 #[inline(always)]
2898 fn raw(start_inclusive: usize, end_inclusive: Option<usize>) -> Self {
2899 Self {
2900 start_inclusive,
2901 end_inclusive,
2902 }
2903 }
2904}
2905
2906impl crate::lib::std::ops::RangeBounds<usize> for Range {
2907 #[inline(always)]
2908 fn start_bound(&self) -> crate::lib::std::ops::Bound<&usize> {
2909 crate::lib::std::ops::Bound::Included(&self.start_inclusive)
2910 }
2911
2912 #[inline(always)]
2913 fn end_bound(&self) -> crate::lib::std::ops::Bound<&usize> {
2914 if let Some(end_inclusive) = &self.end_inclusive {
2915 crate::lib::std::ops::Bound::Included(end_inclusive)
2916 } else {
2917 crate::lib::std::ops::Bound::Unbounded
2918 }
2919 }
2920}
2921
2922impl From<usize> for Range {
2923 #[inline(always)]
2924 fn from(fixed: usize) -> Self {
2925 (fixed..=fixed).into()
2926 }
2927}
2928
2929impl From<crate::lib::std::ops::Range<usize>> for Range {
2930 #[inline(always)]
2931 fn from(range: crate::lib::std::ops::Range<usize>) -> Self {
2932 let start_inclusive = range.start;
2933 let end_inclusive = Some(range.end.saturating_sub(1));
2934 Self::raw(start_inclusive, end_inclusive)
2935 }
2936}
2937
2938impl From<crate::lib::std::ops::RangeFull> for Range {
2939 #[inline(always)]
2940 fn from(_: crate::lib::std::ops::RangeFull) -> Self {
2941 let start_inclusive = 0;
2942 let end_inclusive = None;
2943 Self::raw(start_inclusive, end_inclusive)
2944 }
2945}
2946
2947impl From<crate::lib::std::ops::RangeFrom<usize>> for Range {
2948 #[inline(always)]
2949 fn from(range: crate::lib::std::ops::RangeFrom<usize>) -> Self {
2950 let start_inclusive = range.start;
2951 let end_inclusive = None;
2952 Self::raw(start_inclusive, end_inclusive)
2953 }
2954}
2955
2956impl From<crate::lib::std::ops::RangeTo<usize>> for Range {
2957 #[inline(always)]
2958 fn from(range: crate::lib::std::ops::RangeTo<usize>) -> Self {
2959 let start_inclusive = 0;
2960 let end_inclusive = Some(range.end.saturating_sub(1));
2961 Self::raw(start_inclusive, end_inclusive)
2962 }
2963}
2964
2965impl From<crate::lib::std::ops::RangeInclusive<usize>> for Range {
2966 #[inline(always)]
2967 fn from(range: crate::lib::std::ops::RangeInclusive<usize>) -> Self {
2968 let start_inclusive = *range.start();
2969 let end_inclusive = Some(*range.end());
2970 Self::raw(start_inclusive, end_inclusive)
2971 }
2972}
2973
2974impl From<crate::lib::std::ops::RangeToInclusive<usize>> for Range {
2975 #[inline(always)]
2976 fn from(range: crate::lib::std::ops::RangeToInclusive<usize>) -> Self {
2977 let start_inclusive = 0;
2978 let end_inclusive = Some(range.end);
2979 Self::raw(start_inclusive, end_inclusive)
2980 }
2981}
2982
2983impl crate::lib::std::fmt::Display for Range {
2984 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
2985 self.start_inclusive.fmt(f)?;
2986 match self.end_inclusive {
2987 Some(e) if e == self.start_inclusive => {}
2988 Some(e) => {
2989 "..=".fmt(f)?;
2990 e.fmt(f)?;
2991 }
2992 None => {
2993 "..".fmt(f)?;
2994 }
2995 }
2996 Ok(())
2997 }
2998}
2999
3000impl crate::lib::std::fmt::Debug for Range {
3001 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
3002 write!(f, "{self}")
3003 }
3004}
3005
3006pub trait Accumulate<T>: Sized {
3009 fn initial(capacity: Option<usize>) -> Self;
3011 fn accumulate(&mut self, acc: T);
3013}
3014
3015impl<T> Accumulate<T> for () {
3016 #[inline(always)]
3017 fn initial(_capacity: Option<usize>) -> Self {}
3018 #[inline(always)]
3019 fn accumulate(&mut self, _acc: T) {}
3020}
3021
3022impl<T> Accumulate<T> for usize {
3023 #[inline(always)]
3024 fn initial(_capacity: Option<usize>) -> Self {
3025 0
3026 }
3027 #[inline(always)]
3028 fn accumulate(&mut self, _acc: T) {
3029 *self += 1;
3030 }
3031}
3032
3033#[cfg(feature = "alloc")]
3034impl<T> Accumulate<T> for Vec<T> {
3035 #[inline(always)]
3036 fn initial(capacity: Option<usize>) -> Self {
3037 match capacity {
3038 Some(capacity) => Vec::with_capacity(clamp_capacity::<T>(capacity)),
3039 None => Vec::new(),
3040 }
3041 }
3042 #[inline(always)]
3043 fn accumulate(&mut self, acc: T) {
3044 self.push(acc);
3045 }
3046}
3047
3048#[cfg(feature = "alloc")]
3049impl<'i, T: Clone> Accumulate<&'i [T]> for Vec<T> {
3050 #[inline(always)]
3051 fn initial(capacity: Option<usize>) -> Self {
3052 match capacity {
3053 Some(capacity) => Vec::with_capacity(clamp_capacity::<T>(capacity)),
3054 None => Vec::new(),
3055 }
3056 }
3057 #[inline(always)]
3058 fn accumulate(&mut self, acc: &'i [T]) {
3059 self.extend(acc.iter().cloned());
3060 }
3061}
3062
3063#[cfg(feature = "alloc")]
3064impl Accumulate<char> for String {
3065 #[inline(always)]
3066 fn initial(capacity: Option<usize>) -> Self {
3067 match capacity {
3068 Some(capacity) => String::with_capacity(clamp_capacity::<char>(capacity)),
3069 None => String::new(),
3070 }
3071 }
3072 #[inline(always)]
3073 fn accumulate(&mut self, acc: char) {
3074 self.push(acc);
3075 }
3076}
3077
3078#[cfg(feature = "alloc")]
3079impl<'i> Accumulate<&'i str> for String {
3080 #[inline(always)]
3081 fn initial(capacity: Option<usize>) -> Self {
3082 match capacity {
3083 Some(capacity) => String::with_capacity(clamp_capacity::<char>(capacity)),
3084 None => String::new(),
3085 }
3086 }
3087 #[inline(always)]
3088 fn accumulate(&mut self, acc: &'i str) {
3089 self.push_str(acc);
3090 }
3091}
3092
3093#[cfg(feature = "alloc")]
3094impl<K, V> Accumulate<(K, V)> for BTreeMap<K, V>
3095where
3096 K: crate::lib::std::cmp::Ord,
3097{
3098 #[inline(always)]
3099 fn initial(_capacity: Option<usize>) -> Self {
3100 BTreeMap::new()
3101 }
3102 #[inline(always)]
3103 fn accumulate(&mut self, (key, value): (K, V)) {
3104 self.insert(key, value);
3105 }
3106}
3107
3108#[cfg(feature = "std")]
3109impl<K, V, S> Accumulate<(K, V)> for HashMap<K, V, S>
3110where
3111 K: crate::lib::std::cmp::Eq + crate::lib::std::hash::Hash,
3112 S: BuildHasher + Default,
3113{
3114 #[inline(always)]
3115 fn initial(capacity: Option<usize>) -> Self {
3116 let h = S::default();
3117 match capacity {
3118 Some(capacity) => {
3119 HashMap::with_capacity_and_hasher(clamp_capacity::<(K, V)>(capacity), h)
3120 }
3121 None => HashMap::with_hasher(h),
3122 }
3123 }
3124 #[inline(always)]
3125 fn accumulate(&mut self, (key, value): (K, V)) {
3126 self.insert(key, value);
3127 }
3128}
3129
3130#[cfg(feature = "alloc")]
3131impl<K> Accumulate<K> for BTreeSet<K>
3132where
3133 K: crate::lib::std::cmp::Ord,
3134{
3135 #[inline(always)]
3136 fn initial(_capacity: Option<usize>) -> Self {
3137 BTreeSet::new()
3138 }
3139 #[inline(always)]
3140 fn accumulate(&mut self, key: K) {
3141 self.insert(key);
3142 }
3143}
3144
3145#[cfg(feature = "std")]
3146impl<K, S> Accumulate<K> for HashSet<K, S>
3147where
3148 K: crate::lib::std::cmp::Eq + crate::lib::std::hash::Hash,
3149 S: BuildHasher + Default,
3150{
3151 #[inline(always)]
3152 fn initial(capacity: Option<usize>) -> Self {
3153 let h = S::default();
3154 match capacity {
3155 Some(capacity) => HashSet::with_capacity_and_hasher(clamp_capacity::<K>(capacity), h),
3156 None => HashSet::with_hasher(h),
3157 }
3158 }
3159 #[inline(always)]
3160 fn accumulate(&mut self, key: K) {
3161 self.insert(key);
3162 }
3163}
3164
3165#[cfg(feature = "alloc")]
3166#[inline]
3167pub(crate) fn clamp_capacity<T>(capacity: usize) -> usize {
3168 const MAX_INITIAL_CAPACITY_BYTES: usize = 65536;
3178
3179 let max_initial_capacity =
3180 MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::<T>().max(1);
3181 capacity.min(max_initial_capacity)
3182}
3183
3184pub trait ToUsize {
3191 fn to_usize(&self) -> usize;
3193}
3194
3195impl ToUsize for u8 {
3196 #[inline(always)]
3197 fn to_usize(&self) -> usize {
3198 *self as usize
3199 }
3200}
3201
3202impl ToUsize for u16 {
3203 #[inline(always)]
3204 fn to_usize(&self) -> usize {
3205 *self as usize
3206 }
3207}
3208
3209impl ToUsize for usize {
3210 #[inline(always)]
3211 fn to_usize(&self) -> usize {
3212 *self
3213 }
3214}
3215
3216#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
3217impl ToUsize for u32 {
3218 #[inline(always)]
3219 fn to_usize(&self) -> usize {
3220 *self as usize
3221 }
3222}
3223
3224#[cfg(target_pointer_width = "64")]
3225impl ToUsize for u64 {
3226 #[inline(always)]
3227 fn to_usize(&self) -> usize {
3228 *self as usize
3229 }
3230}
3231
3232#[allow(clippy::len_without_is_empty)]
3234#[allow(clippy::wrong_self_convention)]
3235pub trait AsChar {
3236 fn as_char(self) -> char;
3247
3248 fn is_alpha(self) -> bool;
3257
3258 fn is_alphanum(self) -> bool;
3261 fn is_dec_digit(self) -> bool;
3263 fn is_hex_digit(self) -> bool;
3265 fn is_oct_digit(self) -> bool;
3267 fn len(self) -> usize;
3269 fn is_space(self) -> bool;
3271 fn is_newline(self) -> bool;
3273}
3274
3275impl AsChar for u8 {
3276 #[inline(always)]
3277 fn as_char(self) -> char {
3278 self as char
3279 }
3280 #[inline]
3281 fn is_alpha(self) -> bool {
3282 matches!(self, 0x41..=0x5A | 0x61..=0x7A)
3283 }
3284 #[inline]
3285 fn is_alphanum(self) -> bool {
3286 self.is_alpha() || self.is_dec_digit()
3287 }
3288 #[inline]
3289 fn is_dec_digit(self) -> bool {
3290 matches!(self, 0x30..=0x39)
3291 }
3292 #[inline]
3293 fn is_hex_digit(self) -> bool {
3294 matches!(self, 0x30..=0x39 | 0x41..=0x46 | 0x61..=0x66)
3295 }
3296 #[inline]
3297 fn is_oct_digit(self) -> bool {
3298 matches!(self, 0x30..=0x37)
3299 }
3300 #[inline]
3301 fn len(self) -> usize {
3302 1
3303 }
3304 #[inline]
3305 fn is_space(self) -> bool {
3306 self == b' ' || self == b'\t'
3307 }
3308 #[inline]
3309 fn is_newline(self) -> bool {
3310 self == b'\n'
3311 }
3312}
3313
3314impl AsChar for &u8 {
3315 #[inline(always)]
3316 fn as_char(self) -> char {
3317 (*self).as_char()
3318 }
3319 #[inline(always)]
3320 fn is_alpha(self) -> bool {
3321 (*self).is_alpha()
3322 }
3323 #[inline(always)]
3324 fn is_alphanum(self) -> bool {
3325 (*self).is_alphanum()
3326 }
3327 #[inline(always)]
3328 fn is_dec_digit(self) -> bool {
3329 (*self).is_dec_digit()
3330 }
3331 #[inline(always)]
3332 fn is_hex_digit(self) -> bool {
3333 (*self).is_hex_digit()
3334 }
3335 #[inline(always)]
3336 fn is_oct_digit(self) -> bool {
3337 (*self).is_oct_digit()
3338 }
3339 #[inline(always)]
3340 fn len(self) -> usize {
3341 (*self).len()
3342 }
3343 #[inline(always)]
3344 fn is_space(self) -> bool {
3345 (*self).is_space()
3346 }
3347 #[inline(always)]
3348 fn is_newline(self) -> bool {
3349 (*self).is_newline()
3350 }
3351}
3352
3353impl AsChar for char {
3354 #[inline(always)]
3355 fn as_char(self) -> char {
3356 self
3357 }
3358 #[inline]
3359 fn is_alpha(self) -> bool {
3360 self.is_ascii_alphabetic()
3361 }
3362 #[inline]
3363 fn is_alphanum(self) -> bool {
3364 self.is_alpha() || self.is_dec_digit()
3365 }
3366 #[inline]
3367 fn is_dec_digit(self) -> bool {
3368 self.is_ascii_digit()
3369 }
3370 #[inline]
3371 fn is_hex_digit(self) -> bool {
3372 self.is_ascii_hexdigit()
3373 }
3374 #[inline]
3375 fn is_oct_digit(self) -> bool {
3376 self.is_digit(8)
3377 }
3378 #[inline]
3379 fn len(self) -> usize {
3380 self.len_utf8()
3381 }
3382 #[inline]
3383 fn is_space(self) -> bool {
3384 self == ' ' || self == '\t'
3385 }
3386 #[inline]
3387 fn is_newline(self) -> bool {
3388 self == '\n'
3389 }
3390}
3391
3392impl AsChar for &char {
3393 #[inline(always)]
3394 fn as_char(self) -> char {
3395 (*self).as_char()
3396 }
3397 #[inline(always)]
3398 fn is_alpha(self) -> bool {
3399 (*self).is_alpha()
3400 }
3401 #[inline(always)]
3402 fn is_alphanum(self) -> bool {
3403 (*self).is_alphanum()
3404 }
3405 #[inline(always)]
3406 fn is_dec_digit(self) -> bool {
3407 (*self).is_dec_digit()
3408 }
3409 #[inline(always)]
3410 fn is_hex_digit(self) -> bool {
3411 (*self).is_hex_digit()
3412 }
3413 #[inline(always)]
3414 fn is_oct_digit(self) -> bool {
3415 (*self).is_oct_digit()
3416 }
3417 #[inline(always)]
3418 fn len(self) -> usize {
3419 (*self).len()
3420 }
3421 #[inline(always)]
3422 fn is_space(self) -> bool {
3423 (*self).is_space()
3424 }
3425 #[inline(always)]
3426 fn is_newline(self) -> bool {
3427 (*self).is_newline()
3428 }
3429}
3430
3431pub trait ContainsToken<T> {
3456 fn contains_token(&self, token: T) -> bool;
3458}
3459
3460impl ContainsToken<u8> for u8 {
3461 #[inline(always)]
3462 fn contains_token(&self, token: u8) -> bool {
3463 *self == token
3464 }
3465}
3466
3467impl ContainsToken<&u8> for u8 {
3468 #[inline(always)]
3469 fn contains_token(&self, token: &u8) -> bool {
3470 self.contains_token(*token)
3471 }
3472}
3473
3474impl ContainsToken<char> for u8 {
3475 #[inline(always)]
3476 fn contains_token(&self, token: char) -> bool {
3477 self.as_char() == token
3478 }
3479}
3480
3481impl ContainsToken<&char> for u8 {
3482 #[inline(always)]
3483 fn contains_token(&self, token: &char) -> bool {
3484 self.contains_token(*token)
3485 }
3486}
3487
3488impl<C: AsChar> ContainsToken<C> for char {
3489 #[inline(always)]
3490 fn contains_token(&self, token: C) -> bool {
3491 *self == token.as_char()
3492 }
3493}
3494
3495impl<C, F: Fn(C) -> bool> ContainsToken<C> for F {
3496 #[inline(always)]
3497 fn contains_token(&self, token: C) -> bool {
3498 self(token)
3499 }
3500}
3501
3502impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::Range<C2> {
3503 #[inline(always)]
3504 fn contains_token(&self, token: C1) -> bool {
3505 let start = self.start.clone().as_char();
3506 let end = self.end.clone().as_char();
3507 (start..end).contains(&token.as_char())
3508 }
3509}
3510
3511impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1>
3512 for crate::lib::std::ops::RangeInclusive<C2>
3513{
3514 #[inline(always)]
3515 fn contains_token(&self, token: C1) -> bool {
3516 let start = self.start().clone().as_char();
3517 let end = self.end().clone().as_char();
3518 (start..=end).contains(&token.as_char())
3519 }
3520}
3521
3522impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::RangeFrom<C2> {
3523 #[inline(always)]
3524 fn contains_token(&self, token: C1) -> bool {
3525 let start = self.start.clone().as_char();
3526 (start..).contains(&token.as_char())
3527 }
3528}
3529
3530impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1> for crate::lib::std::ops::RangeTo<C2> {
3531 #[inline(always)]
3532 fn contains_token(&self, token: C1) -> bool {
3533 let end = self.end.clone().as_char();
3534 (..end).contains(&token.as_char())
3535 }
3536}
3537
3538impl<C1: AsChar, C2: AsChar + Clone> ContainsToken<C1>
3539 for crate::lib::std::ops::RangeToInclusive<C2>
3540{
3541 #[inline(always)]
3542 fn contains_token(&self, token: C1) -> bool {
3543 let end = self.end.clone().as_char();
3544 (..=end).contains(&token.as_char())
3545 }
3546}
3547
3548impl<C1: AsChar> ContainsToken<C1> for crate::lib::std::ops::RangeFull {
3549 #[inline(always)]
3550 fn contains_token(&self, _token: C1) -> bool {
3551 true
3552 }
3553}
3554
3555impl<C: AsChar> ContainsToken<C> for &'_ [u8] {
3556 #[inline]
3557 fn contains_token(&self, token: C) -> bool {
3558 let token = token.as_char();
3559 self.iter().any(|t| t.as_char() == token)
3560 }
3561}
3562
3563impl<C: AsChar> ContainsToken<C> for &'_ [char] {
3564 #[inline]
3565 fn contains_token(&self, token: C) -> bool {
3566 let token = token.as_char();
3567 self.iter().any(|t| *t == token)
3568 }
3569}
3570
3571impl<const LEN: usize, C: AsChar> ContainsToken<C> for &'_ [u8; LEN] {
3572 #[inline]
3573 fn contains_token(&self, token: C) -> bool {
3574 let token = token.as_char();
3575 self.iter().any(|t| t.as_char() == token)
3576 }
3577}
3578
3579impl<const LEN: usize, C: AsChar> ContainsToken<C> for &'_ [char; LEN] {
3580 #[inline]
3581 fn contains_token(&self, token: C) -> bool {
3582 let token = token.as_char();
3583 self.iter().any(|t| *t == token)
3584 }
3585}
3586
3587impl<const LEN: usize, C: AsChar> ContainsToken<C> for [u8; LEN] {
3588 #[inline]
3589 fn contains_token(&self, token: C) -> bool {
3590 let token = token.as_char();
3591 self.iter().any(|t| t.as_char() == token)
3592 }
3593}
3594
3595impl<const LEN: usize, C: AsChar> ContainsToken<C> for [char; LEN] {
3596 #[inline]
3597 fn contains_token(&self, token: C) -> bool {
3598 let token = token.as_char();
3599 self.iter().any(|t| *t == token)
3600 }
3601}
3602
3603impl<T> ContainsToken<T> for () {
3604 #[inline(always)]
3605 fn contains_token(&self, _token: T) -> bool {
3606 false
3607 }
3608}
3609
3610macro_rules! impl_contains_token_for_tuple {
3611 ($($haystack:ident),+) => (
3612 #[allow(non_snake_case)]
3613 impl<T, $($haystack),+> ContainsToken<T> for ($($haystack),+,)
3614 where
3615 T: Clone,
3616 $($haystack: ContainsToken<T>),+
3617 {
3618 #[inline]
3619 fn contains_token(&self, token: T) -> bool {
3620 let ($(ref $haystack),+,) = *self;
3621 $($haystack.contains_token(token.clone()) || )+ false
3622 }
3623 }
3624 )
3625}
3626
3627macro_rules! impl_contains_token_for_tuples {
3628 ($haystack1:ident, $($haystack:ident),+) => {
3629 impl_contains_token_for_tuples!(__impl $haystack1; $($haystack),+);
3630 };
3631 (__impl $($haystack:ident),+; $haystack1:ident $(,$haystack2:ident)*) => {
3632 impl_contains_token_for_tuple!($($haystack),+);
3633 impl_contains_token_for_tuples!(__impl $($haystack),+, $haystack1; $($haystack2),*);
3634 };
3635 (__impl $($haystack:ident),+;) => {
3636 impl_contains_token_for_tuple!($($haystack),+);
3637 }
3638}
3639
3640impl_contains_token_for_tuples!(
3641 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21
3642);
3643
3644#[cfg(feature = "simd")]
3645#[inline(always)]
3646fn memchr(token: u8, slice: &[u8]) -> Option<usize> {
3647 memchr::memchr(token, slice)
3648}
3649
3650#[cfg(feature = "simd")]
3651#[inline(always)]
3652fn memchr2(token: (u8, u8), slice: &[u8]) -> Option<usize> {
3653 memchr::memchr2(token.0, token.1, slice)
3654}
3655
3656#[cfg(feature = "simd")]
3657#[inline(always)]
3658fn memchr3(token: (u8, u8, u8), slice: &[u8]) -> Option<usize> {
3659 memchr::memchr3(token.0, token.1, token.2, slice)
3660}
3661
3662#[cfg(not(feature = "simd"))]
3663#[inline(always)]
3664fn memchr(token: u8, slice: &[u8]) -> Option<usize> {
3665 slice.iter().position(|t| *t == token)
3666}
3667
3668#[cfg(not(feature = "simd"))]
3669#[inline(always)]
3670fn memchr2(token: (u8, u8), slice: &[u8]) -> Option<usize> {
3671 slice.iter().position(|t| *t == token.0 || *t == token.1)
3672}
3673
3674#[cfg(not(feature = "simd"))]
3675#[inline(always)]
3676fn memchr3(token: (u8, u8, u8), slice: &[u8]) -> Option<usize> {
3677 slice
3678 .iter()
3679 .position(|t| *t == token.0 || *t == token.1 || *t == token.2)
3680}
3681
3682#[inline(always)]
3683fn memmem(slice: &[u8], literal: &[u8]) -> Option<crate::lib::std::ops::Range<usize>> {
3684 match literal.len() {
3685 0 => Some(0..0),
3686 1 => memchr(literal[0], slice).map(|i| i..i + 1),
3687 _ => memmem_(slice, literal),
3688 }
3689}
3690
3691#[inline(always)]
3692fn memmem2(slice: &[u8], literal: (&[u8], &[u8])) -> Option<crate::lib::std::ops::Range<usize>> {
3693 match (literal.0.len(), literal.1.len()) {
3694 (0, _) | (_, 0) => Some(0..0),
3695 (1, 1) => memchr2((literal.0[0], literal.1[0]), slice).map(|i| i..i + 1),
3696 _ => memmem2_(slice, literal),
3697 }
3698}
3699
3700#[inline(always)]
3701fn memmem3(
3702 slice: &[u8],
3703 literal: (&[u8], &[u8], &[u8]),
3704) -> Option<crate::lib::std::ops::Range<usize>> {
3705 match (literal.0.len(), literal.1.len(), literal.2.len()) {
3706 (0, _, _) | (_, 0, _) | (_, _, 0) => Some(0..0),
3707 (1, 1, 1) => memchr3((literal.0[0], literal.1[0], literal.2[0]), slice).map(|i| i..i + 1),
3708 _ => memmem3_(slice, literal),
3709 }
3710}
3711
3712#[cfg(feature = "simd")]
3713#[inline(always)]
3714fn memmem_(slice: &[u8], literal: &[u8]) -> Option<crate::lib::std::ops::Range<usize>> {
3715 let &prefix = match literal.first() {
3716 Some(x) => x,
3717 None => return Some(0..0),
3718 };
3719 #[allow(clippy::manual_find)] for i in memchr::memchr_iter(prefix, slice) {
3721 if slice[i..].starts_with(literal) {
3722 let i_end = i + literal.len();
3723 return Some(i..i_end);
3724 }
3725 }
3726 None
3727}
3728
3729#[cfg(feature = "simd")]
3730fn memmem2_(slice: &[u8], literal: (&[u8], &[u8])) -> Option<crate::lib::std::ops::Range<usize>> {
3731 let prefix = match (literal.0.first(), literal.1.first()) {
3732 (Some(&a), Some(&b)) => (a, b),
3733 _ => return Some(0..0),
3734 };
3735 #[allow(clippy::manual_find)] for i in memchr::memchr2_iter(prefix.0, prefix.1, slice) {
3737 let subslice = &slice[i..];
3738 if subslice.starts_with(literal.0) {
3739 let i_end = i + literal.0.len();
3740 return Some(i..i_end);
3741 }
3742 if subslice.starts_with(literal.1) {
3743 let i_end = i + literal.1.len();
3744 return Some(i..i_end);
3745 }
3746 }
3747 None
3748}
3749
3750#[cfg(feature = "simd")]
3751fn memmem3_(
3752 slice: &[u8],
3753 literal: (&[u8], &[u8], &[u8]),
3754) -> Option<crate::lib::std::ops::Range<usize>> {
3755 let prefix = match (literal.0.first(), literal.1.first(), literal.2.first()) {
3756 (Some(&a), Some(&b), Some(&c)) => (a, b, c),
3757 _ => return Some(0..0),
3758 };
3759 #[allow(clippy::manual_find)] for i in memchr::memchr3_iter(prefix.0, prefix.1, prefix.2, slice) {
3761 let subslice = &slice[i..];
3762 if subslice.starts_with(literal.0) {
3763 let i_end = i + literal.0.len();
3764 return Some(i..i_end);
3765 }
3766 if subslice.starts_with(literal.1) {
3767 let i_end = i + literal.1.len();
3768 return Some(i..i_end);
3769 }
3770 if subslice.starts_with(literal.2) {
3771 let i_end = i + literal.2.len();
3772 return Some(i..i_end);
3773 }
3774 }
3775 None
3776}
3777
3778#[cfg(not(feature = "simd"))]
3779fn memmem_(slice: &[u8], literal: &[u8]) -> Option<crate::lib::std::ops::Range<usize>> {
3780 for i in 0..slice.len() {
3781 let subslice = &slice[i..];
3782 if subslice.starts_with(literal) {
3783 let i_end = i + literal.len();
3784 return Some(i..i_end);
3785 }
3786 }
3787 None
3788}
3789
3790#[cfg(not(feature = "simd"))]
3791fn memmem2_(slice: &[u8], literal: (&[u8], &[u8])) -> Option<crate::lib::std::ops::Range<usize>> {
3792 for i in 0..slice.len() {
3793 let subslice = &slice[i..];
3794 if subslice.starts_with(literal.0) {
3795 let i_end = i + literal.0.len();
3796 return Some(i..i_end);
3797 }
3798 if subslice.starts_with(literal.1) {
3799 let i_end = i + literal.1.len();
3800 return Some(i..i_end);
3801 }
3802 }
3803 None
3804}
3805
3806#[cfg(not(feature = "simd"))]
3807fn memmem3_(
3808 slice: &[u8],
3809 literal: (&[u8], &[u8], &[u8]),
3810) -> Option<crate::lib::std::ops::Range<usize>> {
3811 for i in 0..slice.len() {
3812 let subslice = &slice[i..];
3813 if subslice.starts_with(literal.0) {
3814 let i_end = i + literal.0.len();
3815 return Some(i..i_end);
3816 }
3817 if subslice.starts_with(literal.1) {
3818 let i_end = i + literal.1.len();
3819 return Some(i..i_end);
3820 }
3821 if subslice.starts_with(literal.2) {
3822 let i_end = i + literal.2.len();
3823 return Some(i..i_end);
3824 }
3825 }
3826 None
3827}