Remove '%S' handling and use visitor for generic format strings
This commit is contained in:
parent
b059a5e647
commit
a59af231f4
39
fmt/printf.h
39
fmt/printf.h
@ -61,6 +61,24 @@ class IsZeroInt : public ArgVisitor<IsZeroInt, bool> {
|
|||||||
bool visit_any_int(T value) { return value == 0; }
|
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>
|
template <typename T, typename U>
|
||||||
struct is_same {
|
struct is_same {
|
||||||
enum { value = 0 };
|
enum { value = 0 };
|
||||||
@ -93,14 +111,14 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void visit_char(char value) {
|
void visit_char(char value) {
|
||||||
if (type_ != 's' && type_ != 'S')
|
if (type_ != 's')
|
||||||
visit_any_int(value);
|
visit_any_int(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename U>
|
template <typename U>
|
||||||
void visit_any_int(U value) {
|
void visit_any_int(U value) {
|
||||||
bool is_signed = type_ == 'd' || type_ == 'i';
|
bool is_signed = type_ == 'd' || type_ == 'i';
|
||||||
if (type_ == 's' || type_ == 'S') {
|
if (type_ == 's') {
|
||||||
is_signed = std::numeric_limits<U>::is_signed;
|
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"));
|
FMT_THROW(FormatError("invalid format string"));
|
||||||
spec.type_ = static_cast<char>(*s++);
|
spec.type_ = static_cast<char>(*s++);
|
||||||
|
|
||||||
if (spec.type_ == 's' || spec.type_ == 'S') {
|
if (spec.type_ == 's') {
|
||||||
if (arg.type == Arg::POINTER) {
|
// set the format type to the default if 's' is specified
|
||||||
spec.type_ = 'p';
|
spec.type_ = internal::DefaultType().visit(arg);
|
||||||
} 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 (arg.type <= Arg::LAST_INTEGER_TYPE) {
|
if (arg.type <= Arg::LAST_INTEGER_TYPE) {
|
||||||
|
|||||||
@ -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("{: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);
|
||||||
EXPECT_PRINTF(fmt::format("{}", u), "%S", u);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PrintfTest, LongLong) {
|
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", "%F", 392.65);
|
EXPECT_PRINTF("392.650000", "%F", 392.65);
|
||||||
EXPECT_PRINTF("392.650000", "%s", 392.65);
|
EXPECT_PRINTF("392.650000", "%s", 392.65);
|
||||||
EXPECT_PRINTF("392.650000", "%S", 392.65);
|
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
safe_sprintf(buffer, "%e", 392.65);
|
safe_sprintf(buffer, "%e", 392.65);
|
||||||
EXPECT_PRINTF(buffer, "%e", 392.65);
|
EXPECT_PRINTF(buffer, "%e", 392.65);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user