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,12 +25,15 @@ 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.substr( 1, optName.size() );
}
return optName;
}

View File

@ -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);
}

View File

@ -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) {