Skip to content

Commit

Permalink
Fix issue fmtlib#3817
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkiage committed Jan 19, 2024
1 parent 0147e08 commit b089024
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,12 @@ template <typename Char> class basic_format_parse_context {
do_check_arg_id(id);
}
FMT_CONSTEXPR void check_arg_id(basic_string_view<Char>) {}
FMT_CONSTEXPR void increment_next_id_if_match(int id) {
if (next_arg_id_ == id) {
// Skip named args.
next_arg_id_++;
}
}
FMT_CONSTEXPR void check_dynamic_spec(int arg_id);
};

Expand Down
1 change: 1 addition & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4269,6 +4269,7 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
int arg_id = context.arg_id(id);
if (arg_id < 0) report_error("argument not found");
parse_context.increment_next_id_if_match(arg_id);
return arg_id;
}

Expand Down
9 changes: 9 additions & 0 deletions test/args-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ TEST(args_test, named_arg_by_ref) {
EXPECT_EQ(fmt::vformat("{band}", store), "Rolling Scones");
}

TEST(args_test, named_with_unnamed) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(1);
store.push_back(fmt::arg("a1", 2));
store.push_back(fmt::arg("a2", 3));
store.push_back(4);
EXPECT_EQ("1 2 3 4", fmt::vformat("{} {a1} {a2} {}", store));
}

TEST(args_test, named_custom_format) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
auto c = custom_type();
Expand Down

0 comments on commit b089024

Please sign in to comment.