@@ -47,6 +47,7 @@ pub mod project_util;
4747pub use foundry_compilers_artifacts as artifacts;
4848pub use foundry_compilers_core:: { error, utils} ;
4949
50+ use crate :: flatten:: Update ;
5051use cache:: CompilerCache ;
5152use compile:: output:: contracts:: VersionedContracts ;
5253use compilers:: multi:: MultiCompiler ;
@@ -64,7 +65,6 @@ use semver::Version;
6465use solc:: SolcSettings ;
6566use std:: {
6667 collections:: { BTreeMap , HashMap , HashSet } ,
67- ops:: Range ,
6868 path:: { Path , PathBuf } ,
6969} ;
7070
@@ -886,20 +886,18 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
886886}
887887
888888/// Utility function to change source content ranges with provided updates.
889- fn replace_source_content < ' a > (
890- source : & str ,
891- updates : impl Iterator < Item = ( Range < usize > , & ' a str ) > ,
892- ) -> String {
893- let mut updated_content = source. to_string ( ) ;
889+ fn replace_source_content < ' a > ( source : & str , updates : impl Iterator < Item = Update > ) -> String {
894890 let mut offset = 0 ;
895- for ( range, update) in updates {
896- let start = range. start - offset;
897- let end = range. end - offset;
891+ let mut content = source. as_bytes ( ) . to_vec ( ) ;
892+ for ( start, end, new_value) in updates {
893+ let start = ( start as isize + offset) as usize ;
894+ let end = ( end as isize + offset) as usize ;
898895
899- updated_content . replace_range ( start..end, update ) ;
900- offset += end - start;
896+ content . splice ( start..end, new_value . bytes ( ) ) ;
897+ offset += new_value . len ( ) as isize - ( end - start) as isize ;
901898 }
902- updated_content
899+
900+ String :: from_utf8_lossy ( & content) . to_string ( )
903901}
904902
905903#[ cfg( test) ]
@@ -1071,15 +1069,16 @@ contract A {
10711069 // logic logic logic
10721070 }
10731071}"# ;
1072+
10741073 let updates = vec ! [
10751074 // Replace function libFn() visibility to external
1076- ( ( 36 .. 44 ) , "external" ) ,
1075+ ( 36 , 44 , "external" . to_string ( ) ) ,
10771076 // Replace contract A name to contract B
1078- ( ( 88 .. 98 ) , "contract B" ) ,
1077+ ( 80 , 90 , "contract B" . to_string ( ) ) ,
10791078 // Remove function c()
1080- ( ( 167 .. 230 ) , "" ) ,
1079+ ( 159 , 222 , "" . to_string ( ) ) ,
10811080 // Replace function e() logic
1082- ( ( 294 .. 314 ) , "// no logic" ) ,
1081+ ( 276 , 296 , "// no logic" . to_string ( ) ) ,
10831082 ]
10841083 . into_iter ( ) ;
10851084
0 commit comments