Skip to content

Commit

Permalink
reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
orion GONZALEZ (contractor) committed Oct 19, 2023
1 parent 6b64e37 commit a075207
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,30 @@ impl Replacer {
Ok(())
}

fn generic_replace<'a>(
&self,
content: &'a [u8],
replace: impl Iterator<Item = impl regex::bytes::Replacer>,
) -> std::borrow::Cow<'a, [u8]> {
let mut content = std::borrow::Cow::Borrowed(content);
for (regex, replace_with) in self.regexes.into_iter().zip(replace) {
content =
regex.replacen(&content, self.max_replacements, replace_with);
}
content
}

pub(crate) fn replace<'a>(
&'a self,
content: &'a [u8],
) -> std::borrow::Cow<'a, [u8]> {
let mut content = std::borrow::Cow::Borrowed(content);
if self.is_literal {
let replace_withs = self.replace_withs.iter().map(|r| regex::bytes::NoExpand(r));
self.regexes.iter().zip(replace_withs).for_each(|(regex, replace_with)| {
content = regex.replacen(
&content,
self.max_replacements,
replace_with,
);
});
let mut rep =
self.replace_withs.iter().map(|r| regex::bytes::NoExpand(r));
self.generic_replace(content, rep)
} else {
for (regex, replace_with) in
self.regexes.iter().zip(&self.replace_withs)
{
content = regex.replacen(
&content,
self.max_replacements,
replace_with,
);
}
self.generic_replace(content, self.replace_withs.into_iter())
}
content
}

pub(crate) fn replace_preview<'a>(
Expand Down

0 comments on commit a075207

Please sign in to comment.