Skip to content

Commit

Permalink
Allow getting size of dynamic format arg store (#4270)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-harnisch authored Dec 19, 2024
1 parent 873670b commit 7c50da5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/fmt/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ template <typename Context> class dynamic_format_arg_store {
data_.reserve(new_cap);
named_info_.reserve(new_cap_named);
}

/// Returns the number of elements in the store.
size_t size() const noexcept { return data_.size(); }
};

FMT_END_NAMESPACE
Expand Down
14 changes: 14 additions & 0 deletions test/args-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,17 @@ TEST(args_test, move_constructor) {
store.reset();
EXPECT_EQ(fmt::vformat("{} {} {a1}", moved_store), "42 foo foo");
}

TEST(args_test, size) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
EXPECT_EQ(store.size(), 0);

store.push_back(42);
EXPECT_EQ(store.size(), 1);

store.push_back("Molybdenum");
EXPECT_EQ(store.size(), 2);

store.clear();
EXPECT_EQ(store.size(), 0);
}

0 comments on commit 7c50da5

Please sign in to comment.