[−][src]Module textwrap::wrap_algorithms
Word wrapping algorithms.
After a text has been broken into words (or Fragment
s), one
now has to decide how to break the fragments into lines. The
simplest algorithm for this is implemented by wrap_first_fit
:
it uses no look-ahead and simply adds fragments to the line as
long as they fit. However, this can lead to poor line breaks if a
large fragment almost-but-not-quite fits on a line. When that
happens, the fragment is moved to the next line and it will leave
behind a large gap. A more advanced algorithm, implemented by
wrap_optimal_fit
, will take this into account. The optimal-fit
algorithm considers all possible line breaks and will attempt to
minimize the gaps left behind by overly short lines.
While both algorithms run in linear time, the first-fit algorithm is about 4 times faster than the optimal-fit algorithm.
Structs
FirstFit | Wrap words using a fast and simple algorithm. |
OptimalFit | Wrap words using an advanced algorithm with look-ahead. |
Traits
WrapAlgorithm | Describes how to wrap words into lines. |
Functions
wrap_first_fit | Wrap abstract fragments into lines with a first-fit algorithm. |
wrap_optimal_fit | Wrap abstract fragments into lines with an optimal-fit algorithm. |