| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Copyright 2016 Two Blue Cubes Ltd. All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Distributed under the Boost Software License, Version 1.0. (See accompanying | 
					
						
							|  |  |  |  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #ifndef CATCH_STRINGREF_H_INCLUDED
 | 
					
						
							|  |  |  | #define CATCH_STRINGREF_H_INCLUDED
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-05 22:53:21 +01:00
										 |  |  | #include <cstddef>
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:39:14 +01:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:50:44 +01:00
										 |  |  | #include <iosfwd>
 | 
					
						
							| 
									
										
										
										
											2017-08-05 22:53:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  | namespace Catch { | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     /// A non-owning string class (similar to the forthcoming std::string_view)
 | 
					
						
							|  |  |  |     /// Note that, because a StringRef may be a substring of another string,
 | 
					
						
							|  |  |  |     /// it may not be null terminated. c_str() must return a null terminated
 | 
					
						
							|  |  |  |     /// string, however, and so the StringRef will internally take ownership
 | 
					
						
							|  |  |  |     /// (taking a copy), if necessary. In theory this ownership is not externally
 | 
					
						
							|  |  |  |     /// visible - but it does mean (substring) StringRefs should not be shared between
 | 
					
						
							|  |  |  |     /// threads.
 | 
					
						
							|  |  |  |     class StringRef { | 
					
						
							| 
									
										
										
										
											2017-11-21 11:08:08 +00:00
										 |  |  |     public: | 
					
						
							|  |  |  |         using size_type = std::size_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |         friend struct StringRefTestAccess; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         char const* m_start; | 
					
						
							|  |  |  |         size_type m_size; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 09:14:49 +01:00
										 |  |  |         char* m_data = nullptr; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |         void takeOwnership(); | 
					
						
							| 
									
										
										
										
											2017-11-17 18:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         static constexpr char const* const s_empty = ""; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     public: // construction/ assignment
 | 
					
						
							| 
									
										
										
										
											2017-11-17 18:33:34 +00:00
										 |  |  |         StringRef() noexcept | 
					
						
							|  |  |  |         :   StringRef( s_empty, 0 ) | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         StringRef( StringRef const& other ) noexcept | 
					
						
							|  |  |  |         :   m_start( other.m_start ), | 
					
						
							|  |  |  |             m_size( other.m_size ) | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         StringRef( StringRef&& other ) noexcept | 
					
						
							|  |  |  |         :   m_start( other.m_start ), | 
					
						
							|  |  |  |             m_size( other.m_size ), | 
					
						
							|  |  |  |             m_data( other.m_data ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             other.m_data = nullptr; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 11:08:08 +00:00
										 |  |  |         StringRef( char const* rawChars ) noexcept; | 
					
						
							| 
									
										
										
										
											2017-11-17 18:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         StringRef( char const* rawChars, size_type size ) noexcept | 
					
						
							|  |  |  |         :   m_start( rawChars ), | 
					
						
							|  |  |  |             m_size( size ) | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         StringRef( std::string const& stdString ) noexcept | 
					
						
							|  |  |  |         :   m_start( stdString.c_str() ), | 
					
						
							|  |  |  |             m_size( stdString.size() ) | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ~StringRef() noexcept { | 
					
						
							|  |  |  |             delete[] m_data; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 14:15:26 -06:00
										 |  |  |         auto operator = ( StringRef const &other ) noexcept -> StringRef& { | 
					
						
							| 
									
										
										
										
											2019-05-26 21:19:55 +02:00
										 |  |  |             delete[] m_data; | 
					
						
							|  |  |  |             m_data = nullptr; | 
					
						
							|  |  |  |             m_start = other.m_start; | 
					
						
							|  |  |  |             m_size = other.m_size; | 
					
						
							| 
									
										
										
										
											2017-11-17 14:15:26 -06:00
										 |  |  |             return *this; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:39:14 +01:00
										 |  |  |         operator std::string() const; | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         void swap( StringRef& other ) noexcept; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     public: // operators
 | 
					
						
							|  |  |  |         auto operator == ( StringRef const& other ) const noexcept -> bool; | 
					
						
							|  |  |  |         auto operator != ( StringRef const& other ) const noexcept -> bool; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 10:46:18 +02:00
										 |  |  |         auto operator[] ( size_type index ) const noexcept -> char; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     public: // named queries
 | 
					
						
							| 
									
										
										
										
											2017-11-17 18:33:34 +00:00
										 |  |  |         auto empty() const noexcept -> bool { | 
					
						
							|  |  |  |             return m_size == 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         auto size() const noexcept -> size_type { | 
					
						
							|  |  |  |             return m_size; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |         auto c_str() const -> char const*; | 
					
						
							| 
									
										
										
										
											2018-02-17 20:41:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     public: // substrings and searches
 | 
					
						
							|  |  |  |         auto substr( size_type start, size_type size ) const noexcept -> StringRef; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 22:05:01 +01:00
										 |  |  |         // Returns the current start pointer.
 | 
					
						
							|  |  |  |         // Note that the pointer can change when if the StringRef is a substring
 | 
					
						
							|  |  |  |         auto currentData() const noexcept -> char const*; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  |     private: // ownership queries - may not be consistent between calls
 | 
					
						
							|  |  |  |         auto isOwned() const noexcept -> bool; | 
					
						
							|  |  |  |         auto isSubstring() const noexcept -> bool; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 09:04:14 +01:00
										 |  |  |     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; | 
					
						
							| 
									
										
										
										
											2017-08-14 08:50:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 22:05:01 +01:00
										 |  |  |     auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; | 
					
						
							|  |  |  |     auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:50:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 12:09:04 +00:00
										 |  |  |     inline auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { | 
					
						
							| 
									
										
										
										
											2017-11-21 11:08:08 +00:00
										 |  |  |         return StringRef( rawChars, size ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  | } // namespace Catch
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 13:19:21 +02:00
										 |  |  | inline auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { | 
					
						
							|  |  |  |     return Catch::StringRef( rawChars, size ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 11:18:14 +01:00
										 |  |  | #endif // CATCH_STRINGREF_H_INCLUDED
 |