diff --git a/format.cc b/format.cc index d5afeaa9..9624bfb9 100644 --- a/format.cc +++ b/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"); diff --git a/format.h b/format.h index 907ce5ab..1195c604 100644 --- a/format.h +++ b/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::write_int(T value, const Spec &spec) { break; } default: - internal::ReportUnknownType(spec.type(), "integer"); + internal::ReportUnknownType( + spec.type(), spec.char_flag() ? "char" : "integer"); break; } }