From eddb84cfc06751c5623a9fe3e56fac101e8c317c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 3 Jun 2019 16:37:15 -0700 Subject: [PATCH] Fix formatting of exotic characters --- include/fmt/core.h | 2 +- include/fmt/format.h | 7 ++++--- test/format-test.cc | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index f65d22f2..377e8127 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -771,7 +771,7 @@ FMT_MAKE_VALUE(uint_type, unsigned char, unsigned) template ::value)> FMT_CONSTEXPR init make_value(Char val) { - return val; + return {val}; } template FMT_CONSTEXPR const Char* parse_precision(const Char* begin, const Char* end, Handler&& handler) { ++begin; - auto c = begin != end ? *begin : 0; + auto c = begin != end ? *begin : Char(); if ('0' <= c && c <= '9') { handler.on_precision(parse_nonnegative_int(begin, end, handler)); } else if (c == '{') { @@ -2763,8 +2763,9 @@ template class basic_writer { auto&& it = reserve(1); *it++ = value; } - void write(wchar_t value) { - static_assert(std::is_same::value, ""); + + template ::value)> + void write(Char value) { auto&& it = reserve(1); *it++ = value; } diff --git a/test/format-test.cc b/test/format-test.cc index 1c187651..a0f87711 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2492,3 +2492,21 @@ TEST(FormatTest, CharTraitsIsNotAmbiguous) { (void)lval; #endif } + +struct mychar { + int value; + mychar() = default; + mychar(char val) : value(val) {} + operator int() const { return value; } +}; + +FMT_BEGIN_NAMESPACE +template <> struct is_char : std::true_type {}; +FMT_END_NAMESPACE + +TEST(FormatTest, FormatCustomChar) { + const mychar format[] = {'{', '}', 0}; + auto result = fmt::format(format, mychar('x')); + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result[0], mychar('x')); +}