2021-05-03 14:00:46 +00:00
|
|
|
#include "LocalesUtils.hpp"
|
|
|
|
|
2021-09-13 18:46:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <charconv>
|
|
|
|
#endif
|
2021-05-10 05:08:38 +00:00
|
|
|
#include <stdexcept>
|
2021-05-03 14:00:46 +00:00
|
|
|
|
2021-09-13 18:46:19 +00:00
|
|
|
#include <fast_float/fast_float.h>
|
|
|
|
|
|
|
|
|
2021-05-03 14:00:46 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
|
|
|
|
CNumericLocalesSetter::CNumericLocalesSetter()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
|
|
|
|
m_orig_numeric_locale = std::setlocale(LC_NUMERIC, nullptr);
|
|
|
|
std::setlocale(LC_NUMERIC, "C");
|
2021-09-13 18:46:19 +00:00
|
|
|
#elif __APPLE__
|
2021-05-03 14:00:46 +00:00
|
|
|
m_original_locale = uselocale((locale_t)0);
|
2021-09-13 18:46:19 +00:00
|
|
|
m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_original_locale);
|
2021-05-03 14:00:46 +00:00
|
|
|
uselocale(m_new_locale);
|
2021-09-13 18:46:19 +00:00
|
|
|
#else // linux / BSD
|
2021-06-04 19:37:49 +00:00
|
|
|
m_original_locale = uselocale((locale_t)0);
|
2021-09-13 18:46:19 +00:00
|
|
|
m_new_locale = duplocale(m_original_locale);
|
|
|
|
m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_new_locale);
|
2021-06-30 08:02:14 +00:00
|
|
|
uselocale(m_new_locale);
|
2021-05-03 14:00:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CNumericLocalesSetter::~CNumericLocalesSetter()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2021-05-10 10:18:30 +00:00
|
|
|
std::setlocale(LC_NUMERIC, m_orig_numeric_locale.data());
|
2021-05-03 14:00:46 +00:00
|
|
|
#else
|
|
|
|
uselocale(m_original_locale);
|
|
|
|
freelocale(m_new_locale);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool is_decimal_separator_point()
|
|
|
|
{
|
|
|
|
char str[5] = "";
|
|
|
|
sprintf(str, "%.1f", 0.5f);
|
|
|
|
return str[1] == '.';
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:08:38 +00:00
|
|
|
|
|
|
|
double string_to_double_decimal_point(const std::string& str, size_t* pos /* = nullptr*/)
|
|
|
|
{
|
|
|
|
double out;
|
2021-09-13 18:46:19 +00:00
|
|
|
size_t p = fast_float::from_chars(str.data(), str.data() + str.size(), out).ptr - str.data();
|
|
|
|
if (pos)
|
|
|
|
*pos = p;
|
2021-05-10 05:08:38 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:00:46 +00:00
|
|
|
std::string float_to_string_decimal_point(double value, int precision/* = -1*/)
|
|
|
|
{
|
2021-09-13 18:46:19 +00:00
|
|
|
// Our Windows build server fully supports C++17 std::to_chars. Let's use it.
|
|
|
|
// Other platforms are behind, fall back to slow stringstreams for now.
|
|
|
|
#ifdef _WIN32
|
|
|
|
constexpr size_t SIZE = 20;
|
|
|
|
char out[SIZE] = "";
|
|
|
|
std::to_chars_result res;
|
|
|
|
if (precision >=0)
|
|
|
|
res = std::to_chars(out, out+SIZE, value, std::chars_format::fixed, precision);
|
|
|
|
else
|
|
|
|
res = std::to_chars(out, out+SIZE, value, std::chars_format::general, 6);
|
|
|
|
if (res.ec == std::errc::value_too_large)
|
|
|
|
throw std::invalid_argument("float_to_string_decimal_point conversion failed.");
|
|
|
|
return std::string(out, res.ptr - out);
|
|
|
|
#else
|
2021-05-03 14:00:46 +00:00
|
|
|
std::stringstream buf;
|
|
|
|
if (precision >= 0)
|
|
|
|
buf << std::fixed << std::setprecision(precision);
|
|
|
|
buf << value;
|
|
|
|
return buf.str();
|
2021-09-13 18:46:19 +00:00
|
|
|
#endif
|
2021-05-03 14:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Slic3r
|
|
|
|
|