Repository: mgeisler/textwrap (language: rust)

Issue:
Respect non-breaking spaces
According to the comment on #28, we treat non-breaking spaces as normal whitespace and break on it. We should probably not do that :-)


Discussion / hints:

Fix patch:
diff --git a/src/lib.rs b/src/lib.rs
index 5f3f7fd5..bb3ebe55 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -175,8 +175,14 @@ impl Wrapper {
         let mut lines = Vec::with_capacity(s.len() / (self.width + 1));
         let mut line = String::with_capacity(self.width);
         let mut remaining = self.width;
+        const NBSP: char = '\u{a0}'; // non-breaking space
+
+        for mut word in s.split(|c: char| c.is_whitespace() && c != NBSP) {
+            // Skip over adjacent whitespace characters.
+            if word.is_empty() {
+                continue;
+            }
 
-        for mut word in s.split_whitespace() {
             // Attempt to fit the word without any splitting.
             if self.fit_part(word, "", &mut remaining, &mut line) {
                 continue;
@@ -548,6 +554,20 @@ mod tests {
         assert_eq!(wrap("foobar", 0), vec!["foobar"]);
     }
 
+    #[test]
+    fn test_non_breaking_space() {
+        let mut wrapper = Wrapper::new(5);
+        wrapper.break_words = false;
+        assert_eq!(wrapper.fill("foo bar baz"), "foo bar baz");
+    }
+
+    #[test]
+    fn test_non_breaking_hyphen() {
+        let mut wrapper = Wrapper::new(5);
+        wrapper.break_words = false;
+        assert_eq!(wrapper.fill("foo‑bar‑baz"), "foo‑bar‑baz");
+    }
+
     #[test]
     fn test_fill() {
         assert_eq!(fill("foo bar baz", 10), "foo bar\nbaz");


Test patch: