Skip to content

Commit abc0530

Browse files
committed
Do a better job of eliding whitespace-only differences from warnings
1 parent 1a8aac3 commit abc0530

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/librustdoc/html/render.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ fn render_markdown(w: &mut fmt::Formatter,
17631763
html_diff::Difference::NodeText { ref elem_text,
17641764
ref opposite_elem_text,
17651765
.. }
1766-
if elem_text.trim() == opposite_elem_text.trim() => false,
1766+
if match_non_whitespace(elem_text, opposite_elem_text) => false,
17671767
_ => true,
17681768
}
17691769
})
@@ -1781,6 +1781,40 @@ fn render_markdown(w: &mut fmt::Formatter,
17811781
write!(w, "<div class='docblock'>{}{}</div>", prefix, output)
17821782
}
17831783

1784+
// Returns true iff s1 and s2 match, ignoring whitespace.
1785+
fn match_non_whitespace(s1: &str, s2: &str) -> bool {
1786+
let s1 = s1.trim();
1787+
let s2 = s2.trim();
1788+
let mut cs1 = s1.chars();
1789+
let mut cs2 = s2.chars();
1790+
while let Some(c1) = cs1.next() {
1791+
if c1.is_whitespace() {
1792+
continue;
1793+
}
1794+
1795+
loop {
1796+
if let Some(c2) = cs2.next() {
1797+
if !c2.is_whitespace() {
1798+
if c1 != c2 {
1799+
return false;
1800+
}
1801+
break;
1802+
}
1803+
} else {
1804+
return false;
1805+
}
1806+
}
1807+
}
1808+
1809+
while let Some(c2) = cs2.next() {
1810+
if !c2.is_whitespace() {
1811+
return false;
1812+
}
1813+
}
1814+
1815+
true
1816+
}
1817+
17841818
fn document_short(w: &mut fmt::Formatter, item: &clean::Item, link: AssocItemLink,
17851819
cx: &Context, prefix: &str) -> fmt::Result {
17861820
if let Some(s) = item.doc_value() {
@@ -3695,3 +3729,35 @@ fn test_name_sorting() {
36953729
sorted.sort_by_key(|&s| name_key(s));
36963730
assert_eq!(names, sorted);
36973731
}
3732+
3733+
#[cfg(test)]
3734+
#[test]
3735+
fn test_match_non_whitespace() {
3736+
assert!(match_non_whitespace("", ""));
3737+
assert!(match_non_whitespace(" ", ""));
3738+
assert!(match_non_whitespace("", " "));
3739+
3740+
assert!(match_non_whitespace("a", "a"));
3741+
assert!(match_non_whitespace(" a ", "a"));
3742+
assert!(match_non_whitespace("a", " a"));
3743+
assert!(match_non_whitespace("abc", "abc"));
3744+
assert!(match_non_whitespace("abc", " abc "));
3745+
assert!(match_non_whitespace("abc ", "abc"));
3746+
assert!(match_non_whitespace("abc xyz", "abc xyz"));
3747+
assert!(match_non_whitespace("abc xyz", "abc\nxyz"));
3748+
assert!(match_non_whitespace("abc xyz", "abcxyz"));
3749+
assert!(match_non_whitespace("abcxyz", "abc xyz"));
3750+
assert!(match_non_whitespace("abc xyz ", " abc xyz\n"));
3751+
3752+
assert!(!match_non_whitespace("a", "b"));
3753+
assert!(!match_non_whitespace(" a ", "c"));
3754+
assert!(!match_non_whitespace("a", " aa"));
3755+
assert!(!match_non_whitespace("abc", "ac"));
3756+
assert!(!match_non_whitespace("abc", " adc "));
3757+
assert!(!match_non_whitespace("abc ", "abca"));
3758+
assert!(!match_non_whitespace("abc xyz", "abc xy"));
3759+
assert!(!match_non_whitespace("abc xyz", "bc\nxyz"));
3760+
assert!(!match_non_whitespace("abc xyz", "abc.xyz"));
3761+
assert!(!match_non_whitespace("abcxyz", "abc.xyz"));
3762+
assert!(!match_non_whitespace("abc xyz ", " abc xyz w"));
3763+
}

0 commit comments

Comments
 (0)