From 3f75e2b69eaf6bdb5a2ce99bc820b0c57dda5b4d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 28 Aug 2019 06:18:34 -0700 Subject: [PATCH] Make buffer_range public and update custom formatting docs (#1281) --- doc/api.rst | 12 ++++++------ include/fmt/compile.h | 2 +- include/fmt/format.h | 24 ++++++++++++------------ include/fmt/printf.h | 3 +-- test/custom-formatter-test.cc | 4 ++-- test/format | 2 +- test/format-test.cc | 4 ++-- test/ostream-test.cc | 2 +- test/printf-test.cc | 3 +-- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index b5adf4b3..dad51f33 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -278,21 +278,21 @@ 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>; + 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_specs *spec = nullptr) - : arg_formatter(ctx, spec) {} + 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 (spec().type() == 'x') + if (specs() && specs()->type == 'x') return (*this)(static_cast(value)); // convert to unsigned and format return arg_formatter::operator()(value); } diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 82625bbc..082b0762 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -426,7 +426,7 @@ template std::basic_string format(const CompiledFormat& cf, const Args&... args) { basic_memory_buffer buffer; - using range = internal::buffer_range; + using range = buffer_range; using context = buffer_context; cf.template vformat_to(range(buffer), {make_format_args(args...)}); diff --git a/include/fmt/format.h b/include/fmt/format.h index 6762e6bc..08cf38dd 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -413,17 +413,6 @@ class output_range { sentinel end() const { return {}; } // Sentinel is not used yet. }; -// A range with an iterator appending to a buffer. -template -class buffer_range - : public output_range>, T> { - public: - using iterator = std::back_insert_iterator>; - using output_range::output_range; - buffer_range(buffer& buf) - : output_range(std::back_inserter(buf)) {} -}; - template inline size_t count_code_points(basic_string_view s) { return s.size(); @@ -478,6 +467,17 @@ void buffer::append(const U* begin, const U* end) { } } // namespace internal +// A range with an iterator appending to a buffer. +template +class buffer_range : public internal::output_range< + std::back_insert_iterator>, T> { + public: + using iterator = std::back_insert_iterator>; + using internal::output_range::output_range; + buffer_range(internal::buffer& buf) + : internal::output_range(std::back_inserter(buf)) {} +}; + // A UTF-8 string view. class u8string_view : public basic_string_view { public: @@ -2598,7 +2598,7 @@ template using basic_writer FMT_DEPRECATED_ALIAS = internal::basic_writer; using writer FMT_DEPRECATED_ALIAS = internal::writer; using wwriter FMT_DEPRECATED_ALIAS = - internal::basic_writer>; + internal::basic_writer>; /** The default argument formatter. */ template diff --git a/include/fmt/printf.h b/include/fmt/printf.h index c803aa95..a0c2d14a 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -368,8 +368,7 @@ template class basic_printf_context { } /** Formats stored arguments and writes the output to the range. */ - template >> + template >> OutputIt format(); }; diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index db1e199c..36007cae 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::arg_formatter> { public: - using range = fmt::internal::buffer_range; + using range = fmt::buffer_range; typedef fmt::arg_formatter base; custom_arg_formatter(fmt::format_context& ctx, diff --git a/test/format b/test/format index d9472f92..f775022f 100644 --- a/test/format +++ b/test/format @@ -718,7 +718,7 @@ template string vformat(string_view fmt, format_args args) { fmt::memory_buffer mbuf; fmt::internal::buffer& buf = mbuf; - using range = fmt::internal::buffer_range; + using range = fmt::buffer_range; detail::format_handler, char, format_context> h(range(std::back_inserter(buf)), fmt, args, {}); fmt::internal::parse_format_string(fmt::to_string_view(fmt), h); diff --git a/test/format-test.cc b/test/format-test.cc index dfc265fc..ffa0a9b2 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -99,7 +99,7 @@ void std_format(long double value, std::wstring& result) { template ::testing::AssertionResult check_write(const T& value, const char* type) { fmt::basic_memory_buffer buffer; - using range = fmt::internal::buffer_range; + using range = fmt::buffer_range; basic_writer writer(buffer); writer.write(value); std::basic_string actual = to_string(buffer); @@ -1911,7 +1911,7 @@ enum TestFixedEnum : short { B }; TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); } #endif -using buffer_range = fmt::internal::buffer_range; +using buffer_range = fmt::buffer_range; class mock_arg_formatter : public fmt::internal::arg_formatter_base { diff --git a/test/ostream-test.cc b/test/ostream-test.cc index fb88ad41..8c2e814a 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -64,7 +64,7 @@ TEST(OStreamTest, Enum) { EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum())); } -using range = fmt::internal::buffer_range; +using range = fmt::buffer_range; struct test_arg_formatter : fmt::arg_formatter { fmt::format_parse_context parse_ctx; diff --git a/test/printf-test.cc b/test/printf-test.cc index aa25a592..47a07692 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -561,8 +561,7 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) { #endif } -typedef fmt::printf_arg_formatter> - formatter_t; +typedef fmt::printf_arg_formatter> formatter_t; typedef fmt::basic_printf_context context_t; // A custom printf argument formatter that doesn't print `-` for floating-point