Skip to content

Commit

Permalink
Use StringRef for Opt's optNames
Browse files Browse the repository at this point in the history
Removes another ~70 allocations.
  • Loading branch information
horenmar committed Dec 26, 2023
1 parent 5d5f42f commit cd3c7eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions src/catch2/internal/catch_clara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/catch2/internal/catch_clara.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/catch2/internal/catch_stringref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cd3c7eb

Please sign in to comment.