config: try to fix float locale issues

This commit is contained in:
Megamouse 2023-02-24 15:26:00 +01:00
parent afad96a52a
commit e46dba43ad
4 changed files with 39 additions and 4 deletions

View file

@ -1,7 +1,6 @@
#include "stdafx.h"
#include "Config.h"
#include "util/types.hpp"
#include "util/yaml.hpp"
#include <charconv>
@ -184,6 +183,27 @@ bool try_to_float(f64* out, std::string_view value, f64 min, f64 max)
return true;
}
bool try_to_string(std::string* out, const f64& value)
{
#ifdef __APPLE__
if (out) *out = std::to_string(value);
return true;
#else
std::array<char, 32> str{};
if (auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), value, std::chars_format::fixed); ec == std::errc())
{
if (out) *out = std::string(str.data(), ptr);
return true;
}
else
{
if (out) cfg_log.error("cfg::try_to_string(): could not convert value '%f' to string. error='%s'", value, std::make_error_code(ec).message());
return false;
}
#endif
}
bool cfg::try_to_enum_value(u64* out, decltype(&fmt_class_string<int>::format) func, std::string_view value)
{
u64 max = umax;