aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-name.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2021-11-03 13:08:48 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2021-11-04 08:09:42 +0200
commit10e634e64b5fffaf19778d8fd3aa854118aa0d53 (patch)
tree99d09d1ae6a7024b7dbc6c436bf1bc780640fe16 /libbuild2/functions-name.cxx
parente612a49ef95cf9ce3d0b5496d724f73cae9aa333 (diff)
Add $size() function to get size of sequence (names, strings, etc)
Diffstat (limited to 'libbuild2/functions-name.cxx')
-rw-r--r--libbuild2/functions-name.cxx41
1 files changed, 41 insertions, 0 deletions
diff --git a/libbuild2/functions-name.cxx b/libbuild2/functions-name.cxx
index 9ba20a8..800c377 100644
--- a/libbuild2/functions-name.cxx
+++ b/libbuild2/functions-name.cxx
@@ -10,6 +10,9 @@ using namespace std;
namespace build2
{
+ extern bool
+ functions_sort_flags (optional<names>); // functions-builtin.cxx
+
// Convert name to target'ish name (see below for the 'ish part). Return
// raw/unprocessed data in case this is an unknown target type (or called
// out of scope). See scope::find_target_type() for details. Allow out-
@@ -174,6 +177,44 @@ namespace build2
return to_target_name (s, move (n), o).first.proj;
};
+ // $size(<names>)
+ //
+ // Return the number of elements in the sequence.
+ //
+ fn["size"] += [] (names ns)
+ {
+ size_t n (0);
+
+ for (auto i (ns.begin ()); i != ns.end (); ++i)
+ {
+ ++n;
+ if (i->pair && !(++i)->directory ())
+ fail << "name pair in names";
+ }
+
+ return n;
+ };
+
+ // $sort(<names> [, <flags>])
+ //
+ // Sort names in ascending order.
+ //
+ // The following flags are supported:
+ //
+ // dedup - in addition to sorting also remove duplicates
+ //
+ fn["sort"] += [] (names ns, optional<names> fs)
+ {
+ //@@ TODO: shouldn't we do this in a pair-aware manner?
+
+ sort (ns.begin (), ns.end ());
+
+ if (functions_sort_flags (move (fs)))
+ ns.erase (unique (ns.begin(), ns.end()), ns.end ());
+
+ return ns;
+ };
+
// Functions that can be called only on real targets.
//
function_family ft (m, "target");