changed locale retrieval to minimize the risk of unwanted accidental refactoring
This commit is contained in:
parent
0b843af56b
commit
2087778ec4
@ -961,6 +961,16 @@ OutputIt format_duration_unit(OutputIt out) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename F>
|
||||||
|
auto with_locale(bool localized, locale_ref loc_ref, F&& f)
|
||||||
|
-> decltype(f(std::locale{})) {
|
||||||
|
if (localized) return f(loc_ref.template get<std::locale>());
|
||||||
|
// in contrast to the case above, classic locale is returned by reference
|
||||||
|
// without introducing a temporary object
|
||||||
|
// (locale copy ctor & dtor are relatively expensive)
|
||||||
|
return f(get_classic_locale());
|
||||||
|
}
|
||||||
|
|
||||||
template <typename FormatContext, typename OutputIt, typename Rep,
|
template <typename FormatContext, typename OutputIt, typename Rep,
|
||||||
typename Period>
|
typename Period>
|
||||||
struct chrono_formatter {
|
struct chrono_formatter {
|
||||||
@ -1062,14 +1072,11 @@ struct chrono_formatter {
|
|||||||
|
|
||||||
void format_localized(const tm& time, char format, char modifier = 0) {
|
void format_localized(const tm& time, char format, char modifier = 0) {
|
||||||
if (isnan(val)) return write_nan();
|
if (isnan(val)) return write_nan();
|
||||||
if (localized) {
|
with_locale(localized, context.locale(),
|
||||||
out = detail::write<char_type>(
|
[this, &time, format, modifier](const std::locale& loc) {
|
||||||
out, time, context.locale().template get<std::locale>(), format,
|
out = detail::write<char_type>(out, time, loc, format,
|
||||||
modifier);
|
modifier);
|
||||||
} else {
|
});
|
||||||
out = detail::write<char_type>(out, time, get_classic_locale(), format,
|
|
||||||
modifier);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_text(const char_type* begin, const char_type* end) {
|
void on_text(const char_type* begin, const char_type* end) {
|
||||||
@ -1234,13 +1241,13 @@ template <typename Char> struct formatter<weekday, Char> {
|
|||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
|
auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||||
auto time = std::tm();
|
auto out = ctx.out();
|
||||||
time.tm_wday = static_cast<int>(wd.c_encoding());
|
return detail::with_locale(
|
||||||
if (localized)
|
localized, ctx.locale(), [&out, wd](const std::locale& loc) {
|
||||||
return detail::write<Char>(ctx.out(), time,
|
auto time = std::tm();
|
||||||
ctx.locale().template get<std::locale>(), 'a');
|
time.tm_wday = static_cast<int>(wd.c_encoding());
|
||||||
return detail::write<Char>(ctx.out(), time, detail::get_classic_locale(),
|
return detail::write<Char>(out, time, loc, 'a');
|
||||||
'a');
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1855,10 +1862,11 @@ template <typename Char> struct formatter<std::tm, Char> {
|
|||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const std::tm& tm, FormatContext& ctx) const
|
auto format(const std::tm& tm, FormatContext& ctx) const
|
||||||
-> decltype(ctx.out()) {
|
-> decltype(ctx.out()) {
|
||||||
if (const auto& loc_ref = ctx.locale())
|
auto out = ctx.out();
|
||||||
return this->do_format(ctx.out(), tm,
|
return detail::with_locale(static_cast<bool>(ctx.locale()), ctx.locale(),
|
||||||
loc_ref.template get<std::locale>());
|
[this, &out, &tm](const std::locale& loc) {
|
||||||
return this->do_format(ctx.out(), tm, detail::get_classic_locale());
|
return do_format(out, tm, loc);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user