From c478a365d8479ca25f6a72d42ecd45dcb9e9569a Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 30 Sep 2019 18:10:09 +0300 Subject: Make $regex.{match,search}() to return NULL for no match if return_match or return_match flag is specified --- libbuild2/functions-regex.cxx | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'libbuild2/functions-regex.cxx') 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 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 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 @@ -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 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. -- cgit v1.1