Skip to content

Commit 4025e5a

Browse files
committed
reuse replace source fn
1 parent 7d0edb8 commit 4025e5a

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

crates/compilers/src/flatten.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
compilers::{Compiler, ParsedSource},
33
filter::MaybeSolData,
4+
replace_source_content,
45
resolver::parse::SolData,
56
ArtifactOutput, CompilerSettings, Graph, Project, ProjectPathsConfig,
67
};
@@ -111,6 +112,7 @@ impl Visitor for ReferencesCollector {
111112
}
112113
}
113114

115+
pub type Update = (usize, usize, String);
114116
/// Updates to be applied to the sources.
115117
/// source_path -> (start, end, new_value)
116118
pub type Updates = HashMap<PathBuf, BTreeSet<(usize, usize, String)>>;
@@ -906,17 +908,8 @@ pub fn combine_version_pragmas(pragmas: Vec<&str>) -> Option<String> {
906908
pub fn apply_updates(sources: &mut Sources, mut updates: Updates) {
907909
for (path, source) in sources {
908910
if let Some(updates) = updates.remove(path) {
909-
let mut offset = 0;
910-
let mut content = source.content.as_bytes().to_vec();
911-
for (start, end, new_value) in updates {
912-
let start = (start as isize + offset) as usize;
913-
let end = (end as isize + offset) as usize;
914-
915-
content.splice(start..end, new_value.bytes());
916-
offset += new_value.len() as isize - (end - start) as isize;
917-
}
918-
919-
source.content = Arc::new(String::from_utf8_lossy(&content).to_string());
911+
source.content =
912+
Arc::new(replace_source_content(source.content.as_str(), updates.into_iter()));
920913
}
921914
}
922915
}

crates/compilers/src/lib.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub mod project_util;
4747
pub use foundry_compilers_artifacts as artifacts;
4848
pub use foundry_compilers_core::{error, utils};
4949

50+
use crate::flatten::Update;
5051
use cache::CompilerCache;
5152
use compile::output::contracts::VersionedContracts;
5253
use compilers::multi::MultiCompiler;
@@ -64,7 +65,6 @@ use semver::Version;
6465
use solc::SolcSettings;
6566
use 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(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

crates/compilers/src/preprocessor.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@ pub(crate) fn interface_representation(
8888
return Err(err);
8989
}
9090

91-
let content =
92-
replace_source_content(content, spans_to_remove.iter().map(|span| (span.to_range(), "")))
93-
.replace("\n", "");
91+
let content = replace_source_content(
92+
content,
93+
spans_to_remove
94+
.iter()
95+
.map(|span| (span.to_range().start, span.to_range().end, String::new())),
96+
)
97+
.replace("\n", "");
9498
Ok(utils::RE_TWO_OR_MORE_SPACES.replace_all(&content, "").to_string())
9599
}
96100

0 commit comments

Comments
 (0)