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 4fecb11
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
36 changes: 15 additions & 21 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,9 @@ FMT_CONSTEXPR auto to_unsigned(Int value) ->
return static_cast<typename std::make_unsigned<Int>::type>(value);
}

// A heuristic to detect std::string and std::[experimental::]string_view.
template <typename T, typename Enable = void>
struct is_string_like : std::false_type {};

// A heuristic to detect std::string and std::string_view.
template <typename T>
struct is_string_like<T, void_t<decltype(std::declval<T>().find_first_of(
typename T::value_type(), 0))>> : std::true_type {
Expand Down Expand Up @@ -570,9 +569,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 +596,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 +674,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,16 +1417,15 @@ 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_same<Char, char_type>::value &&
!std::is_pointer<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> basic_string_view<Char> {
return to_string_view(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)>
template <typename T, typename Char = char_t<T>,
FMT_ENABLE_IF(!std::is_same<Char, char_type>::value &&
!std::is_pointer<T>::value)>
FMT_CONSTEXPR FMT_INLINE auto map(const T&) -> unformattable_char {
return {};
}
Expand Down
9 changes: 7 additions & 2 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ 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<typename decltype(detail::to_string_view(
std::declval<S>()))::value_type>> {
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 4fecb11

Please sign in to comment.