Parse "#REPLACE" macro in MML.

This commit is contained in:
Lyle Mantooth 2024-01-31 15:49:41 -05:00
parent bc7f16af99
commit 37aa237209
Signed by: IslandUsurper
GPG key ID: 6DB52EAE123A5789
5 changed files with 32 additions and 3 deletions

28
src/mfvi.rs Normal file
View file

@ -0,0 +1,28 @@
struct Translation<'a> {
find: &'a str,
replacement: &'a str,
}
pub fn replace_chars(input: &str) -> String {
let trs: Vec<Translation<'_>> = input
.lines()
.filter_map(|line| {
line.strip_prefix("#REPLACE ")
.and_then(|l| l.rsplit_once(' '))
.and_then(|(first, second)| {
let length = std::cmp::min(first.len(), second.len());
Some(Translation {
find: &first[0..length],
replacement: &second[0..length],
})
})
})
.collect();
let mut new = input.to_string();
for tr in trs {
new = new.replace(tr.find, tr.replacement);
}
new
}