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

wrap_optimal_fit() - Checked arithmetic #392

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,11 @@ mod tests {
);
}

#[test]
fn fill_large_width() {
fill("!@", 18446743798827450368);
}

#[test]
#[cfg(not(feature = "smawk"))]
#[cfg(not(feature = "unicode-linebreak"))]
Expand Down
6 changes: 3 additions & 3 deletions src/wrap_algorithms/optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ pub fn wrap_optimal_fit<'a, 'b, T: Fragment>(
// breaking before fragments[i].
//
// First, every extra line cost NLINE_PENALTY.
let mut cost = minima[i].1 + penalties.nline_penalty;
let mut cost = (minima[i].1 as i32).saturating_add(penalties.nline_penalty);

// Next, we add a penalty depending on the line length.
if line_width > target_width {
Expand All @@ -283,12 +283,12 @@ pub fn wrap_optimal_fit<'a, 'b, T: Fragment>(
// Other lines (except for the last line) get a milder
// penalty which depend on the size of the gap.
let gap = (target_width - line_width) as i32;
cost += gap * gap;
cost = cost.saturating_add(gap.saturating_mul(gap));
} else if i + 1 == j && line_width < target_width / penalties.short_last_line_fraction {
// The last line can have any size gap, but we do add a
// penalty if the line is very short (typically because it
// contains just a single word).
cost += penalties.short_last_line_penalty;
cost = cost.saturating_add(penalties.short_last_line_penalty);
}

// Finally, we discourage hyphens.
Expand Down