diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2019-09-30 18:10:09 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2019-10-01 12:15:10 +0300 |
commit | c478a365d8479ca25f6a72d42ecd45dcb9e9569a (patch) | |
tree | a4993602fe16877bee0395acd00991cfeebf9c26 | |
parent | 0d3248f51515d92d5229cd5e6ef30f1f963d7a5f (diff) |
Make $regex.{match,search}() to return NULL for no match if return_match or return_match flag is specified
-rw-r--r-- | libbuild2/functions-regex.cxx | 30 | ||||
-rw-r--r-- | tests/function/regex/testscript | 8 |
2 files changed, 23 insertions, 15 deletions
diff --git a/libbuild2/functions-regex.cxx b/libbuild2/functions-regex.cxx index 7d0ca20..b0472ed 100644 --- a/libbuild2/functions-regex.cxx +++ b/libbuild2/functions-regex.cxx @@ -85,21 +85,23 @@ namespace build2 if (!subs) return value (regex_match (s, rge)); // Return boolean value. - names r; match_results<string::const_iterator> m; if (regex_match (s, m, rge)) { assert (!m.empty ()); + names r; for (size_t i (1); i != m.size (); ++i) { if (m[i].matched) r.emplace_back (m.str (i)); } - } - return value (move (r)); + return value (move (r)); + } + else + return value (); } // Determine if there is a match between the regular expression and some @@ -143,13 +145,14 @@ namespace build2 if (!match && !subs) return value (regex_search (s, rge)); // Return boolean value. - names r; match_results<string::const_iterator> m; if (regex_search (s, m, rge)) { assert (!m.empty ()); + names r; + if (match) { assert (m[0].matched); @@ -164,9 +167,11 @@ namespace build2 r.emplace_back (m.str (i)); } } - } - return value (move (r)); + return value (move (r)); + } + else + return value (); } static pair<regex::flag_type, regex_constants::match_flag_type> @@ -348,14 +353,15 @@ namespace build2 // Match a value of an arbitrary type against the regular expression. // Convert the value to string prior to matching. Return the boolean value // unless return_subs flag is specified (see below), in which case return - // names (empty if no match). + // names (NULL if no match). // // The following flags are supported: // // icase - match ignoring case // // return_subs - return names (rather than boolean), that contain - // sub-strings that match the marked sub-expressions + // sub-strings that match the marked sub-expressions and + // NULL if no match // f[".match"] = [](value s, string re, optional<names> flags) { @@ -373,17 +379,19 @@ namespace build2 // part of a value of an arbitrary type. Convert the value to string prior // to searching. Return the boolean value unless return_match or // return_subs flag is specified (see below) in which case return names - // (empty if no match @@ Why not NULL?). + // (NULL if no match). // // The following flags are supported: // // icase - match ignoring case // // return_match - return names (rather than boolean), that contain a - // sub-string that matches the whole regular expression + // sub-string that matches the whole regular expression and + // NULL if no match // // return_subs - return names (rather than boolean), that contain - // sub-strings that match the marked sub-expressions + // sub-strings that match the marked sub-expressions and + // NULL if no match // // If both return_match and return_subs flags are specified then the // sub-string that matches the whole regular expression comes first. diff --git a/tests/function/regex/testscript b/tests/function/regex/testscript index 977d7b3..1fdb383 100644 --- a/tests/function/regex/testscript +++ b/tests/function/regex/testscript @@ -167,7 +167,7 @@ : failure : - $* <<EOI >'' + $* <<EOI >'[null]' print $regex.match(" bar", '([^\s]+)\s+([^\s]+)', return_subs) EOI } @@ -225,8 +225,8 @@ : failure : - $* <<EOI >'' - print $regex.match(" bar", '([^\s]+)\s+([^\s]+)', return_subs) + $* <<EOI >'[null]' + print $regex.search(" bar", '([^\s]+)\s+([^\s]+)', return_subs) EOI } @@ -247,7 +247,7 @@ : failure : - $* <<EOI >'' + $* <<EOI >'[null]' print $regex.search(" bar", '([^\s]+)\s+([^\s]+)', return_match) EOI } |