@@ -50,33 +50,32 @@ fn apply_diff(src: &mut String, preamble: &str, added: &str, removed: &str) -> R
5050 location to insert:\n {added}\n In 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: \n couldn't find the following text:\n {preamble}\n \n In \
61- the following text:\n {src}"
65+ "Failure on applying a diff: \n Couldn't find the following preamble:\n {preamble}\n In \
66+ the following text:\n {src}\n While trying to remove the following \
67+ text:\n {removed}\n And add the following:\n {added}"
6268 )
6369 } ;
6470
6571 assert ! (
6672 matches. next( ) . is_none( ) ,
6773 "Failure on applying a diff: \n Ambiguous preamble:\n {preamble}\n In the following \
6874 text:\n {src}\n While trying to remove the following text:\n {removed}\n And 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: \n Text to remove not found:\n {removed}\n \n In 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