From 81bd9e8ea3fb47b3484ef1a5e68d25beb0f2a7a2 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 3 Dec 2017 07:32:04 -0800 Subject: [PATCH] args -> format_args --- include/fmt/format.cc | 12 +++---- include/fmt/format.h | 68 ++++++++++++++++++----------------- include/fmt/ostream.cc | 3 +- include/fmt/ostream.h | 2 +- include/fmt/posix.h | 8 ++--- include/fmt/printf.cc | 3 +- include/fmt/printf.h | 10 +++--- test/custom-formatter-test.cc | 2 +- test/format-test.cc | 4 +-- test/ostream-test.cc | 2 +- test/util-test.cc | 6 ++-- 11 files changed, 62 insertions(+), 58 deletions(-) diff --git a/include/fmt/format.cc b/include/fmt/format.cc index 03dad13e..de7589b6 100644 --- a/include/fmt/format.cc +++ b/include/fmt/format.cc @@ -219,7 +219,7 @@ void report_error(FormatFunc func, int error_code, } // namespace FMT_FUNC void system_error::init( - int err_code, string_view format_str, args args) { + int err_code, string_view format_str, format_args args) { error_code_ = err_code; memory_buffer buffer; format_system_error(buffer, err_code, vformat(format_str, args)); @@ -412,17 +412,17 @@ FMT_FUNC void report_windows_error( } #endif -FMT_FUNC void vprint(std::FILE *f, string_view format_str, args args) { +FMT_FUNC void vprint(std::FILE *f, string_view format_str, format_args args) { memory_buffer buffer; vformat_to(buffer, format_str, args); std::fwrite(buffer.data(), 1, buffer.size(), f); } -FMT_FUNC void vprint(string_view format_str, args args) { +FMT_FUNC void vprint(string_view format_str, format_args args) { vprint(stdout, format_str, args); } -FMT_FUNC void vprint_colored(Color c, string_view format, args args) { +FMT_FUNC void vprint_colored(Color c, string_view format, format_args args) { char escape[] = "\x1b[30m"; escape[3] = static_cast('0' + c); std::fputs(escape, stdout); @@ -438,7 +438,7 @@ template struct internal::basic_data; template void basic_fixed_buffer::grow(std::size_t); -template void internal::arg_map::init(const args &args); +template void internal::arg_map::init(const format_args &args); template int internal::char_traits::format_float( char *buffer, std::size_t size, const char *format, @@ -454,7 +454,7 @@ template class basic_context; template void basic_fixed_buffer::grow(std::size_t); -template void internal::arg_map::init(const wargs &args); +template void internal::arg_map::init(const wformat_args &args); template int internal::char_traits::format_float( wchar_t *buffer, std::size_t size, const wchar_t *format, diff --git a/include/fmt/format.h b/include/fmt/format.h index 5005c279..b168bbce 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1129,10 +1129,11 @@ struct string_value { std::size_t size; }; -template +template struct custom_value { typedef void (*format_func)( - basic_buffer &buffer, const void *arg, void *ctx); + basic_buffer &buffer, + const void *arg, Context &ctx); const void *value; format_func format; @@ -1226,7 +1227,7 @@ class value { string_value string; string_value sstring; string_value ustring; - custom_value custom; + custom_value custom; }; constexpr value() : int_value(0) {} @@ -1343,8 +1344,7 @@ class value { // Formats an argument of a custom type, such as a user-defined class. template static void format_custom_arg( - basic_buffer &buffer, const void *arg, void *context) { - Context &ctx = *static_cast(context); + basic_buffer &buffer, const void *arg, Context &ctx) { // Get the formatter type through the context to allow different contexts // have different extension points, e.g. `formatter` for `format` and // `printf_formatter` for `printf`. @@ -1365,7 +1365,7 @@ constexpr basic_arg make_arg(const T &value); struct monostate {}; template -class basic_args; +class basic_format_args; // A formatting argument. It is a trivially copyable/constructible type to // allow storage in basic_memory_buffer. @@ -1382,7 +1382,7 @@ class basic_arg { friend constexpr typename std::result_of::type visit(Visitor &&vis, basic_arg arg); - friend class basic_args; + friend class basic_format_args; friend class internal::arg_map; using char_type = typename Context::char_type; @@ -1390,15 +1390,15 @@ class basic_arg { public: class handle { public: - explicit handle(internal::custom_value custom) + explicit handle(internal::custom_value custom) : custom_(custom) {} void format(basic_buffer &buf, Context &ctx) { - custom_.format(buf, custom_.value, &ctx); + custom_.format(buf, custom_.value, ctx); } private: - internal::custom_value custom_; + internal::custom_value custom_; }; constexpr basic_arg() : type_(internal::NONE) {} @@ -1548,7 +1548,7 @@ inline arg_store make_args(const Args & ... args) { /** Formatting arguments. */ template -class basic_args { +class basic_format_args { public: typedef unsigned size_type; typedef basic_arg format_arg; @@ -1597,10 +1597,10 @@ class basic_args { } public: - basic_args() : types_(0) {} + basic_format_args() : types_(0) {} template - basic_args(const arg_store &store) + basic_format_args(const arg_store &store) : types_(store.TYPES) { set_data(store.data()); } @@ -1613,8 +1613,8 @@ class basic_args { } }; -typedef basic_args args; -typedef basic_args wargs; +typedef basic_format_args format_args; +typedef basic_format_args wformat_args; enum alignment { ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC @@ -1924,7 +1924,7 @@ class arg_map { MapType map_; public: - void init(const basic_args &args); + void init(const basic_format_args &args); const basic_arg *find(const fmt::basic_string_view &name) const { @@ -1939,7 +1939,7 @@ class arg_map { }; template -void arg_map::init(const basic_args &args) { +void arg_map::init(const basic_format_args &args) { if (!map_.empty()) return; typedef internal::named_arg NamedArg; @@ -2102,16 +2102,17 @@ class arg_formatter_base { template class context_base : public basic_parse_context{ private: - basic_args args_; + basic_format_args args_; protected: typedef basic_arg format_arg; - context_base(basic_string_view format_str, basic_args args) + context_base(basic_string_view format_str, + basic_format_args args) : basic_parse_context(format_str), args_(args) {} ~context_base() {} - basic_args args() const { return args_; } + basic_format_args args() const { return args_; } // Returns the argument with specified index. format_arg do_get_arg(unsigned arg_id) { @@ -2843,7 +2844,7 @@ class basic_context : \endrst */ basic_context( - basic_string_view format_str, basic_args args) + basic_string_view format_str, basic_format_args args) : Base(format_str, args) {} format_arg next_arg() { return this->do_get_arg(this->next_arg_id()); } @@ -2860,7 +2861,7 @@ class basic_context : */ class system_error : public std::runtime_error { private: - void init(int err_code, string_view format_str, args args); + void init(int err_code, string_view format_str, format_args args); protected: int error_code_; @@ -3553,7 +3554,7 @@ FMT_API void report_system_error(int error_code, /** A Windows error. */ class windows_error : public system_error { private: - FMT_API void init(int error_code, string_view format_str, args args); + FMT_API void init(int error_code, string_view format_str, format_args args); public: /** @@ -3599,7 +3600,7 @@ FMT_API void report_windows_error(int error_code, enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; -FMT_API void vprint_colored(Color c, string_view format, args args); +FMT_API void vprint_colored(Color c, string_view format, format_args args); /** Formats a string and prints it to stdout using ANSI escape sequences @@ -3615,13 +3616,14 @@ inline void print_colored(Color c, string_view format_str, template void vformat_to(basic_buffer &buffer, basic_string_view format_str, - basic_args args); + basic_format_args args); -inline void vformat_to(buffer &buf, string_view format_str, args args) { +inline void vformat_to(buffer &buf, string_view format_str, format_args args) { vformat_to>(buf, format_str, args); } -inline void vformat_to(wbuffer &buf, wstring_view format_str, wargs args) { +inline void vformat_to(wbuffer &buf, wstring_view format_str, + wformat_args args) { vformat_to>(buf, format_str, args); } @@ -3637,7 +3639,7 @@ inline void format_to(wbuffer &buf, wstring_view format_str, vformat_to(buf, format_str, make_args(args...)); } -inline std::string vformat(string_view format_str, args args) { +inline std::string vformat(string_view format_str, format_args args) { memory_buffer buffer; vformat_to(buffer, format_str, args); return to_string(buffer); @@ -3668,7 +3670,7 @@ inline typename std::enable_if< return vformat(format_str.value(), make_args(args...)); } -inline std::wstring vformat(wstring_view format_str, wargs args) { +inline std::wstring vformat(wstring_view format_str, wformat_args args) { wmemory_buffer buffer; vformat_to(buffer, format_str, args); return to_string(buffer); @@ -3679,7 +3681,7 @@ inline std::wstring format(wstring_view format_str, const Args & ... args) { return vformat(format_str, make_args(args...)); } -FMT_API void vprint(std::FILE *f, string_view format_str, args args); +FMT_API void vprint(std::FILE *f, string_view format_str, format_args args); /** \rst @@ -3696,7 +3698,7 @@ inline void print(std::FILE *f, string_view format_str, vprint(f, format_str, make_args(args...)); } -FMT_API void vprint(string_view format_str, args args); +FMT_API void vprint(string_view format_str, format_args args); /** \rst @@ -4004,12 +4006,12 @@ inline typename basic_context::format_arg /** Formats arguments and writes the output to the buffer. */ template void vformat_to(basic_buffer &buffer, basic_string_view format_str, - basic_args args) { + basic_format_args args) { using iterator = internal::null_terminating_iterator; struct handler : internal::error_handler { handler(basic_buffer &b, basic_string_view str, - basic_args format_args) + basic_format_args format_args) : buffer(b), context(str, format_args) {} void on_text(iterator begin, iterator end) { diff --git a/include/fmt/ostream.cc b/include/fmt/ostream.cc index d7bbb912..0810f254 100644 --- a/include/fmt/ostream.cc +++ b/include/fmt/ostream.cc @@ -27,7 +27,8 @@ FMT_FUNC void write(std::ostream &os, buffer &buf) { } } -FMT_FUNC void vprint(std::ostream &os, string_view format_str, args args) { +FMT_FUNC void vprint(std::ostream &os, string_view format_str, + format_args args) { memory_buffer buffer; vformat_to(buffer, format_str, args); internal::write(os, buffer); diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 66af91b4..af0e91ff 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -100,7 +100,7 @@ struct formatter - inline void print(string_view format_str, const Args & ... format_args) { - vprint(format_str, make_args(format_args...)); + inline void print(string_view format_str, const Args & ... args) { + vprint(format_str, make_args(args...)); } }; diff --git a/include/fmt/printf.cc b/include/fmt/printf.cc index 53d4c0b9..bc43bbf9 100644 --- a/include/fmt/printf.cc +++ b/include/fmt/printf.cc @@ -3,7 +3,8 @@ namespace fmt { template -void printf(basic_writer &w, basic_string_view format, args args); +void printf(basic_writer &w, basic_string_view format, + format_args args); FMT_FUNC int vfprintf(std::FILE *f, string_view format, printf_args args) { memory_buffer buffer; diff --git a/include/fmt/printf.h b/include/fmt/printf.h index e43e9404..1f9731a6 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -330,8 +330,8 @@ class printf_context : appropriate lifetimes. \endrst */ - printf_context( - basic_string_view format_str, basic_args args) + printf_context(basic_string_view format_str, + basic_format_args args) : Base(format_str, args) {} using Base::parse_context; @@ -516,11 +516,11 @@ void printf_context::format(basic_buffer &buffer) { template void printf(basic_buffer &buf, basic_string_view format, - basic_args> args) { + basic_format_args> args) { printf_context(format, args).format(buf); } -typedef basic_args> printf_args; +typedef basic_format_args> printf_args; inline std::string vsprintf(string_view format, printf_args args) { memory_buffer buffer; @@ -543,7 +543,7 @@ inline std::string sprintf(string_view format_str, const Args & ... args) { } inline std::wstring vsprintf( - wstring_view format, basic_args> args) { + wstring_view format, basic_format_args> args) { wmemory_buffer buffer; printf(buffer, format, args); return to_string(buffer); diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index 129d26e1..0787a295 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -29,7 +29,7 @@ class CustomArgFormatter : public fmt::arg_formatter { } }; -std::string custom_vformat(fmt::string_view format_str, fmt::args args) { +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 vwrite. fmt::vformat_to(buffer, format_str, args); diff --git a/test/format-test.cc b/test/format-test.cc index c078365b..0156bbf2 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1425,7 +1425,7 @@ TEST(StrTest, Convert) { EXPECT_EQ("2012-12-9", s); } -std::string vformat_message(int id, const char *format, fmt::args args) { +std::string vformat_message(int id, const char *format, fmt::format_args args) { fmt::memory_buffer buffer; format_to(buffer, "[{}] ", id); vformat_to(buffer, format, args); @@ -1512,7 +1512,7 @@ class MockArgFormatter : public fmt::internal::arg_formatter_base { void operator()(fmt::basic_arg::handle) {} }; -void custom_vformat(fmt::string_view format_str, fmt::args args) { +void custom_vformat(fmt::string_view format_str, fmt::format_args args) { fmt::memory_buffer buffer; fmt::vformat_to(buffer, format_str, args); } diff --git a/test/ostream-test.cc b/test/ostream-test.cc index cb18388b..f7d576ee 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -65,7 +65,7 @@ struct TestArgFormatter : fmt::arg_formatter { TEST(OStreamTest, CustomArg) { fmt::memory_buffer buffer; - fmt::context ctx("", fmt::args()); + fmt::context ctx("", fmt::format_args()); fmt::format_specs spec; TestArgFormatter af(buffer, ctx, spec); visit(af, fmt::internal::make_arg(TestEnum())); diff --git a/test/util-test.cc b/test/util-test.cc index 952181d6..566044c3 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -427,7 +427,7 @@ TEST(UtilTest, Increment) { } TEST(UtilTest, FormatArgs) { - fmt::args args; + fmt::format_args args; EXPECT_FALSE(args[1]); } @@ -456,7 +456,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) { fmt::internal::value arg(t); CustomContext ctx = {false}; fmt::memory_buffer buffer; - arg.custom.format(buffer, &t, &ctx); + arg.custom.format(buffer, &t, ctx); EXPECT_TRUE(ctx.called); } @@ -595,7 +595,7 @@ TEST(UtilTest, CustomArg) { testing::StrictMock v; EXPECT_CALL(v, visit(_)).WillOnce(testing::Invoke([&](handle h) { fmt::memory_buffer buffer; - fmt::context ctx("", fmt::args()); + fmt::context ctx("", fmt::format_args()); h.format(buffer, ctx); EXPECT_EQ("test", std::string(buffer.data(), buffer.size())); return visitor::Result();