00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Tanl_Text_Utf8CharIterator_h
00025 #define Tanl_Text_Utf8CharIterator_h
00026
00027 #include "text/Char.h"
00028 #include "text/CharBuffer.h"
00029 #include "text/Utf8Utils.h"
00030
00031 namespace Tanl {
00032 namespace Text {
00033
00034 template <>
00035 class CharBuffer<Utf8Char>::iterator
00036 {
00037 public:
00038
00039 typedef Char CharType;
00040 typedef char CodeUnit;
00041 typedef ptrdiff_t Distance;
00042
00043 iterator(CodeUnit const* ptr) :
00044 ptr(ptr) {}
00045
00046 CharType operator *() const { return Unicode::toChar(ptr, ptr + 4); }
00047 CharType& operator [](Distance n) const { return *(CharType*)(ptr + n); }
00048
00049 inline void incPtr() { Unicode::incUtfPtr(ptr); }
00050 inline void decPtr() { Unicode::decUtfPtr(ptr, ptr - 4); }
00051
00052 iterator& operator ++() { incPtr(); return *this; }
00053 iterator operator ++(int) { iterator t(*this); incPtr(); return t; }
00054
00055 iterator& operator --() { decPtr(); return *this; }
00056 iterator operator --(int) { iterator t(*this); decPtr(); return t; }
00057
00058 iterator& operator +=(Distance n) { ptr += n; return *this; }
00059 iterator& operator -=(Distance n) { ptr -= n; return *this; }
00060
00061 inline bool operator ==(iterator const& other) {
00062 return ptr == other.ptr;
00063 }
00064
00065 inline bool operator !=(iterator const& other) {
00066 return ptr != other.ptr;
00067 }
00068
00069 iterator operator +(int offset) {
00070 if (offset < 0)
00071 return operator -(-offset);
00072 iterator t(*this);
00073 while (offset-- > 0)
00074 ++t;
00075 return t;
00076 }
00077
00078 iterator operator -(int offset) {
00079 if (offset < 0)
00080 return operator +(-offset);
00081 iterator t(*this);
00082 while (offset++ < 0)
00083 --t;
00084 return t;
00085 }
00086
00087 int operator -(const iterator& other) const { return Unicode::utfDiff(ptr, other.ptr); }
00088
00089 bool operator <(const iterator& other) const { return ptr < other.ptr; }
00090 bool operator <=(const iterator& other) const { return ptr <= other.ptr; }
00091 bool operator >(const iterator& other) const { return ptr > other.ptr; }
00092 bool operator >=(const iterator& other) const { return ptr >= other.ptr; }
00093 bool operator ==(const iterator& other) const { return ptr == other.ptr; }
00094 bool operator !=(const iterator& other) const { return ptr != other.ptr; }
00095
00096 private:
00097 CodeUnit const* ptr;
00098 };
00099
00100 }
00101 }
00102
00103 #endif // Tanl_Text_Utf8CharIterator_h