diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 74d6b00..a63399e 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -6,7 +6,7 @@ permissions: read-all jobs: build: - runs-on: ${{matrix.config.os}} + runs-on: ${{ matrix.config.os }} strategy: fail-fast: false matrix: @@ -14,7 +14,7 @@ jobs: - { os: macos-11 } # https://github.com/actions/virtual-environments/blob/main/images/macos/macos-11-Readme.md#xcode - { os: macos-12 } # https://github.com/actions/virtual-environments/blob/main/images/macos/macos-12-Readme.md#xcode - name: "${{matrix.config.os}}" + name: "${{ matrix.config.os }}" steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 6fb1d28..0e0e8d1 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -32,7 +32,7 @@ jobs: - name: Configure clang run: | - if [[ "${{matrix.compiler.cc}}" == "clang"* ]]; then + if [[ "${{ matrix.compiler.cc }}" == "clang"* ]]; then wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add - sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-9 main" sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main" @@ -44,15 +44,15 @@ jobs: sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-16 main" sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal main" sudo apt update - sudo apt install ${{matrix.compiler.cc}} -y + sudo apt install ${{ matrix.compiler.cc }} -y fi - name: Configure gcc run: | - if [[ "${{matrix.compiler.cc}}" == "gcc"* ]]; then + if [[ "${{ matrix.compiler.cc }}" == "gcc"* ]]; then sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y sudo apt update - sudo apt install ${{matrix.compiler.cxx}} -y + sudo apt install ${{ matrix.compiler.cxx }} -y fi - name: Build Release @@ -60,7 +60,7 @@ jobs: rm -rf build mkdir build cd build - cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}} -DMAGIC_ENUM_OPT_ENABLE_NONASCII:BOOL=${{matrix.compiler.nonascii}} + cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=${{ matrix.compiler.cxx }} -DMAGIC_ENUM_OPT_ENABLE_NONASCII:BOOL=${{ matrix.compiler.nonascii }} cmake --build . -j 4 --config Release ctest --output-on-failure -C Release @@ -69,7 +69,7 @@ jobs: rm -rf build mkdir build cd build - cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}} -DMAGIC_ENUM_OPT_ENABLE_NONASCII:BOOL=${{matrix.compiler.nonascii}} + cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=${{ matrix.compiler.cxx }} -DMAGIC_ENUM_OPT_ENABLE_NONASCII:BOOL=${{ matrix.compiler.nonascii }} cmake --build . -j 4 --config Debug ctest --output-on-failure -C Debug diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 8879d6f..6087122 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -6,7 +6,7 @@ permissions: read-all jobs: build: - runs-on: ${{matrix.config.os}} + runs-on: ${{ matrix.config.os }} strategy: fail-fast: false matrix: diff --git a/include/magic_enum_format.hpp b/include/magic_enum_format.hpp index 79b269f..5438949 100644 --- a/include/magic_enum_format.hpp +++ b/include/magic_enum_format.hpp @@ -32,14 +32,10 @@ #ifndef NEARGYE_MAGIC_ENUM_FORMAT_HPP #define NEARGYE_MAGIC_ENUM_FORMAT_HPP -#if !defined(__cpp_lib_format) -# error "Format is not supported" -#endif - #include "magic_enum.hpp" #if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT) -# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT true +# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1 # define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE #endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT @@ -51,23 +47,48 @@ namespace magic_enum::customize { } } // magic_enum::customize +#if defined(__cpp_lib_format) + #include template -struct std::formatter && magic_enum::customize::enum_format_enabled(), char>> : std::formatter { +struct std::formatter> && magic_enum::customize::enum_format_enabled(), char>> : std::formatter { auto format(E e, format_context& ctx) const { static_assert(std::is_same_v, "formatter requires string_view::value_type type same as char."); using D = std::decay_t; if constexpr (magic_enum::detail::supported::value) { if (const auto name = magic_enum::enum_name>>(e); !name.empty()) { - return std::formatter::format(std::string_view{name.data(), name.size()}, ctx); + return formatter::format(std::string_view{name.data(), name.size()}, ctx); } } - return std::formatter::format(std::to_string(magic_enum::enum_integer(e)), ctx); + return formatter::format(std::to_string(magic_enum::enum_integer(e)), ctx); } }; +#endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT + +#if defined(FMT_VERSION) + +#include + +template +struct fmt::formatter> && magic_enum::customize::enum_format_enabled(), char>> : fmt::formatter { + auto format(E e, format_context& ctx) { + static_assert(std::is_same_v, "formatter requires string_view::value_type type same as char."); + using D = std::decay_t; + + if constexpr (magic_enum::detail::supported::value) { + if (const auto name = magic_enum::enum_name>>(e); !name.empty()) { + return formatter::format(std::string_view{name.data(), name.size()}, ctx); + } + } + return formatter::format(std::to_string(magic_enum::enum_integer(e)), ctx); + } +}; + +#endif // FMT_VERSION + #if defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE) # undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT # undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9ceb1ab..a4c765d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -44,17 +44,16 @@ make_test(test_aliases.cpp test_aliases-cpp17 c++17) make_test(test_containers.cpp test_containers-cpp17 c++17) if(MAGIC_ENUM_OPT_ENABLE_NONASCII) - make_test(test_nonascii.cpp test_nonascii-cpp17 c++17) + make_test(test_nonascii.cpp test_nonascii-cpp17 c++17) endif() - if(HAS_CPP20_FLAG) make_test(test.cpp test-cpp20 c++20) make_test(test_flags.cpp test_flags-cpp20 c++20) make_test(test_aliases.cpp test_aliases-cpp20 c++20) make_test(test_containers.cpp test_containers-cpp20 c++20) if(MAGIC_ENUM_OPT_ENABLE_NONASCII) - make_test(test_nonascii.cpp test_nonascii-cpp20 c++20) + make_test(test_nonascii.cpp test_nonascii-cpp20 c++20) endif() endif() @@ -64,7 +63,7 @@ if(HAS_CPP23_FLAG) make_test(test_aliases.cpp test_aliases-cpp23 c++23) make_test(test_containers.cpp test_containers-cpp23 c++23) if(MAGIC_ENUM_OPT_ENABLE_NONASCII) - make_test(test_nonascii.cpp test_nonascii-cpp23 c++23) + make_test(test_nonascii.cpp test_nonascii-cpp23 c++23) endif() endif() @@ -74,6 +73,6 @@ if(HAS_CPPLATEST_FLAG) make_test(test_aliases.cpp test_aliases-cpplatest c++latest) make_test(test_containers.cpp test_containers-cpplatest c++latest) if(MAGIC_ENUM_OPT_ENABLE_NONASCII) - make_test(test_nonascii.cpp test_nonascii-cpplatest c++latest) + make_test(test_nonascii.cpp test_nonascii-cpplatest c++latest) endif() endif() diff --git a/test/test.cpp b/test/test.cpp index 2687c7b..b5a1a04 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -128,13 +128,11 @@ TEST_CASE("enum_cast") { REQUIRE(enum_cast("Left").value() == Directions::Left); REQUIRE_FALSE(enum_cast("None").has_value()); -#if !defined(MAGIC_ENUM_ENABLE_NONASCII) constexpr auto dr2 = enum_cast("RIGHT", case_insensitive); REQUIRE(dr2.value() == Directions::Right); REQUIRE(enum_cast("up", case_insensitive).value() == Directions::Up); REQUIRE(enum_cast("dOwN", case_insensitive).value() == Directions::Down); REQUIRE_FALSE(enum_cast("Left-", case_insensitive).has_value()); -#endif constexpr auto nt = enum_cast("three"); REQUIRE(enum_cast("one").value() == number::one); @@ -350,13 +348,11 @@ TEST_CASE("enum_contains") { REQUIRE(enum_contains("Left")); REQUIRE_FALSE(enum_contains("None")); -#if !defined(MAGIC_ENUM_ENABLE_NONASCII) auto dr2 = std::string{"RIGHT"}; REQUIRE(enum_contains(dr2, case_insensitive)); REQUIRE(enum_contains("up", case_insensitive)); REQUIRE(enum_contains("dOwN", case_insensitive)); REQUIRE_FALSE(enum_contains("Left-", case_insensitive)); -#endif constexpr auto nt = enum_contains("three"); REQUIRE(enum_contains("one")); @@ -1018,7 +1014,6 @@ TEST_CASE("multdimensional-switch-case") { TEST_CASE("format-base") { REQUIRE(std::format("{}", Color::RED) == "red"); REQUIRE(std::format("{}", Color{0}) == "0"); - REQUIRE(std::format("Test-{:~^10}.", Color::RED) == "Test-~~~red~~~~."); } #endif