From 2b281d5ec758d3bddbf27c4098f0767e2471227a Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 3 Apr 2020 17:36:45 +0300 Subject: Skip unmatched lines in $regex.replace_lines() if format_no_copy flag is specified --- libbuild2/functions-regex.cxx | 8 +++++--- tests/function/regex/testscript | 20 ++++++++++++++++++++ 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 + : + $* <'foo.hxx' + v = "foo.cxx + bar.txt" + + print $regex.replace_lines($v, '(.*)\.cxx', '\1.hxx', format_no_copy) + EOI + : null-fmt : $* <'bar.txt' @@ -173,6 +182,17 @@ bar.txt EOO + : no-copy + : + $* <'bar.hxx' + v = "foo.cxx + bar.txt" + + print $regex.replace_lines($v, \ + '(.*)\.txt', '\1.hxx', \ + format_no_copy return_lines) + EOI + : null-fmt : $* <'bar.txt' -- cgit v1.1