Provide a public method to get StringRef's underlying pointer

This allows reducing the amount of friends needed for its interface
and some extra tricks later.

The bad part is that the pointer can become invalidated via
calls to other StringRef's public methods, but c'est la vie.
This commit is contained in:
Martin Hořeňovský
2018-02-28 22:05:01 +01:00
parent 950ccf4749
commit 05cd05743a
6 changed files with 21 additions and 26 deletions

View File

@@ -637,11 +637,11 @@ String.tests.cpp:<line number>: passed: std::strcmp( ss.c_str(), "hello" ) == 0
String.tests.cpp:<line number>: passed: ss == "hello" for: hello == "hello"
String.tests.cpp:<line number>: passed: isSubstring( ss ) for: true
String.tests.cpp:<line number>: passed: isOwned( ss ) == false for: false == false
String.tests.cpp:<line number>: passed: rawChars == data( s ) for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: rawChars == s.currentData() for: "hello world!" == "hello world!"
String.tests.cpp:<line number>: passed: ss.c_str() != rawChars for: "hello" != "hello world!"
String.tests.cpp:<line number>: passed: isSubstring( ss ) == false for: false == false
String.tests.cpp:<line number>: passed: isOwned( ss ) for: true
String.tests.cpp:<line number>: passed: data( ss ) != data( s ) for: "hello" != "hello world!"
String.tests.cpp:<line number>: passed: ss.currentData() != s.currentData() for: "hello" != "hello world!"
String.tests.cpp:<line number>: passed: ss.size() == 6 for: 6 == 6
String.tests.cpp:<line number>: passed: std::strcmp( ss.c_str(), "world!" ) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: s.c_str() == s2.c_str() for: "hello world!" == "hello world!"

View File

@@ -5076,7 +5076,7 @@ with expansion:
String.tests.cpp:<line number>:
PASSED:
REQUIRE( rawChars == data( s ) )
REQUIRE( rawChars == s.currentData() )
with expansion:
"hello world!" == "hello world!"
@@ -5100,7 +5100,7 @@ with expansion:
String.tests.cpp:<line number>:
PASSED:
REQUIRE( data( ss ) != data( s ) )
REQUIRE( ss.currentData() != s.currentData() )
with expansion:
"hello" != "hello world!"

View File

@@ -5808,7 +5808,7 @@ Message from section two
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
rawChars == data( s )
rawChars == s.currentData()
</Original>
<Expanded>
"hello world!" == "hello world!"
@@ -5840,7 +5840,7 @@ Message from section two
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
data( ss ) != data( s )
ss.currentData() != s.currentData()
</Original>
<Expanded>
"hello" != "hello world!"

View File

@@ -14,9 +14,6 @@ namespace Catch {
static auto isSubstring( StringRef const& stringRef ) -> bool {
return stringRef.isSubstring();
}
static auto data( StringRef const& stringRef ) -> char const* {
return stringRef.data();
}
};
auto isOwned( StringRef const& stringRef ) -> bool {
@@ -25,14 +22,11 @@ namespace Catch {
auto isSubstring( StringRef const& stringRef ) -> bool {
return StringRefTestAccess::isSubstring( stringRef );
}
auto data( StringRef const& stringRef ) -> char const* {
return StringRefTestAccess::data( stringRef );
}
} // namespace Catch2
namespace Catch {
inline auto toString( Catch::StringRef const& stringRef ) -> std::string {
return std::string( data( stringRef ), stringRef.size() );
return std::string( stringRef.currentData(), stringRef.size() );
}
} // namespace Catch
@@ -53,7 +47,7 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
REQUIRE( s.size() == 5 );
REQUIRE( isSubstring( s ) == false );
auto rawChars = data( s );
auto rawChars = s.currentData();
REQUIRE( std::strcmp( rawChars, "hello" ) == 0 );
SECTION( "c_str() does not cause copy" ) {
@@ -74,7 +68,6 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
REQUIRE( isSubstring( original ) == false );
REQUIRE( isOwned( original ) );
}
@@ -92,14 +85,14 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
REQUIRE( isSubstring( ss ) );
REQUIRE( isOwned( ss ) == false );
auto rawChars = data( ss );
REQUIRE( rawChars == data( s ) ); // same pointer value
auto rawChars = ss.currentData();
REQUIRE( rawChars == s.currentData() ); // same pointer value
REQUIRE( ss.c_str() != rawChars );
REQUIRE( isSubstring( ss ) == false );
REQUIRE( isOwned( ss ) );
REQUIRE( data( ss ) != data( s ) ); // different pointer value
REQUIRE( ss.currentData() != s.currentData() ); // different pointer value
}
SECTION( "non-zero-based substring") {