5#ifndef RSMESH_ROUNDTRIP_STRING_H
6#define RSMESH_ROUNDTRIP_STRING_H
8#include "double-conversion/double-conversion.h"
21 template <
class Floating>
22 void to_string(Floating value, double_conversion::StringBuilder* builder);
25 inline void to_string(
double value, double_conversion::StringBuilder* builder) {
26 double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToShortest(value, builder);
30 inline void to_string(
float value, double_conversion::StringBuilder* builder) {
31 double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToShortestSingle(value, builder);
35 inline double to_double(
const std::string& str) {
36 double_conversion::StringToDoubleConverter converter(
37 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK |
38 double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY,
39 std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
"inf",
41 int processed_chars = 0;
42 return converter.StringToDouble(str.c_str(),
static_cast<int>(str.size()), &processed_chars);
45 inline float to_float(
const std::string& str) {
46 double_conversion::StringToDoubleConverter converter(
47 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK |
48 double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSITIVITY,
49 std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN(),
"inf",
51 int processed_chars = 0;
52 return converter.StringToFloat(str.c_str(),
static_cast<int>(str.size()), &processed_chars);
55 template <
class Floating,
56 std::enable_if_t<std::is_floating_point<Floating>::value, std::nullptr_t> =
nullptr>
57 std::string to_string(Floating value) {
58 static constexpr std::size_t buffer_size = 32;
60 std::array<char, buffer_size> buffer{};
61 double_conversion::StringBuilder builder(buffer.data(), buffer_size);
63 detail::to_string(value, &builder);
65 return builder.Finalize();