utf8_iterator.hh (3366B)
1 #ifndef utf8_iterator_hh_INCLUDED 2 #define utf8_iterator_hh_INCLUDED 3 4 #include "utf8.hh" 5 6 #include <iterator> 7 8 namespace Kakoune 9 { 10 11 namespace utf8 12 { 13 14 // adapter for an iterator on bytes which permits to iterate 15 // on unicode codepoints instead. 16 template<typename BaseIt, 17 typename Sentinel = BaseIt, 18 typename CodepointType = Codepoint, 19 typename DifferenceType = CharCount, 20 typename InvalidPolicy = utf8::InvalidPolicy::Pass> 21 class iterator 22 { 23 public: 24 using value_type = CodepointType; 25 using difference_type = DifferenceType; 26 using pointer = CodepointType*; 27 using reference = CodepointType&; 28 using iterator_category = std::bidirectional_iterator_tag; 29 30 iterator() = default; 31 constexpr static bool noexcept_policy = noexcept(InvalidPolicy{}(0)); 32 33 iterator(BaseIt it, Sentinel begin, Sentinel end) noexcept 34 : m_it{std::move(it)}, m_begin{std::move(begin)}, m_end{std::move(end)} 35 {} 36 37 template<typename Container> 38 iterator(BaseIt it, const Container& c) noexcept 39 : m_it{std::move(it)}, m_begin{std::begin(c)}, m_end{std::end(c)} 40 {} 41 42 iterator& operator++() noexcept 43 { 44 utf8::to_next(m_it, m_end); 45 return *this; 46 } 47 48 iterator operator++(int) noexcept 49 { 50 iterator save = *this; 51 ++*this; 52 return save; 53 } 54 55 iterator& operator--() noexcept 56 { 57 utf8::to_previous(m_it, m_begin); 58 return *this; 59 } 60 61 iterator operator--(int) noexcept 62 { 63 iterator save = *this; 64 --*this; 65 return save; 66 } 67 68 iterator operator+(DifferenceType count) const noexcept 69 { 70 iterator res = *this; 71 res += count; 72 return res; 73 } 74 75 iterator& operator+=(DifferenceType count) noexcept 76 { 77 if (count < 0) 78 return operator-=(-count); 79 80 while (count--) 81 operator++(); 82 return *this; 83 } 84 85 iterator operator-(DifferenceType count) const noexcept 86 { 87 iterator res = *this; 88 res -= count; 89 return res; 90 } 91 92 iterator& operator-=(DifferenceType count) noexcept 93 { 94 if (count < 0) 95 return operator+=(-count); 96 97 while (count--) 98 operator--(); 99 return *this; 100 } 101 102 bool operator==(const iterator& other) const noexcept { return m_it == other.m_it; } 103 auto operator<=>(const iterator& other) const noexcept { return m_it <=> other.m_it; } 104 105 template<typename T> 106 requires std::is_same_v<T, BaseIt> or std::is_same_v<T, Sentinel> 107 bool operator==(const T& other) const noexcept { return m_it == other; } 108 auto operator<=>(const BaseIt& other) const noexcept { return m_it <=> other; } 109 110 DifferenceType operator-(const iterator& other) const noexcept(noexcept_policy) 111 { 112 return (DifferenceType)utf8::distance<InvalidPolicy>(other.m_it, m_it); 113 } 114 115 CodepointType operator*() const noexcept(noexcept_policy) 116 { 117 return (CodepointType)utf8::codepoint<InvalidPolicy>(m_it, m_end); 118 } 119 120 CodepointType read() noexcept(noexcept_policy) 121 { 122 return (CodepointType)utf8::read_codepoint<InvalidPolicy>(m_it, m_end); 123 } 124 125 const BaseIt& base() const noexcept { return m_it; } 126 127 private: 128 BaseIt m_it; 129 Sentinel m_begin; 130 Sentinel m_end; 131 }; 132 133 } 134 135 } 136 #endif // utf8_iterator_hh_INCLUDED