|
1 | 1 | #include <xrpl/basics/Number.h> |
| 2 | +// |
2 | 3 | #include <xrpl/beast/utility/instrumentation.h> |
3 | 4 |
|
4 | 5 | #include <algorithm> |
@@ -26,7 +27,7 @@ namespace std { |
26 | 27 | template <> |
27 | 28 | struct make_unsigned<ripple::numberint128> |
28 | 29 | { |
29 | | - using type = typename uint128_t; |
| 30 | + using type = uint128_t; |
30 | 31 | }; |
31 | 32 |
|
32 | 33 | } // namespace std |
@@ -585,35 +586,41 @@ to_string(Number const& amount) |
585 | 586 | return "0"; |
586 | 587 |
|
587 | 588 | 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 | + }(); |
589 | 603 |
|
590 | 604 | // Use scientific notation for exponents that are too small or too large |
591 | 605 | auto const rangeLog = Number::mantissaLog(); |
592 | 606 | if (((exponent != 0) && |
593 | 607 | ((exponent < -(rangeLog + 10)) || (exponent > -(rangeLog - 10))))) |
594 | 608 | { |
595 | | - std::string ret = to_string(mantissa); |
| 609 | + std::string ret = negative ? "-" : ""; |
| 610 | + ret.append(std::to_string(mantissa)); |
596 | 611 | ret.append(1, 'e'); |
597 | 612 | ret.append(std::to_string(exponent)); |
598 | 613 | return ret; |
599 | 614 | } |
600 | 615 |
|
601 | | - bool negative = false; |
602 | | - |
603 | | - if (mantissa < 0) |
604 | | - { |
605 | | - mantissa = -mantissa; |
606 | | - negative = true; |
607 | | - } |
608 | | - |
609 | 616 | // TODO: These numbers are probably wrong for largeRange |
610 | 617 | XRPL_ASSERT( |
611 | 618 | exponent + 43 > 0, "ripple::to_string(Number) : minimum exponent"); |
612 | 619 |
|
613 | 620 | ptrdiff_t const pad_prefix = Number::mantissaLog() + 12; |
614 | 621 | ptrdiff_t const pad_suffix = Number::mantissaLog() + 8; |
615 | 622 |
|
616 | | - std::string const raw_value(to_string(mantissa)); |
| 623 | + std::string const raw_value(std::to_string(mantissa)); |
617 | 624 | std::string val; |
618 | 625 |
|
619 | 626 | val.reserve(raw_value.length() + pad_prefix + pad_suffix); |
|
0 commit comments