Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dynamic_format_arg_store reusable and add reserve() #1677

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,24 @@ class dynamic_format_arg_store
emplace_arg(fmt::arg(arg_name, arg.value));
}
}

/** Erase all elements from the store */
void clear() {
data_.clear();
named_info_.clear();
dynamic_args_ = internal::dynamic_arg_list();
}

/**
Reserves space to store at least *new_cap* arguments including
*new_cap_named* named arguments.
*/
void reserve(size_t new_cap, size_t new_cap_named) {
FMT_ASSERT(new_cap >= new_cap_named,
"Set of argument includes set of named arguments");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument -> arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. thanks for the review.

data_.reserve(new_cap);
named_info_.reserve(new_cap_named);
}
};

/**
Expand Down
26 changes: 26 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,32 @@ TEST(FormatDynArgsTest, NamedCustomFormat) {
EXPECT_EQ("cust=0 and cust=1 and cust=3", result);
}

TEST(FormatDynArgsTest, Clear) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(42);

std::string result = fmt::vformat("{}", store);
EXPECT_EQ("42", result);

store.push_back(43);
result = fmt::vformat("{} and {}", store);
EXPECT_EQ("42 and 43", result);

store.clear();
store.push_back(44);
result = fmt::vformat("{}", store);
EXPECT_EQ("44", result);
}

TEST(FormatDynArgsTest, Reserve) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.reserve(2, 1);
store.push_back(1.5f);
store.push_back(fmt::arg("a1", 42));
std::string result = fmt::vformat("{a1} and {}", store);
EXPECT_EQ("42 and 1.5", result);
}

struct copy_throwable {
copy_throwable() {}
copy_throwable(const copy_throwable&) { throw "deal with it"; }
Expand Down