1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 08:13:27 +00:00

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 GitHub
parent 0ef58c2a20
commit 8ca02b8f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,6 @@
#include <cerrno> #include <cerrno>
#include <cfloat> // DBL_DIG #include <cfloat> // DBL_DIG
#include <chrono> #include <chrono>
#include <clocale> // localeconv()
#include <cstdint> // SIZE_MAX #include <cstdint> // SIZE_MAX
#include <cstdlib> // getenv() #include <cstdlib> // getenv()
#include <cstring> /* strerror() */ #include <cstring> /* strerror() */
@ -514,10 +513,10 @@ std::vector<int> tr_parseNumberRange(std::string_view str)
double tr_truncd(double x, int decimal_places) double tr_truncd(double x, int decimal_places)
{ {
auto buf = std::array<char, 128>{}; 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'; *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'; pt[decimal_places != 0 ? decimal_places + 1 : 0] = '\0';
} }