Remove '%S' handling and use visitor for generic format strings

This commit is contained in:
Brendan McCarthy 2017-01-13 16:46:32 +10:00
parent b059a5e647
commit a59af231f4
2 changed files with 23 additions and 18 deletions

View File

@ -61,6 +61,24 @@ class IsZeroInt : public ArgVisitor<IsZeroInt, bool> {
bool visit_any_int(T value) { return value == 0; }
};
// returns the default type for format specific "%s"
class DefaultType : public ArgVisitor<DefaultType, wchar_t> {
public:
wchar_t visit_char(int) { return 'c'; }
wchar_t visit_bool(bool) { return 's'; }
wchar_t visit_pointer(const void *) { return 'p'; }
template <typename T>
wchar_t visit_any_int(T) { return 'd'; }
template <typename T>
wchar_t visit_any_double(T) { return 'f'; }
wchar_t visit_unhandled_arg() { return 's'; }
};
template <typename T, typename U>
struct is_same {
enum { value = 0 };
@ -93,14 +111,14 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
}
void visit_char(char value) {
if (type_ != 's' && type_ != 'S')
if (type_ != 's')
visit_any_int(value);
}
template <typename U>
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<U>::is_signed;
}
@ -473,20 +491,9 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
FMT_THROW(FormatError("invalid format string"));
spec.type_ = static_cast<char>(*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) {

View File

@ -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);