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

Use f64 instead of usize for fragment widths #421

Merged
merged 3 commits into from
Jan 9, 2022
Merged
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
14 changes: 10 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,17 @@ jobs:
- name: Build fuzz targets
run: cargo fuzz build

- name: Fuzz test
run: cargo fuzz run fill_first_fit -- -max_total_time=30
- name: Fuzz test wrap_first_fit
run: cargo fuzz run wrap_first_fit -- -max_total_time=30

- name: Minimize fuzz corpus
run: cargo fuzz cmin fill_first_fit
- name: Fuzz test wrap_optimal_fit
run: cargo fuzz run wrap_optimal_fit -- -max_total_time=30

- name: Minimize wrap_first_fit corpus
run: cargo fuzz cmin wrap_first_fit

- name: Minimize wrap_optimal_fit corpus
run: cargo fuzz cmin wrap_optimal_fit

binary-sizes:
name: Compute binary sizes
Expand Down
24 changes: 11 additions & 13 deletions examples/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,20 @@ impl<'a> CanvasWord<'a> {
}
}

const PRECISION: usize = 10;

impl textwrap::core::Fragment for CanvasWord<'_> {
#[inline]
fn width(&self) -> usize {
(self.width * PRECISION as f64) as usize
fn width(&self) -> f64 {
self.width
}

#[inline]
fn whitespace_width(&self) -> usize {
(self.whitespace_width * PRECISION as f64) as usize
fn whitespace_width(&self) -> f64 {
self.whitespace_width
}

#[inline]
fn penalty_width(&self) -> usize {
(self.penalty_width * PRECISION as f64) as usize
fn penalty_width(&self) -> f64 {
self.penalty_width
}
}

Expand Down Expand Up @@ -292,7 +290,7 @@ impl Into<OptimalFit> for WasmOptimalFit {
#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub struct WasmOptions {
pub width: usize,
pub width: f64,
pub break_words: bool,
pub word_separator: WasmWordSeparator,
pub word_splitter: WasmWordSplitter,
Expand All @@ -304,7 +302,7 @@ pub struct WasmOptions {
impl WasmOptions {
#[wasm_bindgen(constructor)]
pub fn new(
width: usize,
width: f64,
break_words: bool,
word_separator: WasmWordSeparator,
word_splitter: WasmWordSplitter,
Expand Down Expand Up @@ -359,19 +357,19 @@ pub fn draw_wrapped_text(
.flat_map(|word| {
let canvas_word = CanvasWord::from(ctx, word);
if options.break_words {
canvas_word.break_apart(ctx, options.width as f64)
canvas_word.break_apart(ctx, options.width)
} else {
vec![canvas_word]
}
})
.collect::<Vec<_>>();

let line_lengths = [options.width * PRECISION];
let line_lengths = [options.width];
let wrapped_words = match options.wrap_algorithm {
WasmWrapAlgorithm::FirstFit => wrap_first_fit(&canvas_words, &line_lengths),
WasmWrapAlgorithm::OptimalFit => {
let penalties = options.optimal_fit.into();
wrap_optimal_fit(&canvas_words, &line_lengths, &penalties)
wrap_optimal_fit(&canvas_words, &line_lengths, &penalties).unwrap()
}
_ => Err("WasmOptions has an invalid wrap_algorithm field")?,
};
Expand Down
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ name = "wrap_optimal_fit"
path = "fuzz_targets/wrap_optimal_fit.rs"
test = false
doc = false

[[bin]]
name = "wrap_optimal_fit_usize"
path = "fuzz_targets/wrap_optimal_fit_usize.rs"
test = false
doc = false
16 changes: 8 additions & 8 deletions fuzz/fuzz_targets/wrap_first_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ use libfuzzer_sys::fuzz_target;
use textwrap::core;
use textwrap::wrap_algorithms::wrap_first_fit;

#[derive(Arbitrary, Debug, Eq, PartialEq)]
#[derive(Arbitrary, Debug, PartialEq)]
struct Word {
width: usize,
whitespace_width: usize,
penalty_width: usize,
width: f64,
whitespace_width: f64,
penalty_width: f64,
}

#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> usize { self.width }
fn whitespace_width(&self) -> usize { self.whitespace_width }
fn penalty_width(&self) -> usize { self.penalty_width }
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
fn penalty_width(&self) -> f64 { self.penalty_width }
}

fuzz_target!(|input: (usize, Vec<Word>)| {
fuzz_target!(|input: (f64, Vec<Word>)| {
let width = input.0;
let words = input.1;
let _ = wrap_first_fit(&words, &[width]);
Expand Down
30 changes: 22 additions & 8 deletions fuzz/fuzz_targets/wrap_optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,37 @@ impl Into<OptimalFit> for Penalties {
}
}

#[derive(Arbitrary, Debug, Eq, PartialEq)]
#[derive(Arbitrary, Debug, PartialEq)]
struct Word {
width: usize,
whitespace_width: usize,
penalty_width: usize,
width: f64,
whitespace_width: f64,
penalty_width: f64,
}

#[rustfmt::skip]
impl core::Fragment for Word {
fn width(&self) -> usize { self.width }
fn whitespace_width(&self) -> usize { self.whitespace_width }
fn penalty_width(&self) -> usize { self.penalty_width }
fn width(&self) -> f64 { self.width }
fn whitespace_width(&self) -> f64 { self.whitespace_width }
fn penalty_width(&self) -> f64 { self.penalty_width }
}

// Check wrapping fragments with mostly arbitrary widths. Infinite
// widths are not supported since they instantly trigger an overflow
// in the cost computation. Similarly for very large values: the 1e100
// bound used here is somewhat conservative, the real bound seems to
// be around 1e170.
fuzz_target!(|input: (usize, Vec<Word>, Penalties)| {
let width = input.0;
let words = input.1;
let penalties = input.2.into();
let _ = wrap_optimal_fit(&words, &[width], &penalties);

for word in &words {
for width in [word.width, word.whitespace_width, word.penalty_width] {
if !width.is_finite() || width.abs() > 1e100 {
return;
}
}
}

let _ = wrap_optimal_fit(&words, &[width as f64], &penalties);
});
49 changes: 49 additions & 0 deletions fuzz/fuzz_targets/wrap_optimal_fit_usize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use textwrap::core;
use textwrap::wrap_algorithms::{wrap_optimal_fit, OptimalFit};

#[derive(Arbitrary, Debug)]
struct Penalties {
nline_penalty: usize,
overflow_penalty: usize,
short_last_line_fraction: usize,
short_last_line_penalty: usize,
hyphen_penalty: usize,
}

impl Into<OptimalFit> for Penalties {
fn into(self) -> OptimalFit {
OptimalFit {
nline_penalty: self.nline_penalty,
overflow_penalty: self.overflow_penalty,
short_last_line_fraction: std::cmp::max(1, self.short_last_line_fraction),
short_last_line_penalty: self.short_last_line_penalty,
hyphen_penalty: self.hyphen_penalty,
}
}
}

#[derive(Arbitrary, Debug, PartialEq)]
struct Word {
width: usize,
whitespace_width: usize,
penalty_width: usize,
}

#[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 penalty_width(&self) -> f64 { self.penalty_width as f64 }
}

// Check wrapping fragments generated with integer widths. These
// fragments are of the same form as the ones generated by wrap.
fuzz_target!(|input: (usize, Vec<Word>, Penalties)| {
let width = input.0;
let words = input.1;
let penalties = input.2.into();
let _ = wrap_optimal_fit(&words, &[width as f64], &penalties);
});
20 changes: 10 additions & 10 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ pub fn display_width(text: &str) -> usize {
/// the displayed width of each part, which this trait provides.
pub trait Fragment: std::fmt::Debug {
/// Displayed width of word represented by this fragment.
fn width(&self) -> usize;
fn width(&self) -> f64;

/// Displayed width of the whitespace that must follow the word
/// when the word is not at the end of a line.
fn whitespace_width(&self) -> usize;
fn whitespace_width(&self) -> f64;

/// Displayed width of the penalty that must be inserted if the
/// word falls at the end of a line.
fn penalty_width(&self) -> usize;
fn penalty_width(&self) -> f64;
}

/// A piece of wrappable text, including any trailing whitespace.
Expand Down Expand Up @@ -304,22 +304,22 @@ impl<'a> Word<'a> {

impl Fragment for Word<'_> {
#[inline]
fn width(&self) -> usize {
self.width
fn width(&self) -> f64 {
self.width as f64
}

// We assume the whitespace consist of ' ' only. This allows us to
// compute the display width in constant time.
#[inline]
fn whitespace_width(&self) -> usize {
self.whitespace.len()
fn whitespace_width(&self) -> f64 {
self.whitespace.len() as f64
}

// We assume the penalty is `""` or `"-"`. This allows us to
// compute the display width in constant time.
#[inline]
fn penalty_width(&self) -> usize {
self.penalty.len()
fn penalty_width(&self) -> f64 {
self.penalty.len() as f64
}
}

Expand All @@ -334,7 +334,7 @@ where
{
let mut shortened_words = Vec::new();
for word in words {
if word.width() > line_width {
if word.width() > line_width as f64 {
shortened_words.extend(word.break_apart(line_width));
} else {
shortened_words.push(word);
Expand Down
17 changes: 5 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ pub fn fill_inplace(text: &mut String, width: usize) {
let words = word_separators::AsciiSpace
.find_words(line)
.collect::<Vec<_>>();
let wrapped_words = wrap_algorithms::wrap_first_fit(&words, &[width]);
let wrapped_words = wrap_algorithms::wrap_first_fit(&words, &[width as f64]);

let mut line_offset = offset;
for words in &wrapped_words[..wrapped_words.len() - 1] {
Expand Down Expand Up @@ -1392,19 +1392,12 @@ mod tests {
}

#[test]
#[cfg(not(feature = "smawk"))]
fn max_width() {
// No overflow for the first-fit wrap algorithm.
assert_eq!(wrap("foo bar", usize::max_value()), vec!["foo bar"]);
}
assert_eq!(wrap("foo bar", usize::MAX), vec!["foo bar"]);

#[test]
#[cfg(feature = "smawk")]
#[should_panic(expected = "attempt to multiply with overflow")]
fn max_width() {
// The optimal-fit algorithm overflows for extreme line
// widths. See #247 and #416 for details..
assert_eq!(wrap("foo bar", usize::max_value()), vec!["foo bar"]);
let text = "Hello there! This is some English text. \
It should not be wrapped given the extents below.";
assert_eq!(wrap(text, usize::MAX), vec![text]);
}

#[test]
Expand Down
Loading