Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:02:50

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
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     /// A non-owning string class (similar to the forthcoming std::string_view)
0021     /// Note that, because a StringRef may be a substring of another string,
0022     /// it may not be null terminated.
0023     class StringRef {
0024     public:
0025         using size_type = std::size_t;
0026         using const_iterator = const char*;
0027 
0028     private:
0029         static constexpr char const* const s_empty = "";
0030 
0031         char const* m_start = s_empty;
0032         size_type m_size = 0;
0033 
0034     public: // construction
0035         constexpr StringRef() noexcept = default;
0036 
0037         StringRef( char const* rawChars ) noexcept;
0038 
0039         constexpr StringRef( char const* rawChars, size_type size ) noexcept
0040         :   m_start( rawChars ),
0041             m_size( size )
0042         {}
0043 
0044         StringRef( std::string const& stdString ) noexcept
0045         :   m_start( stdString.c_str() ),
0046             m_size( stdString.size() )
0047         {}
0048 
0049         explicit operator std::string() const {
0050             return std::string(m_start, m_size);
0051         }
0052 
0053     public: // operators
0054         auto operator == ( StringRef other ) const noexcept -> bool {
0055             return m_size == other.m_size
0056                 && (std::memcmp( m_start, other.m_start, m_size ) == 0);
0057         }
0058         auto operator != (StringRef other) const noexcept -> bool {
0059             return !(*this == other);
0060         }
0061 
0062         constexpr auto operator[] ( size_type index ) const noexcept -> char {
0063             assert(index < m_size);
0064             return m_start[index];
0065         }
0066 
0067         bool operator<(StringRef rhs) const noexcept;
0068 
0069     public: // named queries
0070         constexpr auto empty() const noexcept -> bool {
0071             return m_size == 0;
0072         }
0073         constexpr auto size() const noexcept -> size_type {
0074             return m_size;
0075         }
0076 
0077         // Returns a substring of [start, start + length).
0078         // If start + length > size(), then the substring is [start, start + size()).
0079         // If start > size(), then the substring is empty.
0080         constexpr StringRef substr(size_type start, size_type length) const noexcept {
0081             if (start < m_size) {
0082                 const auto shortened_size = m_size - start;
0083                 return StringRef(m_start + start, (shortened_size < length) ? shortened_size : length);
0084             } else {
0085                 return StringRef();
0086             }
0087         }
0088 
0089         // Returns the current start pointer. May not be null-terminated.
0090         constexpr char const* data() const noexcept {
0091             return m_start;
0092         }
0093 
0094         constexpr const_iterator begin() const { return m_start; }
0095         constexpr const_iterator end() const { return m_start + m_size; }
0096 
0097 
0098         friend std::string& operator += (std::string& lhs, StringRef sr);
0099         friend std::ostream& operator << (std::ostream& os, StringRef sr);
0100         friend std::string operator+(StringRef lhs, StringRef rhs);
0101 
0102         /**
0103          * Provides a three-way comparison with rhs
0104          *
0105          * Returns negative number if lhs < rhs, 0 if lhs == rhs, and a positive
0106          * number if lhs > rhs
0107          */
0108         int compare( StringRef rhs ) const;
0109     };
0110 
0111 
0112     constexpr auto operator ""_sr( char const* rawChars, std::size_t size ) noexcept -> StringRef {
0113         return StringRef( rawChars, size );
0114     }
0115 } // namespace Catch
0116 
0117 constexpr auto operator ""_catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef {
0118     return Catch::StringRef( rawChars, size );
0119 }
0120 
0121 #endif // CATCH_STRINGREF_HPP_INCLUDED