mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-28 06:36:02 +00:00
Fix the default locale in ostream_formatter
This commit is contained in:
parent
dd6f657a79
commit
8a39388516
@ -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);
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user