aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-04-03 17:36:45 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-04-03 17:36:45 +0300
commit2b281d5ec758d3bddbf27c4098f0767e2471227a (patch)
tree78eda9c7da62445bda57bfede5bf14d52cf56849
parent0fb42229a5bf13170667701ad6cb468d58348007 (diff)
Skip unmatched lines in $regex.replace_lines() if format_no_copy flag is specified
-rw-r--r--libbuild2/functions-regex.cxx8
-rw-r--r--tests/function/regex/testscript20
2 files changed, 25 insertions, 3 deletions
diff --git a/libbuild2/functions-regex.cxx b/libbuild2/functions-regex.cxx
index 6a8c83a..10067a4 100644
--- a/libbuild2/functions-regex.cxx
+++ b/libbuild2/functions-regex.cxx
@@ -275,6 +275,7 @@ namespace build2
is.exceptions (istringstream::badbit);
const string& efmt (fmt ? *fmt : "");
+ bool no_copy ((fl.second & regex_constants::format_no_copy) != 0);
for (string l; !eof (getline (is, l)); )
{
@@ -282,9 +283,10 @@ namespace build2
string& s (rr.first);
// Skip the empty replacement for a matched line if the format is
- // absent.
+ // absent and an unmatched line if the format_no_copy flag is
+ // specified.
//
- if (!fmt && rr.second && s.empty ())
+ if (rr.second ? !fmt && s.empty () : no_copy)
continue;
if (!rls)
@@ -553,7 +555,7 @@ namespace build2
//
// return_lines - return the simple name (rather than a name list)
// containing the unmatched lines and line replacements
- // seperated with newlines.
+ // separated with newlines.
//
// Note that if format_no_copy is specified, unmatched lines are not
// copied either.
diff --git a/tests/function/regex/testscript b/tests/function/regex/testscript
index fc7fb92..95dfbb8 100644
--- a/tests/function/regex/testscript
+++ b/tests/function/regex/testscript
@@ -146,6 +146,15 @@
print $regex.replace_lines($v, '(.*)\.cxx', '\1.hxx')
EOI
+ : no-copy
+ :
+ $* <<EOI >'foo.hxx'
+ v = "foo.cxx
+ bar.txt"
+
+ print $regex.replace_lines($v, '(.*)\.cxx', '\1.hxx', format_no_copy)
+ EOI
+
: null-fmt
:
$* <<EOI >'bar.txt'
@@ -173,6 +182,17 @@
bar.txt
EOO
+ : no-copy
+ :
+ $* <<EOI >'bar.hxx'
+ v = "foo.cxx
+ bar.txt"
+
+ print $regex.replace_lines($v, \
+ '(.*)\.txt', '\1.hxx', \
+ format_no_copy return_lines)
+ EOI
+
: null-fmt
:
$* <<EOI >'bar.txt'