From c26d7311f44eecf12bfb68e34d113ab720fe756b Mon Sep 17 00:00:00 2001 From: Jamboree Date: Sat, 6 Jun 2015 14:05:08 +0800 Subject: [PATCH] Add FMT_CAPTURE_W --- format.h | 4 ++++ test/format-test.cc | 3 +++ 2 files changed, 7 insertions(+) diff --git a/format.h b/format.h index a9007de9..6e8484a9 100644 --- a/format.h +++ b/format.h @@ -2940,6 +2940,8 @@ inline internal::NamedArg arg(WStringRef name, const T &arg) { #define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id) +#define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L#id, id) + /** \rst Convenient macro to capture the arguments' names and values into several @@ -2956,6 +2958,8 @@ inline internal::NamedArg arg(WStringRef name, const T &arg) { */ #define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__) +#define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__) + namespace fmt { FMT_VARIADIC(std::string, format, StringRef) FMT_VARIADIC_W(std::wstring, format, WStringRef) diff --git a/test/format-test.cc b/test/format-test.cc index 99472fb9..6936fb2a 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -613,6 +613,7 @@ TEST(FormatterTest, NamedArg) { EXPECT_EQ("BBAACC", format("{1}{b}{0}{a}{2}{c}", FMT_CAPTURE(a, b, c))); EXPECT_EQ(" A", format("{a:>2}", FMT_CAPTURE(a))); EXPECT_THROW_MSG(format("{a+}", FMT_CAPTURE(a)), FormatError, "missing '}' in format string"); + EXPECT_THROW_MSG(format("{a}"), FormatError, "argument not found"); EXPECT_THROW_MSG(format("{d}", FMT_CAPTURE(a, b, c)), FormatError, "argument not found"); EXPECT_THROW_MSG(format("{a}{}", FMT_CAPTURE(a)), FormatError, "cannot switch from manual to automatic argument indexing"); @@ -620,6 +621,8 @@ TEST(FormatterTest, NamedArg) { FormatError, "cannot switch from automatic to manual argument indexing"); EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4))); EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2))); + int n = 100; + EXPECT_EQ(L"n=100", format(L"n={n}", FMT_CAPTURE_W(n))); } TEST(FormatterTest, AutoArgIndex) {