From 895d0a06960d9f48190dde7c7aa4a56d614946fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 10 May 2020 07:22:11 +0200 Subject: [PATCH] Small improvements for StringRef * `operator[]` is constexpr * `operator<<` and `operator+=` are hidden friends --- src/catch2/catch_stringref.hpp | 8 +++++--- tests/SelfTest/Baselines/compact.sw.approved.txt | 1 + tests/SelfTest/Baselines/console.std.approved.txt | 2 +- tests/SelfTest/Baselines/console.sw.approved.txt | 6 +++++- tests/SelfTest/Baselines/junit.sw.approved.txt | 2 +- tests/SelfTest/Baselines/tap.sw.approved.txt | 4 +++- tests/SelfTest/Baselines/xml.sw.approved.txt | 6 +++--- tests/SelfTest/IntrospectiveTests/String.tests.cpp | 3 +-- 8 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/catch2/catch_stringref.hpp b/src/catch2/catch_stringref.hpp index 8ed1e731..1e07b6ad 100644 --- a/src/catch2/catch_stringref.hpp +++ b/src/catch2/catch_stringref.hpp @@ -53,7 +53,7 @@ namespace Catch { return !(*this == other); } - auto operator[] ( size_type index ) const noexcept -> char { + constexpr auto operator[] ( size_type index ) const noexcept -> char { assert(index < m_size); return m_start[index]; } @@ -97,10 +97,12 @@ namespace Catch { public: // iterators constexpr const_iterator begin() const { return m_start; } 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 { return StringRef( rawChars, size ); diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index dc97d4d6..4d2fcbe2 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -1298,6 +1298,7 @@ String.tests.cpp:: passed: with 1 message: 'stringref.begin() == ab String.tests.cpp:: passed: with 1 message: 'stringref.begin() != stringref.end()' String.tests.cpp:: passed: with 1 message: 'stringref.substr(10, 0).empty()' String.tests.cpp:: passed: with 1 message: 'stringref.substr(2, 1).data() == abc + 2' +String.tests.cpp:: passed: with 1 message: 'stringref[1] == 'b'' String.tests.cpp:: passed: with 1 message: 'shortened.size() == 2' String.tests.cpp:: passed: with 1 message: 'shortened.data() == abc' String.tests.cpp:: passed: with 1 message: 'shortened.begin() != shortened.end()' diff --git a/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index c5aca7e9..f92173e5 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== 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 diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index efdfab53..4123232c 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -9492,6 +9492,10 @@ String.tests.cpp:: PASSED: with message: stringref.substr(2, 1).data() == abc + 2 +String.tests.cpp:: PASSED: +with message: + stringref[1] == 'b' + String.tests.cpp:: PASSED: with message: shortened.size() == 2 @@ -14789,5 +14793,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== 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 diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index 4ae272ba..9c4dd7cb 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index d31e82ff..b28c70d4 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -2510,6 +2510,8 @@ ok {test-number} - with 1 message: 'stringref.substr(10, 0).empty()' # StringRef at compilation time ok {test-number} - with 1 message: 'stringref.substr(2, 1).data() == abc + 2' # 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' # StringRef at compilation time 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} - # xmlentitycheck ok {test-number} - -1..1904 +1..1905 diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 340e1931..10d7f304 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -11728,7 +11728,7 @@ Message from section two
- +
@@ -17756,7 +17756,7 @@ loose text artifact
- + - + diff --git a/tests/SelfTest/IntrospectiveTests/String.tests.cpp b/tests/SelfTest/IntrospectiveTests/String.tests.cpp index ae1cc817..e616801c 100644 --- a/tests/SelfTest/IntrospectiveTests/String.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/String.tests.cpp @@ -126,8 +126,6 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) { } TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") { - //TODO: - // * substr using Catch::StringRef; SECTION("Simple constructors") { 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.substr(10, 0).empty()); STATIC_REQUIRE(stringref.substr(2, 1).data() == abc + 2); + STATIC_REQUIRE(stringref[1] == 'b'); constexpr StringRef shortened(abc, 2);