Skip to content

Commit cf03715

Browse files
website-test: make diff resolver use more context to avoid ambiguities (#3883)
1 parent b58a874 commit cf03715

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

tools/website-test/build.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,32 @@ fn apply_diff(src: &mut String, preamble: &str, added: &str, removed: &str) -> R
5050
location to insert:\n{added}\nIn the following text:\n{src}",
5151
);
5252

53-
let mut matches = src.match_indices(if preamble.is_empty() {
54-
removed
55-
} else {
56-
preamble
57-
});
58-
let Some((preamble_start, _)) = matches.next() else {
53+
let mut matches = src
54+
.match_indices(preamble)
55+
.filter_map(|(chunk_start, chunk)| {
56+
let removed_start = chunk_start + chunk.len();
57+
let removed_end = removed_start + removed.len();
58+
src.get(removed_start..removed_end)
59+
.eq(&Some(removed))
60+
.then_some((removed_start, removed_end))
61+
});
62+
63+
let Some((removed_start, removed_end)) = matches.next() else {
5964
e!(
60-
"Failure on applying a diff: \ncouldn't find the following text:\n{preamble}\n\nIn \
61-
the following text:\n{src}"
65+
"Failure on applying a diff: \nCouldn't find the following preamble:\n{preamble}\nIn \
66+
the following text:\n{src}\nWhile trying to remove the following \
67+
text:\n{removed}\nAnd add the following:\n{added}"
6268
)
6369
};
6470

6571
assert!(
6672
matches.next().is_none(),
6773
"Failure on applying a diff: \nAmbiguous preamble:\n{preamble}\nIn the following \
6874
text:\n{src}\nWhile trying to remove the following text:\n{removed}\nAnd add the \
69-
following:\n{added}\n"
75+
following:\n{added}"
7076
);
7177

72-
let preamble_end = preamble_start + preamble.len();
73-
assert!(
74-
src.get(preamble_end..preamble_end + removed.len()) == Some(removed),
75-
"Failure on applying a diff: \nText to remove not found:\n{removed}\n\nIn the following \
76-
text:\n{src}",
77-
);
78-
79-
src.replace_range(preamble_end..preamble_end + removed.len(), added);
78+
src.replace_range(removed_start..removed_end, added);
8079
Ok(())
8180
}
8281

0 commit comments

Comments
 (0)