Skip to content

Commit

Permalink
rustdoc: Use iterators to collapse whitespace
Browse files Browse the repository at this point in the history
Thanks, @alexcrichton!
  • Loading branch information
zaeleus committed Apr 6, 2015
1 parent b6c2e82 commit 46cc6e5
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> = {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 46cc6e5

Please sign in to comment.