aboutsummaryrefslogtreecommitdiff
path: root/libbuild2
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-07-18 12:34:52 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-07-18 12:54:55 +0300
commit47d5b304235c5b1f409b01cab95a2191eac5a230 (patch)
treedc82e4971e7c0124a5953b6ab75ec017c6124c83 /libbuild2
parentef81695c5fa356529bac6f8aa7d9bfe1f1c84473 (diff)
Add $regex.find_match() and $regex.find_search() functions
Diffstat (limited to 'libbuild2')
-rw-r--r--libbuild2/functions-regex.cxx98
1 files changed, 98 insertions, 0 deletions
diff --git a/libbuild2/functions-regex.cxx b/libbuild2/functions-regex.cxx
index 10067a4..a59d808 100644
--- a/libbuild2/functions-regex.cxx
+++ b/libbuild2/functions-regex.cxx
@@ -397,6 +397,63 @@ namespace build2
return r;
}
+ static regex::flag_type
+ parse_find_flags (optional<names>&& flags)
+ {
+ regex::flag_type r (regex::ECMAScript);
+
+ if (flags)
+ {
+ for (auto& f: *flags)
+ {
+ string s (convert<string> (move (f)));
+
+ if (s == "icase")
+ r |= regex::icase;
+ else
+ throw invalid_argument ("invalid flag '" + s + "'");
+ }
+ }
+
+ return r;
+ }
+
+ // Return true if any of the list elements match the regular expression.
+ // See find_match() overloads (below) for details.
+ //
+ static bool
+ find_match (names&& s, const string& re, optional<names>&& flags)
+ {
+ regex::flag_type fl (parse_find_flags (move (flags)));
+ regex rge (parse_regex (re, fl));
+
+ for (auto& v: s)
+ {
+ if (regex_match (convert<string> (move (v)), rge))
+ return true;
+ }
+
+ return false;
+ }
+
+ // Return true if a part of any of the list elements matches the regular
+ // expression. See find_search() overloads (below) for details.
+ //
+ static bool
+ find_search (names&& s, const string& re, optional<names>&& flags)
+ {
+ regex::flag_type fl (parse_find_flags (move (flags)));
+ regex rge (parse_regex (re, fl));
+
+ for (auto& v: s)
+ {
+ if (regex_search (convert<string> (move (v)), rge))
+ return true;
+ }
+
+ return false;
+ }
+
// Replace matched parts of list elements using the format string and
// concatenate the transformed elements. See merge() overloads (below) for
// details.
@@ -474,6 +531,25 @@ namespace build2
return match (move (s), convert<string> (move (re)), move (flags));
};
+ // $regex.find_match(<vals>, <pat> [, <flags>])
+ //
+ // Match list elements against the regular expression and return true if
+ // the match is found. Convert the elements to string prior to matching.
+ //
+ // The following flags are supported:
+ //
+ // icase - match ignoring case
+ //
+ f[".find_match"] = [](names s, string re, optional<names> flags)
+ {
+ return find_match (move (s), re, move (flags));
+ };
+
+ f[".find_match"] = [](names s, names re, optional<names> flags)
+ {
+ return find_match (move (s), convert<string> (move (re)), move (flags));
+ };
+
// $regex.search(<val>, <pat> [, <flags>])
//
// Determine if there is a match between the regular expression and some
@@ -507,6 +583,28 @@ namespace build2
return search (move (s), convert<string> (move (re)), move (flags));
};
+ // $regex.find_search(<vals>, <pat> [, <flags>])
+ //
+ // Determine if there is a match between the regular expression and some
+ // part of any of the list elements. Convert the elements to string prior
+ // to matching.
+ //
+ // The following flags are supported:
+ //
+ // icase - match ignoring case
+ //
+ f[".find_search"] = [](names s, string re, optional<names> flags)
+ {
+ return find_search (move (s), re, move (flags));
+ };
+
+ f[".find_search"] = [](names s, names re, optional<names> flags)
+ {
+ return find_search (move (s),
+ convert<string> (move (re)),
+ move (flags));
+ };
+
// $regex.replace(<val>, <pat>, <fmt> [, <flags>])
//
// Replace matched parts in a value of an arbitrary type, using the format