Use StringRef for Opt's optNames

Removes another ~70 allocations.
This commit is contained in:
Martin Hořeňovský 2023-12-26 14:01:15 +01:00
parent 5d5f42f99b
commit cd3c7ebe87
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
3 changed files with 13 additions and 10 deletions

View File

@ -25,13 +25,16 @@ namespace {
; ;
} }
std::string normaliseOpt( std::string const& optName ) { Catch::StringRef normaliseOpt( Catch::StringRef optName ) {
#ifdef CATCH_PLATFORM_WINDOWS if ( optName[0] == '-'
if ( optName[0] == '/' ) #if defined(CATCH_PLATFORM_WINDOWS)
return "-" + optName.substr( 1 ); || optName[0] == '/'
else
#endif #endif
return optName; ) {
return optName.substr( 1, optName.size() );
}
return optName;
} }
} // namespace } // namespace

View File

@ -545,7 +545,7 @@ namespace Catch {
// A parser for options // A parser for options
class Opt : public Detail::ParserRefImpl<Opt> { class Opt : public Detail::ParserRefImpl<Opt> {
protected: protected:
std::vector<std::string> m_optNames; std::vector<StringRef> m_optNames;
public: public:
template <typename LambdaT> template <typename LambdaT>
@ -571,11 +571,11 @@ namespace Catch {
Opt( T& ref, StringRef hint ): Opt( T& ref, StringRef hint ):
ParserRefImpl( ref, hint ) {} ParserRefImpl( ref, hint ) {}
Opt& operator[]( std::string const& optName ) & { Opt& operator[]( StringRef optName ) & {
m_optNames.push_back(optName); m_optNames.push_back(optName);
return *this; return *this;
} }
Opt&& operator[]( std::string const& optName ) && { Opt&& operator[]( StringRef optName ) && {
m_optNames.push_back( optName ); m_optNames.push_back( optName );
return CATCH_MOVE(*this); return CATCH_MOVE(*this);
} }

View File

@ -75,7 +75,7 @@ namespace Catch {
} }
// Returns a substring of [start, start + length). // 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. // If start > size(), then the substring is empty.
constexpr StringRef substr(size_type start, size_type length) const noexcept { constexpr StringRef substr(size_type start, size_type length) const noexcept {
if (start < m_size) { if (start < m_size) {