-
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 1 commit
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 |
---|---|---|
|
@@ -92,9 +92,18 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> { | |
visit_any_int(value); | ||
} | ||
|
||
void visit_char(char value) { | ||
if (type_ != 's' && 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' || 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 +472,23 @@ 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' || 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'; | ||
} | ||
} | ||
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 think that having uppercase
S
is redundant and inconsistent with other specifiers. Please leave onlys
here and below.