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_CharBuffer_h
00025 #define Tanl_Text_CharBuffer_h
00026
00027 #include <iterator>
00028
00029 namespace Tanl {
00030 namespace Text {
00031
00059 template <typename CharTraits>
00060 class CharBuffer
00061 {
00062 public:
00063
00064 typedef typename CharTraits::CharType CharType;
00065 typedef typename CharTraits::CodeUnit CodeUnit;
00066
00067 typedef CharType* pointer;
00068 typedef const CharType* const_pointer;
00069 typedef CharType& reference;
00070 typedef const CharType& const_reference;
00071 typedef size_t size_type;
00072 typedef ptrdiff_t Distance;
00073
00074 CharBuffer(CharType* start = 0, size_t length = 0) :
00075 start((CodeUnit*)start),
00076 length(length)
00077 { }
00078
00079 class iterator :
00080 public std::iterator<std::random_access_iterator_tag, CharType> {
00081
00082 public:
00083
00087 typedef typename CharTraits::CodeUnit CodeUnit;
00088
00089 iterator(CodeUnit const* ptr = 0) : ptr(ptr) { }
00090
00091
00092
00093 CharType operator *() const { return *ptr; }
00094 CharType* operator ->() { return (CharType*)ptr; }
00095
00096 CharType operator[](int n) { return *(ptr + n); }
00097 CharType const operator[](int n) const { return *(ptr + n); }
00098
00099 iterator& operator ++() { ++ptr; return *this; }
00100 iterator& operator --() { --ptr; return *this; }
00101
00102 iterator operator ++(int) { return iterator(ptr++); }
00103 iterator operator --(int) { return iterator(ptr--); }
00104 iterator& operator +=(int n) { ptr += n; return *this; }
00105 iterator& operator -=(int n) { ptr -= n; return *this; }
00106
00107 iterator operator +(int n) const { return iterator(ptr + n); }
00108
00109 iterator operator -(int n) const { return iterator(ptr - n); }
00110
00111 Distance operator -(iterator& other) const { return ptr - other.ptr; }
00112
00113 bool operator <(iterator& other) const { return ptr < other.ptr; }
00114 bool operator <=(const iterator& other) const {
00115 return ptr <= other.ptr; }
00116 bool operator >(const iterator& other) const {
00117 return ptr > other.ptr; }
00118 bool operator >=(const iterator& other) const {
00119 return ptr >= other.ptr; }
00120 bool operator ==(const iterator& other) const {
00121 return ptr == other.ptr; }
00122 bool operator !=(const iterator& other) const {
00123 return ptr != other.ptr; }
00124
00125 operator CodeUnit const*() const { return ptr; }
00126 operator CodeUnit*() const { return (CodeUnit*)ptr;}
00127
00128 iterator StartsWith(char const* other, iterator end);
00129
00130 protected:
00131 CodeUnit const* ptr;
00132 };
00133
00134 class const_iterator : public iterator {
00135 public:
00136 const_iterator(CodeUnit const* ptr = 0) : iterator(ptr)
00137 { }
00138 };
00139
00140 reference operator[](size_type n) { return *(begin() + n); }
00141
00142 iterator begin() { return iterator(start); }
00143 iterator end() { return iterator(start + length); }
00144
00145 const_iterator begin() const { return const_iterator(start); }
00146 const_iterator end() const { return const_iterator(start + length); }
00147
00148 Distance size() { return end() - begin(); }
00149
00150 protected:
00151 CodeUnit const* start;
00152 Distance length;
00153
00154 };
00155
00156 }
00157 }
00158
00159 #endif // Tanl_Text_CharBuffer_h