Cleanup the format API

This commit is contained in:
Victor Zverovich 2021-06-02 16:13:39 -07:00
parent f286139d22
commit d142579e97
2 changed files with 19 additions and 20 deletions

View File

@ -2237,7 +2237,7 @@ FMT_API std::system_error vsystem_error(int error_code, string_view format_str,
/**
\rst
Constructs :class:`std::system_error` with a message formatted with
``fmt::format(message, args...)``.
``fmt::format(fmt, args...)``.
*error_code* is a system error code as given by ``errno``.
**Example**::
@ -2251,10 +2251,10 @@ FMT_API std::system_error vsystem_error(int error_code, string_view format_str,
throw fmt::system_error(errno, "cannot open file '{}'", filename);
\endrst
*/
template <typename... Args>
std::system_error system_error(int error_code, string_view message,
const Args&... args) {
return vsystem_error(error_code, message, fmt::make_format_args(args...));
template <typename... T>
auto system_error(int error_code, format_string<T...> fmt, T&&... args)
-> std::system_error {
return vsystem_error(error_code, fmt, fmt::make_format_args(args...));
}
/**
@ -2413,12 +2413,22 @@ struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {
// };
template <typename Char = char> class dynamic_formatter {
private:
detail::dynamic_format_specs<Char> specs_;
const Char* format_str_;
struct null_handler : detail::error_handler {
void on_align(align_t) {}
void on_sign(sign_t) {}
void on_hash() {}
};
template <typename Context> void handle_specs(Context& ctx) {
detail::handle_dynamic_spec<detail::width_checker>(specs_.width,
specs_.width_ref, ctx);
detail::handle_dynamic_spec<detail::precision_checker>(
specs_.precision, specs_.precision_ref, ctx);
}
public:
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
@ -2439,17 +2449,6 @@ template <typename Char = char> class dynamic_formatter {
if (specs_.precision >= 0) checker.end_precision();
return detail::write<Char>(ctx.out(), val, specs_, ctx.locale());
}
private:
template <typename Context> void handle_specs(Context& ctx) {
detail::handle_dynamic_spec<detail::width_checker>(specs_.width,
specs_.width_ref, ctx);
detail::handle_dynamic_spec<detail::precision_checker>(
specs_.precision, specs_.precision_ref, ctx);
}
detail::dynamic_format_specs<Char> specs_;
const Char* format_str_;
};
/**
@ -2461,14 +2460,14 @@ template <typename Char = char> class dynamic_formatter {
auto s = fmt::format("{}", fmt::ptr(p));
\endrst
*/
template <typename T> const void* ptr(T p) {
template <typename T> auto ptr(T p) -> const void* {
static_assert(std::is_pointer<T>::value, "");
return detail::bit_cast<const void*>(p);
}
template <typename T> const void* ptr(const std::unique_ptr<T>& p) {
template <typename T> auto ptr(const std::unique_ptr<T>& p) -> const void* {
return p.get();
}
template <typename T> const void* ptr(const std::shared_ptr<T>& p) {
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {
return p.get();
}

View File

@ -183,7 +183,7 @@ inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
detail::vformat_to(buffer, fmt, args);
buffer.push_back(L'\0');
if (std::fputws(buffer.data(), f) == -1)
FMT_THROW(system_error(errno, "cannot write to file"));
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
}
inline void vprint(wstring_view fmt, wformat_args args) {