From 54fbc2de9572a2de78890c216cb6e855466d6ccb Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 11 Apr 2019 15:37:44 +0500 Subject: [PATCH] clean-up --- include/magic_enum.hpp | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 226b1fa..a03699b 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -95,38 +95,27 @@ template > return range; } - -[[nodiscard]] constexpr bool is_name_char(char c, bool front) noexcept { - return (!front && c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; -} - template [[nodiscard]] constexpr std::string_view name_impl() noexcept { static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); #if defined(__clang__) - std::string_view name{__PRETTY_FUNCTION__}; + constexpr std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("]") - 1; #elif defined(__GNUC__) && __GNUC__ >= 9 - std::string_view name{__PRETTY_FUNCTION__}; + constexpr std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; #elif defined(_MSC_VER) - std::string_view name{__FUNCSIG__}; + constexpr std::string_view name{__FUNCSIG__}; constexpr auto suffix = sizeof(">(void) noexcept") - 1; #else return {}; // Unsupported compiler. #endif #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) - name.remove_suffix(suffix); - for (std::size_t i = name.size(); i > 0; --i) { - if (!is_name_char(name[i - 1], false)) { - name.remove_prefix(i); - break; - } - } + constexpr auto prefix = name.find_last_of(" :,-)", name.length() - suffix) + 1; - if (name.length() > 0 && is_name_char(name.front(), true)) { - return name; + if ((name[prefix] >= 'a' && name[prefix] <= 'z') || (name[prefix] >= 'A' && name[prefix] <= 'Z')) { + return name.substr(prefix, name.size() - prefix - suffix); } else { return {}; // Value does not have name. } @@ -161,21 +150,21 @@ template constexpr std::array valid{{!name_impl(I + min_impl())>().empty()...}}; constexpr int num_valid = ((valid[I] ? 1 : 0) + ...); - std::array enums{}; + std::array values{}; for (int i = 0, v = 0; i < n && v < num_valid; ++i) { if (valid[i]) { - enums[v++] = static_cast(i + min_impl()); + values[v++] = static_cast(i + min_impl()); } } - return enums; + return values; } template [[nodiscard]] constexpr decltype(auto) names_impl(std::integer_sequence) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::names_impl requires enum type."); - constexpr auto enums = values_impl(range_impl()); - constexpr std::array names{{name_impl()...}}; + constexpr auto values = values_impl(range_impl()); + constexpr std::array names{{name_impl()...}}; return names; }