Skip to content

Commit

Permalink
Cleanup format API
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 22, 2024
1 parent 9282222 commit 891c9a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
6 changes: 3 additions & 3 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ FMT_FUNC void format_error_code(detail::buffer<char>& 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.
Expand Down Expand Up @@ -1434,7 +1434,7 @@ FMT_FUNC void format_system_error(detail::buffer<char>& 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 {
Expand Down
84 changes: 40 additions & 44 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,17 @@ template <> inline auto decimal_point(locale_ref loc) -> wchar_t {
return decimal_point_impl<wchar_t>(loc);
}

#ifndef FMT_HEADER_ONLY
FMT_BEGIN_EXPORT
extern template FMT_API auto thousands_sep_impl<char>(locale_ref)
-> thousands_sep_result<char>;
extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
-> thousands_sep_result<wchar_t>;
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 <typename Char> auto equal2(const Char* lhs, const char* rhs) -> bool {
return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]);
Expand Down Expand Up @@ -3779,44 +3790,13 @@ template <typename Char> struct format_handler {
FMT_NORETURN void on_error(const char* message) { report_error(message); }
};

// DEPRECATED!
template <typename Char = char> struct vformat_args {
using type = basic_format_args<buffered_context<Char>>;
};
template <> struct vformat_args<char> {
using type = format_args;
};

template <typename Char>
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
typename vformat_args<Char>::type args, locale_ref loc = {}) {
auto out = basic_appender<Char>(buf);
parse_format_string(
fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
}

using format_func = void (*)(detail::buffer<char>&, 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<char>& 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<char>(locale_ref)
-> thousands_sep_result<char>;
extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
-> thousands_sep_result<wchar_t>;
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 <typename T, typename Char, type TYPE>
template <typename FormatContext>
FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
Expand All @@ -3830,21 +3810,26 @@ FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
specs_.precision_ref, ctx);
return write<Char>(ctx.out(), val, specs, ctx.locale());
}

// DEPRECATED!
template <typename Char = char> struct vformat_args {
using type = basic_format_args<buffered_context<Char>>;
};
template <> struct vformat_args<char> {
using type = format_args;
};

template <typename Char>
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
typename vformat_args<Char>::type args, locale_ref loc = {}) {
auto out = basic_appender<Char>(buf);
parse_format_string(
fmt, format_handler<Char>{parse_context<Char>(fmt), {out, args, loc}});
}
} // namespace detail

FMT_BEGIN_EXPORT

template <typename T, typename Char>
struct formatter<T, Char, void_t<detail::format_as_result<T>>>
: formatter<detail::format_as_result<T>, Char> {
template <typename FormatContext>
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<detail::format_as_result<T>, Char>::format(val, ctx);
}
};

#define FMT_FORMAT_AS(Type, Base) \
template <typename Char> \
struct formatter<Type, Char> : formatter<Base, Char> { \
Expand Down Expand Up @@ -3884,6 +3869,17 @@ struct formatter<detail::float128, Char>
: detail::native_formatter<detail::float128, Char,
detail::type::float_type> {};

template <typename T, typename Char>
struct formatter<T, Char, void_t<detail::format_as_result<T>>>
: formatter<detail::format_as_result<T>, Char> {
template <typename FormatContext>
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<detail::format_as_result<T>, Char>::format(val, ctx);
}
};

/**
* Converts `p` to `const void*` for pointer formatting.
*
Expand Down
2 changes: 1 addition & 1 deletion src/os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void detail::format_windows_error(detail::buffer<char>& 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

Expand Down

0 comments on commit 891c9a7

Please sign in to comment.