Repository: mgeisler/textwrap (language: rust)

Issue:
Bad hyphenation in layout example
I just noticed that the layout example looks weird:
```
$ cargo run --all-features --example layout
.------------------ Width: 46 -------------------.
| Memory safety without garbage collection. Co-  |
| ncurrency without data races. Zero-cost a-     |
| bstractions.                                   |
.-------------------- Width: 49 --------------------.
| Memory safety without garbage collection. Concu-  |
| rrency without data races. Zero-cost a-           |
| bstractions.                                      |
.-------------------- Width: 50 ---------------------.
| Memory safety without garbage collection. Concu-   |
| rrency without data races. Zero-cost abstra-       |
| ctions.                                            |
```

This is with version 0.9.0. I think there has been a regression since I don't believe it looked like this in the past — notice how all the hyphenated words are broken in weird places.




Discussion / hints:

Fix patch:
diff --git a/README.md b/README.md
index 04e6f413..dbc3fb75 100644
--- a/README.md
+++ b/README.md
@@ -189,6 +189,7 @@ against is now 1.9.0.
 Issues closed:
 
 * Fixed [#99][issue-99]: Word broken even though it would fit on line.
+* Fixed [#107][issue-107]: Automatic hyphenation is off by one.
 
 ### Version 0.9.0 — October 5th, 2017
 
@@ -331,4 +332,5 @@ Contributions will be accepted under the same license.
 [issue-81]: https://github.com/mgeisler/textwrap/issues/81
 [issue-99]: https://github.com/mgeisler/textwrap/issues/99
 [issue-101]: https://github.com/mgeisler/textwrap/issues/101
+[issue-107]: https://github.com/mgeisler/textwrap/issues/107
 [mit]: LICENSE
diff --git a/src/lib.rs b/src/lib.rs
index 7ab544fc..7209e9c4 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -625,7 +625,10 @@ impl<'a> WrapIterImpl<'a> {
                 let splits = wrapper.splitter.split(final_word);
                 for &(head, hyp, _) in splits.iter().rev() {
                     if self.line_width_at_split + head.width() + hyp.width() <= wrapper.width {
-                        self.split += head.len();
+                        // We can fit head into the current line.
+                        // Advance the split point by the width of the
+                        // whitespace and the head length.
+                        self.split += self.split_len + head.len();
                         self.split_len = 0;
                         hyphen = hyp;
                         break;
@@ -1066,6 +1069,17 @@ mod tests {
                    vec!["Interna-", "tionaliza-", "tion"]);
     }
 
+    #[test]
+    #[cfg(feature = "hyphenation")]
+    fn split_len_hyphenation() {
+        // Test that hyphenation takes the width of the wihtespace
+        // into account.
+        let corpus = hyphenation::load(Language::English_US).unwrap();
+        let wrapper = Wrapper::with_splitter(15, corpus);
+        assert_eq!(wrapper.wrap("garbage   collection"),
+                   vec!["garbage   col-", "lection"]);
+    }
+
     #[test]
     #[cfg(feature = "hyphenation")]
     fn borrowed_lines() {


Test patch: