Added the unchecked iterator, added base() to the checked one, updated tests

git-svn-id: http://svn.code.sf.net/p/utfcpp/code@69 a809a056-fc17-0410-9590-b4f493f8b08e
This commit is contained in:
ntrifunovic 2006-11-23 18:10:26 +00:00 committed by King_DuckZ
parent a42b82da3a
commit bfb37e9c26
3 changed files with 61 additions and 1 deletions

View file

@ -250,11 +250,12 @@ namespace utf8
// The iterator class // The iterator class
template <typename octet_iterator> template <typename octet_iterator>
class iterator { class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
octet_iterator it; octet_iterator it;
octet_iterator range_start; octet_iterator range_start;
octet_iterator range_end; octet_iterator range_end;
public: public:
iterator () {};
explicit iterator (const octet_iterator& octet_it, explicit iterator (const octet_iterator& octet_it,
const octet_iterator& range_start, const octet_iterator& range_start,
const octet_iterator& range_end) : const octet_iterator& range_end) :
@ -264,6 +265,7 @@ namespace utf8
throw std::out_of_range("Invalid utf-8 iterator position"); throw std::out_of_range("Invalid utf-8 iterator position");
} }
// the default "big three" are OK // the default "big three" are OK
octet_iterator base () const { return it; }
uint32_t operator * () const uint32_t operator * () const
{ {
octet_iterator temp = it; octet_iterator temp = it;

View file

@ -167,6 +167,48 @@ namespace utf8
return result; return result;
} }
// The iterator class
template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
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::unchecked
} // namespace utf8 } // namespace utf8

View file

@ -257,6 +257,22 @@ int main()
// try it with the return value; // try it with the return value;
utf16_end = utf8to16 (utf8_with_surrogates, utf8_with_surrogates + 9, &utf16result[0]); utf16_end = utf8to16 (utf8_with_surrogates, utf8_with_surrogates + 9, &utf16result[0]);
assert (utf16_end == &utf16result[0] + 4); assert (utf16_end == &utf16result[0] + 4);
// iterator
utf8::unchecked::iterator<char*> un_it(threechars);
utf8::unchecked::iterator<char*> 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<char*> 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<char*>(threechars));
assert (*un_it == 0x10346);
} }