Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to compile time non-char format #924

Merged
merged 2 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,11 @@ struct compile_string {};
template <typename S>
struct is_compile_string : std::is_base_of<compile_string, S> {};

template <typename S>
inline typename std::enable_if<is_compile_string<S>::value, string_view>::type
to_string_view(const S &s) { return {s.data(), s.size() - 1}; }
template <typename S, typename Enable = typename std::enable_if<is_compile_string<S>::value>::type>
inline auto to_string_view(const S &s) -> basic_string_view<typename S::char_type> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to move enable_if from return type to template parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it is possible to use S::char_type inside enable_if since S may not satisfy is_compile_string, so I choose my familiar way to implement this.

typedef typename S::char_type char_type;
return basic_string_view<char_type>{s.data(), s.size() - 1};
}

template <typename Context>
class basic_format_arg;
Expand Down
10 changes: 6 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2243,9 +2243,10 @@ FMT_CONSTEXPR bool check_format_string(
template <typename... Args, typename String>
typename std::enable_if<is_compile_string<String>::value>::type
check_format_string(String format_str) {
typedef typename String::char_type char_type;
FMT_CONSTEXPR_DECL bool invalid_format =
internal::check_format_string<char, internal::error_handler, Args...>(
string_view(format_str.data(), format_str.size()));
internal::check_format_string<char_type, internal::error_handler, Args...>(
basic_string_view<char_type>(format_str.data(), format_str.size()));
(void)invalid_format;
}

Expand Down Expand Up @@ -3597,9 +3598,10 @@ FMT_END_NAMESPACE
#define FMT_STRING(s) [] { \
typedef typename std::decay<decltype(s)>::type pointer; \
struct S : fmt::compile_string { \
typedef typename std::remove_cv<std::remove_pointer<pointer>::type>::type char_type;\
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_type); } \
explicit operator fmt::basic_string_view<char_type>() const { return s; } \
}; \
return S{}; \
}()
Expand Down
4 changes: 4 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"{}"), wargs);
EXPECT_EQ(L"42", w);
}

#endif // FMT_USE_CONSTEXPR
Expand Down