Avoid unnecessary vector shift in re::regex_make_anchored()

There's no reason to inject prefix into our newly allocated str after storing
pattern in there. Just allocate with the needed capacity up front and then
insert in the correct order.
This commit is contained in:
Mahmoud Al-Qudsi
2023-05-02 13:13:11 -05:00
parent 7b0cc33f2e
commit 40be27c002

View File

@@ -4,12 +4,12 @@
/// This is a workaround for the fact that PCRE2_ENDANCHORED is unavailable on pre-2017 PCRE2
/// (e.g. 10.21, on Xenial).
pub fn regex_make_anchored(pattern: &wstr) -> WString {
let mut anchored = pattern.to_owned();
// PATTERN -> ^(:?PATTERN)$.
let prefix = L!("^(?:");
let suffix = L!(")$");
anchored.reserve(pattern.len() + prefix.len() + suffix.len());
anchored.insert_utfstr(0, prefix);
let mut anchored = WString::with_capacity(prefix.len() + pattern.len() + suffix.len());
anchored.push_utfstr(prefix);
anchored.push_utfstr(pattern);
anchored.push_utfstr(suffix);
anchored
}