diff --git a/source/utf8/checked.h b/source/utf8/checked.h index 4647016..bedd85d 100644 --- a/source/utf8/checked.h +++ b/source/utf8/checked.h @@ -250,11 +250,12 @@ namespace utf8 // The iterator class template - class iterator { + class iterator : public std::iterator { octet_iterator it; octet_iterator range_start; octet_iterator range_end; public: + iterator () {}; explicit iterator (const octet_iterator& octet_it, const octet_iterator& range_start, const octet_iterator& range_end) : @@ -264,6 +265,7 @@ namespace utf8 throw std::out_of_range("Invalid utf-8 iterator position"); } // the default "big three" are OK + octet_iterator base () const { return it; } uint32_t operator * () const { octet_iterator temp = it; diff --git a/source/utf8/unchecked.h b/source/utf8/unchecked.h index ac019d9..900f762 100644 --- a/source/utf8/unchecked.h +++ b/source/utf8/unchecked.h @@ -167,6 +167,48 @@ namespace utf8 return result; } + // The iterator class + template + class iterator : public std::iterator { + octet_iterator it; + public: + iterator () {}; + explicit iterator (const octet_iterator& octet_it): it(octet_it) {} + // the default "big three" are OK + octet_iterator base () const { return it; } + uint32_t operator * () const + { + octet_iterator temp = it; + return next(temp); + } + bool operator == (const iterator& rhs) const + { + return (it == rhs.it); + } + iterator& operator ++ () + { + next(it); + return *this; + } + iterator operator ++ (int) + { + iterator temp = *this; + next(it); + return temp; + } + iterator& operator -- () + { + prior(it); + return *this; + } + iterator operator -- (int) + { + iterator temp = *this; + prior(it); + return temp; + } + }; // class iterator + } // namespace utf8::unchecked } // namespace utf8 diff --git a/test_drivers/smoke_test/test.cpp b/test_drivers/smoke_test/test.cpp index 880aca3..3fd18eb 100644 --- a/test_drivers/smoke_test/test.cpp +++ b/test_drivers/smoke_test/test.cpp @@ -257,6 +257,22 @@ int main() // try it with the return value; utf16_end = utf8to16 (utf8_with_surrogates, utf8_with_surrogates + 9, &utf16result[0]); assert (utf16_end == &utf16result[0] + 4); + + // iterator + utf8::unchecked::iterator un_it(threechars); + utf8::unchecked::iterator un_it2 = un_it; + assert (un_it2 == un_it); + assert (*un_it == 0x10346); + assert (*(++un_it) == 0x65e5); + assert ((*un_it++) == 0x65e5); + assert (*un_it == 0x0448); + utf8::unchecked::iterator un_endit (threechars + 9); + assert (++un_it == un_endit); + assert (*(--un_it) == 0x0448); + assert ((*un_it--) == 0x0448); + assert (*un_it == 0x65e5); + assert (--un_it == utf8::unchecked::iterator(threechars)); + assert (*un_it == 0x10346); }