Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields

ext_string.h

00001 
00094 #ifndef _EXT_STRING_H
00095 #define _EXT_STRING_H
00096 
00097 #include <string>
00098 #include <vector>
00099 
00100 namespace std
00101 {
00102 
00107         class ext_string : public string
00108         {
00109                 public:
00115                         ext_string() : string() { }
00116 
00124                         ext_string(const ext_string &s, size_type pos = 0, size_type n = npos) : string(s, pos, npos) { }
00125 
00131                         ext_string(const value_type *s) : string(s) { }
00132 
00139                         ext_string(const value_type *s, size_type n) : string(s, n) { }
00140 
00147                         ext_string(size_type n, value_type c) : string(n, c) { }
00148 
00155                         template <class InputIterator>
00156                                 ext_string(InputIterator first, InputIterator last) : string(first, last) { }
00157 
00161                         ~ext_string() { }
00162 
00192                         vector<ext_string> split(value_type separator, size_type limit = npos) const
00193                         {
00194                                 vector<ext_string> v;
00195 
00196                                 const_iterator 
00197                                         i = begin(),
00198                                         last = i;
00199                                 for (; i != end(); i++)
00200                                 {
00201                                         if (*i == separator)
00202                                         {
00203                                                 v.push_back(ext_string(last, i));
00204                                                 last = i + 1;
00205                                                 if (v.size() >= limit - 1)
00206                                                 {
00207                                                         v.push_back(ext_string(last, end()));
00208                                                         return v;
00209                                                 }
00210                                         }
00211                                 }
00212 
00213                                 if (last != i)
00214                                         v.push_back(ext_string(last, i));
00215 
00216                                 return v;
00217                         }
00218 
00238                         vector<ext_string> split(const string &separator, size_type limit = npos) const
00239                         {
00240                                 vector<ext_string> v;
00241 
00242                                 const_iterator
00243                                         i = begin(),
00244                                         last = i;
00245                                 for (; i != end(); i++)
00246                                 {
00247                                         if (string(i, i + separator.length()) == separator)
00248                                         {
00249                                                 v.push_back(ext_string(last, i));
00250                                                 last = i + separator.length();
00251 
00252                                                 if (v.size() >= limit - 1)
00253                                                 {
00254                                                         v.push_back(ext_string(last, end()));
00255                                                         return v;
00256                                                 }
00257                                         }
00258                                 }
00259 
00260                                 if (last != i)
00261                                         v.push_back(ext_string(last, i));
00262 
00263                                 return v;
00264                         }
00265 
00276                         static long int integer(const string &s)
00277                         {
00278                                 long int retval = 0;
00279                                 bool neg = false;
00280 
00281                                 for (const_iterator i = s.begin(); i != s.end(); i++)
00282                                 {
00283                                         if (i == s.begin())
00284                                         {
00285                                                 if (*i == '-')
00286                                                 {
00287                                                         neg = true;
00288                                                         continue;
00289                                                 }
00290                                                 else if (*i == '+')
00291                                                         continue;
00292                                         }
00293                                         if (*i >= '0' && *i <= '9')
00294                                         {
00295                                                 retval *= 10;
00296                                                 retval += *i - '0';
00297                                         }
00298                                 }
00299 
00300                                 if (neg)
00301                                         retval *= -1;
00302 
00303                                 return retval;
00304                         }
00305 
00315                         long int integer() const
00316                         {
00317                                 return integer(*this);
00318                         }
00319 
00338                         vector<ext_string> chunk_split(size_type chunklen) const
00339                         {
00340                                 vector<ext_string> retval;
00341                                 retval.reserve(size() / chunklen + 1);
00342 
00343                                 size_type count = 0;
00344                                 const_iterator
00345                                         i = begin(),
00346                                         last = i;
00347                                 for (; i != end(); i++, count++)
00348                                 {
00349                                         if (count == chunklen)
00350                                         {
00351                                                 count = 0;
00352                                                 retval.push_back(ext_string(last, i));
00353                                                 last = i;
00354                                         }
00355                                 }
00356                                 
00357                                 if (last != i)
00358                                         retval.push_back(ext_string(last, i));
00359 
00360                                 return retval;
00361                         }
00362 
00389                         template <class InputIterator>
00390                                 static ext_string join(const string &glue, InputIterator first, InputIterator last)
00391                                 {
00392                                         ext_string retval;
00393 
00394                                         for (; first != last; first++)
00395                                         {
00396                                                 retval.append(*first);
00397                                                 retval.append(glue);
00398                                         }
00399                                         retval.erase(retval.length() - glue.length());
00400 
00401                                         return retval;
00402                                 }
00403 
00410                         template <class InputIterator>
00411                                 static ext_string join(value_type glue, InputIterator first, InputIterator last)
00412                                 {
00413                                         ext_string retval;
00414 
00415                                         for (; first != last; first++)
00416                                         {
00417                                                 retval.append(*first);
00418                                                 retval.append(1, glue);
00419                                         }
00420                                         retval.erase(retval.length() - 1);
00421 
00422                                         return retval;
00423                                 }
00424 
00442                         ext_string &replace(const string &needle, const string &s)
00443                         {
00444                                 size_type
00445                                         lastpos = 0,
00446                                         thispos;
00447 
00448                                 while ((thispos = find(needle, lastpos)) != npos)
00449                                 {
00450                                         string::replace(thispos, needle.length(), s);
00451                                         lastpos = thispos + 1;
00452                                 }
00453                                 return *this;
00454                         }
00455 
00466                         ext_string &replace(value_type needle, value_type c)
00467                         {
00468                                 for (iterator i = begin(); i != end(); i++)
00469                                         if (*i == needle)
00470                                                 *i = c;
00471 
00472                                 return *this;
00473                         }
00474 
00490                         ext_string operator*(size_type n)
00491                         {
00492                                 ext_string retval;
00493                                 for (size_type i = 0; i < n; i++)
00494                                         retval.append(*this);
00495 
00496                                 return retval;
00497                         }
00498 
00505                         ext_string &tolower()
00506                         {
00507                                 for (iterator i = begin(); i != end(); i++)
00508                                         if (*i >= 'A' && *i <= 'Z')
00509                                                 *i = (*i) + ('a' - 'A');
00510                                 return *this;
00511                         }
00512 
00519                         ext_string &toupper()
00520                         {
00521                                 for (iterator i = begin(); i != end(); i++)
00522                                         if (*i >= 'a' && *i <= 'z')
00523                                                 *i = (*i) - ('a' - 'A');
00524                                 return *this;
00525                         }
00526 
00532                         size_type count(const string &str) const
00533                         {
00534                                 size_type
00535                                         count = 0,
00536                                         last = 0,
00537                                         cur = 0;
00538 
00539                                 while ((cur = find(str, last + 1)) != npos)
00540                                 {
00541                                         count++;
00542                                         last = cur;
00543                                 }
00544 
00545                                 return count;
00546                         }
00547 
00554                         bool is_alnum() const
00555                         {
00556                                 if (length() == 0)
00557                                         return false;
00558 
00559                                 for (const_iterator i = begin(); i != end(); i++)
00560                                 {
00561                                         if (*i < 'A' || *i > 'Z')
00562                                                 if (*i < '0' || *i > '9')
00563                                                         if (*i < 'a' || *i > 'z')
00564                                                                 return false;
00565                                 }
00566 
00567                                 return true;
00568                         }
00569 
00576                         bool is_alpha() const
00577                         {
00578                                 if (length() == 0)
00579                                         return false;
00580 
00581                                 for (const_iterator i = begin(); i != end(); i++)
00582                                         if (*i < 'A' || (*i > 'Z' && (*i < 'a' || *i > 'z')))
00583                                                 return false;
00584 
00585                                 return true;
00586                         }
00587 
00594                         bool is_numeric() const
00595                         {
00596                                 if (length() == 0)
00597                                         return false;
00598 
00599                                 for (const_iterator i = begin(); i != end(); i++)
00600                                         if (*i < '0' || *i > '9')
00601                                                 return false;
00602 
00603                                 return true;
00604                         }
00605 
00612                         bool is_lower() const
00613                         {
00614                                 if (length() == 0)
00615                                         return false;
00616 
00617                                 for (const_iterator i = begin(); i != end(); i++)
00618                                         if (*i < 'a' || *i < 'z')
00619                                                 return false;
00620 
00621                                 return true;
00622                         }
00623 
00630                         bool is_upper() const
00631                         {
00632                                 if (length() == 0)
00633                                         return false;
00634 
00635                                 for (const_iterator i = begin(); i != end(); i++)
00636                                         if (*i < 'A' || *i > 'Z')
00637                                                 return false;
00638 
00639                                 return true;
00640                         }
00641 
00648                         ext_string &swapcase()
00649                         {
00650                                 for (iterator i = begin(); i != end(); i++)
00651                                         if (*i >= 'A' && *i <= 'Z')
00652                                                 *i += ('a' - 'A');
00653                                         else if (*i >= 'a' && *i <= 'z')
00654                                                 *i -= ('a' - 'A');
00655                                 
00656                                 return *this;
00657                         }
00658         };
00659 }
00660 #endif

Generated on Sun Apr 3 05:45:26 2005 for Extended STL String by  doxygen 1.4.1