diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 6fde1f2..bbd09b9 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -470,17 +470,12 @@ constexpr auto n() noexcept { if constexpr (supported::value) { #if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); - constexpr auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; #elif defined(__clang__) auto name = str_view{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { name = str_view{};; } - constexpr auto prefix = n().size(); - if (name.size_ > prefix && name.str_[prefix] == ':') { - name.size_ -= (prefix + 2); - name.str_ += (prefix + 2); - } #elif defined(__GNUC__) auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; if (name.str_[name.size_ - 1] == ']') { @@ -493,24 +488,26 @@ constexpr auto n() noexcept { if (name.str_[0] == '(') { name = str_view{}; } +#elif defined(_MSC_VER) + str_view name; + if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { + name = str_view{__FUNCSIG__ + 35, sizeof(__FUNCSIG__) - 52}; + } +#else + auto name = str_view{}; +#endif constexpr auto prefix = n().size(); if (name.size_ > prefix && name.str_[prefix] == ':') { name.size_ -= (prefix + 2); name.str_ += (prefix + 2); } -#elif defined(_MSC_VER) - str_view name; - if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { - name = str_view{__FUNCSIG__ + 35, sizeof(__FUNCSIG__) - 52}; - constexpr auto prefix = n().size(); - if (name.size_ > prefix && name.str_[prefix] == ':') { - name.size_ -= (prefix + 2); - name.str_ += (prefix + 2); + for (std::size_t i = 0; i < name.size_; ++i) { + if (name.str_[i] == ':') { + name.size_ -= (i + 2); + name.str_ += (i + 2); + break; } } -#else - auto name = str_view{}; -#endif return name; } else { return str_view{}; // Unsupported compiler or Invalid customize. diff --git a/test/test.cpp b/test/test.cpp index c87da78..234d530 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -453,6 +453,14 @@ enum lt1 { s1, loooooooooooooooooooong1 }; enum lt2 : unsigned { s2, loooooooooooooooooooong2 }; enum class lt3 { s3, loooooooooooooooooooong3 }; enum class lt4 : unsigned { s4, loooooooooooooooooooong4 }; +class foo1 { + public: + enum class lt5 { s5, loooooooooooooooooooong5 }; +}; +class foo2 { + public: + enum lt6 { s6, loooooooooooooooooooong6 }; +}; TEST_CASE("enum_name") { SECTION("automatic storage") { @@ -502,6 +510,10 @@ TEST_CASE("enum_name") { REQUIRE(enum_name(lt3::loooooooooooooooooooong3) == "loooooooooooooooooooong3"); REQUIRE(enum_name(lt4::s4) == "s4"); REQUIRE(enum_name(lt4::loooooooooooooooooooong4) == "loooooooooooooooooooong4"); + REQUIRE(enum_name(foo1::lt5::s5) == "s5"); + REQUIRE(enum_name(foo1::lt5::loooooooooooooooooooong5) == "loooooooooooooooooooong5"); + REQUIRE(enum_name(foo2::s6) == "s6"); + REQUIRE(enum_name(foo2::loooooooooooooooooooong6) == "loooooooooooooooooooong6"); } SECTION("static storage") {