From f555d1952e114baf9e1e7538ad08429b88d731db Mon Sep 17 00:00:00 2001 From: "C. K. Young" Date: Mon, 30 Jan 2023 21:34:08 -0500 Subject: [PATCH] For `many` and `fold`, consider `..0` an invalid range, just as `0..0` is. --- src/multi/tests.rs | 34 ++++++++++++++++++++++++++++++++++ src/traits.rs | 38 +++++++++++--------------------------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/multi/tests.rs b/src/multi/tests.rs index 3f0c92525..0aa192c2d 100644 --- a/src/multi/tests.rs +++ b/src/multi/tests.rs @@ -576,6 +576,7 @@ fn many_test() { ); + #[allow(clippy::reversed_empty_ranges)] fn many_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { many(2..=1, tag("a"))(i) } @@ -584,6 +585,22 @@ fn many_test() { let b = &b"b"[..]; assert_eq!(many_invalid(a), Err(Err::Failure(error_position!(a, ErrorKind::Many)))); assert_eq!(many_invalid(b), Err(Err::Failure(error_position!(b, ErrorKind::Many)))); + + + fn many_invalid_range_to(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { + many(..0, tag("a"))(i) + } + + let a = &b"a"[..]; + let b = &b"b"[..]; + assert_eq!( + many_invalid_range_to(a), + Err(Err::Failure(error_position!(a, ErrorKind::Many))) + ); + assert_eq!( + many_invalid_range_to(b), + Err(Err::Failure(error_position!(b, ErrorKind::Many))) + ); fn many_any(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { @@ -737,6 +754,7 @@ fn fold_test() { ); + #[allow(clippy::reversed_empty_ranges)] fn fold_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { fold(2..=1, tag("a"), Vec::new, fold_into_vec)(i) } @@ -745,6 +763,22 @@ fn fold_test() { let b = &b"b"[..]; assert_eq!(fold_invalid(a), Err(Err::Failure(error_position!(a, ErrorKind::Fold)))); assert_eq!(fold_invalid(b), Err(Err::Failure(error_position!(b, ErrorKind::Fold)))); + + + fn fold_invalid_range_to(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { + fold(..0, tag("a"), Vec::new, fold_into_vec)(i) + } + + let a = &b"a"[..]; + let b = &b"b"[..]; + assert_eq!( + fold_invalid_range_to(a), + Err(Err::Failure(error_position!(a, ErrorKind::Fold))) + ); + assert_eq!( + fold_invalid_range_to(b), + Err(Err::Failure(error_position!(b, ErrorKind::Fold))) + ); fn fold_any(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> { diff --git a/src/traits.rs b/src/traits.rs index 37dff53fb..41a6280e0 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1251,23 +1251,15 @@ impl NomRange for Range { } fn is_inverted(&self) -> bool { - !(self.start < self.end) + self.is_empty() } fn saturating_iter(&self) -> Self::Saturating { - if self.end == 0 { - 1..0 - } else { - 0..self.end - 1 - } + 0..self.end - 1 } fn bounded_iter(&self) -> Self::Bounded { - if self.end == 0 { - 1..0 - } else { - 0..self.end - 1 - } + 0..self.end - 1 } } @@ -1284,7 +1276,7 @@ impl NomRange for RangeInclusive { } fn is_inverted(&self) -> bool { - !RangeInclusive::contains(self, self.start()) + self.is_empty() } fn saturating_iter(&self) -> Self::Saturating { @@ -1298,7 +1290,7 @@ impl NomRange for RangeInclusive { impl NomRange for RangeFrom { type Saturating = SaturatingIterator; - type Bounded = Range; + type Bounded = RangeInclusive; fn bounds(&self) -> (Bound, Bound) { (Bound::Included(self.start), Bound::Unbounded) @@ -1317,7 +1309,7 @@ impl NomRange for RangeFrom { } fn bounded_iter(&self) -> Self::Bounded { - 0..core::usize::MAX + 0..=core::usize::MAX } } @@ -1334,23 +1326,15 @@ impl NomRange for RangeTo { } fn is_inverted(&self) -> bool { - false + self.end == 0 } fn saturating_iter(&self) -> Self::Saturating { - if self.end == 0 { - 1..0 - } else { - 0..self.end - 1 - } + 0..self.end - 1 } fn bounded_iter(&self) -> Self::Bounded { - if self.end == 0 { - 1..0 - } else { - 0..self.end - 1 - } + 0..self.end - 1 } } @@ -1381,7 +1365,7 @@ impl NomRange for RangeToInclusive { impl NomRange for RangeFull { type Saturating = SaturatingIterator; - type Bounded = Range; + type Bounded = RangeInclusive; fn bounds(&self) -> (Bound, Bound) { (Bound::Unbounded, Bound::Unbounded) @@ -1400,7 +1384,7 @@ impl NomRange for RangeFull { } fn bounded_iter(&self) -> Self::Bounded { - 0..core::usize::MAX + 0..=core::usize::MAX } }