From b643520c528fb3fc8e541cb1e743a83f660f5673 Mon Sep 17 00:00:00 2001 From: Thomas Novotny Date: Sun, 18 Nov 2018 21:44:09 +0100 Subject: [PATCH] add missing make_printf_args and make_wprintf_args to printf.h add minimal test for make_printf_args and make_wprintf_args to printf-test.cc --- include/fmt/printf.h | 27 +++++++++++++++++++++++++++ test/printf-test.cc | 16 ++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/fmt/printf.h b/include/fmt/printf.h index e8caecaa..4b0b692c 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -581,6 +581,33 @@ struct printf_context { typedef basic_format_args::type> printf_args; typedef basic_format_args::type> wprintf_args; +typedef printf_context::type printf_char_context; +typedef printf_context::type printf_wchar_context; + + +/** + \rst + Constructs an `~fmt::format_arg_store` object that contains references to + arguments and can be implicitly converted to `~fmt::printf_args`. `Context` + can be omitted in which case it defaults to `~fmt::printf_char_context`. + \endrst + */ +template +inline format_arg_store + make_printf_args(const Args &... args) { return {args...}; } + +/** + \rst + Constructs an `~fmt::format_arg_store` object that contains references to + arguments and can be implicitly converted to `~fmt::wprintf_args`. `Context` + can be omitted in which case it defaults to `~fmt::printf_wchar_context`. + \endrst + */ +template +inline format_arg_store + make_wprintf_args(const Args &... args) { return {args...}; } + + template inline std::basic_string vsprintf(const S &format, diff --git a/test/printf-test.cc b/test/printf-test.cc index d3e222f2..c51e7f27 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -518,3 +518,19 @@ void check_format_string_regression(fmt::string_view s, const Args&... args) { TEST(PrintfTest, CheckFormatStringRegression) { check_format_string_regression("%c%s", 'x', ""); } + + +TEST(PrintfTest, VSPrintfMessageExample) { + EXPECT_EQ( + "[42] something happened", + fmt::vsprintf( + "[%d] %s happened", fmt::make_printf_args( 42, "something" ) ) ); +} + + +TEST(PrintfTest, VSPrintfWMessageExample) { + EXPECT_EQ( + L"[42] something happened", + fmt::vsprintf( + L"[%d] %s happened", fmt::make_wprintf_args( 42, L"something" ) ) ); +}