diff --git a/src/multi/mod.rs b/src/multi/mod.rs index 45168f5b..174bae00 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -420,13 +420,6 @@ where Err(Err::Failure(e)) => return Err(Err::Failure(e)), Err(Err::Incomplete(e)) => return Err(Err::Incomplete(e)), Ok((i1, _)) => { - // infinite loop check: the parser must always consume - if i1.input_len() == len { - return Err(Err::Error(OM::Error::bind(|| { - >::Error::from_error_kind(i, ErrorKind::SeparatedList) - }))); - } - match self .parser .process::>(i1.clone()) @@ -435,10 +428,18 @@ where Err(Err::Failure(e)) => return Err(Err::Failure(e)), Err(Err::Incomplete(e)) => return Err(Err::Incomplete(e)), Ok((i2, o)) => { + // infinite loop check: the parser must always consume + if i2.input_len() == len { + return Err(Err::Error(OM::Error::bind(|| { + >::Error::from_error_kind(i, ErrorKind::SeparatedList) + }))); + } + res = OM::Output::combine(res, o, |mut res, o| { res.push(o); res }); + i = i2; } } @@ -532,13 +533,6 @@ where Err(Err::Failure(e)) => return Err(Err::Failure(e)), Err(Err::Incomplete(e)) => return Err(Err::Incomplete(e)), Ok((i1, _)) => { - // infinite loop check: the parser must always consume - if i1.input_len() == len { - return Err(Err::Error(OM::Error::bind(|| { - >::Error::from_error_kind(i, ErrorKind::SeparatedList) - }))); - } - match self .parser .process::>(i1.clone()) @@ -547,6 +541,13 @@ where Err(Err::Failure(e)) => return Err(Err::Failure(e)), Err(Err::Incomplete(e)) => return Err(Err::Incomplete(e)), Ok((i2, o)) => { + // infinite loop check: the parser must always consume + if i2.input_len() == len { + return Err(Err::Error(OM::Error::bind(|| { + >::Error::from_error_kind(i, ErrorKind::SeparatedList) + }))); + } + res = OM::Output::combine(res, o, |mut res, o| { res.push(o); res diff --git a/src/multi/tests.rs b/src/multi/tests.rs index add52d66..080240cd 100644 --- a/src/multi/tests.rs +++ b/src/multi/tests.rs @@ -21,6 +21,8 @@ use crate::{ #[test] #[cfg(feature = "alloc")] fn separated_list0_test() { + use core::num::NonZeroUsize; + fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { separated_list0(tag(","), tag("abcd")).parse(i) } @@ -33,6 +35,9 @@ fn separated_list0_test() { fn multi_longsep(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { separated_list0(tag(".."), tag("abcd")).parse(i) } + fn empty_both(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { + separated_list0(tag(""), tag("")).parse(i) + } let a = &b"abcdef"[..]; let b = &b"abcd,abcdef"[..]; @@ -51,13 +56,14 @@ fn separated_list0_test() { assert_eq!(multi(c), Ok((&b"azerty"[..], Vec::new()))); let res3 = vec![&b""[..], &b""[..], &b""[..]]; assert_eq!(multi_empty(d), Ok((&b"abc"[..], res3))); - let i_err_pos = &i[3..]; assert_eq!( empty_sep(i), - Err(Err::Error(error_position!( - i_err_pos, - ErrorKind::SeparatedList - ))) + Err(Err::Incomplete(Needed::Size(NonZeroUsize::new(3).unwrap()))) + ); + + assert_eq!( + empty_both(i), + Err(Err::Error(error_position!(i, ErrorKind::SeparatedList))) ); let res4 = vec![&b"abcd"[..], &b"abcd"[..]]; assert_eq!(multi(e), Ok((&b",ef"[..], res4)));