From 05739498c236e8d26ad83bdcad0d9b8c09c6a7ac Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 5 Sep 2017 16:16:48 +1200 Subject: [PATCH] Fixup some nits from #44238 --- src/librustdoc/html/render.rs | 102 ++++++++-------------------------- 1 file changed, 22 insertions(+), 80 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index cc84e340c74..6dd046554e4 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -606,12 +606,20 @@ pub fn run(mut krate: clean::Crate, } // A short, single-line view of `s`. -fn concise_str(s: &str) -> String { +fn concise_str(mut s: &str) -> String { if s.contains('\n') { - return format!("{}...", s.lines().next().expect("Impossible! We just found a newline")); + s = s.lines().next().expect("Impossible! We just found a newline"); } if s.len() > 70 { - return format!("{} ... {}", &s[..50], &s[s.len()-20..]); + let mut lo = 50; + let mut hi = s.len() - 20; + while !s.is_char_boundary(lo) { + lo -= 1; + } + while !s.is_char_boundary(hi) { + hi += 1; + } + return format!("{} ... {}", &s[..lo], &s[hi..]); } s.to_owned() } @@ -1756,18 +1764,18 @@ fn render_markdown(w: &mut fmt::Formatter, // We only emit warnings if the user has opted-in to Pulldown rendering. let output = if render_type == RenderType::Pulldown { let pulldown_output = format!("{}", Markdown(md_text, RenderType::Pulldown)); - let differences = html_diff::get_differences(&pulldown_output, &hoedown_output); - let differences = differences.into_iter() - .filter(|s| { - match *s { - html_diff::Difference::NodeText { ref elem_text, - ref opposite_elem_text, - .. } - if match_non_whitespace(elem_text, opposite_elem_text) => false, - _ => true, + let mut differences = html_diff::get_differences(&pulldown_output, &hoedown_output); + differences.retain(|s| { + match *s { + html_diff::Difference::NodeText { ref elem_text, + ref opposite_elem_text, + .. } + if elem_text.split_whitespace().eq(opposite_elem_text.split_whitespace()) => { + false } - }) - .collect::>(); + _ => true, + } + }); if !differences.is_empty() { scx.markdown_warnings.borrow_mut().push((span, md_text.to_owned(), differences)); @@ -1781,40 +1789,6 @@ fn render_markdown(w: &mut fmt::Formatter, write!(w, "
{}{}
", prefix, output) } -// Returns true iff s1 and s2 match, ignoring whitespace. -fn match_non_whitespace(s1: &str, s2: &str) -> bool { - let s1 = s1.trim(); - let s2 = s2.trim(); - let mut cs1 = s1.chars(); - let mut cs2 = s2.chars(); - while let Some(c1) = cs1.next() { - if c1.is_whitespace() { - continue; - } - - loop { - if let Some(c2) = cs2.next() { - if !c2.is_whitespace() { - if c1 != c2 { - return false; - } - break; - } - } else { - return false; - } - } - } - - while let Some(c2) = cs2.next() { - if !c2.is_whitespace() { - return false; - } - } - - true -} - fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink, cx: &Context, prefix: &str) -> fmt::Result { if let Some(s) = item.doc_value() { @@ -3729,35 +3703,3 @@ fn test_name_sorting() { sorted.sort_by_key(|&s| name_key(s)); assert_eq!(names, sorted); } - -#[cfg(test)] -#[test] -fn test_match_non_whitespace() { - assert!(match_non_whitespace("", "")); - assert!(match_non_whitespace(" ", "")); - assert!(match_non_whitespace("", " ")); - - assert!(match_non_whitespace("a", "a")); - assert!(match_non_whitespace(" a ", "a")); - assert!(match_non_whitespace("a", " a")); - assert!(match_non_whitespace("abc", "abc")); - assert!(match_non_whitespace("abc", " abc ")); - assert!(match_non_whitespace("abc ", "abc")); - assert!(match_non_whitespace("abc xyz", "abc xyz")); - assert!(match_non_whitespace("abc xyz", "abc\nxyz")); - assert!(match_non_whitespace("abc xyz", "abcxyz")); - assert!(match_non_whitespace("abcxyz", "abc xyz")); - assert!(match_non_whitespace("abc xyz ", " abc xyz\n")); - - assert!(!match_non_whitespace("a", "b")); - assert!(!match_non_whitespace(" a ", "c")); - assert!(!match_non_whitespace("a", " aa")); - assert!(!match_non_whitespace("abc", "ac")); - assert!(!match_non_whitespace("abc", " adc ")); - assert!(!match_non_whitespace("abc ", "abca")); - assert!(!match_non_whitespace("abc xyz", "abc xy")); - assert!(!match_non_whitespace("abc xyz", "bc\nxyz")); - assert!(!match_non_whitespace("abc xyz", "abc.xyz")); - assert!(!match_non_whitespace("abcxyz", "abc.xyz")); - assert!(!match_non_whitespace("abc xyz ", " abc xyz w")); -}