Add c specifier support to integral types (#1652)

This commit is contained in:
Victor Zverovich 2020-05-17 08:44:55 -07:00
parent 6b219a58db
commit 6d66de3805
2 changed files with 8 additions and 1 deletions

View File

@ -1235,6 +1235,9 @@ FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) {
case 'L':
handler.on_num();
break;
case 'c':
handler.on_chr();
break;
default:
handler.on_error();
}
@ -1326,6 +1329,7 @@ template <typename ErrorHandler> class int_type_checker : private ErrorHandler {
FMT_CONSTEXPR void on_bin() {}
FMT_CONSTEXPR void on_oct() {}
FMT_CONSTEXPR void on_num() {}
FMT_CONSTEXPR void on_chr() {}
FMT_CONSTEXPR void on_error() {
ErrorHandler::on_error("invalid type specifier");
@ -1561,6 +1565,8 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
num_writer{abs_value, size, groups, sep});
}
void on_chr() { *out++ = static_cast<Char>(abs_value); }
FMT_NORETURN void on_error() {
FMT_THROW(format_error("invalid type specifier"));
}

View File

@ -1082,7 +1082,8 @@ TEST(FormatterTest, FormatShort) {
TEST(FormatterTest, FormatInt) {
EXPECT_THROW_MSG(format("{0:v", 42), format_error,
"missing '}' in format string");
check_unknown_types(42, "bBdoxXnL", "integer");
check_unknown_types(42, "bBdoxXnLc", "integer");
EXPECT_EQ("x", format("{:c}", static_cast<int>('x')));
}
TEST(FormatterTest, FormatBin) {