about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Macias <zaeleus@gmail.com>2015-04-06 13:56:39 -0500
committerMichael Macias <zaeleus@gmail.com>2015-04-06 14:07:02 -0500
commit46cc6e5fc31377c001a5a12c5079388253eecfbc (patch)
tree90d1607628e2ab006b897195c45f4aa86b4c7534
parentb6c2e82b71ebc6d596f6c7651b22dffa37c4e41b (diff)
downloadrust-46cc6e5fc31377c001a5a12c5079388253eecfbc.tar.gz
rust-46cc6e5fc31377c001a5a12c5079388253eecfbc.zip
rustdoc: Use iterators to collapse whitespace
Thanks, @alexcrichton!
-rw-r--r--src/librustdoc/html/markdown.rs29
1 files changed, 8 insertions, 21 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 4b267982f3b..49f6107869e 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -192,25 +192,11 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
 /// Returns a new string with all consecutive whitespace collapsed into
 /// single spaces.
 ///
-/// The input is assumed to be already trimmed.
+/// Any leading or trailing whitespace will be trimmed.
 fn collapse_whitespace(s: &str) -> String {
-    let mut buffer = String::with_capacity(s.len());
-    let mut previous_char_is_whitespace = false;
-
-    for c in s.chars() {
-        if c.is_whitespace() {
-            if !previous_char_is_whitespace {
-                buffer.push(' ');
-            }
-
-            previous_char_is_whitespace = true;
-        } else {
-            buffer.push(c);
-            previous_char_is_whitespace = false;
-        }
-    }
-
-    buffer
+    s.split(|c: char| c.is_whitespace()).filter(|s| {
+        !s.is_empty()
+    }).collect::<Vec<_>>().connect(" ")
 }
 
 thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
@@ -623,8 +609,9 @@ mod tests {
         }
 
         t("foo", "foo");
-        t("foo   bar", "foo bar");
-        t("foo  bar\nbaz", "foo bar baz");
-        t("foo bar \n   baz\t\tqux", "foo bar baz qux");
+        t("foo bar baz", "foo bar baz");
+        t(" foo   bar", "foo bar");
+        t("\tfoo   bar\nbaz", "foo bar baz");
+        t("foo   bar \n   baz\t\tqux\n", "foo bar baz qux");
     }
 }