From 891c9a73ae9a23ed8bffffb8753504b1f6f399d0 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 22 Sep 2024 15:31:52 -0700 Subject: [PATCH] Cleanup format API --- include/fmt/format-inl.h | 6 +-- include/fmt/format.h | 90 +++++++++++++++++++--------------------- src/os.cc | 2 +- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 5bf952c3..38308bf2 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -63,8 +63,8 @@ FMT_FUNC void format_error_code(detail::buffer& out, int error_code, FMT_ASSERT(out.size() <= inline_buffer_size, ""); } -FMT_FUNC void report_error(format_func func, int error_code, - const char* message) noexcept { +FMT_FUNC void do_report_error(format_func func, int error_code, + const char* message) noexcept { memory_buffer full_message; func(full_message, error_code, message); // Don't use fwrite_all because the latter may throw. @@ -1434,7 +1434,7 @@ FMT_FUNC void format_system_error(detail::buffer& out, int error_code, FMT_FUNC void report_system_error(int error_code, const char* message) noexcept { - report_error(format_system_error, error_code, message); + do_report_error(format_system_error, error_code, message); } FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string { diff --git a/include/fmt/format.h b/include/fmt/format.h index b2dee309..9dd68a1e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1297,6 +1297,17 @@ template <> inline auto decimal_point(locale_ref loc) -> wchar_t { return decimal_point_impl(loc); } +#ifndef FMT_HEADER_ONLY +FMT_BEGIN_EXPORT +extern template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result; +extern template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result; +extern template FMT_API auto decimal_point_impl(locale_ref) -> char; +extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; +FMT_END_EXPORT +#endif // FMT_HEADER_ONLY + // Compares two characters for equality. template auto equal2(const Char* lhs, const char* rhs) -> bool { return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]); @@ -3779,6 +3790,27 @@ template struct format_handler { FMT_NORETURN void on_error(const char* message) { report_error(message); } }; +using format_func = void (*)(detail::buffer&, int, const char*); +FMT_API void do_report_error(format_func func, int error_code, + const char* message) noexcept; + +FMT_API void format_error_code(buffer& out, int error_code, + string_view message) noexcept; + +template +template +FMT_CONSTEXPR auto native_formatter::format( + const T& val, FormatContext& ctx) const -> decltype(ctx.out()) { + if (!specs_.dynamic()) + return write(ctx.out(), val, specs_, ctx.locale()); + auto specs = format_specs(specs_); + handle_dynamic_spec(specs.dynamic_width(), specs.width, specs_.width_ref, + ctx); + handle_dynamic_spec(specs.dynamic_precision(), specs.precision, + specs_.precision_ref, ctx); + return write(ctx.out(), val, specs, ctx.locale()); +} + // DEPRECATED! template struct vformat_args { using type = basic_format_args>; @@ -3794,57 +3826,10 @@ void vformat_to(buffer& buf, basic_string_view fmt, parse_format_string( fmt, format_handler{parse_context(fmt), {out, args, loc}}); } - -using format_func = void (*)(detail::buffer&, int, const char*); - -FMT_API void format_error_code(buffer& out, int error_code, - string_view message) noexcept; - -using fmt::report_error; -FMT_API void report_error(format_func func, int error_code, - const char* message) noexcept; - -FMT_BEGIN_EXPORT - -#ifndef FMT_HEADER_ONLY -extern template FMT_API auto thousands_sep_impl(locale_ref) - -> thousands_sep_result; -extern template FMT_API auto thousands_sep_impl(locale_ref) - -> thousands_sep_result; -extern template FMT_API auto decimal_point_impl(locale_ref) -> char; -extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; -#endif // FMT_HEADER_ONLY - -FMT_END_EXPORT - -template -template -FMT_CONSTEXPR auto native_formatter::format( - const T& val, FormatContext& ctx) const -> decltype(ctx.out()) { - if (!specs_.dynamic()) - return write(ctx.out(), val, specs_, ctx.locale()); - auto specs = format_specs(specs_); - handle_dynamic_spec(specs.dynamic_width(), specs.width, specs_.width_ref, - ctx); - handle_dynamic_spec(specs.dynamic_precision(), specs.precision, - specs_.precision_ref, ctx); - return write(ctx.out(), val, specs, ctx.locale()); -} } // namespace detail FMT_BEGIN_EXPORT -template -struct formatter>> - : formatter, Char> { - template - FMT_CONSTEXPR auto format(const T& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto&& val = format_as(value); // Make an lvalue reference for format. - return formatter, Char>::format(val, ctx); - } -}; - #define FMT_FORMAT_AS(Type, Base) \ template \ struct formatter : formatter { \ @@ -3884,6 +3869,17 @@ struct formatter : detail::native_formatter {}; +template +struct formatter>> + : formatter, Char> { + template + FMT_CONSTEXPR auto format(const T& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto&& val = format_as(value); // Make an lvalue reference for format. + return formatter, Char>::format(val, ctx); + } +}; + /** * Converts `p` to `const void*` for pointer formatting. * diff --git a/src/os.cc b/src/os.cc index 102000be..c833a051 100644 --- a/src/os.cc +++ b/src/os.cc @@ -160,7 +160,7 @@ void detail::format_windows_error(detail::buffer& out, int error_code, } void report_windows_error(int error_code, const char* message) noexcept { - report_error(detail::format_windows_error, error_code, message); + do_report_error(detail::format_windows_error, error_code, message); } #endif // _WIN32