From 14f59d415d2e77d70e9282ec100029b87cbb1799 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 27 Jul 2016 21:36:32 +0100 Subject: [PATCH] Avoid underlying iterator copies in utf8_iterator --- src/utf8.hh | 19 +++++++++++++++---- src/utf8_iterator.hh | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/utf8.hh b/src/utf8.hh index 8e7d209a..62d9dcbd 100644 --- a/src/utf8.hh +++ b/src/utf8.hh @@ -25,13 +25,19 @@ inline bool is_character_start(char c) return (c & 0xC0) != 0x80; } -// returns an iterator to next character first byte template -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 +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 +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 Iterator previous(Iterator it, const Iterator& begin) { - while (it != begin and (*(--it) & 0xC0) == 0x80) - ; + to_previous(it, begin); return it; } diff --git a/src/utf8_iterator.hh b/src/utf8_iterator.hh index 6e8f760b..c6589160 100644 --- a/src/utf8_iterator.hh +++ b/src/utf8_iterator.hh @@ -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; }