From b1d10a2884c6f18ea5694c315a554a3f87be8c2d Mon Sep 17 00:00:00 2001 From: MikePopoloski Date: Wed, 18 Jul 2018 21:48:35 -0400 Subject: [PATCH] Add support for dynamic arg sets This allows construction of basic_format_args from a dynamic set of arguments. The syntax is a little clunky and could probably be improved but this at least enables the functionality. --- include/fmt/core.h | 10 ++++++++++ test/format-test.cc | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/fmt/core.h b/include/fmt/core.h index 404c03ef6146..b9f62e5e9ae5 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1145,6 +1145,16 @@ class basic_format_args { set_data(store.data_); } + /** + \rst + Constructs a `basic_format_args` object from a dynamic set of arguments. + \endrst + */ + basic_format_args(const format_arg *args, size_type count) + : types_(-(int64_t)count) { + set_data(args); + } + /** Returns the argument at specified index. */ format_arg get(size_type index) const { format_arg arg = do_get(index); diff --git a/test/format-test.cc b/test/format-test.cc index f6eecc727d9c..b2517e2799ef 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1316,6 +1316,17 @@ TEST(FormatTest, Variadic) { EXPECT_EQ(L"abc1", format(L"{}c{}", L"ab", 1)); } +TEST(FormatTest, Dynamic) { + using ctx = fmt::format_context; + std::vector> args; + args.emplace_back(fmt::internal::make_arg(42)); + args.emplace_back(fmt::internal::make_arg("abc1")); + args.emplace_back(fmt::internal::make_arg(1.2f)); + + std::string result = fmt::vformat("{} and {} and {}", fmt::basic_format_args(args.data(), (unsigned)args.size())); + EXPECT_EQ("42 and abc1 and 1.2", result); +} + TEST(FormatTest, JoinArg) { using fmt::join; int v1[3] = { 1, 2, 3 };