Repository: mgeisler/textwrap (language: rust)

Issue:
Unintended wrapping when using external splitter
Consider the code:

```rust
use hyphenation;
use hyphenation::Load;
use textwrap;

let us_hyph = hyphenation::Standard::from_embedded(
    hyphenation::Language::EnglishUS).unwrap();
let input = "participation is the key to sucess";
let wrapper = textwrap::Wrapper::new(10);
let output = "participat\nion is the\nkey to\nsucess";
assert_eq!(output, wrapper.fill(input));
let wrapper = textwrap::Wrapper::with_splitter(10, us_hyph);
let output = "participa-\ntion\n \nis the key\nto sucess";
assert_eq!(output, wrapper.fill(input));
// expected output "participa-\ntion is the\nkey to\nsucess";
````

The first wrapper works okay (though it has to split the word in the middle) but the second wrapper with the hyphenator ends up having a blank third line instead of the expected result (in comments).

This might be a bug in the hyphenator library, but I'm not familiar enough with the code to know.


Discussion / hints:
Wow, that looks really strange... I don't know where the problem is yet, but I'll look at it.

Fix patch:
diff --git a/src/lib.rs b/src/lib.rs
index 2f82325f..d184f845 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -530,6 +530,7 @@ impl<'a> WrapIterImpl<'a> {
                         // whitespace and the head length.
                         self.split += self.split_len + head.len();
                         self.split_len = 0;
+                        self.line_width_at_split += head.width() + hyp.width();
                         hyphen = hyp;
                         break;
                     }
@@ -560,6 +561,7 @@ impl<'a> WrapIterImpl<'a> {
                     self.line_width += wrapper.subsequent_indent.width();
                     self.line_width -= self.line_width_at_split;
                     self.line_width += char_width;
+                    self.line_width_at_split = wrapper.subsequent_indent.width();
 
                     return Some(line);
                 }
@@ -888,6 +890,23 @@ mod tests {
         );
     }
 
+    #[test]
+    #[cfg(feature = "hyphenation")]
+    fn auto_hyphenation_issue_158() {
+        let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
+        let wrapper = Wrapper::new(10);
+        assert_eq!(
+            wrapper.wrap("participation is the key to success"),
+            vec!["participat", "ion is the", "key to", "success"]
+        );
+
+        let wrapper = Wrapper::with_splitter(10, dictionary);
+        assert_eq!(
+            wrapper.wrap("participation is the key to success"),
+            vec!["participa-", "tion is the", "key to", "success"]
+        );
+    }
+
     #[test]
     #[cfg(feature = "hyphenation")]
     fn split_len_hyphenation() {


Test patch: