Skip to content

Commit

Permalink
Fixed arg_formatter_base::write_pointer to not mutate the format specs.
Browse files Browse the repository at this point in the history
This fixes cases where arg_formatters are reused, like with arg_join.
  • Loading branch information
mwinterb authored and vitaut committed May 1, 2018
1 parent 6cd6661 commit ca31ca1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1431,9 +1431,10 @@ class arg_formatter_base {
}

void write_pointer(const void *p) {
specs_.flags_ = HASH_FLAG;
specs_.type_ = 'x';
writer_.write_int(reinterpret_cast<uintptr_t>(p), specs_);
format_specs specs = specs_;
specs.flags_ = HASH_FLAG;
specs.type_ = 'x';
writer_.write_int(reinterpret_cast<uintptr_t>(p), specs);
}

protected:
Expand Down
4 changes: 4 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ TEST(FormatTest, JoinArg) {
std::vector<float> v2;
v2.push_back(1.2f);
v2.push_back(3.4f);
void *v3[2] = { &v1[0], &v1[1] };

EXPECT_EQ("(1, 2, 3)", format("({})", join(v1, v1 + 3, ", ")));
EXPECT_EQ("(1)", format("({})", join(v1, v1 + 1, ", ")));
Expand All @@ -1298,6 +1299,9 @@ TEST(FormatTest, JoinArg) {
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));

EXPECT_EQ(format("{}, {}", v3[0], v3[1]),
format("{}", join(v3, v3 + 2, ", ")));

#if FMT_USE_TRAILING_RETURN && (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 405)
EXPECT_EQ("(1, 2, 3)", format("({})", join(v1, ", ")));
EXPECT_EQ("(+01.20, +03.40)", format("({:+06.2f})", join(v2, ", ")));
Expand Down

0 comments on commit ca31ca1

Please sign in to comment.