@@ -47,6 +47,7 @@ pub mod project_util;
47
47
pub use foundry_compilers_artifacts as artifacts;
48
48
pub use foundry_compilers_core:: { error, utils} ;
49
49
50
+ use crate :: flatten:: Update ;
50
51
use cache:: CompilerCache ;
51
52
use compile:: output:: contracts:: VersionedContracts ;
52
53
use compilers:: multi:: MultiCompiler ;
@@ -64,7 +65,6 @@ use semver::Version;
64
65
use solc:: SolcSettings ;
65
66
use std:: {
66
67
collections:: { BTreeMap , HashMap , HashSet } ,
67
- ops:: Range ,
68
68
path:: { Path , PathBuf } ,
69
69
} ;
70
70
@@ -886,20 +886,18 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
886
886
}
887
887
888
888
/// 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 ( source : & str , updates : impl Iterator < Item = Update > ) -> String {
894
890
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 ;
898
895
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 ;
901
898
}
902
- updated_content
899
+
900
+ String :: from_utf8_lossy ( & content) . to_string ( )
903
901
}
904
902
905
903
#[ cfg( test) ]
@@ -1071,15 +1069,16 @@ contract A {
1071
1069
// logic logic logic
1072
1070
}
1073
1071
}"# ;
1072
+
1074
1073
let updates = vec ! [
1075
1074
// Replace function libFn() visibility to external
1076
- ( ( 36 .. 44 ) , "external" ) ,
1075
+ ( 36 , 44 , "external" . to_string ( ) ) ,
1077
1076
// Replace contract A name to contract B
1078
- ( ( 88 .. 98 ) , "contract B" ) ,
1077
+ ( 80 , 90 , "contract B" . to_string ( ) ) ,
1079
1078
// Remove function c()
1080
- ( ( 167 .. 230 ) , "" ) ,
1079
+ ( 159 , 222 , "" . to_string ( ) ) ,
1081
1080
// Replace function e() logic
1082
- ( ( 294 .. 314 ) , "// no logic" ) ,
1081
+ ( 276 , 296 , "// no logic" . to_string ( ) ) ,
1083
1082
]
1084
1083
. into_iter ( ) ;
1085
1084
0 commit comments