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.
| 584 | /// Note that, because a StringRef may be a substring of another string, |
| 585 | /// it may not be null terminated. |
| 586 | class StringRef { |
| 587 | public: |
| 588 | using size_type = std::size_t; |
| 589 | using const_iterator = const char*; |
| 590 | |
| 591 | private: |
| 592 | static constexpr char const* const s_empty = ""; |
| 593 | |
| 594 | char const* m_start = s_empty; |
| 595 | size_type m_size = 0; |
| 596 | |
| 597 | public: // construction |
| 598 | constexpr StringRef() noexcept = default; |
| 599 | |
| 600 | StringRef( char const* rawChars ) noexcept; |
| 601 | |
| 602 | constexpr StringRef( char const* rawChars, size_type size ) noexcept |
| 603 | : m_start( rawChars ), |
| 604 | m_size( size ) |
| 605 | {} |
| 606 | |
| 607 | StringRef( std::string const& stdString ) noexcept |
| 608 | : m_start( stdString.c_str() ), |
| 609 | m_size( stdString.size() ) |
| 610 | {} |
| 611 | |
| 612 | explicit operator std::string() const { |
| 613 | return std::string(m_start, m_size); |
| 614 | } |
| 615 | |
| 616 | public: // operators |
| 617 | auto operator == ( StringRef const& other ) const noexcept -> bool; |
| 618 | auto operator != (StringRef const& other) const noexcept -> bool { |
| 619 | return !(*this == other); |
| 620 | } |
| 621 | |
| 622 | auto operator[] ( size_type index ) const noexcept -> char { |
| 623 | assert(index < m_size); |
| 624 | return m_start[index]; |
| 625 | } |
| 626 | |
| 627 | public: // named queries |
| 628 | constexpr auto empty() const noexcept -> bool { |
| 629 | return m_size == 0; |
| 630 | } |
| 631 | constexpr auto size() const noexcept -> size_type { |
| 632 | return m_size; |
| 633 | } |
| 634 | |
| 635 | // Returns the current start pointer. If the StringRef is not |
| 636 | // null-terminated, throws std::domain_exception |
| 637 | auto c_str() const -> char const*; |
| 638 | |
| 639 | public: // substrings and searches |
| 640 | // Returns a substring of [start, start + length). |
| 641 | // If start + length > size(), then the substring is [start, size()). |
| 642 | // If start > size(), then the substring is empty. |
| 643 | auto substr( size_type start, size_type length ) const noexcept -> StringRef; |
no outgoing calls
no test coverage detected