Fix check_format_string (#925)

This commit is contained in:
Victor Zverovich 2018-11-03 16:20:45 -07:00
parent 36161284e2
commit a23d592472
3 changed files with 21 additions and 11 deletions

View File

@ -2243,7 +2243,7 @@ class format_string_checker {
};
template <typename Char, typename ErrorHandler, typename... Args>
FMT_CONSTEXPR bool check_format_string(
FMT_CONSTEXPR bool do_check_format_string(
basic_string_view<Char> s, ErrorHandler eh = ErrorHandler()) {
format_string_checker<Char, ErrorHandler, Args...> checker(s, eh);
parse_format_string<true>(s, checker);
@ -2254,7 +2254,7 @@ template <typename... Args, typename S>
typename std::enable_if<is_compile_string<S>::value>::type
check_format_string(S format_str) {
typedef typename S::char_type char_t;
FMT_CONSTEXPR_DECL bool invalid_format = internal::check_format_string<
FMT_CONSTEXPR_DECL bool invalid_format = internal::do_check_format_string<
char_t, internal::error_handler, Args...>(to_string_view(format_str));
(void)invalid_format;
}
@ -3583,7 +3583,7 @@ class udl_formatter {
std::basic_string<Char> operator()(const Args &... args) const {
FMT_CONSTEXPR_DECL Char s[] = {CHARS..., '\0'};
FMT_CONSTEXPR_DECL bool invalid_format =
check_format_string<Char, error_handler, Args...>(
do_check_format_string<Char, error_handler, Args...>(
basic_string_view<Char>(s, sizeof...(CHARS)));
(void)invalid_format;
return format(s, args...);

View File

@ -1426,13 +1426,14 @@ TEST(FormatterTest, FormatFloat) {
TEST(FormatterTest, FormatDouble) {
check_unknown_types(1.2, "eEfFgGaA", "double");
EXPECT_EQ("0", format("{0:}", 0.0));
EXPECT_EQ("0.000000", format("{0:f}", 0.0));
EXPECT_EQ("392.65", format("{0:}", 392.65));
EXPECT_EQ("392.65", format("{0:g}", 392.65));
EXPECT_EQ("392.65", format("{0:G}", 392.65));
EXPECT_EQ("392.650000", format("{0:f}", 392.65));
EXPECT_EQ("392.650000", format("{0:F}", 392.65));
EXPECT_EQ("0", format("{:}", 0.0));
EXPECT_EQ("0.000000", format("{:f}", 0.0));
EXPECT_EQ("0", format("{:g}", 0.0));
EXPECT_EQ("392.65", format("{:}", 392.65));
EXPECT_EQ("392.65", format("{:g}", 392.65));
EXPECT_EQ("392.65", format("{:G}", 392.65));
EXPECT_EQ("392.650000", format("{:f}", 392.65));
EXPECT_EQ("392.650000", format("{:F}", 392.65));
char buffer[BUFFER_SIZE];
safe_sprintf(buffer, "%e", 392.65);
EXPECT_EQ(buffer, format("{0:e}", 392.65));
@ -2315,7 +2316,7 @@ FMT_CONSTEXPR bool equal(const char *s1, const char *s2) {
template <typename... Args>
FMT_CONSTEXPR bool test_error(const char *fmt, const char *expected_error) {
const char *actual_error = FMT_NULL;
fmt::internal::check_format_string<char, test_error_handler, Args...>(
fmt::internal::do_check_format_string<char, test_error_handler, Args...>(
string_view(fmt, len(fmt)), test_error_handler(actual_error));
return equal(actual_error, expected_error);
}

View File

@ -509,3 +509,12 @@ TEST(PrintfTest, VPrintf) {
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42");
}
template<typename... Args>
void check_format_string_regression(fmt::string_view s, const Args&... args) {
fmt::sprintf(s, args...);
}
TEST(PrintfTest, CheckFormatStringRegression) {
check_format_string_regression("%c%s", 'x', "");
}