Simplify locale handling

This commit is contained in:
Victor Zverovich
2024-09-22 09:47:09 -07:00
parent 80c4d42c66
commit ff92223549
6 changed files with 38 additions and 51 deletions

View File

@ -410,11 +410,11 @@ locale:
that take `std::locale` as a parameter. The locale type is a template that take `std::locale` as a parameter. The locale type is a template
parameter to avoid the expensive `<locale>` include. parameter to avoid the expensive `<locale>` include.
::: format(const Locale&, format_string<T...>, T&&...) ::: format(detail::locale_ref, format_string<T...>, T&&...)
::: format_to(OutputIt, const Locale&, format_string<T...>, T&&...) ::: format_to(OutputIt, detail::locale_ref, format_string<T...>, T&&...)
::: formatted_size(const Locale&, format_string<T...>, T&&...) ::: formatted_size(detail::locale_ref, format_string<T...>, T&&...)
<a id="legacy-checks"></a> <a id="legacy-checks"></a>
### Legacy Compile-Time Checks ### Legacy Compile-Time Checks

View File

@ -2220,9 +2220,12 @@ struct locale_ref {
public: public:
constexpr locale_ref() : locale_(nullptr) {} constexpr locale_ref() : locale_(nullptr) {}
template <typename Locale> explicit locale_ref(const Locale& loc);
template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)>
locale_ref(const Locale& loc);
explicit operator bool() const noexcept { return locale_ != nullptr; } explicit operator bool() const noexcept { return locale_ != nullptr; }
#endif #endif // FMT_USE_LOCALE
template <typename Locale> auto get() const -> Locale; template <typename Locale> auto get() const -> Locale;
}; };

View File

@ -84,7 +84,7 @@ using std::locale;
using std::numpunct; using std::numpunct;
using std::use_facet; using std::use_facet;
template <typename Locale> template <typename Locale, enable_if_t<(sizeof(Locale::collate) != 0), int>>
locale_ref::locale_ref(const Locale& loc) : locale_(&loc) { locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
static_assert(std::is_same<Locale, locale>::value, ""); static_assert(std::is_same<Locale, locale>::value, "");
} }

View File

@ -796,11 +796,6 @@ using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
# define FMT_USE_FULL_CACHE_DRAGONBOX 0 # define FMT_USE_FULL_CACHE_DRAGONBOX 0
#endif #endif
template <typename T, typename Enable = void>
struct is_locale : std::false_type {};
template <typename T>
struct is_locale<T, void_t<decltype(T::classic())>> : std::true_type {};
// An allocator that uses malloc/free to allow removing dependency on the C++ // An allocator that uses malloc/free to allow removing dependency on the C++
// standard libary runtime. // standard libary runtime.
template <typename T> struct allocator { template <typename T> struct allocator {
@ -4213,47 +4208,41 @@ FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
FMT_API void report_system_error(int error_code, const char* message) noexcept; FMT_API void report_system_error(int error_code, const char* message) noexcept;
template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)> inline auto vformat(detail::locale_ref loc, string_view fmt, format_args args)
auto vformat(const Locale& loc, string_view fmt, format_args args)
-> std::string { -> std::string {
auto buf = memory_buffer(); auto buf = memory_buffer();
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); detail::vformat_to(buf, fmt, args, loc);
return {buf.data(), buf.size()}; return {buf.data(), buf.size()};
} }
template <typename Locale, typename... T, template <typename... T>
FMT_ENABLE_IF(detail::is_locale<Locale>::value)> FMT_INLINE auto format(detail::locale_ref loc, format_string<T...> fmt,
FMT_INLINE auto format(const Locale& loc, format_string<T...> fmt, T&&... args) T&&... args) -> std::string {
-> std::string { return vformat(loc, fmt.str, vargs<T...>{{args...}});
return fmt::vformat(loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename OutputIt, typename Locale, template <typename OutputIt,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
detail::is_locale<Locale>::value)> auto vformat_to(OutputIt out, detail::locale_ref loc, string_view fmt,
auto vformat_to(OutputIt out, const Locale& loc, string_view fmt,
format_args args) -> OutputIt { format_args args) -> OutputIt {
auto&& buf = detail::get_buffer<char>(out); auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); detail::vformat_to(buf, fmt, args, loc);
return detail::get_iterator(buf, out); return detail::get_iterator(buf, out);
} }
template <typename OutputIt, typename Locale, typename... T, template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
detail::is_locale<Locale>::value)> FMT_INLINE auto format_to(OutputIt out, detail::locale_ref loc,
FMT_INLINE auto format_to(OutputIt out, const Locale& loc,
format_string<T...> fmt, T&&... args) -> OutputIt { format_string<T...> fmt, T&&... args) -> OutputIt {
return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}}); return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename Locale, typename... T, template <typename... T>
FMT_ENABLE_IF(detail::is_locale<Locale>::value)> FMT_NODISCARD FMT_INLINE auto formatted_size(detail::locale_ref loc,
FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
format_string<T...> fmt, format_string<T...> fmt,
T&&... args) -> size_t { T&&... args) -> size_t {
auto buf = detail::counting_buffer<>(); auto buf = detail::counting_buffer<>();
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
detail::locale_ref(loc));
return buf.count(); return buf.count();
} }

