aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-path.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-path.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-path.cxx')
-rw-r--r--libbuild2/functions-path.cxx43
1 files changed, 41 insertions, 2 deletions
diff --git a/libbuild2/functions-path.cxx b/libbuild2/functions-path.cxx
index b79585d..0c9b57f 100644
--- a/libbuild2/functions-path.cxx
+++ b/libbuild2/functions-path.cxx
@@ -541,8 +541,8 @@ namespace build2
// $sort(<paths> [, <flags>])
// $sort(<dir_paths> [, <flags>])
//
- // Sort paths in ascending order. Note that on case-insensitive filesystem
- // the order is case-insensitive.
+ // Sort paths in ascending order. Note that on hosts with a case-
+ // insensitive filesystem the order is case-insensitive.
//
// The following flags are supported:
//
@@ -568,6 +568,45 @@ namespace build2
return v;
};
+ // $find(<paths>, <path>)
+ // $find(<dir_paths>, <dir_path>)
+ //
+ // Return true if the path sequence contains the specified path. Note that
+ // on hosts with a case-insensitive filesystem the comparison is
+ // case-insensitive.
+ //
+ f["find"] += [](paths vs, value v)
+ {
+ return find (vs.begin (), vs.end (),
+ convert<path> (move (v))) != vs.end ();
+ };
+
+ f["find"] += [](dir_paths vs, value v)
+ {
+ return find (vs.begin (), vs.end (),
+ convert<dir_path> (move (v))) != vs.end ();
+ };
+
+ // $find_index(<paths>, <path>)
+ // $find_index(<dir_paths>, <dir_path>)
+ //
+ // Return the index of the first element in the path sequence that is
+ // equal to the specified path or $size(<paths>) if none is found. Note
+ // that on hosts with a case-insensitive filesystem the comparison is
+ // case-insensitive.
+ //
+ f["find_index"] += [](paths vs, value v)
+ {
+ auto i (find (vs.begin (), vs.end (), convert<path> (move (v))));
+ return i != vs.end () ? i - vs.begin () : vs.size ();
+ };
+
+ f["find_index"] += [](dir_paths vs, value v)
+ {
+ auto i (find (vs.begin (), vs.end (), convert<dir_path> (move (v))));
+ return i != vs.end () ? i - vs.begin () : vs.size ();
+ };
+
// $path.match(<val>, <pat> [, <start>])
//
// Match a filesystem entry name against a name pattern (both are strings),