Make StringRef's operator std::string explicit

This way it is explicit when there is a `StringRef` -> `std::string`
conversion and makes it easier to look for allocations that could
be avoided.

Doing this has already removed one allocation per registered test
case, as there was a completely pointless `StringRef` -> `std::string`
conversion when parsing tags of a test case.
This commit is contained in:
Martin Hořeňovský
2019-09-08 14:49:40 +02:00
parent 14362533bb
commit 7b865daccc
13 changed files with 23 additions and 67 deletions

View File

@@ -56,13 +56,13 @@ namespace Catch {
if( isFalseTest( m_info.resultDisposition ) )
return "!(" + m_info.capturedExpression + ")";
else
return m_info.capturedExpression;
return static_cast<std::string>(m_info.capturedExpression);
}
std::string AssertionResult::getExpressionInMacro() const {
std::string expr;
if( m_info.macroName[0] == 0 )
expr = m_info.capturedExpression;
expr = static_cast<std::string>(m_info.capturedExpression);
else {
expr.reserve( m_info.macroName.size() + m_info.capturedExpression.size() + 4 );
expr += m_info.macroName;

View File

@@ -113,7 +113,7 @@ namespace Catch {
case ',':
if (start != pos && openings.size() == 0) {
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = trimmed(start, pos);
m_messages.back().message = static_cast<std::string>(trimmed(start, pos));
m_messages.back().message += " := ";
start = pos;
}
@@ -121,7 +121,7 @@ namespace Catch {
}
assert(openings.size() == 0 && "Mismatched openings");
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = trimmed(start, names.size() - 1);
m_messages.back().message = static_cast<std::string>(trimmed(start, names.size() - 1));
m_messages.back().message += " := ";
}
Capturer::~Capturer() {

View File

@@ -230,7 +230,7 @@ namespace Catch {
m_unfinishedSections.push_back(endInfo);
}
#if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
void RunContext::benchmarkPreparing(std::string const& name) {
m_reporter->benchmarkPreparing(name);
@@ -279,7 +279,7 @@ namespace Catch {
// Don't rebuild the result -- the stringification itself can cause more fatal errors
// Instead, fake a result data.
AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } );
tempResult.message = message;
tempResult.message = static_cast<std::string>(message);
AssertionResult result(m_lastAssertionInfo, tempResult);
assertionEnded(result);
@@ -442,7 +442,7 @@ namespace Catch {
m_lastAssertionInfo = info;
AssertionResultData data( resultType, LazyExpression( false ) );
data.message = message;
data.message = static_cast<std::string>(message);
AssertionResult assertionResult{ m_lastAssertionInfo, data };
assertionEnded( assertionResult );
if( !assertionResult.isOk() )

View File

@@ -23,6 +23,7 @@ namespace Catch {
class StringRef {
public:
using size_type = std::size_t;
using const_iterator = const char*;
private:
friend struct StringRefTestAccess;
@@ -78,7 +79,7 @@ namespace Catch {
return *this;
}
operator std::string() const;
explicit operator std::string() const;
void swap( StringRef& other ) noexcept;
@@ -105,6 +106,10 @@ namespace Catch {
// Note that the pointer can change when if the StringRef is a substring
auto currentData() const noexcept -> char const*;
public: // iterators
const_iterator begin() const { return m_start; }
const_iterator end() const { return m_start + m_size; }
private: // ownership queries - may not be consistent between calls
auto isOwned() const noexcept -> bool;
auto isSubstring() const noexcept -> bool;

View File

@@ -59,8 +59,7 @@ namespace Catch {
std::vector<std::string> tags;
std::string desc, tag;
bool inTag = false;
std::string _descOrTags = nameAndTags.tags;
for (char c : _descOrTags) {
for (char c : nameAndTags.tags) {
if( !inTag ) {
if( c == '[' )
inTag = true;
@@ -93,7 +92,7 @@ namespace Catch {
tags.push_back( "." );
}
TestCaseInfo info( nameAndTags.name, _className, desc, tags, _lineInfo );
TestCaseInfo info( static_cast<std::string>(nameAndTags.name), _className, desc, tags, _lineInfo );
return TestCase( _testCase, std::move(info) );
}

View File

@@ -105,7 +105,7 @@ namespace Catch {
}
std::string extractClassName( StringRef const& classOrQualifiedMethodName ) {
std::string className = classOrQualifiedMethodName;
std::string className(classOrQualifiedMethodName);
if( startsWith( className, '&' ) )
{
std::size_t lastColons = className.rfind( "::" );

View File

@@ -654,7 +654,7 @@ namespace Catch { \
template<> struct StringMaker<enumName> { \
static std::string convert( enumName value ) { \
static const auto& enumInfo = ::Catch::getMutableRegistryHub().getMutableEnumValuesRegistry().registerEnum( #enumName, #__VA_ARGS__, { __VA_ARGS__ } ); \
return enumInfo.lookup( static_cast<int>( value ) ); \
return static_cast<std::string>(enumInfo.lookup( static_cast<int>( value ) )); \
} \
}; \
}