mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
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:
@@ -43,7 +43,7 @@ namespace Catch {
|
||||
const_cast<StringRef*>( this )->takeOwnership();
|
||||
return m_start;
|
||||
}
|
||||
auto StringRef::data() const noexcept -> char const* {
|
||||
auto StringRef::currentData() const noexcept -> char const* {
|
||||
return m_start;
|
||||
}
|
||||
|
||||
@@ -112,11 +112,11 @@ namespace Catch {
|
||||
}
|
||||
|
||||
auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& {
|
||||
return os.write(str.m_start, str.m_size);
|
||||
return os.write(str.currentData(), str.size());
|
||||
}
|
||||
|
||||
auto operator+=( std::string& lhs, StringRef const& rhs ) -> std::string& {
|
||||
lhs.append(rhs.m_start, rhs.m_size);
|
||||
lhs.append(rhs.currentData(), rhs.size());
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
@@ -84,16 +84,12 @@ namespace Catch {
|
||||
|
||||
void swap( StringRef& other ) noexcept;
|
||||
|
||||
friend auto operator << (std::ostream& os, StringRef const& sr)->std::ostream&;
|
||||
|
||||
public: // operators
|
||||
auto operator == ( StringRef const& other ) const noexcept -> bool;
|
||||
auto operator != ( StringRef const& other ) const noexcept -> bool;
|
||||
|
||||
auto operator[] ( size_type index ) const noexcept -> char;
|
||||
|
||||
friend auto operator += ( std::string& lhs, StringRef const& rhs ) -> std::string&;
|
||||
|
||||
public: // named queries
|
||||
auto empty() const noexcept -> bool {
|
||||
return m_size == 0;
|
||||
@@ -108,16 +104,22 @@ namespace Catch {
|
||||
public: // substrings and searches
|
||||
auto substr( size_type start, size_type size ) const noexcept -> StringRef;
|
||||
|
||||
// Returns the current start pointer.
|
||||
// Note that the pointer can change when if the StringRef is a substring
|
||||
auto currentData() const noexcept -> char const*;
|
||||
|
||||
private: // ownership queries - may not be consistent between calls
|
||||
auto isOwned() const noexcept -> bool;
|
||||
auto isSubstring() const noexcept -> bool;
|
||||
auto data() const noexcept -> char const*;
|
||||
};
|
||||
|
||||
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string;
|
||||
auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string;
|
||||
auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string;
|
||||
|
||||
auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&;
|
||||
auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;
|
||||
|
||||
|
||||
inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
|
||||
return StringRef( rawChars, size );
|
||||
|
Reference in New Issue
Block a user