diff --git a/include/fmt/format.h b/include/fmt/format.h index 1a327416..729d2752 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2018,18 +2018,18 @@ class context_base : public parse_context{ basic_args args() const { return args_; } // Returns the argument with specified index. - format_arg do_get_arg(unsigned arg_index, const char *&error) { + format_arg do_get_arg(unsigned arg_index) { format_arg arg = args_[arg_index]; - if (!arg && !error) - error = "argument index out of range"; + if (!arg) + this->on_error("argument index out of range"); return arg; } // Checks if manual indexing is used and returns the argument with // specified index. - format_arg get_arg(unsigned arg_index, const char *&error) { + format_arg get_arg(unsigned arg_index) { return this->check_no_auto_index() ? - this->do_get_arg(arg_index, error) : format_arg(); + this->do_get_arg(arg_index) : format_arg(); } public: @@ -2751,19 +2751,11 @@ class basic_context : : Base(format_str, args) {} format_arg next_arg() { - const char *error = 0; - format_arg arg = this->do_get_arg(this->next_arg_index(), error); - if (error) - FMT_THROW(format_error(error)); - return arg; + return this->do_get_arg(this->next_arg_index()); } format_arg get_arg(unsigned arg_index) { - const char *error = 0; - format_arg arg = this->do_get_arg(arg_index, error); - if (error) - FMT_THROW(format_error(error)); - return arg; + return this->do_get_arg(arg_index); } // Checks if manual indexing is used and returns the argument with diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 263fa5ab..850ded74 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -367,18 +367,9 @@ template typename printf_context::format_arg printf_context::get_arg( iterator it, unsigned arg_index) { (void)it; - const char *error = 0; - format_arg arg; - if (arg_index == std::numeric_limits::max()) { - arg_index = this->next_arg_index(); - if (!error) - arg = this->do_get_arg(arg_index, error); - } else { - arg = Base::get_arg(arg_index - 1, error); - } - if (error) - FMT_THROW(format_error(!*it ? "invalid format string" : error)); - return arg; + if (arg_index == std::numeric_limits::max()) + return this->do_get_arg(this->next_arg_index()); + return Base::get_arg(arg_index - 1); } template diff --git a/test/format-test.cc b/test/format-test.cc index 9d19fd5f..dce3240d 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1906,4 +1906,7 @@ TEST(FormatTest, FormatStringErrors) { EXPECT_ERROR("{1}{}", "cannot switch from manual to automatic argument indexing", int, int); + EXPECT_ERROR("{}{1}", + "cannot switch from automatic to manual argument indexing", + int, int); } diff --git a/test/printf-test.cc b/test/printf-test.cc index aabc5e26..c95fc7b9 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -116,7 +116,7 @@ TEST(PrintfTest, InvalidArgIndex) { format_error, "argument index out of range"); EXPECT_THROW_MSG(fmt::sprintf("%2$", 42), - format_error, "invalid format string"); + format_error, "argument index out of range"); EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", BIG_NUM), 42), format_error, "number is too big"); }