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
| 520 | /// visible - but it does mean (substring) StringRefs should not be shared between |
| 521 | /// threads. |
| 522 | class StringRef { |
| 523 | public: |
| 524 | using size_type = std::size_t; |
| 525 | |
| 526 | private: |
| 527 | friend struct StringRefTestAccess; |
| 528 | |
| 529 | char const* m_start; |
| 530 | size_type m_size; |
| 531 | |
| 532 | char* m_data = nullptr; |
| 533 | |
| 534 | void takeOwnership(); |
| 535 | |
| 536 | static constexpr char const* const s_empty = ""; |
| 537 | |
| 538 | public: // construction/ assignment |
| 539 | StringRef() noexcept |
| 540 | : StringRef( s_empty, 0 ) |
| 541 | {} |
| 542 | |
| 543 | StringRef( StringRef const& other ) noexcept |
| 544 | : m_start( other.m_start ), |
| 545 | m_size( other.m_size ) |
| 546 | {} |
| 547 | |
| 548 | StringRef( StringRef&& other ) noexcept |
| 549 | : m_start( other.m_start ), |
| 550 | m_size( other.m_size ), |
| 551 | m_data( other.m_data ) |
| 552 | { |
| 553 | other.m_data = nullptr; |
| 554 | } |
| 555 | |
| 556 | StringRef( char const* rawChars ) noexcept; |
| 557 | |
| 558 | StringRef( char const* rawChars, size_type size ) noexcept |
| 559 | : m_start( rawChars ), |
| 560 | m_size( size ) |
| 561 | {} |
| 562 | |
| 563 | StringRef( std::string const& stdString ) noexcept |
| 564 | : m_start( stdString.c_str() ), |
| 565 | m_size( stdString.size() ) |
| 566 | {} |
| 567 | |
| 568 | ~StringRef() noexcept { |
| 569 | delete[] m_data; |
| 570 | } |
| 571 | |
| 572 | auto operator = ( StringRef const &other ) noexcept -> StringRef& { |
| 573 | delete[] m_data; |
| 574 | m_data = nullptr; |
| 575 | m_start = other.m_start; |
| 576 | m_size = other.m_size; |
| 577 | return *this; |
| 578 | } |
| 579 |
no outgoing calls
no test coverage detected