2012-10-08 16:25:58 +04:00
|
|
|
#ifndef utf8_iterator_hh_INCLUDED
|
|
|
|
#define utf8_iterator_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "utf8.hh"
|
|
|
|
|
2015-03-27 16:18:52 +03:00
|
|
|
#include <iterator>
|
|
|
|
|
2012-10-08 16:25:58 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace utf8
|
|
|
|
{
|
|
|
|
|
|
|
|
// adapter for an iterator on bytes which permits to iterate
|
|
|
|
// on unicode codepoints instead.
|
2012-10-13 20:31:29 +04:00
|
|
|
template<typename Iterator,
|
2014-10-13 22:54:40 +04:00
|
|
|
typename InvalidPolicy = utf8::InvalidPolicy::Pass>
|
2015-03-27 16:18:52 +03:00
|
|
|
class iterator : public std::iterator<std::forward_iterator_tag,
|
|
|
|
Codepoint, CharCount>
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-09-23 21:39:21 +03:00
|
|
|
iterator(Iterator it, Iterator begin, Iterator end)
|
|
|
|
: m_it{std::move(it)}, m_begin{std::move(begin)}, m_end{std::move(end)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
iterator(Iterator it, const Container& c)
|
|
|
|
: m_it{std::move(it)}, m_begin{begin(c)}, m_end{end(c)}
|
|
|
|
{}
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator& operator++()
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
2015-09-23 21:39:21 +03:00
|
|
|
m_it = utf8::next(m_it, m_end);
|
2012-10-08 16:25:58 +04:00
|
|
|
invalidate_value();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator operator++(int)
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator save = *this;
|
2012-10-08 16:25:58 +04:00
|
|
|
++*this;
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator& operator--()
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
2015-09-23 21:39:21 +03:00
|
|
|
m_it = utf8::previous(m_it, m_begin);
|
2012-10-08 16:25:58 +04:00
|
|
|
invalidate_value();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator operator--(int)
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator save = *this;
|
2012-10-08 16:25:58 +04:00
|
|
|
--*this;
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator operator+(CharCount count) const
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
|
|
|
if (count < 0)
|
2013-12-03 23:48:53 +04:00
|
|
|
return operator-(-count);
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator res = *this;
|
2012-10-08 16:25:58 +04:00
|
|
|
while (count--)
|
|
|
|
++res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator operator-(CharCount count) const
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
|
|
|
if (count < 0)
|
2013-12-03 23:48:53 +04:00
|
|
|
return operator+(-count);
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
iterator res = *this;
|
2012-10-08 16:25:58 +04:00
|
|
|
while (count--)
|
|
|
|
--res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:10:57 +04:00
|
|
|
bool operator==(const iterator& other) { return m_it == other.m_it; }
|
|
|
|
bool operator!=(const iterator& other) { return m_it != other.m_it; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2015-09-23 21:39:21 +03:00
|
|
|
bool operator< (const iterator& other) const { return m_it < other.m_it; }
|
|
|
|
bool operator<= (const iterator& other) const { return m_it <= other.m_it; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2015-09-23 21:39:21 +03:00
|
|
|
bool operator> (const iterator& other) const { return m_it > other.m_it; }
|
|
|
|
bool operator>= (const iterator& other) const { return m_it >= other.m_it; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2015-09-23 21:39:21 +03:00
|
|
|
bool operator==(const Iterator& other) { return m_it == other; }
|
|
|
|
bool operator!=(const Iterator& other) { return m_it != other; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2015-09-23 21:39:21 +03:00
|
|
|
bool operator< (const Iterator& other) const { return m_it < other; }
|
|
|
|
bool operator<= (const Iterator& other) const { return m_it <= other; }
|
|
|
|
|
|
|
|
bool operator> (const Iterator& other) const { return m_it > other; }
|
|
|
|
bool operator>= (const Iterator& other) const { return m_it >= other; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
2015-09-23 21:30:47 +03:00
|
|
|
CharCount operator-(const iterator& other) const
|
2012-10-08 16:25:58 +04:00
|
|
|
{
|
2015-09-23 21:30:47 +03:00
|
|
|
return utf8::distance(other.m_it, m_it);
|
2012-10-08 16:25:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Codepoint operator*() const
|
|
|
|
{
|
|
|
|
return get_value();
|
|
|
|
}
|
|
|
|
|
2013-06-05 21:19:35 +04:00
|
|
|
const Iterator& base() const { return m_it; }
|
|
|
|
Iterator& base() { return m_it; }
|
2012-10-08 16:25:58 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void invalidate_value() { m_value = -1; }
|
|
|
|
Codepoint get_value() const
|
|
|
|
{
|
|
|
|
if (m_value == -1)
|
2015-09-23 21:39:21 +03:00
|
|
|
m_value = utf8::codepoint<InvalidPolicy>(m_it, m_end);
|
2012-10-08 16:25:58 +04:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator m_it;
|
2015-09-23 21:39:21 +03:00
|
|
|
Iterator m_begin;
|
|
|
|
Iterator m_end;
|
2012-10-08 16:25:58 +04:00
|
|
|
mutable Codepoint m_value = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-05 21:19:49 +04:00
|
|
|
}
|
2012-10-08 16:25:58 +04:00
|
|
|
#endif // utf8_iterator_hh_INCLUDED
|