From cd3c7ebe873c3da91ce57c6b2221ba95ee7f93e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 26 Dec 2023 14:01:15 +0100 Subject: [PATCH] Use StringRef for Opt's optNames Removes another ~70 allocations. --- 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 { protected: - std::vector m_optNames; + std::vector m_optNames; public: template @@ -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) {