Added the first version of the iterator to the code. Started upgrading the html documentation
git-svn-id: http://svn.code.sf.net/p/utfcpp/code@65 a809a056-fc17-0410-9590-b4f493f8b08e
This commit is contained in:
parent
24f4090afa
commit
d2ee7164b6
3 changed files with 917 additions and 646 deletions
File diff suppressed because it is too large
Load diff
|
@ -38,7 +38,7 @@ namespace utf8
|
|||
uint32_t cp;
|
||||
public:
|
||||
invalid_code_point(uint32_t cp) : cp(cp) {}
|
||||
const char* what() { return "Invalid code point"; }
|
||||
virtual const char* what() const throw() { return "Invalid code point"; }
|
||||
uint32_t code_point() const {return cp;}
|
||||
};
|
||||
|
||||
|
@ -46,7 +46,7 @@ namespace utf8
|
|||
uint8_t u8;
|
||||
public:
|
||||
invalid_utf8 (uint8_t u) : u8(u) {}
|
||||
const char* what() { return "Invalid UTF-8"; }
|
||||
virtual const char* what() const throw() { return "Invalid UTF-8"; }
|
||||
uint8_t utf8_octet() const {return u8;}
|
||||
};
|
||||
|
||||
|
@ -54,13 +54,13 @@ namespace utf8
|
|||
uint16_t u16;
|
||||
public:
|
||||
invalid_utf16 (uint16_t u) : u16(u) {}
|
||||
const char* what() { return "Invalid UTF-16"; }
|
||||
virtual const char* what() const throw() { return "Invalid UTF-16"; }
|
||||
uint16_t utf16_word() const {return u16;}
|
||||
};
|
||||
|
||||
class not_enough_room : public std::exception {
|
||||
public:
|
||||
const char* what() { return "Not enough space"; }
|
||||
virtual const char* what() const throw() { return "Not enough space"; }
|
||||
};
|
||||
|
||||
/// The library API - functions intended to be called by the users
|
||||
|
@ -236,6 +236,45 @@ namespace utf8
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
// The iterator class
|
||||
template <typename octet_iterator>
|
||||
class iterator {
|
||||
static const typename std::iterator_traits<octet_iterator>::difference_type MAX_UTF8_SEQUENCE_LENGTH = 4;
|
||||
octet_iterator it;
|
||||
public:
|
||||
explicit iterator (const octet_iterator& octet_it) : it(octet_it) {}
|
||||
// the default "big three" are OK
|
||||
uint32_t operator * () const
|
||||
{
|
||||
octet_iterator temp = it;
|
||||
return next(temp, temp + MAX_UTF8_SEQUENCE_LENGTH);
|
||||
}
|
||||
bool operator == (const iterator& rhs) const { return (it == rhs.it); }
|
||||
iterator& operator ++ ()
|
||||
{
|
||||
next(it, it + MAX_UTF8_SEQUENCE_LENGTH);
|
||||
return *this;
|
||||
}
|
||||
iterator operator ++ (int)
|
||||
{
|
||||
iterator temp = *this;
|
||||
next(it, it + MAX_UTF8_SEQUENCE_LENGTH);
|
||||
return temp;
|
||||
}
|
||||
iterator& operator -- ()
|
||||
{
|
||||
previous(it, it - MAX_UTF8_SEQUENCE_LENGTH);
|
||||
return *this;
|
||||
}
|
||||
iterator operator -- (int)
|
||||
{
|
||||
iterator temp = *this;
|
||||
previous(it, it - MAX_UTF8_SEQUENCE_LENGTH);
|
||||
return temp;
|
||||
}
|
||||
}; // class iterator
|
||||
|
||||
} // namespace utf8
|
||||
|
||||
#endif //header guard
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include "../../source/utf8.h"
|
||||
using namespace utf8;
|
||||
using namespace std;
|
||||
|
@ -131,6 +130,22 @@ int main()
|
|||
char* fixed_invalid_sequence = "a????z";
|
||||
assert (std::equal(replace_invalid_result.begin(), replace_invalid_result.end(), fixed_invalid_sequence));
|
||||
|
||||
// iterator
|
||||
utf8::iterator<char*> it(threechars);
|
||||
utf8::iterator<char*> it2 = it;
|
||||
assert (it2 == it);
|
||||
assert (*it == 0x10346);
|
||||
assert (*(++it) == 0x65e5);
|
||||
assert ((*it++) == 0x65e5);
|
||||
assert (*it == 0x0448);
|
||||
utf8::iterator<char*> endit (threechars + 9);
|
||||
assert (++it == endit);
|
||||
assert (*(--it) == 0x0448);
|
||||
assert ((*it--) == 0x0448);
|
||||
assert (*it == 0x65e5);
|
||||
assert (--it == utf8::iterator<char*>(threechars));
|
||||
assert (*it == 0x10346);
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//// Unchecked variants
|
||||
//////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue