diff --git a/LICENSE b/LICENSE index c33667f..cd842fb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016 - 2018 Daniil Goncharov +Copyright (c) 2016, 2018 Daniil Goncharov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index f0258fb..4838080 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/langu Linux/OSX | Windows -----------|--------- -[![Build Status](https://travis-ci.org/Terik23/nameof.svg?branch=master)](https://travis-ci.org/Terik23/nameof)|[![Build status](https://ci.appveyor.com/api/projects/status/4tyl8lee10ckw10k/branch/master?svg=true)](https://ci.appveyor.com/project/Terik23/nameof/branch/master) +[![Build Status](https://travis-ci.org/Neargye/nameof.svg?branch=master)](https://travis-ci.org/Neargye/nameof)|[![Build status](https://ci.appveyor.com/api/projects/status/yq5fk0d9mwljbubt/branch/master?svg=true)](https://ci.appveyor.com/project/Neargye/nameof/branch/master) Used to obtain the simple name of a variable, type, function. Before, you had to use string literals to refer to definitions, which is brittle when renaming code elements because tools do not know to check these string literals. @@ -24,7 +24,7 @@ std::cout << NAMEOF(person.address.zip_code) << std::endl; // prints "zip_code" * Compile-time * Compilation check -## [Example & Key Use Cases](https://github.com/Terik23/nameof/blob/master/example/example.cpp) +## [Example & Key Use Cases](https://github.com/Neargye/nameof/blob/master/example/example.cpp) * Name of a variable, function and etc diff --git a/example/example.cpp b/example/example.cpp index 0baf7e7..f9d1bf5 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -80,6 +80,7 @@ void TestCase1() { std::cout << NAMEOF_TYPE(int[]) << std::endl; // int[] std::cout << NAMEOF_TYPE(SomeStruct) << std::endl; // SomeStruct std::cout << NAMEOF_TYPE(Long::LL) << std::endl; // LL + std::cout << NAMEOF_TYPE(const volatile int) << std::endl; // const volatile int std::cout << NAMEOF_FULL(someVar.SomeField) << std::endl; // someVar.SomeField std::cout << NAMEOF_FULL(&SomeStruct::SomeMethod2) << std::endl; // &SomeStruct::SomeMethod2 diff --git a/src/nameof.hpp b/src/nameof.hpp index c8d36cd..ddeb1ba 100644 --- a/src/nameof.hpp +++ b/src/nameof.hpp @@ -1,8 +1,8 @@ -// nameof() c++11 https://github.com/Terik23/nameof -// Vesion 0.1.4 +// nameof() c++11 https://github.com/Neargye/nameof +// Vesion 0.1.5 // // Licensed under the MIT License . -// Copyright (c) 2016 - 2018 Daniil Goncharov . +// Copyright (c) 2016, 2018 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -27,16 +27,15 @@ namespace nameof { -template -inline constexpr const char* Nameof(const char(&name)[N], const size_t length = N) { - return length == 0 ? name - : (name[length - 1] == ' ' || name[length - 1] == '.' || - name[length - 1] == '>' || name[length - 1] == ':' || - name[length - 1] == '&' || name[length - 1] == '*' || - name[length - 1] == '+' || name[length - 1] == '~' || - name[length - 1] == '-' || name[length - 1] == '!') - ? &name[length] - : Nameof(name, length - 1); +inline constexpr bool IsLexeme(const char s) { + return (s == '.' || s == '>' || s == ':' || s == '&' || s == '*' || + s == '+' || s == '~' || s == '-' || s == '!'); +} +template +inline constexpr const char* Nameof(const char(&name)[N], const std::size_t length = N) { + return length == 0 ? name : IsLexeme(name[length - 1]) + ? &name[length] + : Nameof(name, length - 1); } } diff --git a/test/test.cpp b/test/test.cpp index a3583bb..0504ac9 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -116,6 +116,7 @@ TEST_CASE("NAMEOF_TYPE") { SECTION("NAMEOF_TYPE") { REQUIRE(std::strcmp(NAMEOF_TYPE(int[]), "int[]") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE(int), "int") == 0); + REQUIRE(std::strcmp(NAMEOF_TYPE(const volatile int[]), "const volatile int[]") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE(std::string), "string") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE(SomeStruct), "SomeStruct") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE(Long::LL), "LL") == 0); @@ -163,7 +164,8 @@ TEST_CASE("NAMEOF_FULL") { TEST_CASE("NAMEOF_TYPE_FULL") { SECTION("NAMEOF_TYPE_FULL") { REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int[]), "int[]") == 0); - REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int), "int") == 0); + REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(const volatile int[]), "const volatile int[]") == 0); + REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(Long::LL), "Long::LL") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(std::string), "std::string") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(SomeStruct), "SomeStruct") == 0); REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(Long::LL), "Long::LL") == 0);