Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: remove crash! macro #5589

Merged
merged 13 commits into from
Dec 15, 2023
41 changes: 19 additions & 22 deletions src/uu/fmt/src/linebreak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use std::io::{BufWriter, Stdout, Write};
use std::{cmp, i64, mem};

use uucore::crash;

use crate::parasplit::{ParaWords, Paragraph, WordInfo};
use crate::FmtOptions;

Expand Down Expand Up @@ -363,28 +361,27 @@ fn find_kp_breakpoints<'a, T: Iterator<Item = &'a WordInfo<'a>>>(
}

fn build_best_path<'a>(paths: &[LineBreak<'a>], active: &[usize]) -> Vec<(&'a WordInfo<'a>, bool)> {
let mut breakwords = vec![];
// of the active paths, we select the one with the fewest demerits
let mut best_idx = match active.iter().min_by_key(|&&a| paths[a].demerits) {
None => crash!(
1,
"Failed to find a k-p linebreak solution. This should never happen."
),
Some(&s) => s,
};

// now, chase the pointers back through the break list, recording
// the words at which we should break
loop {
let next_best = &paths[best_idx];
match next_best.linebreak {
None => return breakwords,
Some(prev) => {
breakwords.push((prev, next_best.break_before));
best_idx = next_best.prev;
active
.iter()
.min_by_key(|&&a| paths[a].demerits)
.iter()
.flat_map(|&&(mut best_idx)| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.iter()
.flat_map(|&&(mut best_idx)| {
.map(|&(mut best_idx)| {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

let mut breakwords = vec![];
// now, chase the pointers back through the break list, recording
// the words at which we should break
loop {
let next_best = &paths[best_idx];
match next_best.linebreak {
None => return breakwords,
Some(prev) => {
breakwords.push((prev, next_best.break_before));
best_idx = next_best.prev;
}
}
}
}
}
})
.collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.collect()
.unwrap_or_default()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

}

// "infinite" badness is more like (1+BAD_INFTY)^2 because of how demerits are computed
Expand Down