From 8b2a9f18089f01e8d6b329581ebce8c9d2e32129 Mon Sep 17 00:00:00 2001 From: Matthew Toohey Date: Mon, 28 Nov 2022 21:57:07 -0500 Subject: [PATCH] Fix fuzz tests and related bug --- fuzz/fuzz_targets/wrap_first_fit.rs | 4 ++-- fuzz/fuzz_targets/wrap_optimal_fit.rs | 4 ++-- fuzz/fuzz_targets/wrap_optimal_fit_usize.rs | 4 ++-- src/core.rs | 2 +- src/wrap.rs | 4 +++- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/fuzz/fuzz_targets/wrap_first_fit.rs b/fuzz/fuzz_targets/wrap_first_fit.rs index bc7136ee..a5681641 100644 --- a/fuzz/fuzz_targets/wrap_first_fit.rs +++ b/fuzz/fuzz_targets/wrap_first_fit.rs @@ -14,12 +14,12 @@ struct Word { #[rustfmt::skip] impl core::Fragment for Word { fn width(&self) -> f64 { self.width } - fn whitespace_width(&self) -> f64 { self.whitespace_width } + fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width } fn penalty_width(&self) -> f64 { self.penalty_width } } fuzz_target!(|input: (f64, Vec)| { let width = input.0; let words = input.1; - let _ = wrap_first_fit(&words, &[width]); + let _ = wrap_first_fit(&words, &[width], 0); }); diff --git a/fuzz/fuzz_targets/wrap_optimal_fit.rs b/fuzz/fuzz_targets/wrap_optimal_fit.rs index 7f09d39a..e17ac137 100644 --- a/fuzz/fuzz_targets/wrap_optimal_fit.rs +++ b/fuzz/fuzz_targets/wrap_optimal_fit.rs @@ -35,7 +35,7 @@ struct Word { #[rustfmt::skip] impl core::Fragment for Word { fn width(&self) -> f64 { self.width } - fn whitespace_width(&self) -> f64 { self.whitespace_width } + fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width } fn penalty_width(&self) -> f64 { self.penalty_width } } @@ -57,5 +57,5 @@ fuzz_target!(|input: (usize, Vec, Penalties)| { } } - let _ = wrap_optimal_fit(&words, &[width as f64], &penalties); + let _ = wrap_optimal_fit(&words, &[width as f64], 0, &penalties); }); diff --git a/fuzz/fuzz_targets/wrap_optimal_fit_usize.rs b/fuzz/fuzz_targets/wrap_optimal_fit_usize.rs index 162cde19..196f88fa 100644 --- a/fuzz/fuzz_targets/wrap_optimal_fit_usize.rs +++ b/fuzz/fuzz_targets/wrap_optimal_fit_usize.rs @@ -35,7 +35,7 @@ struct Word { #[rustfmt::skip] impl core::Fragment for Word { fn width(&self) -> f64 { self.width as f64 } - fn whitespace_width(&self) -> f64 { self.whitespace_width as f64 } + fn whitespace_width(&self, _: u8) -> f64 { self.whitespace_width as f64 } fn penalty_width(&self) -> f64 { self.penalty_width as f64 } } @@ -45,5 +45,5 @@ fuzz_target!(|input: (usize, Vec, Penalties)| { let width = input.0; let words = input.1; let penalties = input.2.into(); - let _ = wrap_optimal_fit(&words, &[width as f64], &penalties); + let _ = wrap_optimal_fit(&words, &[width as f64], 0, &penalties); }); diff --git a/src/core.rs b/src/core.rs index 3ed166f6..6075a361 100644 --- a/src/core.rs +++ b/src/core.rs @@ -243,7 +243,7 @@ impl<'a> Word<'a> { /// All trailing whitespace is automatically taken to be the whitespace part /// of the word. pub fn from(word: &str) -> Word<'_> { - let trimmed = word.trim_end(); + let trimmed = word.trim_end_matches(&[' ', '\t']); Word { word: trimmed, // trimmed shouldn't contain whitespace, so we don't need to pass diff --git a/src/wrap.rs b/src/wrap.rs index 9ca08bc9..4e75f224 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -203,7 +203,7 @@ pub(crate) fn wrap_single_line<'a>( options.subsequent_indent }; if line.len() < options.width && options.tab_width <= 1 && indent.is_empty() { - lines.push(Cow::from(line.trim_end_matches(' '))); + lines.push(Cow::from(line.trim_end_matches(&[' ', '\t']))); } else { wrap_single_line_slow_path(line, options, lines) } @@ -217,6 +217,8 @@ pub(crate) fn wrap_single_line_slow_path<'a>( options: &Options<'_>, lines: &mut Vec>, ) { + let line = line.trim_end_matches(&[' ', '\t']); + let initial_width = options .width .saturating_sub(display_width(options.initial_indent, options.tab_width));