aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-integer.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-02-12 05:44:51 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-02-12 05:44:51 +0200
commit7bbe8042dbbea81c713576e1ce69d00bbba5d4b6 (patch)
tree2e0fe544517e6251b18c0c5ba6f57dd74c3a19aa /libbuild2/functions-integer.cxx
parent0249399f06ab77c58e02698a6a0b2352f0dbf1ae (diff)
Move to_string(uint64_t,base,width) to utility, use everywhere
Diffstat (limited to 'libbuild2/functions-integer.cxx')
-rw-r--r--libbuild2/functions-integer.cxx46
1 files changed, 5 insertions, 41 deletions
diff --git a/libbuild2/functions-integer.cxx b/libbuild2/functions-integer.cxx
index 934f753..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