From 839da764e7e86c712e8383e5c593e322e09ae2d4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 29 Nov 2017 14:07:04 +0800 Subject: [PATCH] Regex: avoid unneeded allocations and moves by reusing MatchResults storage --- src/regex.hh | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/regex.hh b/src/regex.hh index 58f71270..4c7b8ecb 100644 --- a/src/regex.hh +++ b/src/regex.hh @@ -95,6 +95,8 @@ struct MatchResults m_values.swap(other.m_values); } + Vector& values() { return m_values; } + private: Vector m_values; }; @@ -116,10 +118,8 @@ bool regex_match(It begin, It end, const Regex& re) template bool regex_match(It begin, It end, MatchResults& res, const Regex& re) { - Vector captures; - const bool matched = regex_match(begin, end, captures, *re.impl()); - res = matched ? MatchResults{std::move(captures)} : MatchResults{}; - return matched; + res.values().clear(); + return regex_match(begin, end, res.values(), *re.impl()); } template @@ -133,10 +133,8 @@ template bool regex_search(It begin, It end, MatchResults& res, const Regex& re, RegexExecFlags flags = RegexExecFlags::None) { - Vector captures; - const bool matched = regex_search(begin, end, captures, *re.impl(), flags); - res = matched ? MatchResults{std::move(captures)} : MatchResults{}; - return matched; + res.values().clear(); + return regex_search(begin, end, res.values(), *re.impl(), flags); } String option_to_string(const Regex& re);