View File

@ -433,8 +433,8 @@ template <> struct formatter<std::error_code> {
} }
template <typename FormatContext> template <typename FormatContext>
FMT_CONSTEXPR20 auto format(const std::error_code& ec, FormatContext& ctx) const FMT_CONSTEXPR20 auto format(const std::error_code& ec,
-> decltype(ctx.out()) { FormatContext& ctx) const -> decltype(ctx.out()) {
auto specs = specs_; auto specs = specs_;
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
ctx); ctx);

View File

@ -191,11 +191,9 @@ auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
} }
template <typename Locale, typename S, template <typename S, typename Char = detail::format_string_char_t<S>,
typename Char = detail::format_string_char_t<S>, FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& inline auto vformat(detail::locale_ref loc, const S& fmt,
detail::is_exotic_char<Char>::value)>
inline auto vformat(const Locale& loc, const S& fmt,
typename detail::vformat_args<Char>::type args) typename detail::vformat_args<Char>::type args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
auto buf = basic_memory_buffer<Char>(); auto buf = basic_memory_buffer<Char>();
@ -204,11 +202,10 @@ inline auto vformat(const Locale& loc, const S& fmt,
return {buf.data(), buf.size()}; return {buf.data(), buf.size()};
} }
template <typename Locale, typename S, typename... T, template <typename S, typename... T,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
detail::is_exotic_char<Char>::value)> inline auto format(detail::locale_ref loc, const S& fmt, T&&... args)
inline auto format(const Locale& loc, const S& fmt, T&&... args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
return vformat(loc, detail::to_string_view(fmt), return vformat(loc, detail::to_string_view(fmt),
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
@ -235,12 +232,11 @@ inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
} }
template <typename Locale, typename S, typename OutputIt, typename... Args, template <typename S, typename OutputIt, typename... Args,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value)>
detail::is_exotic_char<Char>::value)> inline auto vformat_to(OutputIt out, detail::locale_ref loc, const S& fmt,
inline auto vformat_to(OutputIt out, const Locale& loc, const S& fmt,
typename detail::vformat_args<Char>::type args) typename detail::vformat_args<Char>::type args)
-> OutputIt { -> OutputIt {
auto&& buf = detail::get_buffer<Char>(out); auto&& buf = detail::get_buffer<Char>(out);
@ -248,12 +244,11 @@ inline auto vformat_to(OutputIt out, const Locale& loc, const S& fmt,
return detail::get_iterator(buf, out); return detail::get_iterator(buf, out);
} }
template <typename OutputIt, typename Locale, typename S, typename... T, template <typename OutputIt, typename S, typename... T,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
bool enable = detail::is_output_iterator<OutputIt, Char>::value && bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
detail::is_locale<Locale>::value &&
detail::is_exotic_char<Char>::value> detail::is_exotic_char<Char>::value>
inline auto format_to(OutputIt out, const Locale& loc, const S& fmt, inline auto format_to(OutputIt out, detail::locale_ref loc, const S& fmt,
T&&... args) -> T&&... args) ->
typename std::enable_if<enable, OutputIt>::type { typename std::enable_if<enable, OutputIt>::type {
return vformat_to(out, loc, detail::to_string_view(fmt), return vformat_to(out, loc, detail::to_string_view(fmt),