Skip to content

Commit

Permalink
fix(multi): Rename many_till to many_till0
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 7, 2023
1 parent 5e38951 commit aa49fc0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/combinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//! | [`count`][crate::multi::count] | `count(take(2), 3)` | `"abcdefgh"` | `Ok(("gh", vec!["ab", "cd", "ef"]))` |Applies the child parser a specified number of times|
//! | [`many0`][crate::multi::many0] | `many0(tag("ab"))` | `"abababc"` | `Ok(("c", vec!["ab", "ab", "ab"]))` |Applies the parser 0 or more times and returns the list of results in a Vec. `many1` does the same operation but must return at least one element|
//! | [`many_m_n`][crate::multi::many_m_n] | `many_m_n(1, 3, tag("ab"))` | `"ababc"` | `Ok(("c", vec!["ab", "ab"]))` |Applies the parser between m and n times (n included) and returns the list of results in a Vec|
//! | [`many_till`][crate::multi::many_till] | `many_till(tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `Ok(("g", (vec!["ab", "ab"], "ef")))` |Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second|
//! | [`many_till0`][crate::multi::many_till0] | `many_till0(tag( "ab" ), tag( "ef" ))` | `"ababefg"` | `Ok(("g", (vec!["ab", "ab"], "ef")))` |Applies the first parser until the second applies. Returns a tuple containing the list of results from the first in a Vec and the result of the second|
//! | [`separated_list0`][crate::multi::separated_list0] | `separated_list0(tag(","), tag("ab"))` | `"ab,ab,ab."` | `Ok((".", vec!["ab", "ab", "ab"]))` |`separated_list1` works like `separated_list0` but must returns at least one element|
//! | [`fold_many0`][crate::multi::fold_many0] | `fold_many0(be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([], 6))` |Applies the parser 0 or more times and folds the list of return values. The `fold_many1` version must apply the child parser at least one time|
//! | [`fold_many_m_n`][crate::multi::fold_many_m_n] | `fold_many_m_n(1, 2, be_u8, \|\| 0, \|acc, item\| acc + item)` | `[1, 2, 3]` | `Ok(([3], 3))` |Applies the parser between m and n times (n included) and folds the list of return value|
Expand Down
19 changes: 16 additions & 3 deletions src/multi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ where
}
}

/// **WARNING:** Deprecated, replaced with [`many_till0`]
#[deprecated(since = "0.3.0", note = "Replaced with `many_till0`")]
pub fn many_till<I, O, C, P, E, F, G>(f: F, g: G) -> impl FnMut(I) -> IResult<I, (C, P), E>
where
I: Input,
C: Accumulate<O>,
F: Parser<I, O, E>,
G: Parser<I, P, E>,
E: ParseError<I>,
{
many_till0(f, g)
}

/// Applies the parser `f` until the parser `g` produces a result.
///
/// Returns a tuple of the results of `f` in a `Vec` and the result of `g`.
Expand All @@ -134,11 +147,11 @@ where
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
#[cfg_attr(feature = "std", doc = "```")]
/// # use winnow::{error::ErrMode, error::{Error, ErrorKind}, error::Needed, IResult};
/// use winnow::multi::many_till;
/// use winnow::multi::many_till0;
/// use winnow::bytes::tag;
///
/// fn parser(s: &str) -> IResult<&str, (Vec<&str>, &str)> {
/// many_till(tag("abc"), tag("end"))(s)
/// many_till0(tag("abc"), tag("end"))(s)
/// };
///
/// assert_eq!(parser("abcabcend"), Ok(("", (vec!["abc", "abc"], "end"))));
Expand All @@ -147,7 +160,7 @@ where
/// assert_eq!(parser(""), Err(ErrMode::Backtrack(Error::new("", ErrorKind::Tag))));
/// assert_eq!(parser("abcendefg"), Ok(("efg", (vec!["abc"], "end"))));
/// ```
pub fn many_till<I, O, C, P, E, F, G>(mut f: F, mut g: G) -> impl FnMut(I) -> IResult<I, (C, P), E>
pub fn many_till0<I, O, C, P, E, F, G>(mut f: F, mut g: G) -> impl FnMut(I) -> IResult<I, (C, P), E>
where
I: Input,
C: Accumulate<O>,
Expand Down
4 changes: 2 additions & 2 deletions src/multi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
use crate::{
lib::std::vec::Vec,
multi::{
count, fold_many0, fold_many1, fold_many_m_n, length_count, many_m_n, many_till,
count, fold_many0, fold_many1, fold_many_m_n, length_count, many_m_n, many_till0,
separated_list0, separated_list1,
},
};
Expand Down Expand Up @@ -206,7 +206,7 @@ fn many1_test() {
fn many_till_test() {
#[allow(clippy::type_complexity)]
fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> {
many_till(tag("abcd"), tag("efgh"))(i)
many_till0(tag("abcd"), tag("efgh"))(i)
}

let a = b"abcdabcdefghabcd";
Expand Down
8 changes: 4 additions & 4 deletions tests/testsuite/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ fn overflow_incomplete_many1() {

#[test]
#[cfg(feature = "alloc")]
fn overflow_incomplete_many_till() {
use winnow::{bytes::tag, multi::many_till};
fn overflow_incomplete_many_till0() {
use winnow::{bytes::tag, multi::many_till0};

#[allow(clippy::type_complexity)]
fn multi(i: Streaming<&[u8]>) -> IResult<Streaming<&[u8]>, (Vec<&[u8]>, &[u8])> {
many_till(length_data(be_u64), tag("abc"))(i)
many_till0(length_data(be_u64), tag("abc"))(i)
}

// Trigger an overflow in many_till
// Trigger an overflow in many_till0
assert_eq!(
multi(Streaming(
&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]
Expand Down

0 comments on commit aa49fc0

Please sign in to comment.