From 8a39388516cf3630b0498fce373de0dfd54bdcac Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 24 Nov 2023 09:23:58 -0800 Subject: [PATCH] Fix the default locale in ostream_formatter --- include/fmt/ostream.h | 7 +++---- test/ostream-test.cc | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 782ace5cc3cf..2ed35df16f0d 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -91,12 +91,11 @@ void write_buffer(std::basic_ostream& os, buffer& buf) { } template -void format_value(buffer& buf, const T& value, - locale_ref loc = locale_ref()) { +void format_value(buffer& buf, const T& value) { auto&& format_buf = formatbuf>(buf); auto&& output = std::basic_ostream(&format_buf); #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) - if (loc) output.imbue(loc.get()); + output.imbue(std::locale::classic()); // The default is always unlocalized. #endif output << value; output.exceptions(std::ios_base::failbit | std::ios_base::badbit); @@ -117,7 +116,7 @@ struct basic_ostream_formatter : formatter, Char> { auto format(const T& value, basic_format_context& ctx) const -> OutputIt { auto buffer = basic_memory_buffer(); - detail::format_value(buffer, value, ctx.locale()); + detail::format_value(buffer, value); return formatter, Char>::format( {buffer.data(), buffer.size()}, ctx); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index b2d15466dc46..98ee0757b2e4 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -17,7 +17,7 @@ struct test {}; // included after fmt/format.h. namespace fmt { template <> struct formatter : formatter { - auto format(const test&, format_context& ctx) -> decltype(ctx.out()) { + auto format(const test&, format_context& ctx) const -> decltype(ctx.out()) { return formatter::format(42, ctx); } }; @@ -289,3 +289,20 @@ TEST(ostream_test, closed_ofstream) { std::ofstream ofs; fmt::print(ofs, "discard"); } + +struct unlocalized {}; + +auto operator<<(std::ostream& os, unlocalized) + -> std::ostream& { + return os << 12345; +} + +namespace fmt { +template <> struct formatter : ostream_formatter {}; +} // namespace fmt + +TEST(ostream_test, unlocalized) { + auto loc = get_locale("en_US.UTF-8"); + std::locale::global(loc); + EXPECT_EQ(fmt::format(loc, "{}", unlocalized()), "12345"); +}