Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset parser #342

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/parser/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,48 @@ where [
.then_partial(|&mut len| take(len))
}
}
#[derive(Copy, Clone)]
pub struct Offset<P>(usize, P);

impl<Input, O, P> Parser<Input> for Offset<P>
where
Input: RangeStream,
P: Parser<Input, Output = O>,
{
type Output = O;
type PartialState = ();

#[inline]
fn parse_lazy(&mut self, input: &mut Input) -> ParseResult<O, <Input as StreamOnce>::Error> {
let before = input.checkpoint();
ctry!(uncons_range(input, self.0));
let result = self.1.parse_lazy(input);
ctry!(input.reset(before).committed());
let (o, _input) = ctry!(result);
PeekOk(o)
}

forward_parser!(Input, add_error add_committed_expected_error parser_count, 1);
}

/// Parser that skip `count` and then parse data resetting back to original location
///
/// ```
/// # extern crate combine;
/// # use combine::parser::range::{take, offset};
/// # use combine::*;
/// # fn main() {
/// let mut parser = offset(3, take(3));
/// assert_eq!(parser.parse("1234567890"), Ok(("456", "1234567890")));
/// # }
/// ```
pub fn offset<Input, P>(count: usize, p: P) -> Offset<P>
where
P: Parser<Input>,
Input: RangeStream,
{
Offset(count, p)
}

#[cfg(test)]
mod tests {
Expand Down
6 changes: 1 addition & 5 deletions src/stream/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ use std::io::{self, BufRead, Read};
))]
use std::pin::Pin;

#[cfg(any(
feature = "futures-03",
feature = "tokio-02",
feature = "tokio-03"
))]
#[cfg(any(feature = "futures-03", feature = "tokio-02", feature = "tokio-03"))]
use std::mem::MaybeUninit;

#[cfg(feature = "futures-core-03")]
Expand Down
2 changes: 1 addition & 1 deletion src/stream/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<S, P, C> Decoder<S, P, C> {

impl<S, P, C> Decoder<S, P, C>
where
C: ,
C:,
{
#[doc(hidden)]
pub fn __before_parse<R>(&mut self, mut reader: R) -> io::Result<()>
Expand Down