diff --git a/source/utf8/unchecked.h b/source/utf8/unchecked.h index def0009..ce2d207 100644 --- a/source/utf8/unchecked.h +++ b/source/utf8/unchecked.h @@ -260,6 +260,68 @@ namespace utf8 return temp; } }; // class iterator + + //the iterator2 class + template + class iterator2 : public std::iterator { + private: + uint32_t cp{}; + octet_iterator p{}; + const octet_iterator e{}; + bool ok{}; + + void read() + { + ok = p != e; + if(ok) + { + cp = utf8::unchecked::next(p); + } + } + public: + iterator2 () {} + iterator2 (octet_iterator begin, octet_iterator end): p{begin}, e{end} { read(); } + iterator2 (const iterator2& a): cp{a.cp}, p{a.p}, e{a.e}, ok{a.ok} {} + + uint32_t operator * () const { return cp; } + uint32_t* operator -> () const { return &cp; } + + iterator2& operator ++ () + { + if(!ok) + { + throw std::runtime_error("no such element"); + } + read(); + return *this; + } + iterator2 operator ++ (int) + { + if(!ok) + { + throw std::runtime_error("no such element"); + } + iterator2 tmp = *this; + read(); + return tmp; + } + + bool equals(const iterator2& a) const + { + return ok == a.ok && (!ok || p == a.p); + } + }; // class iterator2 + + template + inline bool operator==(const iterator2& a, const iterator2& b) + { + return a.equals(b); + } + template + inline bool operator!=(const iterator2& a, const iterator2& b) + { + return !a.equals(b); + } } // namespace utf8::unchecked } // namespace utf8