File indexing completed on 2025-09-18 09:08:52
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_STRINGREF_HPP_INCLUDED
0009 #define CATCH_STRINGREF_HPP_INCLUDED
0010
0011 #include <cstddef>
0012 #include <string>
0013 #include <iosfwd>
0014 #include <cassert>
0015
0016 #include <cstring>
0017
0018 namespace Catch {
0019
0020
0021
0022
0023 class StringRef {
0024 public:
0025 using size_type = std::size_t;
0026 using const_iterator = const char*;
0027
0028 static constexpr size_type npos{ static_cast<size_type>( -1 ) };
0029
0030 private:
0031 static constexpr char const* const s_empty = "";
0032
0033 char const* m_start = s_empty;
0034 size_type m_size = 0;
0035
0036 public:
0037 constexpr StringRef() noexcept = default;
0038
0039 StringRef( char const* rawChars ) noexcept;
0040
0041 constexpr StringRef( char const* rawChars, size_type size ) noexcept
0042 : m_start( rawChars ),
0043 m_size( size )
0044 {}
0045
0046 StringRef( std::string const& stdString ) noexcept
0047 : m_start( stdString.c_str() ),
0048 m_size( stdString.size() )
0049 {}
0050
0051 explicit operator std::string() const {
0052 return std::string(m_start, m_size);
0053 }
0054
0055 public:
0056 auto operator == ( StringRef other ) const noexcept -> bool {
0057 return m_size == other.m_size
0058 && (std::memcmp( m_start, other.m_start, m_size ) == 0);
0059 }
0060 auto operator != (StringRef other) const noexcept -> bool {
0061 return !(*this == other);
0062 }
0063
0064 constexpr auto operator[] ( size_type index ) const noexcept -> char {
0065 assert(index < m_size);
0066 return m_start[index];
0067 }
0068
0069 bool operator<(StringRef rhs) const noexcept;
0070
0071 public:
0072 constexpr auto empty() const noexcept -> bool {
0073 return m_size == 0;
0074 }
0075 constexpr auto size() const noexcept -> size_type {
0076 return m_size;
0077 }
0078
0079
0080
0081
0082 constexpr StringRef substr(size_type start, size_type length) const noexcept {
0083 if (start < m_size) {
0084 const auto shortened_size = m_size - start;
0085 return StringRef(m_start + start, (shortened_size < length) ? shortened_size : length);
0086 } else {
0087 return StringRef();
0088 }
0089 }
0090
0091
0092 constexpr char const* data() const noexcept {
0093 return m_start;
0094 }
0095
0096 constexpr const_iterator begin() const { return m_start; }
0097 constexpr const_iterator end() const { return m_start + m_size; }
0098
0099
0100 friend std::string& operator += (std::string& lhs, StringRef rhs);
0101 friend std::ostream& operator << (std::ostream& os, StringRef str);
0102 friend std::string operator+(StringRef lhs, StringRef rhs);
0103
0104
0105
0106
0107
0108
0109
0110 int compare( StringRef rhs ) const;
0111 };
0112
0113
0114 constexpr auto operator ""_sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
0115 return StringRef( rawChars, size );
0116 }
0117 }
0118
0119 constexpr auto operator ""_catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
0120 return Catch::StringRef( rawChars, size );
0121 }
0122
0123 #endif