Avoid locale use in `tr_truncd()` (#5587)

`tr_parseNum<>()` is implemented with `fast_float::from_chars()`, and
the latter is documented as "expecting a locale-indepent format
equivalent to what is used by std::strtod in the default ("C") locale".
To accomodate locale independent number parsing, switch back to
locale-independent number formatting in `tr_truncd()` by both removing a
`L` format specifier from `fmt::format_to_n()` call and using a fixed
"." decimal separator when truncating the value.
This commit is contained in:
Mike Gelfand 2023-06-11 02:09:18 +04:00 committed by Charles Kerr
parent edcddd00dd
commit 3b44fa034d
1 changed files with 2 additions and 3 deletions

View File

@ -9,7 +9,6 @@
#include <cerrno>
#include <cfloat> // DBL_DIG
#include <chrono>
#include <clocale> // localeconv()
#include <cstdint> // SIZE_MAX
#include <cstdlib> // getenv()
#include <cstring> /* strerror() */
@ -514,10 +513,10 @@ std::vector<int> tr_parseNumberRange(std::string_view str)
double tr_truncd(double x, int decimal_places)
{
auto buf = std::array<char, 128>{};
auto const [out, len] = fmt::format_to_n(std::data(buf), std::size(buf) - 1, "{:.{}Lf}", x, DBL_DIG);
auto const [out, len] = fmt::format_to_n(std::data(buf), std::size(buf) - 1, "{:.{}f}", x, DBL_DIG);
*out = '\0';
if (auto* const pt = strstr(std::data(buf), localeconv()->decimal_point); pt != nullptr)
if (auto* const pt = strchr(std::data(buf), '.'); pt != nullptr)
{
pt[decimal_places != 0 ? decimal_places + 1 : 0] = '\0';
}