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
00042 iterator(CodeUnit const* ptr) :
00043 ptr(ptr) {}
00044
00045 CharType operator *() const { return Unicode::toChar(ptr, ptr + 6); }
00046 CharType& operator [](Distance n) const { return *(CharType*)(ptr + n); }
00047
00048 inline void incPtr() { Unicode::incUtfPtr(ptr, ptr + 6); }
00049 inline void decPtr() { Unicode::decUtfPtr(ptr, ptr + 6); }
00050
00051 iterator& operator ++() { incPtr(); return *this; }
00052 iterator operator ++(int) { iterator t(*this); incPtr(); return t; }
00053
00054 iterator& operator --() { decPtr(); return *this; }
00055 iterator operator --(int) { iterator t(*this); decPtr(); return t; }
00056
00057 iterator& operator +=(Distance n) { ptr += n; return *this; }
00058 iterator& operator -=(Distance n) { ptr -= n; return *this; }
00059
00060 inline bool operator ==(iterator const& other) {
00061 return ptr == other.ptr;
00062 }
00063
00064 inline bool operator !=(iterator const& other) {
00065 return ptr != other.ptr;
00066 }
00067
00068 iterator operator +(int offset) {
00069 if (offset < 0)
00070 return operator -(-offset);
00071 iterator t(*this);
00072 while (offset-- > 0)
00073 ++t;
00074 return t;
00075 }
00076
00077 iterator operator -(int offset) {
00078 if (offset < 0)
00079 return operator +(-offset);
00080 iterator t(*this);
00081 while (offset++ < 0)
00082 --t;
00083 return t;
00084 }
00085
00086 int operator -(const iterator& other) const { return Unicode::utfDiff(ptr, other.ptr); }
00087
00088 bool operator <(const iterator& other) const { return ptr < other.ptr; }
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
00095 private:
00096 CodeUnit const* ptr;
00097 };
00098
00099 }
00100 }
00101
00102 #endif // Tanl_Text_Utf8CharIterator_h