aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-06-27 19:22:33 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-06-27 19:22:33 +0300
commit4dabcf0062bc4bdfa807f7b8952eac649c9d65b5 (patch)
tree65efed57d70472bee71e8c8efe259d60b03b9d58
parentaabb1b516a89366247bad3b8b927ab33bd9114d0 (diff)
Add support for format_no_copy flag in regex_replace_ex()
-rw-r--r--libbutl/regex.hxx2
-rw-r--r--libbutl/regex.txx24
2 files changed, 19 insertions, 7 deletions
diff --git a/libbutl/regex.hxx b/libbutl/regex.hxx
index 9090d5a..e4fd6a7 100644
--- a/libbutl/regex.hxx
+++ b/libbutl/regex.hxx
@@ -25,7 +25,7 @@ namespace butl
// Notes and limitations:
//
// - The only valid regex_constants flags are match_default,
- // format_first_only (format_no_copy can easily be supported).
+ // format_first_only and format_no_copy.
//
// - If backslash doesn't start any of the listed sequences then it is
// silently dropped and the following character is copied as is.
diff --git a/libbutl/regex.txx b/libbutl/regex.txx
index cb8cfe0..9c6342b 100644
--- a/libbutl/regex.txx
+++ b/libbutl/regex.txx
@@ -20,8 +20,8 @@ namespace butl
using str_it = typename string_type::const_iterator;
using regex_it = regex_iterator<str_it>;
- bool first_only ((flags & std::regex_constants::format_first_only) ==
- std::regex_constants::format_first_only);
+ bool first_only ((flags & std::regex_constants::format_first_only) != 0);
+ bool no_copy ((flags & std::regex_constants::format_no_copy) != 0);
locale cl; // Copy of the global C++ locale.
string_type r;
@@ -41,11 +41,19 @@ namespace butl
// Copy the preceeding unmatched substring, save the beginning of the
// one that follows.
//
- r.append (ub, m.prefix ().second);
- ub = m.suffix ().first;
+ if (!no_copy)
+ {
+ r.append (ub, m.prefix ().second);
+ ub = m.suffix ().first;
+ }
if (first_only && i != b)
- r.append (m[0].first, m[0].second); // Append matched substring.
+ {
+ // Append matched substring.
+ //
+ if (!no_copy)
+ r.append (m[0].first, m[0].second);
+ }
else
{
// The standard implementation calls m.format() here. We perform our
@@ -212,7 +220,11 @@ namespace butl
}
}
- r.append (ub, s.end ()); // Append the rightmost non-matched substring.
+ // Append the rightmost non-matched substring.
+ //
+ if (!no_copy)
+ r.append (ub, s.end ());
+
return make_pair (move (r), match);
}
}