Skip to content

Commit

Permalink
Change the standard Input impl to support any slice
Browse files Browse the repository at this point in the history
The main change is that the Item is a reference instead of a copy
  • Loading branch information
marcdejonge committed Jan 30, 2025
1 parent d3d2540 commit 6369ccb
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 338 deletions.
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
33 changes: 17 additions & 16 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 @@ -278,10 +278,10 @@ where
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed, IResult};
/// use nom::bytes::complete::take_while;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while(is_alphabetic)(s)
/// take_while(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -314,10 +314,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::streaming::take_while1;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while1(is_alphabetic)(s)
/// take_while1(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -349,10 +349,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::streaming::take_while_m_n;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while_m_n(3, 6, is_alphabetic)(s)
/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
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

0 comments on commit 6369ccb

Please sign in to comment.