00001
00014
00015 #ifndef basic_substring_h_sdlksldkslkdwelkjsodiuslkeslkduoileiousd
00016 #define basic_substring_h_sdlksldkslkdwelkjsodiuslkeslkduoileiousd
00017
00018 #include <string>
00019 using namespace std;
00020
00024 namespace tvr_std {
00025
00095 template<class Ch>
00096 class basic_substring {
00097 public:
00099 typedef typename basic_string<Ch>::size_type size_type;
00100
00113 basic_substring(basic_string<Ch>& s,
00114 size_type start,
00115 size_type len,
00116 bool doReverse=false) : ps(&s), n(len), pos(start) {
00117 if ((pos + n) > s.length()) {
00118 n = s.length();
00119 }
00120 this->doReverse = doReverse;
00121 };
00122
00130 basic_substring(basic_string<Ch>& s,
00131 const basic_string<Ch>& s2,
00132 bool doReverse=false) : ps(&s), n(s2.length()) {
00133 if (doReverse)
00134 pos = s.rfind(s2);
00135 else
00136 pos = s.find(s2);
00137 this->doReverse = doReverse;
00138 };
00139
00147 basic_substring(basic_string<Ch>& s,
00148 const Ch* p,
00149 bool doReverse=false) : ps(&s) {
00150 basic_string<Ch> temp(p);
00151 n = temp.length();
00152 if (doReverse)
00153 pos = s.rfind(temp);
00154 else
00155 pos = s.find(temp);
00156 this->doReverse = doReverse;
00157 };
00159
00174
00182 basic_substring& operator=(const basic_string<Ch>& in) {
00183 if ((*this) == true) {
00184 basic_string<Ch> s = (*this);
00185 ps->replace(pos, n, in);
00186 if (doReverse)
00187 pos = ps->rfind(s, pos-1);
00188 else
00189 pos = ps->find(s, pos+1);
00190 }
00191 return *this;
00192 };
00193
00203 basic_substring& operator=(const basic_substring<Ch>& in) {
00204 ps = in.ps;
00205 pos = in.pos;
00206 n = in.n;
00207 doReverse = in.doReverse;
00208 return *this;
00209 };
00210
00218 basic_substring& operator=(const Ch* in) {
00219 basic_string<Ch> s(in);
00220 return (*this) = s;
00221 };
00222
00230 basic_substring& operator=(Ch in) {
00231 basic_string<Ch> s;
00232 s = in;
00233 return (*this) = s;
00234 };
00236
00242
00248 basic_substring& prepend(const basic_string<Ch>& in) {
00249 ps->insert(pos, in);
00250 pos += in.length();
00251 return *this;
00252 };
00253
00259 basic_substring& prepend(const Ch* in) {
00260 basic_string<Ch> arg(in);
00261 return prepend(arg);
00262 };
00263
00269 basic_substring& append(const basic_string<Ch>& in) {
00270 size_type tmp = pos + n;
00271 ps->insert(tmp, in);
00272 return *this;
00273 };
00274
00280 basic_substring& append(const Ch* in) {
00281 basic_string<Ch> arg(in);
00282 return append(arg);
00283 };
00285
00291
00296 operator const basic_string<Ch> () const {
00297 return basic_string<Ch> (*ps, pos, n);
00298 };
00303 operator const bool () const {
00304 return pos != basic_string<Ch>::npos;
00305 };
00307
00317
00318 basic_substring& operator ++() {
00319 basic_string<Ch> substring;
00320 substring = (basic_string<Ch>)*this;
00321 pos = ps->find(substring, pos+1);
00322 return *this;
00323 };
00324
00325 basic_substring& operator ++(int) {
00326 return ++(*this);
00327 };
00328
00329 basic_substring& operator --() {
00330 basic_string<Ch> substring;
00331 substring = (basic_string<Ch>)*this;
00332 if (pos != 0) {
00333 pos = ps->rfind(substring, pos-1);
00334 } else {
00335 pos = size_type::npos;
00336 }
00337 return *this;
00338 };
00339
00340 basic_substring& operator --(int) {
00341 return --(*this);
00342 };
00344 private:
00345 basic_string<Ch>* ps;
00346 size_type pos;
00347 size_type n;
00348 bool doReverse;
00349 };
00350
00352 typedef basic_substring<char> substring;
00354 typedef basic_substring<wchar_t> wsubstring;
00355
00356 };
00357
00358 #endif // basic_substring_h_sdlksldkslkdwelkjsodiuslkeslkduoileiousd