winnow/stream/
mod.rs

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