diff --git a/doc/reference.md b/doc/reference.md index 949a3f7..d8c0eb8 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -92,6 +92,13 @@ constexpr optional enum_cast(string_view value, BinaryPredicate p) noexcept(i // color.value() -> Color::GREEN } + // case insensitive enum_cast + auto color = magic_enum::enum_cast(value, magic_enum::case_insensitive); + + // enum_cast with BinaryPredicate + auto color = magic_enum::enum_cast(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); } + + // enum_cast with default auto color_or_default = magic_enum::enum_cast(value).value_or(Color::NONE); ``` diff --git a/example/example.cpp b/example/example.cpp index dc838d6..4408dbe 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -54,6 +54,12 @@ int main() { std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0 } + // Case insensitive enum_cast. + c2 = magic_enum::enum_cast("blue", magic_enum::case_insensitive); + if (c2.has_value()) { + std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0 + } + // Integer value to enum value. auto c3 = magic_enum::enum_cast(10); if (c3.has_value()) { diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index f0ab576..c133042 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -301,7 +301,7 @@ class case_insensitive { public: template - constexpr auto operator()(L lhs,R rhs) const noexcept -> std::enable_if_t, char_type> && std::is_same_v, char_type>, bool> { + constexpr auto operator()(L lhs, R rhs) const noexcept -> std::enable_if_t, char_type> && std::is_same_v, char_type>, bool> { return Op{}(to_lower(lhs), to_lower(rhs)); } }; @@ -434,6 +434,7 @@ constexpr auto n() noexcept { name.str_ += 37; } #elif defined(_MSC_VER) + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). str_view name; name.str_ = __FUNCSIG__; name.str_ += 40; @@ -511,6 +512,7 @@ constexpr auto n() noexcept { #elif defined(_MSC_VER) str_view name; if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). name.str_ = __FUNCSIG__; name.str_ += 35; name.size_ = sizeof(__FUNCSIG__) - 52; @@ -548,6 +550,7 @@ constexpr auto n() noexcept { constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; # else + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). str_view name; name.str_ = __FUNCSIG__; name.size_ = sizeof(__FUNCSIG__) - 17;