Skip to content

Commit 4a78e81

Browse files
committed
rustdoc: Use iterators to collapse whitespace
Thanks, @alexcrichton!
1 parent b6c2e82 commit 4a78e81

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,11 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
192192
/// Returns a new string with all consecutive whitespace collapsed into
193193
/// single spaces.
194194
///
195-
/// The input is assumed to be already trimmed.
195+
/// Any leading or trailing whitespace be trimmed.
196196
fn collapse_whitespace(s: &str) -> String {
197-
let mut buffer = String::with_capacity(s.len());
198-
let mut previous_char_is_whitespace = false;
199-
200-
for c in s.chars() {
201-
if c.is_whitespace() {
202-
if !previous_char_is_whitespace {
203-
buffer.push(' ');
204-
}
205-
206-
previous_char_is_whitespace = true;
207-
} else {
208-
buffer.push(c);
209-
previous_char_is_whitespace = false;
210-
}
211-
}
212-
213-
buffer
197+
s.split(|c: char| c.is_whitespace()).filter(|s| {
198+
!s.is_empty()
199+
}).collect::<Vec<_>>().connect(" ")
214200
}
215201

216202
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
@@ -623,8 +609,9 @@ mod tests {
623609
}
624610

625611
t("foo", "foo");
626-
t("foo bar", "foo bar");
627-
t("foo bar\nbaz", "foo bar baz");
628-
t("foo bar \n baz\t\tqux", "foo bar baz qux");
612+
t("foo bar baz", "foo bar baz");
613+
t(" foo bar", "foo bar");
614+
t("\tfoo bar\nbaz", "foo bar baz");
615+
t("foo bar \n baz\t\tqux\n", "foo bar baz qux");
629616
}
630617
}

0 commit comments

Comments
 (0)