From 3fe4d26dbf3493f4641f9326344adbba0d6a9e84 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Tue, 29 Sep 2015 15:25:00 +0200 Subject: [PATCH] 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 --- util/cast.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/util/cast.hpp b/util/cast.hpp index 538f8925bbe..a9e97d6ecb4 100644 --- a/util/cast.hpp +++ b/util/cast.hpp @@ -33,6 +33,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include +#include + namespace cast { template @@ -47,8 +50,19 @@ template inline std::string to_string_with_preci static_assert(std::is_arithmetic::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; } }