From 785b3f253d69ad08d177526d42d2cb7f8f3d9d9c Mon Sep 17 00:00:00 2001 From: neargye Date: Tue, 8 Mar 2022 10:32:38 +0200 Subject: [PATCH] clean-up --- include/magic_enum.hpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 9526b3c..628cfc0 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -137,8 +137,6 @@ static_assert((MAGIC_ENUM_RANGE_MAX - MAGIC_ENUM_RANGE_MIN) < (std::numeric_limi // If need custom names for enum, add specialization enum_name for necessary enum type. template constexpr string_view enum_name(E) noexcept { - static_assert(std::is_enum_v, "magic_enum::customize::enum_name requires enum type."); - return {}; } @@ -146,6 +144,9 @@ constexpr string_view enum_name(E) noexcept { namespace detail { +template +inline constexpr bool always_false_v = false; + template struct supported #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) @@ -230,15 +231,22 @@ constexpr string_view pretty_name(string_view name) noexcept { return {}; // Invalid name. } -template -constexpr auto to_lower([[maybe_unused]] CharType ch) noexcept -> std::enable_if_t, char>, char> { +class case_insensitive { + static constexpr char to_lower(char c) noexcept { + return (c >= 'A' && c <= 'Z') ? static_cast(c + ('a' - 'A')) : c; + } + + public: + template + constexpr auto operator()([[maybe_unused]] L lhs, [[maybe_unused]] R rhs) noexcept -> std::enable_if_t, char> && std::is_same_v, char>, bool> { #if defined(MAGIC_ENUM_ENABLE_NONASCII) - static_assert(!std::is_same_v, "magic_enum::detail::to_lower not supported Non-ASCII feature."); - return {}; + static_assert(always_false_v, "magic_enum::case_insensitive not supported Non-ASCII feature."); + return false; #else - return 'A' <= ch && ch <= 'Z' ? ch - 'A' + 'a' : ch; + return to_lower(lhs) == to_lower(rhs); #endif -} + } +}; constexpr std::size_t find(string_view str, char c) noexcept { #if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) @@ -277,8 +285,8 @@ constexpr bool cmp_equal(string_view lhs, string_view rhs, [[maybe_unused]] Bina constexpr bool workaround = false; #endif constexpr bool custom_predicate = - !std::is_same_v, std::equal_to> && - !std::is_same_v, std::equal_to<>>; + !std::is_same_v, std::equal_to> && + !std::is_same_v, std::equal_to<>>; if constexpr (custom_predicate || workaround) { if (lhs.size() != rhs.size()) { @@ -808,15 +816,7 @@ template } // allows you to write magic_enum::enum_cast("bar", magic_enum::case_insensitive); -inline constexpr auto case_insensitive = [](auto lhs, auto rhs) noexcept - -> std::enable_if_t, char> && std::is_same_v, char>, bool> { -#if defined(MAGIC_ENUM_ENABLE_NONASCII) - static_assert(!std::is_same_v, "magic_enum::case_insensitive not supported Non-ASCII feature."); - return {}; -#else - return detail::to_lower(lhs) == detail::to_lower(rhs); -#endif -}; +inline constexpr auto case_insensitive = detail::case_insensitive{}; // Obtains enum value from name. // Returns optional with enum value.