Skip to content

Commit be6c378

Browse files
committed
Change replace_source_content sig, move apply_updates
1 parent 79d0a0f commit be6c378

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

crates/compilers/src/flatten.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
2+
apply_updates,
23
compilers::{Compiler, ParsedSource},
34
filter::MaybeSolData,
4-
replace_source_content,
55
resolver::parse::SolData,
66
ArtifactOutput, CompilerSettings, Graph, Project, ProjectPathsConfig, Updates,
77
};
@@ -899,12 +899,3 @@ pub fn combine_version_pragmas(pragmas: Vec<&str>) -> Option<String> {
899899

900900
None
901901
}
902-
903-
pub fn apply_updates(sources: &mut Sources, mut updates: Updates) {
904-
for (path, source) in sources {
905-
if let Some(updates) = updates.remove(path) {
906-
source.content =
907-
Arc::new(replace_source_content(source.content.as_str(), updates.into_iter()));
908-
}
909-
}
910-
}

crates/compilers/src/lib.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ use semver::Version;
6969
use solc::SolcSettings;
7070
use std::{
7171
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
72+
ops::Range,
7273
path::{Path, PathBuf},
74+
sync::Arc,
7375
};
7476

7577
/// Represents a project workspace and handles `solc` compiling of all contracts in that workspace.
@@ -889,13 +891,28 @@ fn rebase_path(base: &Path, path: &Path) -> PathBuf {
889891
new_path.to_slash_lossy().into_owned().into()
890892
}
891893

894+
/// Utility function to apply a set of updates to provided sources.
895+
fn apply_updates(sources: &mut Sources, updates: Updates) {
896+
for (path, source) in sources {
897+
if let Some(updates) = updates.get(path) {
898+
source.content = Arc::new(replace_source_content(
899+
source.content.as_str(),
900+
updates.iter().map(|(start, end, update)| ((*start..*end), update.as_str())),
901+
));
902+
}
903+
}
904+
}
905+
892906
/// Utility function to change source content ranges with provided updates.
893-
fn replace_source_content(source: &str, updates: impl Iterator<Item = Update>) -> String {
907+
fn replace_source_content<'a>(
908+
source: &str,
909+
updates: impl IntoIterator<Item = (Range<usize>, &'a str)>,
910+
) -> String {
894911
let mut offset = 0;
895912
let mut content = source.as_bytes().to_vec();
896-
for (start, end, new_value) in updates {
897-
let start = (start as isize + offset) as usize;
898-
let end = (end as isize + offset) as usize;
913+
for (range, new_value) in updates {
914+
let start = (range.start as isize + offset) as usize;
915+
let end = (range.end as isize + offset) as usize;
899916

900917
content.splice(start..end, new_value.bytes());
901918
offset += new_value.len() as isize - (end - start) as isize;
@@ -1076,15 +1093,14 @@ contract A {
10761093

10771094
let updates = vec![
10781095
// Replace function libFn() visibility to external
1079-
(36, 44, "external".to_string()),
1096+
(36..44, "external"),
10801097
// Replace contract A name to contract B
1081-
(80, 90, "contract B".to_string()),
1098+
(80..90, "contract B"),
10821099
// Remove function c()
1083-
(159, 222, String::new()),
1100+
(159..222, ""),
10841101
// Replace function e() logic
1085-
(276, 296, "// no logic".to_string()),
1086-
]
1087-
.into_iter();
1102+
(276..296, "// no logic"),
1103+
];
10881104

10891105
assert_eq!(
10901106
replace_source_content(original_content, updates),

crates/compilers/src/preprocessor.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::project::Preprocessor;
22
use crate::{
3-
flatten::apply_updates,
3+
apply_updates,
44
multi::{MultiCompiler, MultiCompilerInput, MultiCompilerLanguage},
55
replace_source_content,
66
solc::{SolcCompiler, SolcVersionedInput},
@@ -88,13 +88,9 @@ pub(crate) fn interface_representation(
8888
return Err(err);
8989
}
9090

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", "");
91+
let content =
92+
replace_source_content(content, spans_to_remove.iter().map(|span| (span.to_range(), "")))
93+
.replace("\n", "");
9894
Ok(utils::RE_TWO_OR_MORE_SPACES.replace_all(&content, "").to_string())
9995
}
10096

0 commit comments

Comments
 (0)