aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2022-09-22 13:10:08 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2022-09-22 13:10:08 +0200
commit7d137fd6a9ceb54574481082e9944de168b06b78 (patch)
tree973e78cd6cf64ca53b50f5dbc026449e945cdaeb
parent7b9c113d52bbcecf45c9407e4ee30ee559418cb2 (diff)
Add $integer_sequence(<begin>, <end>[, <step>]) function
It returns the list of uint64 integers starting from <begin> (including) to <end> (excluding) with the specified <step> or 1 if unspecified. For example: hdr = foo.hxx bar.hxx baz.hxx src = foo.cxx bar.cxx baz.cxx assert ($size($hdr) == $size($src)) "hdr and src expected to be parallel" for i: $integer_sequence(0, $size($hdr)) { h = ($hdr[$i]) s = ($src[$i]) ... }
-rw-r--r--libbuild2/functions-builtin.cxx31
-rw-r--r--tests/function/builtin/testscript8
2 files changed, 39 insertions, 0 deletions
diff --git a/libbuild2/functions-builtin.cxx b/libbuild2/functions-builtin.cxx
index 5129a05..7000f16 100644
--- a/libbuild2/functions-builtin.cxx
+++ b/libbuild2/functions-builtin.cxx
@@ -123,6 +123,37 @@ namespace build2
f["identity"] += [](value* v) {return move (*v);};
+ // $integer_sequence(<begin>, <end>[, <step>])
+ //
+ // Return the list of uint64 integers starting from <begin> (including) to
+ // <end> (excluding) with the specified <step> or 1 if unspecified. If
+ // <begin> is greater than <end>, empty list is returned.
+ //
+ // Note that currently negative numbers are not supported but this could
+ // be handled if required (e.g., by returning int64s in this case).
+ //
+ // Note also that we could improve this by adding a shortcut to get the
+ // indexes of a list (for example, $indexes(<list>) plus potentially a
+ // similar $keys() function for maps).
+ //
+ f["integer_sequence"] += [](value begin, value end, optional<value> step)
+ {
+ uint64_t b (convert<uint64_t> (move (begin)));
+ uint64_t e (convert<uint64_t> (move (end)));
+ uint64_t s (step ? convert<uint64_t> (move (*step)) : 1);
+
+ uint64s r;
+ if (b < e)
+ {
+ r.reserve (static_cast<size_t> ((e - b) / s + 1));
+
+ for (; b < e; b += s)
+ r.push_back (static_cast<size_t> (b));
+ }
+
+ return r;
+ };
+
// string
//
f["string"] += [](bool b) {return b ? "true" : "false";};
diff --git a/tests/function/builtin/testscript b/tests/function/builtin/testscript
index 02c73ee..bbfd4e5 100644
--- a/tests/function/builtin/testscript
+++ b/tests/function/builtin/testscript
@@ -77,6 +77,14 @@
$* <'print $type($identity(abc))' >'' : untyped
}
+: integer-sequence
+:
+{
+ $* <'print $integer_sequence(1, 3)' >'1 2' : basics
+ $* <'print $integer_sequence(1, 0)' >'' : empty
+ $* <'print $integer_sequence(0, 8, 2)' >'0 2 4 6' : step
+}
+
: string
:
{