aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-integer.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'libbuild2/functions-integer.cxx')
-rw-r--r--libbuild2/functions-integer.cxx66
1 files changed, 22 insertions, 44 deletions
diff --git a/libbuild2/functions-integer.cxx b/libbuild2/functions-integer.cxx
index ddfc250..8f9e2cf 100644
--- a/libbuild2/functions-integer.cxx
+++ b/libbuild2/functions-integer.cxx
@@ -11,54 +11,18 @@ namespace build2
extern bool
functions_sort_flags (optional<names>); // functions-builtin.cxx
- static const char hex_digits[] = "0123456789abcdef";
-
static string
to_string (uint64_t i, optional<value> base, optional<value> width)
{
- uint64_t b (base ? convert<uint64_t> (move (*base)) : 10);
+ int b (base ?
+ static_cast<int> (convert<uint64_t> (move (*base)))
+ : 10);
+
size_t w (width
? static_cast<size_t> (convert<uint64_t> (move (*width)))
: 0);
- // One day we can switch to C++17 std::to_chars().
- //
- string r;
- switch (b)
- {
- case 10:
- {
- r = to_string (i);
- if (w > r.size ())
- r.insert (0, w - r.size (), '0');
- break;
- }
- case 16:
- {
- r.reserve (18);
- r += "0x";
-
- for (size_t j (64); j != 0; )
- {
- j -= 4;
- size_t d ((i >> j) & 0x0f);
-
- // Omit leading zeros but watch out for the i==0 corner case.
- //
- if (d != 0 || r.size () != 2 || j == 0)
- r += hex_digits[d];
- }
-
- if (w > r.size () - 2)
- r.insert (2, w - (r.size () - 2), '0');
-
- break;
- }
- default:
- throw invalid_argument ("unsupported base");
- }
-
- return r;
+ return (to_string (i, b, w));
}
void
@@ -69,6 +33,19 @@ namespace build2
// $string(<int64>)
// $string(<uint64>[, <base>[, <width>]])
//
+ // Convert an integer to a string. For unsigned integers we can specify
+ // the desired base and width. For example:
+ //
+ // x = [uint64] 0x0000ffff
+ //
+ // c.poptions += "-DOFFSET=$x" # -DOFFSET=65535
+ // c.poptions += "-DOFFSET=$string($x, 16)" # -DOFFSET=0xffff
+ // c.poptions += "-DOFFSET=$string($x, 16, 8)" # -DOFFSET=0x0000ffff
+ //
+
+ // Note that we don't handle NULL values for these type since they have no
+ // empty representation.
+ //
f["string"] += [](int64_t i) {return to_string (i);};
f["string"] += [](uint64_t i, optional<value> base, optional<value> width)
@@ -79,9 +56,10 @@ namespace build2
// $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
+ // <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).
//
@@ -120,7 +98,7 @@ namespace build2
//
// The following flags are supported:
//
- // dedup - in addition to sorting also remove duplicates
+ // dedup - in addition to sorting also remove duplicates
//
f["sort"] += [](int64s v, optional<names> fs)
{
@@ -161,7 +139,7 @@ namespace build2
// $find_index(<ints>, <int>)
//
// Return the index of the first element in the integer sequence that is
- // equal to the specified integer or $size(<ints>) if none is found.
+ // equal to the specified integer or `$size(ints)` if none is found.
//
f["find_index"] += [](int64s vs, value v)
{