Avoid underlying iterator copies in utf8_iterator

This commit is contained in:
Maxime Coste 2016-07-27 21:36:32 +01:00
parent df0773feeb
commit 14f59d415d
2 changed files with 17 additions and 6 deletions

View File

@ -25,13 +25,19 @@ inline bool is_character_start(char c)
return (c & 0xC0) != 0x80;
}
// returns an iterator to next character first byte
template<typename Iterator>
Iterator next(Iterator it, const Iterator& end)
void to_next(Iterator& it, const Iterator& end)
{
if (it != end and read(it) & 0x80)
while (it != end and (*(it) & 0xC0) == 0x80)
++it;
}
// returns an iterator to next character first byte
template<typename Iterator>
Iterator next(Iterator it, const Iterator& end)
{
to_next(it, end);
return it;
}
@ -45,12 +51,17 @@ Iterator finish(Iterator it, const Iterator& end)
return it;
}
template<typename Iterator>
void to_previous(Iterator& it, const Iterator& begin)
{
while (it != begin and (*(--it) & 0xC0) == 0x80)
;
}
// returns an iterator to the previous character first byte
template<typename Iterator>
Iterator previous(Iterator it, const Iterator& begin)
{
while (it != begin and (*(--it) & 0xC0) == 0x80)
;
to_previous(it, begin);
return it;
}

View File

@ -34,7 +34,7 @@ public:
iterator& operator++()
{
m_it = utf8::next(m_it, m_end);
utf8::to_next(m_it, m_end);
invalidate_value();
return *this;
}
@ -48,7 +48,7 @@ public:
iterator& operator--()
{
m_it = utf8::previous(m_it, m_begin);
utf8::to_previous(m_it, m_begin);
invalidate_value();
return *this;
}