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
| 562 | /// visible - but it does mean (substring) StringRefs should not be shared between |
| 563 | /// threads. |
| 564 | class StringRef { |
| 565 | public: |
| 566 | using size_type = std::size_t; |
| 567 | |
| 568 | private: |
| 569 | friend struct StringRefTestAccess; |
| 570 | |
| 571 | char const* m_start; |
| 572 | size_type m_size; |
| 573 | |
| 574 | char* m_data = nullptr; |
| 575 | |
| 576 | void takeOwnership(); |
| 577 | |
| 578 | static constexpr char const* const s_empty = ""; |
| 579 | |
| 580 | public: // construction/ assignment |
| 581 | StringRef() noexcept |
| 582 | : StringRef( s_empty, 0 ) |
| 583 | {} |
| 584 | |
| 585 | StringRef( StringRef const& other ) noexcept |
| 586 | : m_start( other.m_start ), |
| 587 | m_size( other.m_size ) |
| 588 | {} |
| 589 | |
| 590 | StringRef( StringRef&& other ) noexcept |
| 591 | : m_start( other.m_start ), |
| 592 | m_size( other.m_size ), |
| 593 | m_data( other.m_data ) |
| 594 | { |
| 595 | other.m_data = nullptr; |
| 596 | } |
| 597 | |
| 598 | StringRef( char const* rawChars ) noexcept; |
| 599 | |
| 600 | StringRef( char const* rawChars, size_type size ) noexcept |
| 601 | : m_start( rawChars ), |
| 602 | m_size( size ) |
| 603 | {} |
| 604 | |
| 605 | StringRef( std::string const& stdString ) noexcept |
| 606 | : m_start( stdString.c_str() ), |
| 607 | m_size( stdString.size() ) |
| 608 | {} |
| 609 | |
| 610 | ~StringRef() noexcept { |
| 611 | delete[] m_data; |
| 612 | } |
| 613 | |
| 614 | auto operator = ( StringRef const &other ) noexcept -> StringRef& { |
| 615 | delete[] m_data; |
| 616 | m_data = nullptr; |
| 617 | m_start = other.m_start; |
| 618 | m_size = other.m_size; |
| 619 | return *this; |
| 620 | } |
| 621 |
no outgoing calls
no test coverage detected