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 cb981f2
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,30 @@ impl Replacer {
&'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,
);
});
} else {
fn r<'a>(
content: &'a [u8],
replace: impl Iterator<Item = impl regex::bytes::Replacer>,
patterns: impl Iterator<Item = regex::bytes::Regex>,
max_replacements: usize,
) -> std::borrow::Cow<'a, [u8]> {
let mut content = std::borrow::Cow::Borrowed(content);
for (regex, replace_with) in
self.regexes.iter().zip(&self.replace_withs)
patterns.zip(replace)
{
content = regex.replacen(
&content,
self.max_replacements,
max_replacements,
replace_with,
);
}
content
}
if self.is_literal {
let mut rep = self.replace_withs.iter().map(|r| regex::bytes::NoExpand(r));
r(content, rep, self.regexes.into_iter(), self.max_replacements)
} else {
r(content, self.replace_withs.into_iter(), self.regexes.into_iter(), self.max_replacements)
}
content
}

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

0 comments on commit cb981f2

Please sign in to comment.