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

Change the standard Input impl to support any slice #1810

Open
wants to merge 3 commits into
base: main
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
2 changes: 1 addition & 1 deletion .github/workflows/cifuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
dry-run: false
language: rust
- name: Upload Crash
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure() && steps.build.outcome == 'success'
with:
name: artifacts
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/benches/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ struct Header<'a> {

#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
fn is_token(c: u8) -> bool {
match c {
fn is_token(c: &u8) -> bool {
match *c {
128..=255 => false,
0..=31 => false,
b'(' => false,
Expand All @@ -48,23 +48,23 @@ fn is_token(c: u8) -> bool {
}
}

fn not_line_ending(c: u8) -> bool {
c != b'\r' && c != b'\n'
fn not_line_ending(c: &u8) -> bool {
*c != b'\r' && *c != b'\n'
}

fn is_space(c: u8) -> bool {
c == b' '
fn is_space(c: &u8) -> bool {
*c == b' '
}

fn is_not_space(c: u8) -> bool {
c != b' '
fn is_not_space(c: &u8) -> bool {
*c != b' '
}
fn is_horizontal_space(c: u8) -> bool {
c == b' ' || c == b'\t'
fn is_horizontal_space(c: &u8) -> bool {
*c == b' ' || *c == b'\t'
}

fn is_version(c: u8) -> bool {
c >= b'0' && c <= b'9' || c == b'.'
fn is_version(c: &u8) -> bool {
*c >= b'0' && *c <= b'9' || *c == b'.'
}

fn line_ending<'a>()-> impl Parser<&'a[u8], Output=&'a[u8], Error=Error<&'a[u8]>> {
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/benches/http_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ struct Header<'a> {

#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
fn is_token(c: u8) -> bool {
match c {
fn is_token(c: &u8) -> bool {
match *c {
128..=255 => false,
0..=31 => false,
b'(' => false,
Expand All @@ -48,23 +48,23 @@ fn is_token(c: u8) -> bool {
}
}

fn not_line_ending(c: u8) -> bool {
c != b'\r' && c != b'\n'
fn not_line_ending(c: &u8) -> bool {
*c != b'\r' && *c != b'\n'
}

fn is_space(c: u8) -> bool {
c == b' '
fn is_space(c: &u8) -> bool {
*c == b' '
}

fn is_not_space(c: u8) -> bool {
c != b' '
fn is_not_space(c: &u8) -> bool {
*c != b' '
}
fn is_horizontal_space(c: u8) -> bool {
c == b' ' || c == b'\t'
fn is_horizontal_space(c: &u8) -> bool {
*c == b' ' || *c == b'\t'
}

fn is_version(c: u8) -> bool {
c >= b'0' && c <= b'9' || c == b'.'
fn is_version(c: &u8) -> bool {
*c >= b'0' && *c <= b'9' || *c == b'.'
}

fn request_line(input: &[u8]) -> IResult<&[u8], Request<'_>> {
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/benches/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::str;

fn category(i: &[u8]) -> IResult<&[u8], &str> {
map_res(
delimited(char('['), take_while(|c| c != b']'), char(']')),
delimited(char('['), take_while(|c: &u8| *c != b']'), char(']')),
str::from_utf8,
)
.parse_complete(i)
Expand All @@ -28,8 +28,8 @@ fn key_value(i: &[u8]) -> IResult<&[u8], (&str, &str)> {
let (i, key) = map_res(alphanumeric, str::from_utf8).parse_complete(i)?;
let (i, _) = tuple((opt(space), char('='), opt(space))).parse_complete(i)?;
let (i, val) =
map_res(take_while(|c| c != b'\n' && c != b';'), str::from_utf8).parse_complete(i)?;
let (i, _) = opt(pair(char(';'), take_while(|c| c != b'\n'))).parse_complete(i)?;
map_res(take_while(|c: &u8| *c != b'\n' && *c != b';'), str::from_utf8).parse_complete(i)?;
let (i, _) = opt(pair(char(';'), take_while(|c: &u8| *c != b'\n'))).parse_complete(i)?;
Ok((i, (key, val)))
}

Expand Down
14 changes: 7 additions & 7 deletions src/bits/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use crate::traits::{Input, ToUsize};
/// // Tries to consume 12 bits but only 8 are available
/// assert_eq!(parser(([0b00010010].as_ref(), 0), 12), Err(nom::Err::Error(Error{input: ([0b00010010].as_ref(), 0), code: ErrorKind::Eof })));
/// ```
pub fn take<I, O, C, E: ParseError<(I, usize)>>(
pub fn take<'a, I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Input<Item = u8>,
I: Input<Item = &'a u8>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand All @@ -59,7 +59,7 @@ where
break;
}
let val: O = if offset == 0 {
byte.into()
(*byte).into()
} else {
((byte << offset) >> offset).into()
};
Expand All @@ -80,12 +80,12 @@ where
}

/// Generates a parser taking `count` bits and comparing them to `pattern`
pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
pub fn tag<'a, I, O, C, E: ParseError<(I, usize)>>(
pattern: O,
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Input<Item = u8> + Clone,
I: Input<Item = &'a u8> + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand Down Expand Up @@ -118,9 +118,9 @@ where
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
/// ```
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
pub fn bool<'a, I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Input<Item = u8>,
I: Input<Item = &'a u8>,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
14 changes: 7 additions & 7 deletions src/bits/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::lib::std::ops::{AddAssign, Div, Shl, Shr};
use crate::traits::{Input, ToUsize};

/// Generates a parser taking `count` bits
pub fn take<I, O, C, E: ParseError<(I, usize)>>(
pub fn take<'a, I, O, C, E: ParseError<(I, usize)>>(
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Input<Item = u8>,
I: Input<Item = &'a u8>,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O>,
{
Expand All @@ -34,7 +34,7 @@ where
break;
}
let val: O = if offset == 0 {
byte.into()
(*byte).into()
} else {
((byte << offset) >> offset).into()
};
Expand All @@ -56,12 +56,12 @@ where
}

/// Generates a parser taking `count` bits and comparing them to `pattern`
pub fn tag<I, O, C, E: ParseError<(I, usize)>>(
pub fn tag<'a, I, O, C, E: ParseError<(I, usize)>>(
pattern: O,
count: C,
) -> impl Fn((I, usize)) -> IResult<(I, usize), O, E>
where
I: Input<Item = u8> + Clone,
I: Input<Item = &'a u8> + Clone,
C: ToUsize,
O: From<u8> + AddAssign + Shl<usize, Output = O> + Shr<usize, Output = O> + PartialEq,
{
Expand Down Expand Up @@ -94,9 +94,9 @@ where
/// assert_eq!(parse(([0b10000000].as_ref(), 0)), Ok((([0b10000000].as_ref(), 1), true)));
/// assert_eq!(parse(([0b10000000].as_ref(), 1)), Ok((([0b10000000].as_ref(), 2), false)));
/// ```
pub fn bool<I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
pub fn bool<'a, I, E: ParseError<(I, usize)>>(input: (I, usize)) -> IResult<(I, usize), bool, E>
where
I: Input<Item = u8>,
I: Input<Item = &'a u8>,
{
let (res, bit): (_, u32) = take(1usize)(input)?;
Ok((res, bit != 0))
Expand Down
6 changes: 3 additions & 3 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use core::marker::PhantomData;
use crate::error::ParseError;
use crate::internal::{IResult, Parser};
use crate::traits::{Compare, FindSubstring, FindToken, ToUsize};
use crate::Complete;
use crate::Emit;
use crate::Input;
use crate::OutputM;
use crate::{Complete, IntoInput};

/// Recognizes a pattern
///
Expand Down Expand Up @@ -358,8 +358,8 @@ where
/// ```
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl FnMut(I) -> IResult<I, I, Error>
where
I: Input + FindSubstring<T>,
T: Input + Clone,
I: Input + FindSubstring<T::Input>,
T: IntoInput,
{
let mut parser = super::take_until(tag);

Expand Down
21 changes: 11 additions & 10 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::error::ParseError;
use crate::internal::{Err, Needed, Parser};
use crate::lib::std::result::Result::*;
use crate::traits::{Compare, CompareResult};
use crate::AsChar;
use crate::Check;
use crate::ExtendInto;
use crate::FindSubstring;
Expand All @@ -23,6 +22,7 @@ use crate::Mode;
use crate::OutputM;
use crate::OutputMode;
use crate::ToUsize;
use crate::{AsChar, IntoInput};

/// Recognizes a pattern.
///
Expand All @@ -44,11 +44,11 @@ use crate::ToUsize;
/// ```
pub fn tag<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
where
I: Input + Compare<T>,
T: Input + Clone,
I: Input + Compare<T::Input>,
T: IntoInput,
{
Tag {
tag,
tag: tag.into_input(),
e: PhantomData,
}
}
Expand Down Expand Up @@ -113,11 +113,11 @@ where
/// ```
pub fn tag_no_case<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
where
I: Input + Compare<T>,
T: Input + Clone,
I: Input + Compare<T::Input>,
T: IntoInput,
{
TagNoCase {
tag,
tag: tag.into_input(),
e: PhantomData,
}
}
Expand Down Expand Up @@ -595,11 +595,12 @@ where
/// ```
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl Parser<I, Output = I, Error = Error>
where
I: Input + FindSubstring<T>,
T: Clone,
I: Input + FindSubstring<T::Input>,
T: IntoInput,
T::Input: Clone,
{
TakeUntil {
tag,
tag: tag.into_input(),
e: PhantomData,
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use core::marker::PhantomData;
use crate::error::ParseError;
use crate::internal::{IResult, Parser};
use crate::traits::{Compare, FindSubstring, FindToken, ToUsize};
use crate::Emit;
use crate::Input;
use crate::OutputM;
use crate::Streaming;
use crate::{Emit, IntoInput};

/// Recognizes a pattern.
///
Expand All @@ -30,12 +30,12 @@ use crate::Streaming;
/// ```
pub fn tag<T, I, Error: ParseError<I>>(tag: T) -> impl Fn(I) -> IResult<I, I, Error>
where
I: Input + Compare<T>,
T: Input + Clone,
I: Input + Compare<T::Input>,
T: IntoInput + Clone,
{
move |i: I| {
let mut parser = super::Tag {
tag: tag.clone(),
tag: tag.clone().into_input(),
e: PhantomData,
};

Expand Down Expand Up @@ -64,12 +64,12 @@ where
/// ```
pub fn tag_no_case<T, I, Error: ParseError<I>>(tag: T) -> impl Fn(I) -> IResult<I, I, Error>
where
I: Input + Compare<T>,
T: Input + Clone,
I: Input + Compare<T::Input>,
T: IntoInput + Clone,
{
move |i: I| {
let mut parser = super::TagNoCase {
tag: tag.clone(),
tag: tag.clone().into_input(),
e: PhantomData,
};

Expand Down Expand Up @@ -369,8 +369,9 @@ where
/// ```
pub fn take_until<T, I, Error: ParseError<I>>(tag: T) -> impl FnMut(I) -> IResult<I, I, Error>
where
I: Input + FindSubstring<T>,
T: Clone,
I: Input + FindSubstring<T::Input>,
T: IntoInput,
T::Input: Clone,
{
let mut parser = super::take_until(tag);

Expand Down
Loading
Loading