Skip to content

Commit 4abb6d9

Browse files
committed
Turns out there's no to_string(__int128_t)
1 parent 7cd48a7 commit 4abb6d9

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/libxrpl/basics/Number.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <xrpl/basics/Number.h>
2+
//
23
#include <xrpl/beast/utility/instrumentation.h>
34

45
#include <algorithm>
@@ -26,7 +27,7 @@ namespace std {
2627
template <>
2728
struct make_unsigned<ripple::numberint128>
2829
{
29-
using type = typename uint128_t;
30+
using type = uint128_t;
3031
};
3132

3233
} // namespace std
@@ -585,35 +586,41 @@ to_string(Number const& amount)
585586
return "0";
586587

587588
auto const exponent = amount.exponent();
588-
auto mantissa = amount.mantissa();
589+
590+
bool const negative = amount.mantissa() < 0;
591+
592+
auto const mantissa = [&]() {
593+
auto mantissa = amount.mantissa();
594+
if (negative)
595+
{
596+
mantissa = -mantissa;
597+
}
598+
XRPL_ASSERT(
599+
mantissa < std::numeric_limits<std::uint64_t>::max(),
600+
"ripple::to_string(Number) : mantissa fits in uin64_t");
601+
return static_cast<std::uint64_t>(mantissa);
602+
}();
589603

590604
// Use scientific notation for exponents that are too small or too large
591605
auto const rangeLog = Number::mantissaLog();
592606
if (((exponent != 0) &&
593607
((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10)))))
594608
{
595-
std::string ret = to_string(mantissa);
609+
std::string ret = negative ? "-" : "";
610+
ret.append(std::to_string(mantissa));
596611
ret.append(1, 'e');
597612
ret.append(std::to_string(exponent));
598613
return ret;
599614
}
600615

601-
bool negative = false;
602-
603-
if (mantissa < 0)
604-
{
605-
mantissa = -mantissa;
606-
negative = true;
607-
}
608-
609616
// TODO: These numbers are probably wrong for largeRange
610617
XRPL_ASSERT(
611618
exponent + 43 > 0, "ripple::to_string(Number) : minimum exponent");
612619

613620
ptrdiff_t const pad_prefix = Number::mantissaLog() + 12;
614621
ptrdiff_t const pad_suffix = Number::mantissaLog() + 8;
615622

616-
std::string const raw_value(to_string(mantissa));
623+
std::string const raw_value(std::to_string(mantissa));
617624
std::string val;
618625

619626
val.reserve(raw_value.length() + pad_prefix + pad_suffix);

0 commit comments

Comments
 (0)