2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) trait RangeExt<T> {
|
2019-04-11 12:30:29 -07:00
|
|
|
fn range_contains(&self, i: &T) -> bool;
|
2021-10-14 17:00:58 -07:00
|
|
|
|
|
|
|
fn display(&self) -> DisplayRange<&Self> {
|
|
|
|
DisplayRange(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct DisplayRange<T>(T);
|
|
|
|
|
|
|
|
impl Display for DisplayRange<&Range<usize>> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
if self.0.start == self.0.end {
|
2024-01-12 18:44:13 -08:00
|
|
|
write!(f, "0")?;
|
|
|
|
} else if self.0.start == self.0.end - 1 {
|
2021-10-14 17:00:58 -07:00
|
|
|
write!(f, "{}", self.0.start)?;
|
|
|
|
} else if self.0.end == usize::MAX {
|
|
|
|
write!(f, "{} or more", self.0.start)?;
|
|
|
|
} else {
|
2024-01-12 18:44:13 -08:00
|
|
|
write!(f, "{} to {}", self.0.start, self.0.end - 1)?;
|
2021-10-14 17:00:58 -07:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2018-12-08 14:29:41 -08:00
|
|
|
impl<T> RangeExt<T> for Range<T>
|
|
|
|
where
|
2019-04-11 15:23:14 -07:00
|
|
|
T: PartialOrd,
|
2018-12-08 14:29:41 -08:00
|
|
|
{
|
2019-04-11 12:30:29 -07:00
|
|
|
fn range_contains(&self, i: &T) -> bool {
|
|
|
|
i >= &self.start && i < &self.end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RangeExt<T> for RangeInclusive<T>
|
|
|
|
where
|
2019-04-11 15:23:14 -07:00
|
|
|
T: PartialOrd,
|
2019-04-11 12:30:29 -07:00
|
|
|
{
|
|
|
|
fn range_contains(&self, i: &T) -> bool {
|
|
|
|
i >= self.start() && i <= self.end()
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-11-07 10:55:15 -08:00
|
|
|
mod tests {
|
2017-11-16 23:30:08 -08:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2019-04-11 15:23:14 -07:00
|
|
|
fn exclusive() {
|
|
|
|
assert!(!(0..0).range_contains(&0));
|
|
|
|
assert!(!(1..10).range_contains(&0));
|
|
|
|
assert!(!(1..10).range_contains(&10));
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(1..10).range_contains(&0));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!((0..1).range_contains(&0));
|
|
|
|
assert!((10..20).range_contains(&15));
|
2019-04-11 12:30:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-11 15:23:14 -07:00
|
|
|
fn inclusive() {
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(0..=10).range_contains(&11));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!(!(1..=10).range_contains(&0));
|
2019-04-11 12:30:29 -07:00
|
|
|
assert!(!(5..=10).range_contains(&4));
|
2019-04-11 15:23:14 -07:00
|
|
|
assert!((0..=0).range_contains(&0));
|
|
|
|
assert!((0..=1).range_contains(&0));
|
|
|
|
assert!((0..=10).range_contains(&0));
|
|
|
|
assert!((0..=10).range_contains(&10));
|
|
|
|
assert!((0..=10).range_contains(&7));
|
|
|
|
assert!((1..=10).range_contains(&10));
|
|
|
|
assert!((10..=20).range_contains(&15));
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2021-10-14 17:00:58 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display() {
|
2024-01-12 18:44:13 -08:00
|
|
|
assert!(!(1..1).contains(&1));
|
|
|
|
assert!((1..1).is_empty());
|
|
|
|
assert!((5..5).is_empty());
|
|
|
|
assert_eq!((1..1).display().to_string(), "0");
|
|
|
|
assert_eq!((1..2).display().to_string(), "1");
|
|
|
|
assert_eq!((5..6).display().to_string(), "5");
|
|
|
|
assert_eq!((5..10).display().to_string(), "5 to 9");
|
2021-10-14 17:00:58 -07:00
|
|
|
assert_eq!((1..usize::MAX).display().to_string(), "1 or more");
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|