Skip to content

Commit

Permalink
Add missing presentation type checks for std::string (#2402)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 2, 2021
1 parent 889bbf2 commit 1d73845
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2577,8 +2577,8 @@ FMT_CONSTEXPR auto check_cstring_type_spec(Char spec, ErrorHandler&& eh = {})
return false;
}

template <typename Char, typename ErrorHandler>
FMT_CONSTEXPR void check_string_type_spec(Char spec, ErrorHandler&& eh) {
template <typename Char, typename ErrorHandler = error_handler>
FMT_CONSTEXPR void check_string_type_spec(Char spec, ErrorHandler&& eh = {}) {
if (spec != 0 && spec != 's') eh.on_error("invalid type specifier");
}

Expand Down
1 change: 1 addition & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,7 @@ FMT_CONSTEXPR auto write(OutputIt out,
basic_string_view<type_identity_t<Char>> s,
const basic_format_specs<Char>& specs, locale_ref)
-> OutputIt {
check_string_type_spec(specs.type);
return write(out, s, specs);
}
template <typename Char, typename OutputIt>
Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,8 @@ TEST(format_test, format_pointer) {

TEST(format_test, format_string) {
EXPECT_EQ("test", fmt::format("{0}", std::string("test")));
EXPECT_THROW(fmt::format(fmt::runtime("{:x}"), std::string("test")),
fmt::format_error);
}

TEST(format_test, format_string_view) {
Expand Down

0 comments on commit 1d73845

Please sign in to comment.