Skip to content

Commit

Permalink
Fix the default locale in ostream_formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 24, 2023
1 parent dd6f657 commit 8a39388
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 3 additions & 4 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
}

template <typename Char, typename T>
void format_value(buffer<Char>& buf, const T& value,
locale_ref loc = locale_ref()) {
void format_value(buffer<Char>& buf, const T& value) {
auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
auto&& output = std::basic_ostream<Char>(&format_buf);
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
if (loc) output.imbue(loc.get<std::locale>());
output.imbue(std::locale::classic()); // The default is always unlocalized.
#endif
output << value;
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
Expand All @@ -117,7 +116,7 @@ struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
-> OutputIt {
auto buffer = basic_memory_buffer<Char>();
detail::format_value(buffer, value, ctx.locale());
detail::format_value(buffer, value);
return formatter<basic_string_view<Char>, Char>::format(
{buffer.data(), buffer.size()}, ctx);
}
Expand Down
19 changes: 18 additions & 1 deletion test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct test {};
// included after fmt/format.h.
namespace fmt {
template <> struct formatter<test> : formatter<int> {
auto format(const test&, format_context& ctx) -> decltype(ctx.out()) {
auto format(const test&, format_context& ctx) const -> decltype(ctx.out()) {
return formatter<int>::format(42, ctx);
}
};
Expand Down Expand Up @@ -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<unlocalized> : 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");
}

0 comments on commit 8a39388

Please sign in to comment.