Small improvements for StringRef

* `operator[]` is constexpr
* `operator<<` and `operator+=` are hidden friends
This commit is contained in:
Martin Hořeňovský 2020-05-10 07:22:11 +02:00
parent 094d840efe
commit 895d0a0696
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 20 additions and 12 deletions

View File

@ -53,7 +53,7 @@ namespace Catch {
return !(*this == other); return !(*this == other);
} }
auto operator[] ( size_type index ) const noexcept -> char { constexpr auto operator[] ( size_type index ) const noexcept -> char {
assert(index < m_size); assert(index < m_size);
return m_start[index]; return m_start[index];
} }
@ -97,10 +97,12 @@ namespace Catch {
public: // iterators public: // iterators
constexpr const_iterator begin() const { return m_start; } constexpr const_iterator begin() const { return m_start; }
constexpr const_iterator end() const { return m_start + m_size; } constexpr const_iterator end() const { return m_start + m_size; }
friend std::string& operator += (std::string& lhs, StringRef const& sr);
friend std::ostream& operator << (std::ostream& os, StringRef const& sr);
}; };
auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&;
auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;
constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
return StringRef( rawChars, size ); return StringRef( rawChars, size );

View File

@ -1298,6 +1298,7 @@ String.tests.cpp:<line number>: passed: with 1 message: 'stringref.begin() == ab
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.begin() != stringref.end()' String.tests.cpp:<line number>: passed: with 1 message: 'stringref.begin() != stringref.end()'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(10, 0).empty()' String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(10, 0).empty()'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(2, 1).data() == abc + 2' String.tests.cpp:<line number>: passed: with 1 message: 'stringref.substr(2, 1).data() == abc + 2'
String.tests.cpp:<line number>: passed: with 1 message: 'stringref[1] == 'b''
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.size() == 2' String.tests.cpp:<line number>: passed: with 1 message: 'shortened.size() == 2'
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.data() == abc' String.tests.cpp:<line number>: passed: with 1 message: 'shortened.data() == abc'
String.tests.cpp:<line number>: passed: with 1 message: 'shortened.begin() != shortened.end()' String.tests.cpp:<line number>: passed: with 1 message: 'shortened.begin() != shortened.end()'

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
=============================================================================== ===============================================================================
test cases: 334 | 260 passed | 70 failed | 4 failed as expected test cases: 334 | 260 passed | 70 failed | 4 failed as expected
assertions: 1895 | 1743 passed | 131 failed | 21 failed as expected assertions: 1896 | 1744 passed | 131 failed | 21 failed as expected

View File

@ -9492,6 +9492,10 @@ String.tests.cpp:<line number>: PASSED:
with message: with message:
stringref.substr(2, 1).data() == abc + 2 stringref.substr(2, 1).data() == abc + 2
String.tests.cpp:<line number>: PASSED:
with message:
stringref[1] == 'b'
String.tests.cpp:<line number>: PASSED: String.tests.cpp:<line number>: PASSED:
with message: with message:
shortened.size() == 2 shortened.size() == 2
@ -14789,5 +14793,5 @@ Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 334 | 244 passed | 86 failed | 4 failed as expected test cases: 334 | 244 passed | 86 failed | 4 failed as expected
assertions: 1912 | 1743 passed | 148 failed | 21 failed as expected assertions: 1913 | 1744 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="1913" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1914" 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"/>

View File

@ -2510,6 +2510,8 @@ ok {test-number} - with 1 message: 'stringref.substr(10, 0).empty()'
# StringRef at compilation time # StringRef at compilation time
ok {test-number} - with 1 message: 'stringref.substr(2, 1).data() == abc + 2' ok {test-number} - with 1 message: 'stringref.substr(2, 1).data() == abc + 2'
# StringRef at compilation time # StringRef at compilation time
ok {test-number} - with 1 message: 'stringref[1] == 'b''
# StringRef at compilation time
ok {test-number} - with 1 message: 'shortened.size() == 2' ok {test-number} - with 1 message: 'shortened.size() == 2'
# StringRef at compilation time # StringRef at compilation time
ok {test-number} - with 1 message: 'shortened.data() == abc' ok {test-number} - with 1 message: 'shortened.data() == abc'
@ -3816,5 +3818,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..1904 1..1905

View File

@ -11728,7 +11728,7 @@ Message from section two
</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" >
<Section name="Simple constructors" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" > <Section name="Simple constructors" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="14" failures="0" expectedFailures="0"/> <OverallResults successes="15" failures="0" expectedFailures="0"/>
</Section> </Section>
<Section name="UDL construction" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" > <Section name="UDL construction" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<OverallResults successes="6" failures="0" expectedFailures="0"/> <OverallResults successes="6" failures="0" expectedFailures="0"/>
@ -17756,7 +17756,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1743" failures="149" expectedFailures="21"/> <OverallResults successes="1744" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1743" failures="148" expectedFailures="21"/> <OverallResults successes="1744" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -126,8 +126,6 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
} }
TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") { TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
//TODO:
// * substr
using Catch::StringRef; using Catch::StringRef;
SECTION("Simple constructors") { SECTION("Simple constructors") {
constexpr StringRef empty{}; constexpr StringRef empty{};
@ -144,6 +142,7 @@ TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
STATIC_REQUIRE(stringref.begin() != stringref.end()); STATIC_REQUIRE(stringref.begin() != stringref.end());
STATIC_REQUIRE(stringref.substr(10, 0).empty()); STATIC_REQUIRE(stringref.substr(10, 0).empty());
STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2); STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2);
STATIC_REQUIRE(stringref[1] == 'b');
constexpr StringRef shortened(abc, 2); constexpr StringRef shortened(abc, 2);