-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow %s as generic format specifier in printf #453
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }; | ||
|
@@ -92,9 +110,18 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> { | |
visit_any_int(value); | ||
} | ||
|
||
void visit_char(char value) { | ||
if (type_ != 's') | ||
visit_any_int(value); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this handled by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ArgConverter runs before DefaultType, and I couldn't see a clean way to switch it around. |
||
|
||
template <typename U> | ||
void visit_any_int(U value) { | ||
bool is_signed = type_ == 'd' || type_ == 'i'; | ||
if (type_ == 's') { | ||
is_signed = std::numeric_limits<U>::is_signed; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
||
using internal::Arg; | ||
typedef typename internal::Conditional< | ||
is_same<T, void>::value, U, T>::type TargetType; | ||
|
@@ -463,6 +490,12 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) { | |
if (!*s) | ||
FMT_THROW(FormatError("invalid format string")); | ||
spec.type_ = static_cast<char>(*s++); | ||
|
||
if (spec.type_ == 's') { | ||
// set the format type to the default if 's' is specified | ||
spec.type_ = internal::DefaultType().visit(arg); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please use 2 space indent |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please convert this if-else chain into a visitor and merge with the similar logic under for integers right below. |
||
|
||
if (arg.type <= Arg::LAST_INTEGER_TYPE) { | ||
// Normalize type. | ||
switch (spec.type_) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using
g
here because it is the default floating-point format.