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) StringRe
| 375 | /// visible - but it does mean (substring) StringRefs should not be shared between |
| 376 | /// threads. |
| 377 | class StringRef { |
| 378 | public: |
| 379 | using size_type = std::size_t; |
| 380 | |
| 381 | private: |
| 382 | friend struct StringRefTestAccess; |
| 383 | |
| 384 | char const* m_start; |
| 385 | size_type m_size; |
| 386 | |
| 387 | char* m_data = nullptr; |
| 388 | |
| 389 | void takeOwnership(); |
| 390 | |
| 391 | static constexpr char const* const s_empty = ""; |
| 392 | |
| 393 | public: // construction/ assignment |
| 394 | StringRef() noexcept |
| 395 | : StringRef( s_empty, 0 ) |
| 396 | {} |
| 397 | |
| 398 | StringRef( StringRef const& other ) noexcept |
| 399 | : m_start( other.m_start ), |
| 400 | m_size( other.m_size ) |
| 401 | {} |
| 402 | |
| 403 | StringRef( StringRef&& other ) noexcept |
| 404 | : m_start( other.m_start ), |
| 405 | m_size( other.m_size ), |
| 406 | m_data( other.m_data ) |
| 407 | { |
| 408 | other.m_data = nullptr; |
| 409 | } |
| 410 | |
| 411 | StringRef( char const* rawChars ) noexcept; |
| 412 | |
| 413 | StringRef( char const* rawChars, size_type size ) noexcept |
| 414 | : m_start( rawChars ), |
| 415 | m_size( size ) |
| 416 | {} |
| 417 | |
| 418 | StringRef( std::string const& stdString ) noexcept |
| 419 | : m_start( stdString.c_str() ), |
| 420 | m_size( stdString.size() ) |
| 421 | {} |
| 422 | |
| 423 | ~StringRef() noexcept { |
| 424 | delete[] m_data; |
| 425 | } |
| 426 | |
| 427 | auto operator = ( StringRef const &other ) noexcept -> StringRef& { |
| 428 | delete[] m_data; |
| 429 | m_data = nullptr; |
| 430 | m_start = other.m_start; |
| 431 | m_size = other.m_size; |
| 432 | return *this; |
| 433 | } |
| 434 |
no outgoing calls
no test coverage detected