mirror of
https://github.com/fmtlib/fmt.git
synced 2025-08-01 03:34:45 +02:00
Report error on duplicate named arg names (#4367)
This commit is contained in:
@@ -1064,13 +1064,28 @@ template <typename Char> struct named_arg_info {
|
||||
int id;
|
||||
};
|
||||
|
||||
template <typename Char>
|
||||
FMT_CONSTEXPR void check_for_duplicate(named_arg_info<Char>* named_args,
|
||||
int named_arg_index,
|
||||
basic_string_view<Char> arg_name) {
|
||||
if (named_arg_index <= 0) return;
|
||||
|
||||
for (auto i = 0; i < named_arg_index; ++i) {
|
||||
if (basic_string_view<Char>(named_args[i].name) == arg_name) {
|
||||
report_error("duplicate named args found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Char, typename T, FMT_ENABLE_IF(!is_named_arg<T>::value)>
|
||||
void init_named_arg(named_arg_info<Char>*, int& arg_index, int&, const T&) {
|
||||
++arg_index;
|
||||
}
|
||||
|
||||
template <typename Char, typename T, FMT_ENABLE_IF(is_named_arg<T>::value)>
|
||||
void init_named_arg(named_arg_info<Char>* named_args, int& arg_index,
|
||||
int& named_arg_index, const T& arg) {
|
||||
check_for_duplicate<Char>(named_args, named_arg_index, arg.name);
|
||||
named_args[named_arg_index++] = {arg.name, arg_index++};
|
||||
}
|
||||
|
||||
@@ -1084,6 +1099,7 @@ template <typename T, typename Char,
|
||||
FMT_ENABLE_IF(is_static_named_arg<T>::value)>
|
||||
FMT_CONSTEXPR void init_static_named_arg(named_arg_info<Char>* named_args,
|
||||
int& arg_index, int& named_arg_index) {
|
||||
check_for_duplicate<Char>(named_args, named_arg_index, T::name);
|
||||
named_args[named_arg_index++] = {T::name, arg_index++};
|
||||
}
|
||||
|
||||
|
@@ -599,6 +599,8 @@ TEST(format_test, named_arg) {
|
||||
EXPECT_THROW_MSG((void)fmt::format(runtime("{a} {}"), fmt::arg("a", 2), 42),
|
||||
format_error,
|
||||
"cannot switch from manual to automatic argument indexing");
|
||||
EXPECT_THROW_MSG((void)fmt::format("{a}", fmt::arg("a", 1),
|
||||
fmt::arg("a", 10)), format_error, "duplicate named args found");
|
||||
}
|
||||
|
||||
TEST(format_test, auto_arg_index) {
|
||||
|
Reference in New Issue
Block a user