From 0958e734b6033a37f5effe5cd2b461820605008f Mon Sep 17 00:00:00 2001 From: XZiar Date: Fri, 26 Oct 2018 15:55:03 -0700 Subject: [PATCH 1/2] add non-char support for compile-time format check --- include/fmt/core.h | 8 +++++--- include/fmt/format.h | 10 ++++++---- test/format-test.cc | 4 ++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 02c89c291fe1..20c4cc858b55 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -472,9 +472,11 @@ struct compile_string {}; template struct is_compile_string : std::is_base_of {}; -template -inline typename std::enable_if::value, string_view>::type - to_string_view(const S &s) { return {s.data(), s.size() - 1}; } +template ::value>> +inline auto to_string_view(const S &s) -> basic_string_view { + typedef typename S::Char Char; + return basic_string_view{s.data(), s.size() - 1}; +} template class basic_format_arg; diff --git a/include/fmt/format.h b/include/fmt/format.h index 76ceee4ba3c9..1689b176e414 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2243,9 +2243,10 @@ FMT_CONSTEXPR bool check_format_string( template typename std::enable_if::value>::type check_format_string(String format_str) { + typedef typename String::Char Char; FMT_CONSTEXPR_DECL bool invalid_format = - internal::check_format_string( - string_view(format_str.data(), format_str.size())); + internal::check_format_string( + basic_string_view(format_str.data(), format_str.size())); (void)invalid_format; } @@ -3597,9 +3598,10 @@ FMT_END_NAMESPACE #define FMT_STRING(s) [] { \ typedef typename std::decay::type pointer; \ struct S : fmt::compile_string { \ + typedef typename std::remove_cv::type>::type Char;\ static FMT_CONSTEXPR pointer data() { return s; } \ - static FMT_CONSTEXPR size_t size() { return sizeof(s); } \ - explicit operator fmt::string_view() const { return s; } \ + static FMT_CONSTEXPR size_t size() { return sizeof(s) / sizeof(Char); } \ + explicit operator fmt::basic_string_view() const { return s; } \ }; \ return S{}; \ }() diff --git a/test/format-test.cc b/test/format-test.cc index 4157ad68f616..041649c1803d 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1898,6 +1898,7 @@ TEST(FormatTest, UdlTemplate) { EXPECT_EQ("foo", "foo"_format()); EXPECT_EQ(" 42", "{0:10}"_format(42)); EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); + EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42)); } #endif // FMT_USE_USER_DEFINED_LITERALS @@ -2417,6 +2418,9 @@ TEST(FormatTest, VFormatTo) { std::wstring w; fmt::vformat_to(std::back_inserter(w), L"{}", wargs); EXPECT_EQ(L"42", w); + w.clear(); + fmt::vformat_to(std::back_inserter(w), FMT_STRING(L"{}"), args); + EXPECT_EQ("42", w); } #endif // FMT_USE_CONSTEXPR From 33474d4a1df771e830c3eace2e231d57782ee4fa Mon Sep 17 00:00:00 2001 From: XZiar Date: Fri, 26 Oct 2018 20:32:24 -0700 Subject: [PATCH 2/2] change type naming and fix sfinae bug --- include/fmt/core.h | 8 ++++---- include/fmt/format.h | 12 ++++++------ test/format-test.cc | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 20c4cc858b55..7fa4d2a248c8 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -472,10 +472,10 @@ struct compile_string {}; template struct is_compile_string : std::is_base_of {}; -template ::value>> -inline auto to_string_view(const S &s) -> basic_string_view { - typedef typename S::Char Char; - return basic_string_view{s.data(), s.size() - 1}; +template ::value>::type> +inline auto to_string_view(const S &s) -> basic_string_view { + typedef typename S::char_type char_type; + return basic_string_view{s.data(), s.size() - 1}; } template diff --git a/include/fmt/format.h b/include/fmt/format.h index 1689b176e414..94561e339f87 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2243,10 +2243,10 @@ FMT_CONSTEXPR bool check_format_string( template typename std::enable_if::value>::type check_format_string(String format_str) { - typedef typename String::Char Char; + typedef typename String::char_type char_type; FMT_CONSTEXPR_DECL bool invalid_format = - internal::check_format_string( - basic_string_view(format_str.data(), format_str.size())); + internal::check_format_string( + basic_string_view(format_str.data(), format_str.size())); (void)invalid_format; } @@ -3598,10 +3598,10 @@ FMT_END_NAMESPACE #define FMT_STRING(s) [] { \ typedef typename std::decay::type pointer; \ struct S : fmt::compile_string { \ - typedef typename std::remove_cv::type>::type Char;\ + typedef typename std::remove_cv::type>::type char_type;\ static FMT_CONSTEXPR pointer data() { return s; } \ - static FMT_CONSTEXPR size_t size() { return sizeof(s) / sizeof(Char); } \ - explicit operator fmt::basic_string_view() const { return s; } \ + static FMT_CONSTEXPR size_t size() { return sizeof(s) / sizeof(char_type); } \ + explicit operator fmt::basic_string_view() const { return s; } \ }; \ return S{}; \ }() diff --git a/test/format-test.cc b/test/format-test.cc index 041649c1803d..7a71ed58705c 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2419,8 +2419,8 @@ TEST(FormatTest, VFormatTo) { fmt::vformat_to(std::back_inserter(w), L"{}", wargs); EXPECT_EQ(L"42", w); w.clear(); - fmt::vformat_to(std::back_inserter(w), FMT_STRING(L"{}"), args); - EXPECT_EQ("42", w); + fmt::vformat_to(std::back_inserter(w), FMT_STRING(L"{}"), wargs); + EXPECT_EQ(L"42", w); } #endif // FMT_USE_CONSTEXPR