From 785087aa43cf962855724b8fa5da393022204a14 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 29 Sep 2022 13:12:19 +0200 Subject: Add $find(, ), $find_index(, ) 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() if none is found. For string sequences, it's possible to request case- insensitive comparison with a flag. --- libbuild2/functions-path.cxx | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'libbuild2/functions-path.cxx') 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( [, ]) // $sort( [, ]) // - // 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(, ) + // $find(, ) + // + // 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 (move (v))) != vs.end (); + }; + + f["find"] += [](dir_paths vs, value v) + { + return find (vs.begin (), vs.end (), + convert (move (v))) != vs.end (); + }; + + // $find_index(, ) + // $find_index(, ) + // + // Return the index of the first element in the path sequence that is + // equal to the specified path or $size() 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 (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 (move (v)))); + return i != vs.end () ? i - vs.begin () : vs.size (); + }; + // $path.match(, [, ]) // // Match a filesystem entry name against a name pattern (both are strings), -- cgit v1.1