diff options
| author | Martin Hořeňovský | 2021-09-27 16:10:07 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2021-09-27 19:07:45 +0200 |
| commit | f02c2678a1a891f9577712fe160c1f6f3baef3a8 (patch) | |
| tree | f83a473695dedf054dd241f65997274823b3966b /src/catch2/internal/catch_test_registry.cpp | |
| parent | 21b99d6f589090983136bf764514261dc863ed9a (diff) | |
| download | catch3-f02c2678a1a891f9577712fe160c1f6f3baef3a8.tar.gz | |
Use StringRef for className in TestCaseInfo
Diffstat (limited to 'src/catch2/internal/catch_test_registry.cpp')
| -rw-r--r-- | src/catch2/internal/catch_test_registry.cpp | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/catch2/internal/catch_test_registry.cpp b/src/catch2/internal/catch_test_registry.cpp index d33ba27a..c9e9fe4f 100644 --- a/src/catch2/internal/catch_test_registry.cpp +++ b/src/catch2/internal/catch_test_registry.cpp @@ -13,21 +13,36 @@ #include <catch2/internal/catch_string_manip.hpp> #include <catch2/internal/catch_move_and_forward.hpp> +#include <algorithm> +#include <iterator> + namespace Catch { namespace { - std::string extractClassName( StringRef classOrQualifiedMethodName ) { - std::string className( classOrQualifiedMethodName ); - if ( startsWith( className, '&' ) ) { - std::size_t lastColons = className.rfind( "::" ); - std::size_t penultimateColons = - className.rfind( "::", lastColons - 1 ); - if ( penultimateColons == std::string::npos ) - penultimateColons = 1; - className = className.substr( penultimateColons, - lastColons - penultimateColons ); + StringRef extractClassName( StringRef classOrMethodName ) { + if ( !startsWith( classOrMethodName, '&' ) ) { + return classOrMethodName; } - return className; + + // Remove the leading '&' to avoid having to special case it later + const auto methodName = + classOrMethodName.substr( 1, classOrMethodName.size() ); + + auto reverseStart = std::make_reverse_iterator( methodName.end() ); + auto reverseEnd = std::make_reverse_iterator( methodName.begin() ); + + // We make a simplifying assumption that ":" is only present + // in the input as part of "::" from C++ typenames (this is + // relatively safe assumption because the input is generated + // as stringification of type through preprocessor). + auto lastColons = std::find( reverseStart, reverseEnd, ':' ) + 1; + auto secondLastColons = + std::find( lastColons + 1, reverseEnd, ':' ); + + auto const startIdx = reverseEnd - secondLastColons; + auto const classNameSize = secondLastColons - lastColons - 1; + + return methodName.substr( startIdx, classNameSize ); } } // namespace |