Completely rip out Boost's Spirit / Karma for casting.
This rips out the Bost Spirit / Karma conversion code, using the stdlib and lightweight alternatives instead. The main benefit is an immense decrease in compilation times, for every translation unit that requires the `util/cast.hpp` header. Note: compared to the version before, there is a minor change in behavior: the double `-0` was printed as `0` before and is now printed as `-0`. This comes from the IEE754 standard, specifying signed zeros, that is `+0` and `-0`. Interesting for us: JavaScript uses IEE754, resulting in no breakage if used in arithmetic. Small test case, left hand side was before, right hand side is now: $ ./a.out -1.123457 vs -1.123457 -1 vs -1 -1.3 vs -1.3 0 vs -0 0 vs 0 0 vs 0 1.3 vs 1.3 1.123457 vs 1.123457 References: - https://en.wikipedia.org/wiki/Signed_zero - http://www.boost.org/doc/libs/1_59_0/doc/html/boost/algorithm/trim_right_if.html - http://www.boost.org/doc/libs/1_59_0/doc/html/boost/algorithm/is_any_of.html
This commit is contained in:
parent
f9f0ffb64d
commit
5a257416ca
@ -33,6 +33,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <iomanip>
|
||||
#include <type_traits>
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
|
||||
namespace cast
|
||||
{
|
||||
template <typename Enumeration>
|
||||
@ -47,8 +50,19 @@ template <typename T, int Precision = 6> inline std::string to_string_with_preci
|
||||
static_assert(std::is_arithmetic<T>::value, "integral or floating point type required");
|
||||
|
||||
std::ostringstream out;
|
||||
out << std::setprecision(Precision) << x;
|
||||
return out.str();
|
||||
out << std::fixed << std::setprecision(Precision) << x;
|
||||
auto rv = out.str();
|
||||
|
||||
// Javascript has no separation of float / int, digits without a '.' are integral typed
|
||||
// X.Y.0 -> X.Y
|
||||
// X.0 -> X
|
||||
boost::trim_right_if(rv, boost::is_any_of("0"));
|
||||
boost::trim_right_if(rv, boost::is_any_of("."));
|
||||
// Note:
|
||||
// - assumes the locale to use '.' as digit separator
|
||||
// - this is not identical to: trim_right_if(rv, is_any_of('0 .'))
|
||||
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user