diff --git a/fmt/printf.h b/fmt/printf.h index 6ee63a74..f695d1aa 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -61,6 +61,24 @@ class IsZeroInt : public ArgVisitor { bool visit_any_int(T value) { return value == 0; } }; +// returns the default type for format specific "%s" +class DefaultType : public ArgVisitor { + public: + wchar_t visit_char(int) { return 'c'; } + + wchar_t visit_bool(bool) { return 's'; } + + wchar_t visit_pointer(const void *) { return 'p'; } + + template + wchar_t visit_any_int(T) { return 'd'; } + + template + wchar_t visit_any_double(T) { return 'f'; } + + wchar_t visit_unhandled_arg() { return 's'; } +}; + template struct is_same { enum { value = 0 }; @@ -93,14 +111,14 @@ class ArgConverter : public ArgVisitor, void> { } void visit_char(char value) { - if (type_ != 's' && type_ != 'S') + if (type_ != 's') visit_any_int(value); } template void visit_any_int(U value) { bool is_signed = type_ == 'd' || type_ == 'i'; - if (type_ == 's' || type_ == 'S') { + if (type_ == 's') { is_signed = std::numeric_limits::is_signed; } @@ -473,20 +491,9 @@ void PrintfFormatter::format(BasicCStringRef format_str) { FMT_THROW(FormatError("invalid format string")); spec.type_ = static_cast(*s++); - if (spec.type_ == 's' || spec.type_ == 'S') { - if (arg.type == Arg::POINTER) { - spec.type_ = 'p'; - } else if (arg.type == Arg::CHAR) { - spec.type_ = 'c'; - } else if (arg.type == Arg::BOOL) { - // leave bool alone - } else if (arg.type <= Arg::LAST_INTEGER_TYPE) { - // all other integer types - spec.type_ = 'd'; - } else if (arg.type <= Arg::LAST_NUMERIC_TYPE) { - // all floating point types - spec.type_ = 'f'; - } + if (spec.type_ == 's') { + // set the format type to the default if 's' is specified + spec.type_ = internal::DefaultType().visit(arg); } if (arg.type <= Arg::LAST_INTEGER_TYPE) { diff --git a/test/printf-test.cc b/test/printf-test.cc index bbbb9f5b..2d818683 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -390,7 +390,6 @@ TEST(PrintfTest, Int) { EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42); EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42); EXPECT_PRINTF(fmt::format("{}", u), "%s", u); - EXPECT_PRINTF(fmt::format("{}", u), "%S", u); } TEST(PrintfTest, LongLong) { @@ -404,7 +403,6 @@ TEST(PrintfTest, Float) { EXPECT_PRINTF("392.650000", "%f", 392.65); EXPECT_PRINTF("392.650000", "%F", 392.65); EXPECT_PRINTF("392.650000", "%s", 392.65); - EXPECT_PRINTF("392.650000", "%S", 392.65); char buffer[BUFFER_SIZE]; safe_sprintf(buffer, "%e", 392.65); EXPECT_PRINTF(buffer, "%e", 392.65);