Deprecate arg_formatter

This commit is contained in:
Victor Zverovich 2020-07-04 06:41:24 -07:00
parent a62d060554
commit 01309a34ab
5 changed files with 19 additions and 58 deletions

View File

@ -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<fmt::buffer_range<char>>;
// 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<unsigned>(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<custom_arg_formatter>(buffer, format_str, args);
return fmt::to_string(buffer);
}
template <typename ...Args>
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

View File

@ -566,7 +566,8 @@ template <typename U>
void buffer<T>::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<char>& 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 <typename OutputIt, typename Char>
class arg_formatter : public detail::arg_formatter_base<OutputIt, Char> {
class arg_formatter : public arg_formatter_base<OutputIt, Char> {
private:
using char_type = Char;
using base = detail::arg_formatter_base<OutputIt, Char>;
using base = arg_formatter_base<OutputIt, Char>;
using context_type = basic_format_context<OutputIt, Char>;
context_type& ctx_;
@ -2929,6 +2929,11 @@ class arg_formatter : public detail::arg_formatter_base<OutputIt, Char> {
return ctx_.out();
}
};
} // namespace detail
template <typename OutputIt, typename Char>
using arg_formatter FMT_DEPRECATED_ALIAS =
detail::arg_formatter<OutputIt, Char>;
/**
An error returned by an operating system or a language runtime,
@ -3139,8 +3144,8 @@ struct formatter<T, Char,
specs_.width_ref, ctx);
detail::handle_dynamic_spec<detail::precision_checker>(
specs_.precision, specs_.precision_ref, ctx);
using af = arg_formatter<typename FormatContext::iterator,
typename FormatContext::char_type>;
using af = detail::arg_formatter<typename FormatContext::iterator,
typename FormatContext::char_type>;
return visit_format_arg(af(ctx, nullptr, &specs_),
detail::make_arg<FormatContext>(val));
}
@ -3235,8 +3240,8 @@ template <typename Char = char> class dynamic_formatter {
}
if (specs_.alt) checker.on_hash();
if (specs_.precision >= 0) checker.end_precision();
using af = arg_formatter<typename FormatContext::iterator,
typename FormatContext::char_type>;
using af = detail::arg_formatter<typename FormatContext::iterator,
typename FormatContext::char_type>;
visit_format_arg(af(ctx, nullptr, &specs_),
detail::make_arg<FormatContext>(val));
return ctx.out();
@ -3506,7 +3511,7 @@ template <
inline OutputIt vformat_to(
OutputIt out, const S& format_str,
format_args_t<type_identity_t<OutputIt>, char_t<S>> args) {
using af = arg_formatter<OutputIt, char_t<S>>;
using af = detail::arg_formatter<OutputIt, char_t<S>>;
return vformat_to<af>(out, to_string_view(format_str), args);
}

View File

@ -56,7 +56,7 @@ template <typename S, typename OutputIt, typename... Args,
inline OutputIt vformat_to(
OutputIt out, const std::locale& loc, const S& format_str,
format_args_t<type_identity_t<OutputIt>, Char> args) {
using af = arg_formatter<OutputIt, Char>;
using af = detail::arg_formatter<OutputIt, Char>;
return vformat_to<af>(out, to_string_view(format_str), args,
detail::locale_ref(loc));
}

View File

@ -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<fmt::format_context::iterator, char> {
: public fmt::detail::arg_formatter<fmt::format_context::iterator, char> {
public:
using base = fmt::arg_formatter<fmt::format_context::iterator, char>;
using base = fmt::detail::arg_formatter<fmt::format_context::iterator, char>;
custom_arg_formatter(fmt::format_context& ctx,
fmt::format_parse_context* parse_ctx,

View File

@ -65,10 +65,10 @@ TEST(OStreamTest, Enum) {
}
struct test_arg_formatter
: fmt::arg_formatter<fmt::format_context::iterator, char> {
: fmt::detail::arg_formatter<fmt::format_context::iterator, char> {
fmt::format_parse_context parse_ctx;
test_arg_formatter(fmt::format_context& ctx, fmt::format_specs& s)
: fmt::arg_formatter<fmt::format_context::iterator, char>(
: fmt::detail::arg_formatter<fmt::format_context::iterator, char>(
ctx, &parse_ctx, &s),
parse_ctx("") {}
};