From 7b9c113d52bbcecf45c9407e4ee30ee559418cb2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 22 Sep 2022 11:30:34 +0200 Subject: Add support for hex notation for uint64 type Specifically, now we can do: x = [uint64] 0x0000ffff cxx.poptions += "-DOFFSET=$x" # -DOFFSET=65535 cxx.poptions += "-DOFFSET=$string($x, 16)" # -DOFFSET=0xffff cxx.poptions += "-DOFFSET=$string($x, 16, 8)" # -DOFFSET=0x0000ffff Note that there is no hex notation support for the int64 (signed) type. --- libbuild2/variable.cxx | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'libbuild2/variable.cxx') diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 4bd01dc..9cdad0b 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -515,13 +515,22 @@ namespace build2 { try { - // May throw invalid_argument or out_of_range. - // - size_t i; - int64_t r (stoll (n.value, &i)); + const string& v (n.value); + + if (!wspace (v[0])) + { + // Note that unlike uint64, we don't support hex notation for int64. + + // May throw invalid_argument or out_of_range. + // + size_t i; + int64_t r (stoll (v, &i)); + + if (i == v.size ()) + return r; - if (i == n.value.size ()) - return r; + // Fall through. + } // Fall through. } @@ -563,13 +572,22 @@ namespace build2 { try { - // May throw invalid_argument or out_of_range. - // - size_t i; - uint64_t r (stoull (n.value, &i)); + const string& v (n.value); + + if (!wspace (v[0])) + { + int b (v[0] == '0' && (v[1] == 'x' || v[1] == 'X') ? 16 : 10); + + // May throw invalid_argument or out_of_range. + // + size_t i; + uint64_t r (stoull (v, &i, b)); + + if (i == v.size ()) + return r; - if (i == n.value.size ()) - return r; + // Fall through. + } // Fall through. } -- cgit v1.1