mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Use overridden locale in ostream
This commit is contained in:
committed by
Victor Zverovich
parent
ceff9b0b2e
commit
c58b7d9c2f
@ -1076,6 +1076,8 @@ class locale_ref {
|
|||||||
locale_ref() : locale_(nullptr) {}
|
locale_ref() : locale_(nullptr) {}
|
||||||
template <typename Locale> explicit locale_ref(const Locale& loc);
|
template <typename Locale> explicit locale_ref(const Locale& loc);
|
||||||
|
|
||||||
|
explicit operator bool() const FMT_NOEXCEPT { return locale_ != nullptr; }
|
||||||
|
|
||||||
template <typename Locale> Locale get() const;
|
template <typename Locale> Locale get() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -89,9 +89,11 @@ void write(std::basic_ostream<Char>& os, buffer<Char>& buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void format_value(buffer<Char>& buf, const T& value) {
|
void format_value(buffer<Char>& buf, const T& value,
|
||||||
|
locale_ref loc = locale_ref()) {
|
||||||
formatbuf<Char> format_buf(buf);
|
formatbuf<Char> format_buf(buf);
|
||||||
std::basic_ostream<Char> output(&format_buf);
|
std::basic_ostream<Char> output(&format_buf);
|
||||||
|
if (loc) output.imbue(loc.get<std::locale>());
|
||||||
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
||||||
output << value;
|
output << value;
|
||||||
buf.resize(buf.size());
|
buf.resize(buf.size());
|
||||||
@ -104,7 +106,7 @@ struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
|||||||
template <typename Context>
|
template <typename Context>
|
||||||
auto format(const T& value, Context& ctx) -> decltype(ctx.out()) {
|
auto format(const T& value, Context& ctx) -> decltype(ctx.out()) {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
format_value(buffer, value);
|
format_value(buffer, value, ctx.locale());
|
||||||
basic_string_view<Char> str(buffer.data(), buffer.size());
|
basic_string_view<Char> str(buffer.data(), buffer.size());
|
||||||
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user