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

ext_string.h

00001 
00096 #ifndef _EXT_STRING_H
00097 #define _EXT_STRING_H
00098 
00099 #include <string>
00100 #include <vector>
00101 
00102 namespace std
00103 {
00104 
00109    class ext_string : public string
00110    {
00111       public:
00117          ext_string() : string() { }
00118 
00126          ext_string(const string &s, size_type pos = 0, size_type n = npos) : string(s, pos, npos) { }
00127 
00133          ext_string(const value_type *s) : string(s) { }
00134 
00141          ext_string(const value_type *s, size_type n) : string(s, n) { }
00142 
00149          ext_string(size_type n, value_type c) : string(n, c) { }
00150 
00157          template <class InputIterator>
00158             ext_string(InputIterator first, InputIterator last) : string(first, last) { }
00159 
00163          ~ext_string() { }
00164 
00170          vector<ext_string> split(size_type limit = npos) const
00171          {
00172             vector<ext_string> v;
00173 
00174             const_iterator 
00175                i = begin(),
00176                  last = i;
00177             for (; i != end(); i++)
00178             {
00179                if (*i == ' ' || *i == '\n' || *i == '\t' || *i == '\r')
00180                {
00181                   if (i + 1 != end() && (i[1] == ' ' || i[1] == '\n' || i[1] == '\t' || i[1] == '\r'))
00182                      continue;
00183                   v.push_back(ext_string(last, i));
00184                   last = i + 1;
00185                   if (v.size() >= limit - 1)
00186                   {
00187                      v.push_back(ext_string(last, end()));
00188                      return v;
00189                   }
00190                }
00191             }
00192 
00193             if (last != i)
00194                v.push_back(ext_string(last, i));
00195 
00196             return v;
00197          }
00198 
00228          vector<ext_string> split(value_type separator, size_type limit = npos) const
00229          {
00230             vector<ext_string> v;
00231 
00232             const_iterator 
00233                i = begin(),
00234                last = i;
00235             for (; i != end(); i++)
00236             {
00237                if (*i == separator)
00238                {
00239                   v.push_back(ext_string(last, i));
00240                   last = i + 1;
00241                   if (v.size() >= limit - 1)
00242                   {
00243                      v.push_back(ext_string(last, end()));
00244                      return v;
00245                   }
00246                }
00247             }
00248 
00249             if (last != i)
00250                v.push_back(ext_string(last, i));
00251 
00252             return v;
00253          }
00254 
00274          vector<ext_string> split(const string &separator, size_type limit = npos) const
00275          {
00276             vector<ext_string> v;
00277 
00278             const_iterator
00279                i = begin(),
00280                last = i;
00281             for (; i != end(); i++)
00282             {
00283                if (string(i, i + separator.length()) == separator)
00284                {
00285                   v.push_back(ext_string(last, i));
00286                   last = i + separator.length();
00287 
00288                   if (v.size() >= limit - 1)
00289                   {
00290                      v.push_back(ext_string(last, end()));
00291                      return v;
00292                   }
00293                }
00294             }
00295 
00296             if (last != i)
00297                v.push_back(ext_string(last, i));
00298 
00299             return v;
00300          }
00301 
00312          static long int integer(const string &s)
00313          {
00314             long int retval = 0;
00315             bool neg = false;
00316 
00317             for (const_iterator i = s.begin(); i != s.end(); i++)
00318             {
00319                if (i == s.begin())
00320                {
00321                   if (*i == '-')
00322                   {
00323                      neg = true;
00324                      continue;
00325                   }
00326                   else if (*i == '+')
00327                      continue;
00328                }
00329                if (*i >= '0' && *i <= '9')
00330                {
00331                   retval *= 10;
00332                   retval += *i - '0';
00333                }
00334                else
00335                   break;
00336             }
00337 
00338             if (neg)
00339                retval *= -1;
00340 
00341             return retval;
00342          }
00343 
00353          long int integer() const
00354          {
00355             return integer(*this);
00356          }
00357 
00376          vector<ext_string> chunk_split(size_type chunklen) const
00377          {
00378             vector<ext_string> retval;
00379             retval.reserve(size() / chunklen + 1);
00380 
00381             size_type count = 0;
00382             const_iterator
00383                i = begin(),
00384                last = i;
00385             for (; i != end(); i++, count++)
00386             {
00387                if (count == chunklen)
00388                {
00389                   count = 0;
00390                   retval.push_back(ext_string(last, i));
00391                   last = i;
00392                }
00393             }
00394             
00395             if (last != i)
00396                retval.push_back(ext_string(last, i));
00397 
00398             return retval;
00399          }
00400 
00427          template <class InputIterator>
00428             static ext_string join(const string &glue, InputIterator first, InputIterator last)
00429             {
00430                ext_string retval;
00431 
00432                for (; first != last; first++)
00433                {
00434                   retval.append(*first);
00435                   retval.append(glue);
00436                }
00437                retval.erase(retval.length() - glue.length());
00438 
00439                return retval;
00440             }
00441 
00448          template <class InputIterator>
00449             static ext_string join(value_type glue, InputIterator first, InputIterator last)
00450             {
00451                ext_string retval;
00452 
00453                for (; first != last; first++)
00454                {
00455                   retval.append(*first);
00456                   retval.append(1, glue);
00457                }
00458                retval.erase(retval.length() - 1);
00459 
00460                return retval;
00461             }
00462 
00480          ext_string &replace(const string &needle, const string &s)
00481          {
00482             size_type
00483                lastpos = 0,
00484                thispos;
00485 
00486             while ((thispos = find(needle, lastpos)) != npos)
00487             {
00488                string::replace(thispos, needle.length(), s);
00489                lastpos = thispos + 1;
00490             }
00491             return *this;
00492          }
00493 
00504          ext_string &replace(value_type needle, value_type c)
00505          {
00506             for (iterator i = begin(); i != end(); i++)
00507                if (*i == needle)
00508                   *i = c;
00509 
00510             return *this;
00511          }
00512 
00528          ext_string operator*(size_type n)
00529          {
00530             ext_string retval;
00531             for (size_type i = 0; i < n; i++)
00532                retval.append(*this);
00533 
00534             return retval;
00535          }
00536 
00543          ext_string &tolower()
00544          {
00545             for (iterator i = begin(); i != end(); i++)
00546                if (*i >= 'A' && *i <= 'Z')
00547                   *i = (*i) + ('a' - 'A');
00548             return *this;
00549          }
00550 
00557          ext_string &toupper()
00558          {
00559             for (iterator i = begin(); i != end(); i++)
00560                if (*i >= 'a' && *i <= 'z')
00561                   *i = (*i) - ('a' - 'A');
00562             return *this;
00563          }
00564 
00570          size_type count(const string &str) const
00571          {
00572             size_type
00573                count = 0,
00574                last = 0,
00575                cur = 0;
00576 
00577             while ((cur = find(str, last + 1)) != npos)
00578             {
00579                count++;
00580                last = cur;
00581             }
00582 
00583             return count;
00584          }
00585 
00592          bool is_alnum() const
00593          {
00594             if (length() == 0)
00595                return false;
00596 
00597             for (const_iterator i = begin(); i != end(); i++)
00598             {
00599                if (*i < 'A' || *i > 'Z')
00600                   if (*i < '0' || *i > '9')
00601                      if (*i < 'a' || *i > 'z')
00602                         return false;
00603             }
00604 
00605             return true;
00606          }
00607 
00614          bool is_alpha() const
00615          {
00616             if (length() == 0)
00617                return false;
00618 
00619             for (const_iterator i = begin(); i != end(); i++)
00620                if (*i < 'A' || (*i > 'Z' && (*i < 'a' || *i > 'z')))
00621                   return false;
00622 
00623             return true;
00624          }
00625 
00632          bool is_numeric() const
00633          {
00634             if (length() == 0)
00635                return false;
00636 
00637             for (const_iterator i = begin(); i != end(); i++)
00638                if (*i < '0' || *i > '9')
00639                   return false;
00640 
00641             return true;
00642          }
00643 
00650          bool is_lower() const
00651          {
00652             if (length() == 0)
00653                return false;
00654 
00655             for (const_iterator i = begin(); i != end(); i++)
00656                if (*i < 'a' || *i < 'z')
00657                   return false;
00658 
00659             return true;
00660          }
00661 
00668          bool is_upper() const
00669          {
00670             if (length() == 0)
00671                return false;
00672 
00673             for (const_iterator i = begin(); i != end(); i++)
00674                if (*i < 'A' || *i > 'Z')
00675                   return false;
00676 
00677             return true;
00678          }
00679 
00686          ext_string &swapcase()
00687          {
00688             for (iterator i = begin(); i != end(); i++)
00689                if (*i >= 'A' && *i <= 'Z')
00690                   *i += ('a' - 'A');
00691                else if (*i >= 'a' && *i <= 'z')
00692                   *i -= ('a' - 'A');
00693             
00694             return *this;
00695          }
00696    };
00697 }
00698 #endif

Generated on Sun Apr 17 05:08:48 2005 for Extended STL String by  doxygen 1.4.1