mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-06 23:30:29 +00:00
Don't duplicate integer format specifiers when formatting char as integer.
This commit is contained in:
parent
a997de90eb
commit
d699c2a0d9
11
format.cc
11
format.cc
@ -432,14 +432,9 @@ class fmt::internal::ArgFormatter :
|
||||
|
||||
void visit_char(int value) {
|
||||
if (spec_.type_ && spec_.type_ != 'c') {
|
||||
switch (spec_.type_) {
|
||||
// TODO: don't duplicate integer format specifiers here
|
||||
case 'd': case 'x': case 'X': case 'b': case 'B': case 'o':
|
||||
writer_.write_int(value, spec_);
|
||||
return;
|
||||
default:
|
||||
internal::ReportUnknownType(spec_.type_, "char");
|
||||
}
|
||||
spec_.flags_ |= CHAR_FLAG;
|
||||
writer_.write_int(value, spec_);
|
||||
return;
|
||||
}
|
||||
if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
|
||||
throw FormatError("invalid format specifier for char");
|
||||
|
11
format.h
11
format.h
@ -906,7 +906,10 @@ enum Alignment {
|
||||
};
|
||||
|
||||
// Flags.
|
||||
enum { SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8 };
|
||||
enum {
|
||||
SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8,
|
||||
CHAR_FLAG = 0x10 // Argument has char type - used in error reporting.
|
||||
};
|
||||
|
||||
// An empty format specifier.
|
||||
struct EmptySpec {};
|
||||
@ -921,6 +924,7 @@ struct TypeSpec : EmptySpec {
|
||||
bool sign_flag() const { return false; }
|
||||
bool plus_flag() const { return false; }
|
||||
bool hash_flag() const { return false; }
|
||||
bool char_flag() const { return false; }
|
||||
|
||||
char type() const { return TYPE; }
|
||||
char fill() const { return ' '; }
|
||||
@ -959,6 +963,7 @@ struct AlignTypeSpec : AlignSpec {
|
||||
bool sign_flag() const { return false; }
|
||||
bool plus_flag() const { return false; }
|
||||
bool hash_flag() const { return false; }
|
||||
bool char_flag() const { return false; }
|
||||
|
||||
char type() const { return TYPE; }
|
||||
};
|
||||
@ -976,6 +981,7 @@ struct FormatSpec : AlignSpec {
|
||||
bool sign_flag() const { return (flags_ & SIGN_FLAG) != 0; }
|
||||
bool plus_flag() const { return (flags_ & PLUS_FLAG) != 0; }
|
||||
bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; }
|
||||
bool char_flag() const { return (flags_ & CHAR_FLAG) != 0; }
|
||||
|
||||
int precision() const { return precision_; }
|
||||
|
||||
@ -1682,7 +1688,8 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
internal::ReportUnknownType(spec.type(), "integer");
|
||||
internal::ReportUnknownType(
|
||||
spec.type(), spec.char_flag() ? "char" : "integer");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user