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 u16 instead of usize for line width #418

Closed
wants to merge 2 commits 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
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ jobs:
- name: Build fuzz targets
run: cargo fuzz build

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

- name: Minimize fuzz corpus
- name: Fuzz test fill_optimal_fit
run: cargo fuzz run fill_optimal_fit -- -max_total_time=30

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

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

binary-sizes:
name: Compute binary sizes
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion benches/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use criterion::{criterion_group, criterion_main};

use lipsum::lipsum_words_from_seed;

const LINE_LENGTH: usize = 60;
const LINE_LENGTH: u32 = 60;

/// Generate a lorem ipsum text with the given number of characters.
fn lorem_ipsum(length: usize) -> String {
Expand Down
7 changes: 4 additions & 3 deletions examples/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ fn main() {
Zero-cost abstractions.";
let mut prev_lines = vec![];

let mut options = Options::new(0).word_splitter(Box::new(HyphenSplitter) as Box<dyn WordSplitter>);
let mut options =
Options::new(0).word_splitter(Box::new(HyphenSplitter) as Box<dyn WordSplitter>);
#[cfg(feature = "hyphenation")]
{
use hyphenation::Load;
Expand All @@ -21,9 +22,9 @@ fn main() {
let lines = wrap(example, &options);
if lines != prev_lines {
let title = format!(" Width: {} ", width);
println!(".{:-^1$}.", title, width + 2);
println!(".{:-^1$}.", title, width as usize + 2);
for line in &lines {
println!("| {:1$} |", line, width);
println!("| {:1$} |", line, width as usize);
}
prev_lines = lines;
}
Expand Down
38 changes: 19 additions & 19 deletions examples/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@ impl<'a> CanvasWord<'a> {
}
}

const PRECISION: usize = 10;
const PRECISION: u32 = 10;

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

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

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

Expand Down Expand Up @@ -250,22 +250,22 @@ pub enum WasmWrapAlgorithm {
#[wasm_bindgen]
#[derive(Copy, Clone, Debug, Default)]
pub struct WasmOptimalFit {
pub nline_penalty: i32,
pub overflow_penalty: i32,
pub short_last_line_fraction: usize,
pub short_last_line_penalty: i32,
pub hyphen_penalty: i32,
pub nline_penalty: u32,
pub overflow_penalty: u32,
pub short_last_line_fraction: u32,
pub short_last_line_penalty: u32,
pub hyphen_penalty: u32,
}

#[wasm_bindgen]
impl WasmOptimalFit {
#[wasm_bindgen(constructor)]
pub fn new(
nline_penalty: i32,
overflow_penalty: i32,
short_last_line_fraction: usize,
short_last_line_penalty: i32,
hyphen_penalty: i32,
nline_penalty: u32,
overflow_penalty: u32,
short_last_line_fraction: u32,
short_last_line_penalty: u32,
hyphen_penalty: u32,
) -> WasmOptimalFit {
WasmOptimalFit {
nline_penalty,
Expand All @@ -292,7 +292,7 @@ impl Into<OptimalFit> for WasmOptimalFit {
#[wasm_bindgen]
#[derive(Copy, Clone, Debug)]
pub struct WasmOptions {
pub width: usize,
pub width: u32,
pub break_words: bool,
pub word_separator: WasmWordSeparator,
pub word_splitter: WasmWordSplitter,
Expand All @@ -304,7 +304,7 @@ pub struct WasmOptions {
impl WasmOptions {
#[wasm_bindgen(constructor)]
pub fn new(
width: usize,
width: u32,
break_words: bool,
word_separator: WasmWordSeparator,
word_splitter: WasmWordSplitter,
Expand Down
6 changes: 3 additions & 3 deletions examples/wasm/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ <h1>Textwrap WebAssembly Demo</h1>
<div class="option">
<label for="overflow-penalty">Overflow penalty:</label>
<input type="number" id="overflow-penalty-text">
<input type="range" id="overflow-penalty" min="-1000" max="10000" value="7500">
<input type="range" id="overflow-penalty" min="0" max="10000" value="7500">
</div>

<div class="option">
Expand All @@ -109,13 +109,13 @@ <h1>Textwrap WebAssembly Demo</h1>
<div class="option">
<label for="short-last-line-penalty">Short line penalty:</label>
<input type="number" id="short-last-line-penalty-text">
<input type="range" id="short-last-line-penalty" min="-2000" max="2000" value="100">
<input type="range" id="short-last-line-penalty" min="0" max="2000" value="100">
</div>

<div class="option">
<label for="hyphen-penalty">Hyphen penalty:</label>
<input type="number" id="hyphen-penalty-text">
<input type="range" id="hyphen-penalty" min="-2000" max="2000" value="100">
<input type="range" id="hyphen-penalty" min="0" max="2000" value="100">
</div>
</div>

Expand Down
19 changes: 16 additions & 3 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,34 @@ edition = "2018"
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.3"
arbitrary = { version = "1", features = ["derive"] }
libfuzzer-sys = "0.4"
textwrap = { path = ".." }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "fill_first_fit"
path = "fuzz_targets/fill_first_fit.rs"
test = false
doc = false

[[bin]]
name = "wrap_first_fit"
path = "fuzz_targets/wrap_first_fit.rs"
test = false
doc = false

[[bin]]
name = "fill_optimal_fit"
path = "fuzz_targets/fill_optimal_fit.rs"
test = false
doc = false

[[bin]]
name = "fill_first_fit"
path = "fuzz_targets/fill_first_fit.rs"
name = "wrap_optimal_fit"
path = "fuzz_targets/wrap_optimal_fit.rs"
test = false
doc = false
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fill_first_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target;
use textwrap::wrap_algorithms;
use textwrap::Options;

fuzz_target!(|input: (String, usize)| {
fuzz_target!(|input: (String, u32)| {
let options = Options::new(input.1).wrap_algorithm(wrap_algorithms::FirstFit);
let _ = textwrap::fill(&input.0, &options);
});
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fill_optimal_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target;
use textwrap::wrap_algorithms;
use textwrap::Options;

fuzz_target!(|input: (String, usize)| {
fuzz_target!(|input: (String, u32)| {
let options = Options::new(input.1).wrap_algorithm(wrap_algorithms::OptimalFit::default());
let _ = textwrap::fill(&input.0, &options);
});
25 changes: 25 additions & 0 deletions fuzz/fuzz_targets/wrap_first_fit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use textwrap::core;
use textwrap::wrap_algorithms::wrap_first_fit;

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

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

fuzz_target!(|input: (u32, Vec<Word>)| {
let width = input.0;
let words = input.1;
let _ = wrap_first_fit(&words, &[width]);
});
47 changes: 47 additions & 0 deletions fuzz/fuzz_targets/wrap_optimal_fit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![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: u32,
overflow_penalty: u32,
short_last_line_fraction: u32,
short_last_line_penalty: u32,
hyphen_penalty: u32,
}

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, Eq, PartialEq)]
struct Word {
width: u32,
whitespace_width: u32,
penalty_width: u32,
}

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

fuzz_target!(|input: (u32, Vec<Word>, Penalties)| {
let width = input.0;
let words = input.1;
let penalties = input.2.into();
let _ = wrap_optimal_fit(&words, &[width], &penalties);
});
Loading