Skip to content

Commit

Permalink
Switch to InclusiveRange
Browse files Browse the repository at this point in the history
  • Loading branch information
DerDrodt committed Mar 21, 2024
1 parent 778a5da commit 2cf650d
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,43 +642,41 @@ impl PageRangeFormat {
/// Use a page range format to format a range of pages.
pub fn format(
self,
range: std::ops::Range<i32>,
range: std::ops::RangeInclusive<i32>,
buf: &mut impl fmt::Write,
separator: Option<&str>,
) -> Result<(), fmt::Error> {
let separator = separator.unwrap_or("–");

write!(buf, "{}{}", range.start, separator)?;
let end = if range.end >= range.start {
range.end
let start = *range.start();
write!(buf, "{}{}", start, separator)?;
let end = if *range.end() >= start {
*range.end()
} else {
expand(range.start, range.end)
expand(start, *range.end())
};

match self {
_ if range.start < 0 || range.end < 0 => write!(buf, "{}", end),
_ if start < 0 || *range.end() < 0 => write!(buf, "{}", end),
PageRangeFormat::Expanded => write!(buf, "{}", end),

PageRangeFormat::Chicago15 | PageRangeFormat::Chicago16
if range.start < 100 || range.start % 100 == 0 =>
if start < 100 || start % 100 == 0 =>
{
write!(buf, "{}", end)
}
PageRangeFormat::Minimal => {
write!(buf, "{}", changed_part(range.start, end, 0))
write!(buf, "{}", changed_part(start, end, 0))
}
PageRangeFormat::MinimalTwo if end < 10 => {
write!(buf, "{}", changed_part(range.start, end, 1))
write!(buf, "{}", changed_part(start, end, 1))
}
PageRangeFormat::Chicago15
if range.start > 100 && (1..10).contains(&(range.start % 100)) =>
if start > 100 && (1..10).contains(&(start % 100)) =>
{
write!(buf, "{}", changed_part(range.start, end, 0))
write!(buf, "{}", changed_part(start, end, 0))
}
PageRangeFormat::Chicago15
if closest_smaller_power_of_10(range.start) == 1000 =>
{
let changed = changed_part(range.start, end, 1);
PageRangeFormat::Chicago15 if closest_smaller_power_of_10(start) == 1000 => {
let changed = changed_part(start, end, 1);
if closest_smaller_power_of_10(changed) == 100 {
write!(buf, "{end}")
} else {
Expand All @@ -688,7 +686,7 @@ impl PageRangeFormat {
PageRangeFormat::Chicago15
| PageRangeFormat::Chicago16
| PageRangeFormat::MinimalTwo => {
write!(buf, "{}", changed_part(range.start, end, 1))
write!(buf, "{}", changed_part(start, end, 1))
}
}
}
Expand Down Expand Up @@ -3671,7 +3669,7 @@ mod test {
fn page_range() {
fn run(format: PageRangeFormat, start: i32, end: i32) -> String {
let mut buf = String::new();
format.format(start..end, &mut buf, None).unwrap();
format.format(start..=end, &mut buf, None).unwrap();
buf
}

Expand Down

0 comments on commit 2cf650d

Please sign in to comment.