aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-string.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2022-09-29 13:12:19 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2022-09-29 14:14:51 +0200
commit785087aa43cf962855724b8fa5da393022204a14 (patch)
tree4a24ca94679d72cf160d2ef528bba2c8dac45cb4 /libbuild2/functions-string.cxx
parentd7cb460833e6dde3e3b958b993eee3eee4ae3bf0 (diff)
Add $find(<sequence>, <value>), $find_index(<sequence>, <value>) functions
The $find() function returns true if the sequence contains the specified value. The $find_index() function returns the index of the first element in the sequence that is equal to the specified value or $size(<sequence>) if none is found. For string sequences, it's possible to request case- insensitive comparison with a flag.
Diffstat (limited to 'libbuild2/functions-string.cxx')
-rw-r--r--libbuild2/functions-string.cxx56
1 files changed, 56 insertions, 0 deletions
diff --git a/libbuild2/functions-string.cxx b/libbuild2/functions-string.cxx
index 4a03a5e..a41449a 100644
--- a/libbuild2/functions-string.cxx
+++ b/libbuild2/functions-string.cxx
@@ -8,6 +8,32 @@ using namespace std;
namespace build2
{
+ static size_t
+ find_index (const strings& vs, value&& v, optional<names>&& fs)
+ {
+ bool ic (false);
+ if (fs)
+ {
+ for (name& f: *fs)
+ {
+ string s (convert<string> (move (f)));
+
+ if (s == "icase")
+ ic = true;
+ else
+ throw invalid_argument ("invalid flag '" + s + "'");
+ }
+ }
+
+ auto i (find_if (vs.begin (), vs.end (),
+ [ic, y = convert<string> (move (v))] (const string& x)
+ {
+ return (ic ? icasecmp (x, y) : x.compare (y)) == 0;
+ }));
+
+ return i != vs.end () ? i - vs.begin () : vs.size ();
+ };
+
void
string_functions (function_map& m)
{
@@ -135,6 +161,36 @@ namespace build2
return v;
};
+ // $find(<strings>, <string>[, <flags>])
+ //
+ // Return true if the string sequence contains the specified string.
+ //
+ // The following flags are supported:
+ //
+ // icase - compare ignoring case
+ //
+ // See also $regex.find_{match,search}().
+ //
+ f["find"] += [](strings vs, value v, optional<names> fs)
+ {
+ return find_index (vs, move (v), move (fs)) != vs.size ();
+ };
+
+ // $find_index(<strings>, <string>[, <flags>])
+ //
+ // Return the index of the first element in the string sequence that
+ // is equal to the specified string or $size(<strings>) if none is
+ // found.
+ //
+ // The following flags are supported:
+ //
+ // icase - compare ignoring case
+ //
+ f["find_index"] += [](strings vs, value v, optional<names> fs)
+ {
+ return find_index (vs, move (v), move (fs));
+ };
+
// String-specific overloads from builtins.
//
function_family b (m, "builtin");