From f500b3274b4c937d315a652aad3bfcdd808b49ec Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 2 Nov 2021 15:18:05 +0200 Subject: Add $sort() function Available overloads: $sort( [, ]) $sort( [, ]) $sort( [, ]) $sort( [, ]) $sort( [, ]) The following flag is supported by the all overloads: dedup - in addition to sorting also remove duplicates Additionally, the strings overload also support the following flag: icase - sort ignoring case Note that on case-insensitive filesystem the paths and dir_paths overload's order is case-insensitive. --- libbuild2/functions-path.cxx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libbuild2/functions-path.cxx') diff --git a/libbuild2/functions-path.cxx b/libbuild2/functions-path.cxx index b7c9a8d..d6ff83f 100644 --- a/libbuild2/functions-path.cxx +++ b/libbuild2/functions-path.cxx @@ -10,6 +10,9 @@ using namespace std; namespace build2 { + extern bool + functions_sort_flags (optional); // functions-builtin.cxx + static value path_thunk (const scope* base, vector_view args, @@ -411,6 +414,36 @@ namespace build2 return extension (convert (move (ns))); }; + // $sort( [, ]) + // $sort( [, ]) + // + // Sort paths in ascending order. Note that on case-insensitive filesystem + // the order is case-insensitive. + // + // The following flags are supported: + // + // dedup - in addition to sorting also remove duplicates + // + f["sort"] += [](paths v, optional fs) + { + sort (v.begin (), v.end ()); + + if (functions_sort_flags (move (fs))) + v.erase (unique (v.begin(), v.end()), v.end ()); + + return v; + }; + + f["sort"] += [](dir_paths v, optional fs) + { + sort (v.begin (), v.end ()); + + if (functions_sort_flags (move (fs))) + v.erase (unique (v.begin(), v.end()), v.end ()); + + return v; + }; + // $path.match(, [, ]) // // Match a filesystem entry name against a name pattern (both are strings), -- cgit v1.1