2024-01-31 15:49:41 -05:00
|
|
|
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| {
|
2024-02-04 08:53:10 -05:00
|
|
|
line.strip_prefix("#REPLACE")
|
|
|
|
.and_then(|l| l.trim().rsplit_once(' '))
|
2024-01-31 15:49:41 -05:00
|
|
|
.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
|
|
|
|
}
|