Handle nested braces in join (#638)

This commit is contained in:
Victor Zverovich 2018-01-20 06:26:22 -08:00
parent c719d94473
commit 6822466aa3
2 changed files with 15 additions and 9 deletions

View File

@ -4111,10 +4111,14 @@ template <typename ArgFormatter, typename Char, typename It>
void format_arg(fmt::BasicFormatter<Char, ArgFormatter> &f,
const Char *&format_str, const ArgJoin<Char, It>& e) {
const Char* end = format_str;
if (*end == ':')
++end;
while (*end && *end != '}')
int brace_level = 1;
while (*end) {
if (*end == '}' && --brace_level == 0)
break;
if (*end == '{')
++brace_level;
++end;
}
if (*end != '}')
FMT_THROW(FormatError("missing '}' in format string"));

View File

@ -1611,13 +1611,15 @@ TEST(FormatTest, JoinArg) {
v2.push_back(1.2f);
v2.push_back(3.4f);
EXPECT_EQ("(1, 2, 3)", format("({})", join(v1 + 0, v1 + 3, ", ")));
EXPECT_EQ("(1)", format("({})", join(v1 + 0, v1 + 1, ", ")));
EXPECT_EQ("()", format("({})", join(v1 + 0, v1 + 0, ", ")));
EXPECT_EQ("(001, 002, 003)", format("({:03})", join(v1 + 0, v1 + 3, ", ")));
EXPECT_EQ("(+01.20, +03.40)", format("({:+06.2f})", join(v2.begin(), v2.end(), ", ")));
EXPECT_EQ("(1, 2, 3)", format("({})", join(v1, v1 + 3, ", ")));
EXPECT_EQ("(1)", format("({})", join(v1, v1 + 1, ", ")));
EXPECT_EQ("()", format("({})", join(v1, v1, ", ")));
EXPECT_EQ("(001, 002, 003)", format("({:03})", join(v1, v1 + 3, ", ")));
EXPECT_EQ("(+01.20, +03.40)",
format("({:+06.2f})", join(v2.begin(), v2.end(), ", ")));
EXPECT_EQ(L"(1, 2, 3)", format(L"({})", join(v1 + 0, v1 + 3, L", ")));
EXPECT_EQ(L"(1, 2, 3)", format(L"({})", join(v1, v1 + 3, L", ")));
EXPECT_EQ("1, 2, 3", format("{0:{1}}", join(v1, v1 + 3, ", "), 1));
#if FMT_HAS_GXX_CXX11
EXPECT_EQ("(1, 2, 3)", format("({})", join(v1, ", ")));