Support to_string for IOUAmount and XRPAmount

This commit is contained in:
Nik Bougalis
2015-08-22 08:12:27 -07:00
parent acd03faee5
commit 324c42ae09
4 changed files with 126 additions and 0 deletions

View File

@@ -24,6 +24,7 @@
#include <beast/utility/Zero.h>
#include <boost/operators.hpp>
#include <cstdint>
#include <string>
#include <utility>
using beast::zero;
@@ -137,6 +138,9 @@ public:
}
};
std::string
to_string (IOUAmount const& amount);
}
#endif

View File

@@ -25,6 +25,7 @@
#include <beast/utility/Zero.h>
#include <boost/operators.hpp>
#include <cstdint>
#include <string>
#include <beast/cxx14/type_traits.h>
using beast::zero;
@@ -127,6 +128,13 @@ public:
}
};
inline
std::string
to_string (XRPAmount const& amount)
{
return std::to_string (amount.drops ());
}
/** Returns true if the amount does not exceed the initial XRP in existence. */
inline
bool isLegalAmount (XRPAmount const& amount)

View File

@@ -19,6 +19,8 @@
#include <BeastConfig.h>
#include <ripple/protocol/IOUAmount.h>
#include <algorithm>
#include <beast/cxx14/iterator.h>
#include <stdexcept>
namespace ripple {
@@ -145,4 +147,100 @@ IOUAmount::operator<(IOUAmount const& other) const
return mantissa_ < other.mantissa_;
}
std::string
to_string (IOUAmount const& amount)
{
// keep full internal accuracy, but make more human friendly if possible
if (amount == zero)
return "0";
int const exponent = amount.exponent ();
auto mantissa = amount.mantissa ();
// Use scientific notation for exponents that are too small or too large
if (((exponent != 0) && ((exponent < -25) || (exponent > -5))))
{
std::string ret = std::to_string (mantissa);
ret.append (1, 'e');
ret.append (std::to_string (exponent));
return ret;
}
bool negative = false;
if (mantissa < 0)
{
mantissa = -mantissa;
negative = true;
}
assert (exponent + 43 > 0);
size_t const pad_prefix = 27;
size_t const pad_suffix = 23;
std::string const raw_value (std::to_string (mantissa));
std::string val;
val.reserve (raw_value.length () + pad_prefix + pad_suffix);
val.append (pad_prefix, '0');
val.append (raw_value);
val.append (pad_suffix, '0');
size_t const offset (exponent + 43);
auto pre_from (val.begin ());
auto const pre_to (val.begin () + offset);
auto const post_from (val.begin () + offset);
auto post_to (val.end ());
// Crop leading zeroes. Take advantage of the fact that there's always a
// fixed amount of leading zeroes and skip them.
if (std::distance (pre_from, pre_to) > pad_prefix)
pre_from += pad_prefix;
assert (post_to >= post_from);
pre_from = std::find_if (pre_from, pre_to,
[](char c)
{
return c != '0';
});
// Crop trailing zeroes. Take advantage of the fact that there's always a
// fixed amount of trailing zeroes and skip them.
if (std::distance (post_from, post_to) > pad_suffix)
post_to -= pad_suffix;
assert (post_to >= post_from);
post_to = std::find_if(
std::make_reverse_iterator (post_to),
std::make_reverse_iterator (post_from),
[](char c)
{
return c != '0';
}).base();
std::string ret;
if (negative)
ret.append (1, '-');
// Assemble the output:
if (pre_from == pre_to)
ret.append (1, '0');
else
ret.append(pre_from, pre_to);
if (post_to != post_from)
{
ret.append (1, '.');
ret.append (post_from, post_to);
}
return ret;
}
}

View File

@@ -145,6 +145,21 @@ public:
expect (n != -n);
}
void testToString()
{
testcase("IOU strings");
expect(to_string(IOUAmount (-2, 0)) == "-2");
expect(to_string(IOUAmount (0, 0)) == "0");
expect(to_string(IOUAmount (2, 0)) == "2");
expect(to_string(IOUAmount (25, -3)) == "0.025");
expect(to_string(IOUAmount (-25, -3)) == "-0.025");
expect(to_string(IOUAmount (25, 1)) == "250");
expect(to_string(IOUAmount (-25, 1)) == "-250");
expect(to_string(IOUAmount (2, 20)) == "2000000000000000e5");
expect(to_string(IOUAmount (-2, -20)) == "-2000000000000000e-35");
}
//--------------------------------------------------------------------------
void run ()
@@ -153,6 +168,7 @@ public:
testSigNum ();
testBeastZero ();
testComparisons ();
testToString ();
}
};