From f13906f4084cec10b7701ce0e189c0e372f90b55 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 19 Jun 2019 13:51:36 -0700 Subject: [PATCH] back_insert_range -> buffer_range --- include/fmt/format.h | 52 ++++++++++++++++++----------------- include/fmt/locale.h | 2 +- include/fmt/prepare.h | 6 ++-- include/fmt/printf.h | 2 +- test/custom-formatter-test.cc | 5 ++-- test/format | 2 +- test/format-test.cc | 4 +-- test/ostream-test.cc | 2 +- test/printf-test.cc | 3 +- 9 files changed, 39 insertions(+), 39 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 41f0112f..505c309a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -191,10 +191,6 @@ FMT_END_NAMESPACE FMT_BEGIN_NAMESPACE namespace internal { -#ifndef FMT_USE_GRISU -# define FMT_USE_GRISU 1 -#endif - // A fallback implementation of uintptr_t for systems that lack it. struct fallback_uintptr { unsigned char value[sizeof(void*)]; @@ -205,11 +201,6 @@ using uintptr_t = ::uintptr_t; using uintptr_t = fallback_uintptr; #endif -template inline bool use_grisu() { - return FMT_USE_GRISU && std::numeric_limits::is_iec559 && - sizeof(T) <= sizeof(double); -} - // An equivalent of `*reinterpret_cast(&source)` that doesn't produce // undefined behavior (e.g. due to type aliasing). // Example: uint64_t d = bit_cast(2.718); @@ -224,6 +215,30 @@ inline Dest bit_cast(const Source& source) { // An implementation of iterator_t for pre-C++20 systems. template using iterator_t = decltype(std::begin(std::declval())); + +#ifndef FMT_USE_GRISU +# define FMT_USE_GRISU 1 +#endif + +template inline bool use_grisu() { + return FMT_USE_GRISU && std::numeric_limits::is_iec559 && + sizeof(T) <= sizeof(double); +} + +// A range whose output iterator appends to a buffer. +template class buffer_range { + public: + using value_type = T; + using iterator = std::back_insert_iterator>; + + private: + iterator begin_; + + public: + buffer_range(internal::buffer& buf) : begin_(std::back_inserter(buf)) {} + explicit buffer_range(iterator it) : begin_(it) {} + iterator begin() const { return begin_; } +}; } // namespace internal template @@ -243,22 +258,9 @@ class output_range { OutputIt begin() const { return it_; } }; -// A range where begin() returns back_insert_iterator. -template -class back_insert_range - : public output_range> { - using base = output_range>; - - public: - using value_type = typename Container::value_type; - - back_insert_range(Container& c) : base(std::back_inserter(c)) {} - back_insert_range(typename base::iterator it) : base(it) {} -}; - template class basic_writer; -using writer = basic_writer>>; -using wwriter = basic_writer>>; +using writer = basic_writer>; +using wwriter = basic_writer>; /** A formatting error such as invalid format string. */ class format_error : public std::runtime_error { @@ -3284,7 +3286,7 @@ template typename buffer_context::iterator internal::vformat_to( internal::buffer& buf, basic_string_view format_str, basic_format_args> args) { - typedef back_insert_range> range; + using range = buffer_range; return vformat_to>(buf, to_string_view(format_str), args); } diff --git a/include/fmt/locale.h b/include/fmt/locale.h index f3e3d676..dd6def10 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -19,7 +19,7 @@ typename buffer_context::iterator vformat_to( const std::locale& loc, buffer& buf, basic_string_view format_str, basic_format_args> args) { - typedef back_insert_range> range; + using range = buffer_range; return vformat_to>(buf, to_string_view(format_str), args, internal::locale_ref(loc)); } diff --git a/include/fmt/prepare.h b/include/fmt/prepare.h index 5a83ee13..6c149c25 100644 --- a/include/fmt/prepare.h +++ b/include/fmt/prepare.h @@ -214,7 +214,7 @@ class prepared_format { std::basic_string format(const Args&... args) const { basic_memory_buffer buffer; - typedef back_insert_range> range; + using range = buffer_range; this->vformat_to(range(buffer), basic_format_args{ make_args_checked(format_, args...)}); return to_string(buffer); @@ -224,7 +224,7 @@ class prepared_format { inline std::back_insert_iterator format_to( std::back_insert_iterator out, const Args&... args) const { internal::container_buffer buffer(internal::get_container(out)); - typedef back_insert_range> range; + using range = buffer_range; this->vformat_to(range(buffer), basic_format_args{ make_args_checked(format_, args...)}); return out; @@ -241,7 +241,7 @@ class prepared_format { template inline typename buffer_context::iterator format_to( basic_memory_buffer& buf, const Args&... args) const { - typedef back_insert_range> range; + using range = buffer_range; return this->vformat_to( range(buf), basic_format_args{make_args_checked(format_, args...)}); diff --git a/include/fmt/printf.h b/include/fmt/printf.h index d52f849c..6acac0e9 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -368,7 +368,7 @@ template class basic_printf_context { /** Formats stored arguments and writes the output to the range. */ template >>> + printf_arg_formatter>> OutputIt format(); }; diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index b9513e15..8f3ab043 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -18,10 +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::back_insert_range>> { + : public fmt::arg_formatter> { public: - typedef fmt::back_insert_range> range; + using range = fmt::internal::buffer_range; typedef fmt::arg_formatter base; custom_arg_formatter(fmt::format_context& ctx, diff --git a/test/format b/test/format index 60dc60e5..3da26344 100644 --- a/test/format +++ b/test/format @@ -719,7 +719,7 @@ template string vformat(string_view fmt, format_args args) { fmt::memory_buffer mbuf; fmt::internal::buffer& buf = mbuf; - typedef fmt::back_insert_range> range; + using range = fmt::internal::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 5d6df9a8..1c292ba0 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; - typedef fmt::back_insert_range> range; + using range = fmt::internal::buffer_range; fmt::basic_writer writer(buffer); writer.write(value); std::basic_string actual = to_string(buffer); @@ -1894,7 +1894,7 @@ enum TestFixedEnum : short { B }; TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); } #endif -typedef fmt::back_insert_range> buffer_range; +using buffer_range = fmt::internal::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 27c62347..fb88ad41 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())); } -typedef fmt::back_insert_range> range; +using range = fmt::internal::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 5794abb4..77966b5b 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -561,8 +561,7 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) { #endif } -typedef fmt::printf_arg_formatter< - fmt::back_insert_range>> +typedef fmt::printf_arg_formatter> formatter_t; typedef fmt::basic_printf_context context_t;