Add op+(StringRef, StringRef) -> std::string

This commit is contained in:
Martin Hořeňovský 2020-05-31 22:30:41 +02:00
parent f2b9508081
commit 317145514f
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
10 changed files with 89 additions and 7 deletions

View File

@ -39,6 +39,14 @@ namespace Catch {
return os.write(str.data(), str.size()); return os.write(str.data(), str.size());
} }
std::string operator+(StringRef lhs, StringRef rhs) {
std::string ret;
ret.reserve(lhs.size() + rhs.size());
ret += lhs;
ret += rhs;
return ret;
}
auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& { auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& {
lhs.append(rhs.data(), rhs.size()); lhs.append(rhs.data(), rhs.size());
return lhs; return lhs;

View File

@ -101,6 +101,7 @@ namespace Catch {
friend std::string& operator += (std::string& lhs, StringRef const& sr); friend std::string& operator += (std::string& lhs, StringRef const& sr);
friend std::ostream& operator << (std::ostream& os, StringRef const& sr); friend std::ostream& operator << (std::ostream& os, StringRef const& sr);
friend std::string operator+(StringRef lhs, StringRef rhs);
}; };

View File

@ -1289,6 +1289,10 @@ String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringre
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringref" == "a stringref" String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringref" == "a stringref"
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: lhs == "some string += the stringref contents" for: "some string += the stringref contents"
==
"some string += the stringref contents"
String.tests.cpp:<line number>: passed: together == "abrakadabra" for: "abrakadabra" == "abrakadabra"
String.tests.cpp:<line number>: passed: with 1 message: 'empty.size() == 0' String.tests.cpp:<line number>: passed: with 1 message: 'empty.size() == 0'
String.tests.cpp:<line number>: passed: with 1 message: 'empty.begin() == empty.end()' String.tests.cpp:<line number>: passed: with 1 message: 'empty.begin() == empty.end()'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.size() == 3' String.tests.cpp:<line number>: passed: with 1 message: 'stringref.size() == 3'

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
=============================================================================== ===============================================================================
test cases: 337 | 263 passed | 70 failed | 4 failed as expected test cases: 337 | 263 passed | 70 failed | 4 failed as expected
assertions: 1924 | 1772 passed | 131 failed | 21 failed as expected assertions: 1926 | 1774 passed | 131 failed | 21 failed as expected

View File

@ -9449,6 +9449,32 @@ String.tests.cpp:<line number>: PASSED:
with expansion: with expansion:
11 == 11 11 == 11
-------------------------------------------------------------------------------
StringRef
std::string += StringRef
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( lhs == "some string += the stringref contents" )
with expansion:
"some string += the stringref contents"
==
"some string += the stringref contents"
-------------------------------------------------------------------------------
StringRef
StringRef + StringRef
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( together == "abrakadabra" )
with expansion:
"abrakadabra" == "abrakadabra"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
StringRef at compilation time StringRef at compilation time
Simple constructors Simple constructors
@ -15033,5 +15059,5 @@ Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 337 | 247 passed | 86 failed | 4 failed as expected test cases: 337 | 247 passed | 86 failed | 4 failed as expected
assertions: 1941 | 1772 passed | 148 failed | 21 failed as expected assertions: 1943 | 1774 passed | 148 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="132" tests="1942" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1944" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -1145,6 +1145,8 @@ Matchers.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="StringRef/from std::string/assigned" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/from std::string/assigned" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/explicitly constructed" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/to std::string/explicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/assigned" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/to std::string/assigned" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/std::string += StringRef" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/StringRef + StringRef" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}"/> <testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}"/>

View File

@ -155,6 +155,8 @@
<testCase name="StringRef/from std::string/assigned" duration="{duration}"/> <testCase name="StringRef/from std::string/assigned" duration="{duration}"/>
<testCase name="StringRef/to std::string/explicitly constructed" duration="{duration}"/> <testCase name="StringRef/to std::string/explicitly constructed" duration="{duration}"/>
<testCase name="StringRef/to std::string/assigned" duration="{duration}"/> <testCase name="StringRef/to std::string/assigned" duration="{duration}"/>
<testCase name="StringRef/std::string += StringRef" duration="{duration}"/>
<testCase name="StringRef/StringRef + StringRef" duration="{duration}"/>
<testCase name="StringRef at compilation time/Simple constructors" duration="{duration}"/> <testCase name="StringRef at compilation time/Simple constructors" duration="{duration}"/>
<testCase name="StringRef at compilation time/UDL construction" duration="{duration}"/> <testCase name="StringRef at compilation time/UDL construction" duration="{duration}"/>
</file> </file>

View File

@ -2491,6 +2491,10 @@ ok {test-number} - stdStr.size() == sr.size() for: 11 == 11
ok {test-number} - stdStr == "a stringref" for: "a stringref" == "a stringref" ok {test-number} - stdStr == "a stringref" for: "a stringref" == "a stringref"
# StringRef # StringRef
ok {test-number} - stdStr.size() == sr.size() for: 11 == 11 ok {test-number} - stdStr.size() == sr.size() for: 11 == 11
# StringRef
ok {test-number} - lhs == "some string += the stringref contents" for: "some string += the stringref contents" == "some string += the stringref contents"
# StringRef
ok {test-number} - together == "abrakadabra" for: "abrakadabra" == "abrakadabra"
# StringRef at compilation time # StringRef at compilation time
ok {test-number} - with 1 message: 'empty.size() == 0' ok {test-number} - with 1 message: 'empty.size() == 0'
# StringRef at compilation time # StringRef at compilation time
@ -3874,5 +3878,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
ok {test-number} - ok {test-number} -
# xmlentitycheck # xmlentitycheck
ok {test-number} - ok {test-number} -
1..1933 1..1935

View File

@ -11724,6 +11724,30 @@ Message from section two
</Section> </Section>
<OverallResults successes="2" failures="0" expectedFailures="0"/> <OverallResults successes="2" failures="0" expectedFailures="0"/>
</Section> </Section>
<Section name="std::string += StringRef" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
lhs == "some string += the stringref contents"
</Original>
<Expanded>
"some string += the stringref contents"
==
"some string += the stringref contents"
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="StringRef + StringRef" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
together == "abrakadabra"
</Original>
<Expanded>
"abrakadabra" == "abrakadabra"
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="StringRef at compilation time" tags="[constexpr][StringRef][Strings]" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" > <TestCase name="StringRef at compilation time" tags="[constexpr][StringRef][Strings]" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
@ -18031,7 +18055,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1772" failures="149" expectedFailures="21"/> <OverallResults successes="1774" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1772" failures="148" expectedFailures="21"/> <OverallResults successes="1774" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -4,7 +4,6 @@
#include <cstring> #include <cstring>
TEST_CASE( "StringRef", "[Strings][StringRef]" ) { TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
using Catch::StringRef; using Catch::StringRef;
SECTION( "Empty string" ) { SECTION( "Empty string" ) {
@ -123,6 +122,18 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
REQUIRE( stdStr.size() == sr.size() ); REQUIRE( stdStr.size() == sr.size() );
} }
} }
SECTION("std::string += StringRef") {
StringRef sr = "the stringref contents";
std::string lhs("some string += ");
lhs += sr;
REQUIRE(lhs == "some string += the stringref contents");
}
SECTION("StringRef + StringRef") {
StringRef sr1 = "abraka", sr2 = "dabra";
std::string together = sr1 + sr2;
REQUIRE(together == "abrakadabra");
}
} }
TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") { TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {