Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorMartin Hořeňovský2019-09-08 14:49:40 +0200
committerMartin Hořeňovský2019-09-08 15:58:10 +0200
commit7b865daccc7bba2dcc25a4f0b9ea7348b1b4d61f (patch)
treed6964f2f65b8fbcd70e8041e16677339f675f261 /include
parent14362533bb50596049f1b46bcc6d333208576a87 (diff)
downloadcatch3-7b865daccc7bba2dcc25a4f0b9ea7348b1b4d61f.tar.gz
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.
Diffstat (limited to 'include')
-rw-r--r--include/internal/catch_assertionresult.cpp4
-rw-r--r--include/internal/catch_message.cpp4
-rw-r--r--include/internal/catch_run_context.cpp6
-rw-r--r--include/internal/catch_stringref.h7
-rw-r--r--include/internal/catch_test_case_info.cpp5
-rw-r--r--include/internal/catch_test_case_registry_impl.cpp2
-rw-r--r--include/internal/catch_tostring.h2
7 files changed, 17 insertions, 13 deletions
diff --git a/include/internal/catch_assertionresult.cpp b/include/internal/catch_assertionresult.cpp
index 8f505d04..5da1cfdf 100644
--- a/include/internal/catch_assertionresult.cpp
+++ b/include/internal/catch_assertionresult.cpp
@@ -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;
diff --git a/include/internal/catch_message.cpp b/include/internal/catch_message.cpp
index faceb50c..8f391bcb 100644
--- a/include/internal/catch_message.cpp
+++ b/include/internal/catch_message.cpp
@@ -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() {
diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp
index d2acc652..79f906d6 100644
--- a/include/internal/catch_run_context.cpp
+++ b/include/internal/catch_run_context.cpp
@@ -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() )
diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h
index f7e4a360..b83e61bb 100644
--- a/include/internal/catch_stringref.h
+++ b/include/internal/catch_stringref.h
@@ -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;
diff --git a/include/internal/catch_test_case_info.cpp b/include/internal/catch_test_case_info.cpp
index 4baa9d4b..685ee30f 100644
--- a/include/internal/catch_test_case_info.cpp
+++ b/include/internal/catch_test_case_info.cpp
@@ -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) );
}
diff --git a/include/internal/catch_test_case_registry_impl.cpp b/include/internal/catch_test_case_registry_impl.cpp
index bb596804..84bfdae1 100644
--- a/include/internal/catch_test_case_registry_impl.cpp
+++ b/include/internal/catch_test_case_registry_impl.cpp
@@ -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( "::" );
diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 8668b1eb..640fc8cb 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -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 ) )); \
} \
}; \
}