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