From 0cf84e1f006988c114bdca36715d3a2c0601a7d5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 30 Aug 2017 10:23:06 +0300 Subject: Generalize regex_replace_ex() function --- libbutl/regex.txx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'libbutl/regex.txx') diff --git a/libbutl/regex.txx b/libbutl/regex.txx index 52bddfd..536cabf 100644 --- a/libbutl/regex.txx +++ b/libbutl/regex.txx @@ -7,11 +7,12 @@ namespace butl { - template - std::pair, bool> + template + bool regex_replace_ex (const std::basic_string& s, const std::basic_regex& re, const std::basic_string& fmt, + F&& append, std::regex_constants::match_flag_type flags) { using namespace std; @@ -24,7 +25,6 @@ namespace butl bool no_copy ((flags & std::regex_constants::format_no_copy) != 0); locale cl; // Copy of the global C++ locale. - string_type r; // Beginning of the last unmatched substring. // @@ -59,7 +59,7 @@ namespace butl // if (!no_copy) { - r.append (ub, m.prefix ().second); + append (ub, m.prefix ().second); ub = m.suffix ().first; } @@ -68,7 +68,7 @@ namespace butl // Append matched substring. // if (!no_copy) - r.append (m[0].first, m[0].second); + append (m[0].first, m[0].second); } else { @@ -100,6 +100,8 @@ namespace butl return c; }; + string_type r; + auto append_chr = [&r, &conv_chr] (C c) { r.push_back (conv_chr (c)); @@ -233,14 +235,16 @@ namespace butl } } } + + append (r.begin (), r.end ()); } } // Append the rightmost non-matched substring. // if (!no_copy) - r.append (ub, s.end ()); + append (ub, s.end ()); - return make_pair (move (r), match); + return match; } } -- cgit v1.1