From 46475eca5d1a0061d7bd42fb9d166d5eb2bd80b4 Mon Sep 17 00:00:00 2001 From: terik23 Date: Mon, 25 Mar 2019 01:12:29 +0500 Subject: [PATCH] improve nameof_enum_impl & nameof_type_impl --- include/nameof.hpp | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 69fafd6..d4d9d27 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -136,12 +136,13 @@ template constexpr auto prefix = sizeof("class std::basic_string_view > __cdecl nameof::detail::nameof_type_impl>(void) noexcept") - 1; #else - std::string_view name{"nameof_type::unsupported_compiler"}; - constexpr auto prefix = 0; - constexpr auto suffix = 0; + return "nameof_type::unsupported_compiler"; #endif + +#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) name.remove_prefix(prefix); name.remove_suffix(suffix); +# if defined(_MSC_VER) if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') { name.remove_prefix(sizeof("enum")); } @@ -151,33 +152,42 @@ template if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') { name.remove_prefix(sizeof("struct")); } +# endif while (name.back() == ' ') { name.remove_suffix(1); } - return name; +#endif } template [[nodiscard]] constexpr std::string_view nameof_enum_impl() noexcept { #if defined(__clang__) - constexpr auto str = __PRETTY_FUNCTION__; - constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; + std::string_view name{__PRETTY_FUNCTION__}; constexpr auto suffix = sizeof("]") - 1; - return detail::pretty_name({str, size - suffix}, false); #elif defined(__GNUC__) - constexpr auto str = __PRETTY_FUNCTION__; - constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1; + std::string_view name{__PRETTY_FUNCTION__}; constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_type_impl>().length() + sizeof("; V = "); constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; - return {str + prefix, size - prefix - suffix}; + name.remove_prefix(prefix); #elif defined(_MSC_VER) - constexpr auto str = __FUNCSIG__; - constexpr auto size = sizeof(__FUNCSIG__) - 1; + std::string_view name{__FUNCSIG__}; constexpr auto suffix = sizeof(">(void) noexcept") - 1; - return detail::pretty_name({str, size - suffix}, false); #else - return {"nameof_enum::unsupported_compiler"}; + return "nameof_enum::unsupported_compiler"; +#endif + +#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) + name.remove_suffix(suffix); +# if defined(__clang__) || defined(_MSC_VER) + for (std::size_t i = name.size(); i > 0; --i) { + if (!is_name_char(name[i - 1])) { + name.remove_prefix(i); + break; + } + } +# endif + return name; #endif } @@ -220,7 +230,7 @@ struct nameof_enum_t final { template >> [[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept { - return detail::pretty_name(name, with_suffix); + return pretty_name(name, with_suffix); } [[nodiscard]] constexpr std::string_view nameof_raw_impl(std::string_view name) noexcept {