From 8ca02b8f283bc6291accc4f1aed261f08d4ee0af Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sun, 11 Jun 2023 02:09:18 +0400 Subject: [PATCH] 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. --- libtransmission/utils.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index 38954e3b5..948f0a111 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -9,7 +9,6 @@ #include #include // DBL_DIG #include -#include // localeconv() #include // SIZE_MAX #include // getenv() #include /* strerror() */ @@ -514,10 +513,10 @@ std::vector tr_parseNumberRange(std::string_view str) double tr_truncd(double x, int decimal_places) { auto buf = std::array{}; - 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'; }