Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for non-ASCII characters (UNIX/Linux) #95

Merged
merged 7 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.14)

project(magic_enum VERSION "0.7.3" LANGUAGES CXX)

Expand All @@ -8,6 +8,7 @@ else()
set(IS_TOPLEVEL_PROJECT FALSE)
endif()

option(MAGIC_ENUM_OPT_ENABLE_NONASCII "Enable support for non-ASCII enumeration identifier" ${IS_TOPLEVEL_PROJECT})
option(MAGIC_ENUM_OPT_BUILD_EXAMPLES "Build magic_enum examples" ${IS_TOPLEVEL_PROJECT})
option(MAGIC_ENUM_OPT_BUILD_TESTS "Build and perform magic_enum tests" ${IS_TOPLEVEL_PROJECT})
option(MAGIC_ENUM_OPT_INSTALL "Generate and install magic_enum target" ${IS_TOPLEVEL_PROJECT})
Expand Down
6 changes: 6 additions & 0 deletions doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
#define MAGIC_ENUM_RANGE_MAX 255
```

* To add support for non-ASCII enumeration identifier, use special macros:
```cpp
#define MAGIC_ENUM_ENABLE_NONASCII
#include <magic_enum.hpp>
```

## `enum_cast`

```cpp
Expand Down
10 changes: 9 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include(CheckCXXCompilerFlag)
include(CheckCXXCompilerFlag)

if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
set(OPTIONS -Wall -Wextra -pedantic-errors -Werror)
Expand All @@ -20,3 +20,11 @@ endfunction()
make_example(example)
make_example(enum_flag_example)
make_example(example_custom_name)
if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
if(((CMAKE_CXX_COMPILER_ID MATCHES "GNU") AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0) OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
set(OPTIONS ${OPTIONS} -DMAGIC_ENUM_ENABLE_NONASCII)
make_example(example_nonascii_name)
elseif((CMAKE_CXX_COMPILER_ID MATCHES "MSVC"))
message(WARNING "Non-ASCII feature on Windows is not supported yet")
endif()
endif()
44 changes: 44 additions & 0 deletions example/example_nonascii_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 - 2021 Uruha Komachin <uruhakomachin@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <iostream>

#include <magic_enum.hpp>

enum class Language : int {
日本語 = 10,
한국어 = 20,
English = 30,
😃 = 40,
};

int main() {
std::cout << magic_enum::enum_name(Language::日本語) << std::endl; // Japanese
std::cout << magic_enum::enum_name(Language::한국어) << std::endl; // Korean
std::cout << magic_enum::enum_name(Language::English) << std::endl; // English
std::cout << magic_enum::enum_name(Language::😃) << std::endl; // Emoji

std::cout << std::boolalpha;
std::cout << (magic_enum::enum_cast<Language>("日本語").value() == Language::日本語) << std::endl; // true

return 0;
}
6 changes: 6 additions & 0 deletions include/magic_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ constexpr string_view pretty_name(string_view name) noexcept {
if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
(name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
(name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
#if defined(MAGIC_ENUM_ENABLE_NONASCII)
(name[i - 1] & 0x80) ||
#endif
(name[i - 1] == '_'))) {
name.remove_prefix(i);
break;
Expand All @@ -210,6 +213,9 @@ constexpr string_view pretty_name(string_view name) noexcept {

if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
(name.front() >= 'A' && name.front() <= 'Z') ||
#if defined(MAGIC_ENUM_ENABLE_NONASCII)
(name.front() & 0x80) ||
#endif
(name.front() == '_'))) {
return name;
}
Expand Down
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include(CheckCXXCompilerFlag)
include(CheckCXXCompilerFlag)

set(SOURCES test.cpp)

Expand All @@ -18,6 +18,14 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
check_cxx_compiler_flag(-std=c++20 HAS_CPP20_FLAG)
endif()

if(MAGIC_ENUM_OPT_ENABLE_NONASCII)
if(((CMAKE_CXX_COMPILER_ID MATCHES "GNU") AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10.0) OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
set(OPTIONS ${OPTIONS} -DMAGIC_ENUM_ENABLE_NONASCII)
elseif((CMAKE_CXX_COMPILER_ID MATCHES "MSVC"))
message(WARNING "Non-ASCII feature on Windows is not supported yet")
endif()
endif()

function(make_test src target std)
add_executable(${target} ${src})
target_compile_options(${target} PRIVATE ${OPTIONS})
Expand Down
Loading