Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Use StringRef for Opt's optNames

Removes another ~70 allocations.
Commit
cd3c7ebe873c3da91ce57c6b2221ba95ee7f93e6
Author
Martin Hořeňovský ·
Parents
5d5f42f99b74

Files · Download .tar.gz

Changes

 src/catch2/internal/catch_clara.cpp     | 15 +++++++++------
 src/catch2/internal/catch_clara.hpp     |  6 +++---
 src/catch2/internal/catch_stringref.hpp |  2 +-
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/catch2/internal/catch_clara.cpp b/src/catch2/internal/catch_clara.cpp
index 9510fcc2..b4f5d054 100644
--- a/src/catch2/internal/catch_clara.cpp
+++ b/src/catch2/internal/catch_clara.cpp
@@ -25,13 +25,16 @@ namespace {
             ;
     }
 
-    std::string normaliseOpt( std::string const& optName ) {
-#ifdef CATCH_PLATFORM_WINDOWS
-        if ( optName[0] == '/' )
-            return "-" + optName.substr( 1 );
-        else
+    Catch::StringRef normaliseOpt( Catch::StringRef optName ) {
+        if ( optName[0] == '-'
+#if defined(CATCH_PLATFORM_WINDOWS)
+             || optName[0] == '/'
 #endif
-            return optName;
+        ) {
+            return optName.substr( 1, optName.size() );
+        }
+
+        return optName;
     }
 
 } // namespace
diff --git a/src/catch2/internal/catch_clara.hpp b/src/catch2/internal/catch_clara.hpp
index 4bcef406..a5c5e4e4 100644
--- a/src/catch2/internal/catch_clara.hpp
+++ b/src/catch2/internal/catch_clara.hpp
@@ -545,7 +545,7 @@ namespace Catch {
         // A parser for options
         class Opt : public Detail::ParserRefImpl<Opt> {
         protected:
-            std::vector<std::string> m_optNames;
+            std::vector<StringRef> m_optNames;
 
         public:
             template <typename LambdaT>
@@ -571,11 +571,11 @@ namespace Catch {
             Opt( T& ref, StringRef hint ):
                 ParserRefImpl( ref, hint ) {}
 
-            Opt& operator[]( std::string const& optName ) & {
+            Opt& operator[]( StringRef optName ) & {
                 m_optNames.push_back(optName);
                 return *this;
             }
-            Opt&& operator[]( std::string const& optName ) && {
+            Opt&& operator[]( StringRef optName ) && {
                 m_optNames.push_back( optName );
                 return CATCH_MOVE(*this);
             }
diff --git a/src/catch2/internal/catch_stringref.hpp b/src/catch2/internal/catch_stringref.hpp
index 99bb9a98..93f25171 100644
--- a/src/catch2/internal/catch_stringref.hpp
+++ b/src/catch2/internal/catch_stringref.hpp
@@ -75,7 +75,7 @@ namespace Catch {
         }
 
         // Returns a substring of [start, start + length).
-        // If start + length > size(), then the substring is [start, start + size()).
+        // If start + length > size(), then the substring is [start, size()).
         // If start > size(), then the substring is empty.
         constexpr StringRef substr(size_type start, size_type length) const noexcept {
             if (start < m_size) {