Allows to iterate over space-separated strings in StringSliceBase.
| 33 | |
| 34 | // Allows to iterate over space-separated strings in StringSliceBase. |
| 35 | class JoinIterator |
| 36 | { |
| 37 | public: |
| 38 | using difference_type = ptrdiff_t; |
| 39 | using value_type = strings::UniChar; |
| 40 | using pointer = strings::UniChar *; |
| 41 | using reference = strings::UniChar &; |
| 42 | using iterator_category = std::forward_iterator_tag; |
| 43 | |
| 44 | static JoinIterator Begin(StringSliceBase const & slice); |
| 45 | static JoinIterator End(StringSliceBase const & slice); |
| 46 | |
| 47 | bool operator==(JoinIterator const & rhs) const |
| 48 | { |
| 49 | return &m_slice == &rhs.m_slice && m_string == rhs.m_string && m_offset == rhs.m_offset; |
| 50 | } |
| 51 | |
| 52 | bool operator!=(JoinIterator const & rhs) const { return !(*this == rhs); } |
| 53 | |
| 54 | value_type operator*() const { return GetChar(m_string, m_offset); } |
| 55 | |
| 56 | JoinIterator & operator++(); |
| 57 | |
| 58 | private: |
| 59 | enum class Position |
| 60 | { |
| 61 | Begin, |
| 62 | End |
| 63 | }; |
| 64 | |
| 65 | JoinIterator(StringSliceBase const & slice, Position position); |
| 66 | |
| 67 | // Normalizes current position, i.e. moves to the next valid |
| 68 | // character if current position is invalid, or to the end of the |
| 69 | // whole sequence if there are no valid positions. |
| 70 | void Normalize(); |
| 71 | |
| 72 | size_t GetSize(size_t string) const; |
| 73 | |
| 74 | size_t GetMaxSize() const { return m_slice.Size() == 0 ? 0 : m_slice.Size() * 2 - 1; } |
| 75 | |
| 76 | value_type GetChar(size_t string, size_t offset) const; |
| 77 | |
| 78 | StringSliceBase const & m_slice; |
| 79 | |
| 80 | // Denotes the current string the iterator points to. Even values |
| 81 | // of |m_string| divided by two correspond to indices in |
| 82 | // |m_slice|. Odd values correspond to intermediate space |
| 83 | // characters. |
| 84 | size_t m_string; |
| 85 | |
| 86 | // An index of the current character in the current string. |
| 87 | size_t m_offset; |
| 88 | }; |
| 89 | } // namespace search |