From e57ec7d563d5b8d7ec591152e059958ab31c8637 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 20 Oct 2020 17:39:50 -0700 Subject: [PATCH] Merge vformat_to overloads --- include/fmt/core.h | 3 ++- include/fmt/format.h | 40 ++++++++++++++----------------- include/fmt/locale.h | 10 +++----- src/format.cc | 2 +- test/format-test.cc | 56 -------------------------------------------- 5 files changed, 23 insertions(+), 88 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 2d657a8d..531a93aa 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1943,7 +1943,8 @@ FMT_API std::string vformat(string_view format_str, format_args args); template buffer_appender vformat_to( buffer& buf, basic_string_view format_str, - basic_format_args)> args); + basic_format_args)> args, + detail::locale_ref loc = {}); template ::value)> diff --git a/include/fmt/format.h b/include/fmt/format.h index b98c3004..eaae6d36 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3579,25 +3579,6 @@ FMT_CONSTEXPR void advance_to( ctx.advance_to(ctx.begin() + (p - &*ctx.begin())); } -/** Formats arguments and writes the output to the range. */ -template -typename Context::iterator vformat_to( - typename ArgFormatter::iterator out, basic_string_view format_str, - basic_format_args args, - detail::locale_ref loc = detail::locale_ref()) { - if (format_str.size() == 2 && detail::equal2(format_str.data(), "{}")) { - auto arg = args.get(0); - if (!arg) detail::error_handler().on_error("argument not found"); - using iterator = typename ArgFormatter::iterator; - return visit_format_arg( - detail::default_arg_formatter{out, args, loc}, arg); - } - detail::format_handler h(out, format_str, args, - loc); - detail::parse_format_string(format_str, h); - return h.context.out(); -} - /** \rst Converts ``p`` to ``const void*`` for pointer formatting. @@ -3767,14 +3748,27 @@ std::basic_string to_string(const basic_memory_buffer& buf) { template detail::buffer_appender detail::vformat_to( detail::buffer& buf, basic_string_view format_str, - basic_format_args>> args) { - using af = arg_formatter::iterator, Char>; - return vformat_to(buffer_appender(buf), format_str, args); + basic_format_args>> args, + detail::locale_ref loc) { + using iterator = typename buffer_context::iterator; + auto out = buffer_appender(buf); + if (format_str.size() == 2 && detail::equal2(format_str.data(), "{}")) { + auto arg = args.get(0); + if (!arg) detail::error_handler().on_error("argument not found"); + return visit_format_arg( + detail::default_arg_formatter{out, args, loc}, arg); + } + detail::format_handler, Char, + buffer_context> + h(out, format_str, args, loc); + detail::parse_format_string(format_str, h); + return h.context.out(); } #ifndef FMT_HEADER_ONLY extern template format_context::iterator detail::vformat_to( - detail::buffer&, string_view, basic_format_args); + detail::buffer&, string_view, basic_format_args, + detail::locale_ref); namespace detail { extern template FMT_API std::string grouping_impl(locale_ref loc); extern template FMT_API std::string grouping_impl(locale_ref loc); diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 3f5d7519..aeda64aa 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -20,9 +20,8 @@ typename buffer_context::iterator vformat_to( const std::locale& loc, buffer& buf, basic_string_view format_str, basic_format_args>> args) { - using af = arg_formatter::iterator, Char>; - return vformat_to(buffer_appender(buf), to_string_view(format_str), - args, detail::locale_ref(loc)); + return vformat_to(buf, to_string_view(format_str), args, + detail::locale_ref(loc)); } template @@ -56,10 +55,7 @@ inline OutputIt vformat_to( OutputIt out, const std::locale& loc, const S& format_str, basic_format_args>> args) { decltype(detail::get_buffer(out)) buf(detail::get_buffer_init(out)); - using af = - detail::arg_formatter::iterator, Char>; - vformat_to(detail::buffer_appender(buf), to_string_view(format_str), - args, detail::locale_ref(loc)); + vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc)); return detail::get_iterator(buf); } diff --git a/src/format.cc b/src/format.cc index a64a1f38..7f0b6882 100644 --- a/src/format.cc +++ b/src/format.cc @@ -46,7 +46,7 @@ template FMT_API void detail::buffer::append(const char*, const char*); template FMT_API FMT_BUFFER_CONTEXT(char)::iterator detail::vformat_to( detail::buffer&, string_view, - basic_format_args); + basic_format_args, detail::locale_ref); template FMT_API int detail::snprintf_float(double, int, detail::float_specs, detail::buffer&); diff --git a/test/format-test.cc b/test/format-test.cc index 365c1594..192fcf7e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1842,62 +1842,6 @@ TEST(FormatTest, StrongEnum) { } #endif -using buffer_iterator = fmt::format_context::iterator; - -class mock_arg_formatter - : public fmt::detail::arg_formatter_base { - private: -#if FMT_USE_INT128 - MOCK_METHOD1(call, void(__int128_t value)); -#else - MOCK_METHOD1(call, void(long long value)); -#endif - - public: - using base = fmt::detail::arg_formatter_base; - - mock_arg_formatter(fmt::format_context& ctx, fmt::format_parse_context*, - fmt::format_specs* s = nullptr, const char* = nullptr) - : base(ctx.out(), s, ctx.locale()) { - EXPECT_CALL(*this, call(42)); - } - - template - typename std::enable_if::value && - fmt::detail::is_signed::value, - iterator>::type - operator()(T value) { - call(value); - return base::operator()(value); - } - - template - typename std::enable_if::value && - fmt::detail::is_signed::value), - iterator>::type - operator()(T value) { - return base::operator()(value); - } - - iterator operator()(fmt::basic_format_arg::handle) { - return base::operator()(fmt::monostate()); - } -}; - -static void custom_vformat(fmt::string_view format_str, fmt::format_args args) { - fmt::memory_buffer buf; - fmt::vformat_to(fmt::detail::buffer_appender(buf), - format_str, args); -} - -template -void custom_format(const char* format_str, const Args&... args) { - auto va = fmt::make_format_args(args...); - return custom_vformat(format_str, va); -} - -TEST(FormatTest, CustomArgFormatter) { custom_format("{}", 42); } - TEST(FormatTest, NonNullTerminatedFormatString) { EXPECT_EQ("42", format(string_view("{}foo", 2), 42)); }