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-builtin.cxx | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'libbuild2/functions-builtin.cxx') diff --git a/libbuild2/functions-builtin.cxx b/libbuild2/functions-builtin.cxx index 7000f16..de79538 100644 --- a/libbuild2/functions-builtin.cxx +++ b/libbuild2/functions-builtin.cxx @@ -219,6 +219,39 @@ namespace build2 return v; }; + // $find(, ) + // + // Return true if the integer sequence contains the specified integer. + // + f["find"] += [](int64s vs, value v) + { + return find (vs.begin (), vs.end (), + convert (move (v))) != vs.end (); + }; + + f["find"] += [](uint64s vs, value v) + { + return find (vs.begin (), vs.end (), + convert (move (v))) != vs.end (); + }; + + // $find_index(, ) + // + // Return the index of the first element in the integer sequence that is + // equal to the specified integer or $size() if none is found. + // + f["find_index"] += [](int64s 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"] += [](uint64s vs, value v) + { + auto i (find (vs.begin (), vs.end (), convert (move (v)))); + return i != vs.end () ? i - vs.begin () : vs.size (); + }; + // getenv // // Return NULL if the environment variable is not set, untyped value -- cgit v1.1