Skip to content

Commit

Permalink
Reformulate wrapping in terms of boxes, glue, and penalties
Browse files Browse the repository at this point in the history
This is a complete rewrite of the core word wrapping functionality.
Before, we would step though the input string and (attempt to) keep
track of all aspects of the state. This didn't always work (see at
least #122, #158, #158, and #193) and it's inflexible.

This commit replaces the old algorithm with a new one which works on a
more abstract level. We now first

1. First split the input string into "words". A word is a substring of
   the original string, including any trailing whitespace.

2. We split each word according to the `WordSplitter`.

3. We then simply put the words into lines based on the display width.

This is slower than the previous algorithm. The `fill/1600` benchmark
shows that is now takes ~18 microseconds to wrap a 1600 character long
string. That is around 8 microseconds longer than before.
  • Loading branch information
mgeisler committed Nov 8, 2020
1 parent fe5b491 commit 1fc0ba3
Show file tree
Hide file tree
Showing 5 changed files with 803 additions and 367 deletions.
2 changes: 1 addition & 1 deletion examples/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ mod unix_only {
)?;
row += 2;

let mut lines = wrap(text, options).collect::<Vec<_>>();
let mut lines = wrap(text, options);
if let Some(line) = lines.last() {
// If `text` ends with a newline, the final wrapped line
// contains this newline. This will in turn leave the
Expand Down
2 changes: 1 addition & 1 deletion examples/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {

for width in 15..60 {
options.width = width;
let lines = wrap(example, &options).collect::<Vec<_>>();
let lines = wrap(example, &options);
if lines != prev_lines {
let title = format!(" Width: {} ", width);
println!(".{:-^1$}.", title, width + 2);
Expand Down
Loading

0 comments on commit 1fc0ba3

Please sign in to comment.