Regex: avoid unneeded allocations and moves by reusing MatchResults storage

This commit is contained in:
Maxime Coste 2017-11-29 14:07:04 +08:00
parent f950fe3376
commit 839da764e7

View File

@ -95,6 +95,8 @@ struct MatchResults
m_values.swap(other.m_values); m_values.swap(other.m_values);
} }
Vector<Iterator, MemoryDomain::Regex>& values() { return m_values; }
private: private:
Vector<Iterator, MemoryDomain::Regex> m_values; Vector<Iterator, MemoryDomain::Regex> m_values;
}; };
@ -116,10 +118,8 @@ bool regex_match(It begin, It end, const Regex& re)
template<typename It> template<typename It>
bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re) bool regex_match(It begin, It end, MatchResults<It>& res, const Regex& re)
{ {
Vector<It, MemoryDomain::Regex> captures; res.values().clear();
const bool matched = regex_match(begin, end, captures, *re.impl()); return regex_match(begin, end, res.values(), *re.impl());
res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched;
} }
template<typename It> template<typename It>
@ -133,10 +133,8 @@ template<typename It, MatchDirection direction = MatchDirection::Forward>
bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re, bool regex_search(It begin, It end, MatchResults<It>& res, const Regex& re,
RegexExecFlags flags = RegexExecFlags::None) RegexExecFlags flags = RegexExecFlags::None)
{ {
Vector<It, MemoryDomain::Regex> captures; res.values().clear();
const bool matched = regex_search<It, direction>(begin, end, captures, *re.impl(), flags); return regex_search<It, direction>(begin, end, res.values(), *re.impl(), flags);
res = matched ? MatchResults<It>{std::move(captures)} : MatchResults<It>{};
return matched;
} }
String option_to_string(const Regex& re); String option_to_string(const Regex& re);