Skip to content

Commit

Permalink
Simplify char_t
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 5, 2024
1 parent a5d4c2b commit 9de9183
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
27 changes: 11 additions & 16 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ template <> struct is_char<char> : std::true_type {};

namespace detail {

// Constructs fmt::basic_string_view<Char> from types **implicitly** convertible
// to it, deducing Char. Explicitly convertible types such as compile-time
// strings are intentionally excluded.
// Constructs fmt::basic_string_view<Char> from types implicitly convertible
// to it, deducing Char. Explicitly convertible types such as the ones returned
// from FMT_STRING are intentionally excluded.
template <typename Char, FMT_ENABLE_IF(is_char<Char>::value)>
FMT_INLINE auto to_string_view(const Char* s) -> basic_string_view<Char> {
return s;
Expand All @@ -597,12 +597,6 @@ template <typename S, typename = void>
struct is_string
: std::is_class<decltype(detail::to_string_view(std::declval<S>()))> {};

template <typename S, typename = void> struct char_t_impl {};
template <typename S> struct char_t_impl<S, enable_if_t<is_string<S>::value>> {
using result = decltype(to_string_view(std::declval<S>()));
using type = typename result::value_type;
};

enum class type {
none_type,
// Integer types should go first,
Expand Down Expand Up @@ -681,8 +675,10 @@ enum {
/** Throws ``format_error`` with a given message. */
FMT_NORETURN FMT_API void throw_format_error(const char* message);

/** String's character type. */
template <typename S> using char_t = typename detail::char_t_impl<S>::type;
/** String's character (code unit) type. */
template <typename S,
typename V = decltype(detail::to_string_view(std::declval<S>()))>
using char_t = typename V::value_type;

/**
\rst
Expand Down Expand Up @@ -1422,11 +1418,10 @@ template <typename Context> struct arg_mapper {
FMT_CONSTEXPR FMT_INLINE auto map(const char_type* val) -> const char_type* {
return val;
}
template <typename T,
FMT_ENABLE_IF(is_string<T>::value && !std::is_pointer<T>::value &&
std::is_same<char_type, char_t<T>>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val)
-> basic_string_view<char_type> {
template <typename T, typename Char = char_t<T>,
FMT_ENABLE_IF(!std::is_pointer<T>::value &&
std::is_same<Char, char_type>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> basic_string_view<Char> {
return to_string_view(val);
}
template <typename T,
Expand Down
7 changes: 5 additions & 2 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ enum char8_type : unsigned char {};
template <typename T>
using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;

template <typename S, typename = void>
struct format_string_char : char_t_impl<S> {};
template <typename S, typename = void> struct format_string_char {};

template <typename S> struct format_string_char<S, void_t<char_t<S>>> {
using type = char_t<S>;
};

template <typename S>
struct format_string_char<S, enable_if_t<is_compile_string<S>::value>> {
Expand Down

0 comments on commit 9de9183

Please sign in to comment.