diff --git a/doc/api.rst b/doc/api.rst index 07ed6d4c..2787bd1d 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -335,50 +335,6 @@ arguments, the container that stores pointers to them will be allocated using the default allocator. Also floating-point formatting falls back on ``sprintf`` which may do allocations. -Custom Formatting of Built-in Types ------------------------------------ - -It is possible to change the way arguments are formatted by providing a -custom argument formatter class:: - - using arg_formatter = fmt::arg_formatter>; - - // A custom argument formatter that formats negative integers as unsigned - // with the ``x`` format specifier. - class custom_arg_formatter : public arg_formatter { - public: - custom_arg_formatter(fmt::format_context& ctx, - fmt::format_parse_context* parse_ctx = nullptr, - fmt::format_specs* spec = nullptr) - : arg_formatter(ctx, parse_ctx, spec) {} - - using arg_formatter::operator(); - - auto operator()(int value) { - if (specs() && specs()->type == 'x') - return (*this)(static_cast(value)); // convert to unsigned and format - return arg_formatter::operator()(value); - } - }; - - std::string custom_vformat(fmt::string_view format_str, fmt::format_args args) { - fmt::memory_buffer buffer; - // Pass custom argument formatter as a template arg to vformat_to. - fmt::vformat_to(buffer, format_str, args); - return fmt::to_string(buffer); - } - - template - inline std::string custom_format( - fmt::string_view format_str, const Args&... args) { - return custom_vformat(format_str, fmt::make_format_args(args...)); - } - - std::string s = custom_format("{:x}", -42); // s == "ffffffd6" - -.. doxygenclass:: fmt::arg_formatter - :members: - .. _ranges-api: Ranges and Tuple Formatting diff --git a/include/fmt/format.h b/include/fmt/format.h index c75089d8..1c0b3840 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -566,7 +566,8 @@ template void buffer::append(const U* begin, const U* end) { size_t new_size = size_ + to_unsigned(end - begin); reserve(new_size); - std::uninitialized_copy(begin, end, make_checked(ptr_ + size_, capacity_ - size_)); + std::uninitialized_copy(begin, end, + make_checked(ptr_ + size_, capacity_ - size_)); size_ = new_size; } } // namespace detail @@ -2886,14 +2887,13 @@ FMT_API void format_error_code(buffer& out, int error_code, FMT_API void report_error(format_func func, int error_code, string_view message) FMT_NOEXCEPT; -} // namespace detail /** The default argument formatter. */ template -class arg_formatter : public detail::arg_formatter_base { +class arg_formatter : public arg_formatter_base { private: using char_type = Char; - using base = detail::arg_formatter_base; + using base = arg_formatter_base; using context_type = basic_format_context; context_type& ctx_; @@ -2929,6 +2929,11 @@ class arg_formatter : public detail::arg_formatter_base { return ctx_.out(); } }; +} // namespace detail + +template +using arg_formatter FMT_DEPRECATED_ALIAS = + detail::arg_formatter; /** An error returned by an operating system or a language runtime, @@ -3139,8 +3144,8 @@ struct formatter( specs_.precision, specs_.precision_ref, ctx); - using af = arg_formatter; + using af = detail::arg_formatter; return visit_format_arg(af(ctx, nullptr, &specs_), detail::make_arg(val)); } @@ -3235,8 +3240,8 @@ template class dynamic_formatter { } if (specs_.alt) checker.on_hash(); if (specs_.precision >= 0) checker.end_precision(); - using af = arg_formatter; + using af = detail::arg_formatter; visit_format_arg(af(ctx, nullptr, &specs_), detail::make_arg(val)); return ctx.out(); @@ -3506,7 +3511,7 @@ template < inline OutputIt vformat_to( OutputIt out, const S& format_str, format_args_t, char_t> args) { - using af = arg_formatter>; + using af = detail::arg_formatter>; return vformat_to(out, to_string_view(format_str), args); } diff --git a/include/fmt/locale.h b/include/fmt/locale.h index dd75b45f..988d15cd 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -56,7 +56,7 @@ template , Char> args) { - using af = arg_formatter; + using af = detail::arg_formatter; return vformat_to(out, to_string_view(format_str), args, detail::locale_ref(loc)); } diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index c5d748e8..1b0c1e13 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -18,9 +18,9 @@ // A custom argument formatter that doesn't print `-` for floating-point values // rounded to 0. class custom_arg_formatter - : public fmt::arg_formatter { + : public fmt::detail::arg_formatter { public: - using base = fmt::arg_formatter; + using base = fmt::detail::arg_formatter; custom_arg_formatter(fmt::format_context& ctx, fmt::format_parse_context* parse_ctx, diff --git a/test/ostream-test.cc b/test/ostream-test.cc index e572cf19..1c87d46d 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -65,10 +65,10 @@ TEST(OStreamTest, Enum) { } struct test_arg_formatter - : fmt::arg_formatter { + : fmt::detail::arg_formatter { fmt::format_parse_context parse_ctx; test_arg_formatter(fmt::format_context& ctx, fmt::format_specs& s) - : fmt::arg_formatter( + : fmt::detail::arg_formatter( ctx, &parse_ctx, &s), parse_ctx("") {} };