Skip to content

Commit

Permalink
Completely rip out Boost's Spirit / Karma for casting.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniel-j-h committed Sep 29, 2015
1 parent b086323 commit 3fe4d26
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions util/cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 3fe4d26

Please sign in to comment.