Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

basic_string.hh

00001 /*
00002 *  Name:      basic_string.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Extension class for STL string
00005 *  Date:      $Date: 2003/04/14 00:18:32 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1999-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_TEXT_BASIC_STRING__
00027 #define _MPCL_TEXT_BASIC_STRING__
00028 
00029 #include <algorithm>
00030 #include <cstring>
00031 #include <locale>
00032 #include <string>
00033 #include "../exceptions.hh"
00034 
00035 
00037 namespace mpcl
00038 {
00039 
00041   namespace text
00042   {
00043 
00049     template < typename TChar                                ,
00050                typename TTraits    = std::char_traits<TChar> ,
00051                typename TAllocator = std::allocator<TChar>   >
00052     class TBasicString : public std::basic_string<TChar, TTraits, TAllocator>
00053     {
00054 
00055       public:
00056 
00058         typedef
00059           typename std::basic_string<TChar, TTraits, TAllocator>::allocator_type
00060           allocator_type;
00061 
00063         typedef
00064           typename std::basic_string<TChar, TTraits, TAllocator>::const_iterator
00065           const_iterator;
00066 
00068         typedef
00069           typename std::basic_string<TChar, TTraits, TAllocator>::const_pointer
00070           const_pointer;
00071 
00073         typedef
00074           typename std::basic_string<TChar, TTraits, TAllocator>::const_reference
00075           const_reference;
00076 
00078         typedef
00079           typename std::basic_string<TChar, TTraits, TAllocator>::const_reverse_iterator
00080           const_reverse_iterator;
00081 
00083         typedef
00084           typename std::basic_string<TChar, TTraits, TAllocator>::difference_type
00085           difference_type;
00086 
00088         typedef
00089           typename std::basic_string<TChar, TTraits, TAllocator>::iterator
00090           iterator;
00091 
00093         typedef
00094           typename std::basic_string<TChar, TTraits, TAllocator>::pointer
00095           pointer;
00096 
00098         typedef
00099           typename std::basic_string<TChar, TTraits, TAllocator>::reference
00100           reference;
00101 
00103         typedef
00104           typename std::basic_string<TChar, TTraits, TAllocator>::reverse_iterator
00105           reverse_iterator;
00106 
00108         typedef
00109           typename std::basic_string<TChar, TTraits, TAllocator>::size_type
00110           size_type;
00111 
00113         typedef
00114           typename std::basic_string<TChar, TTraits, TAllocator>::traits_type
00115           traits_type;
00116 
00118         typedef
00119           typename std::basic_string<TChar, TTraits, TAllocator>::value_type
00120           value_type;
00121 
00122 
00123       public:
00124 
00125         //
00126         //  C O N S T R U C T O R S
00127         //
00128 
00130         explicit TBasicString (void)                      :
00131           std::basic_string<TChar, TTraits, TAllocator>() {}
00132 
00134         TBasicString (const std::basic_string<TChar, TTraits, TAllocator>& rkySOURCE) :
00135           std::basic_string<TChar, TTraits, TAllocator> (rkySOURCE)                   {}
00136 
00138         TBasicString (const TBasicString& rkySOURCE)                :
00139           std::basic_string<TChar, TTraits, TAllocator> (rkySOURCE) {}
00140 
00142         TBasicString ( const TBasicString& rkySOURCE      ,
00143                        size_type           zOFFSET        ,
00144                        size_type           zLENGTH = npos )                           :
00145           std::basic_string<TChar, TTraits, TAllocator> (rkySOURCE, zOFFSET, zLENGTH) {}
00146 
00153         TBasicString (const TChar* pkcSOURCE, size_type zLENGTH) :
00154           std::basic_string<TChar, TTraits, TAllocator>()
00155         {
00156           if ( !pkcSOURCE )
00157           {
00158             throw TConstraintException ("null string", __FILE__, __LINE__);
00159           }
00160           assign (pkcSOURCE, zLENGTH);
00161         }
00162 
00168         TBasicString (const TChar* pkcSOURCE) :
00169           std::basic_string<TChar, TTraits, TAllocator>()
00170         {
00171           if ( !pkcSOURCE )
00172           {
00173             throw TConstraintException ("null string", __FILE__, __LINE__);
00174           }
00175           assign (pkcSOURCE);
00176         }
00177 
00179         TBasicString (size_type zTIMES, TChar cSOURCE)                    :
00180           std::basic_string<TChar, TTraits, TAllocator> (zTIMES, cSOURCE) {}
00181 
00183         template<typename InputIterator>
00184         TBasicString(InputIterator tBEGIN_ITER, InputIterator tEND_ITER)         :
00185           std::basic_string<TChar, TTraits, TAllocator> (tBEGIN_ITER, tEND_ITER) {}
00186 
00192         TBasicString& operator = (const TBasicString& rkySOURCE)
00193         {
00194           std::basic_string<TChar, TTraits, TAllocator>::operator = (rkySOURCE);
00195           return *this;
00196         }
00197 
00203         TBasicString& operator = (const TChar* pkcSOURCE)
00204         {
00205           if ( !pkcSOURCE )
00206           {
00207             throw TConstraintException ("null string", __FILE__, __LINE__);
00208           }
00209           std::basic_string<TChar, TTraits, TAllocator>::operator = (pkcSOURCE);
00210           return *this;
00211         }
00212 
00218         TBasicString& operator = (TChar cSOURCE)
00219         {
00220           std::basic_string<TChar, TTraits, TAllocator>::operator = (cSOURCE);
00221           return *this;
00222         }
00223 
00231         size_type replaceAll ( const std::basic_string<TChar, TTraits, TAllocator>& rkySOURCE1 ,
00232                                const std::basic_string<TChar, TTraits, TAllocator>& rkySOURCE2 )
00233         {
00234           return replaceAll (rkySOURCE1.c_str(), rkySOURCE2.c_str());
00235         }
00236 
00244         size_type replaceAll (const TChar* pkcSOURCE1, const TChar* pkcSOURCE2);
00245 
00253         size_type replaceAll (TChar cSOURCE1, TChar cSOURCE2);
00254 
00262         TBasicString& leftFill (TChar cSOURCE, size_type zLENGTH);
00263 
00271         TBasicString& rightFill (TChar cSOURCE, size_type zLENGTH);
00272 
00278         TBasicString& suppress (TChar cSOURCE);
00279 
00285         TBasicString& suppressFirst (TChar cSOURCE);
00286 
00292         TBasicString& suppressLast (TChar cSOURCE);
00293 
00299         TBasicString& uppercase (void);
00300 
00306         TBasicString& lowercase (void);
00307 
00308 
00309       public:
00310 
00312         //  S E L E C T O R S
00313         //
00314 
00320         size_type occurrences (const TChar* pkcSOURCE) const;
00321 
00327         size_type occurrences (TChar cSOURCE) const;
00328 
00335         template <typename TOutputIter>
00336         const TBasicString& split (TChar cSOURCE, TOutputIter tTARGET_ITER) const
00337         {
00338           Split (begin(), end(), cSOURCE, tTARGET_ITER);
00339           return *this;
00340         }
00341 
00342     };  // class TBasicString
00343 
00344 
00345     template <typename TChar, typename TTraits, typename TAllocator>
00346     inline typename TBasicString<TChar, TTraits, TAllocator>::size_type TBasicString<TChar, TTraits, TAllocator>::
00347     replaceAll (const TChar* pkcSOURCE1, const TChar* pkcSOURCE2)
00348     {
00349 
00350       register size_type   zTimes = 0;
00351 
00352       if ( !pkcSOURCE1 || !pkcSOURCE2 )
00353       {
00354         throw TConstraintException ("null string in parameters", __FILE__, __LINE__);
00355       }
00356       else
00357       {
00358         register size_type   zOffset       = 0;
00359         register size_type   zSourceLength = std::strlen (pkcSOURCE1);
00360         register size_type   zTargetLength = std::strlen (pkcSOURCE2);
00361 
00362         while ( npos != (zOffset = find (pkcSOURCE1, zOffset)) )
00363         {
00364           erase (zOffset, zSourceLength);
00365           insert (zOffset, pkcSOURCE2);
00366           zOffset += zTargetLength;
00367           ++zTimes;
00368         }
00369       }
00370       return zTimes;
00371 
00372     }  // replaceAll()
00373 
00374 
00375     template <typename TChar, typename TTraits, typename TAllocator>
00376     inline typename TBasicString<TChar, TTraits, TAllocator>::size_type TBasicString<TChar, TTraits, TAllocator>::
00377     replaceAll (TChar cSOURCE1, TChar cSOURCE2)
00378     {
00379 
00380       register size_type   zTimes  = 0;
00381       register size_type   zOffset = 0;
00382 
00383       while ( npos != (zOffset = find (cSOURCE1, zOffset)) )
00384       {
00385         erase (zOffset, 1);
00386         insert (zOffset, 1, cSOURCE2);
00387         ++zOffset;
00388         ++zTimes;
00389       }
00390       return zTimes;
00391 
00392     }  // replaceAll()
00393 
00394 
00395     template <typename TChar, typename TTraits, typename TAllocator>
00396     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00397     leftFill (TChar cSOURCE, size_type zLENGTH)
00398     {
00399 
00400       size_type   zThisLength = length();
00401 
00402       if ( zLENGTH > zThisLength )
00403       {
00404         assign (std::string (zLENGTH - zThisLength, cSOURCE) + *this);
00405       }
00406       return *this;
00407 
00408     }  // leftFill()
00409 
00410 
00411     template <typename TChar, typename TTraits, typename TAllocator>
00412     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00413     rightFill (TChar cSOURCE, size_type zLENGTH)
00414     {
00415 
00416       size_t   zThisLength = length();
00417 
00418       if ( zLENGTH > zThisLength )
00419       {
00420         append (std::string (zLENGTH - zThisLength, cSOURCE));
00421       }
00422       return *this;
00423 
00424     }  // rightFill()
00425 
00426 
00427     template <typename TChar, typename TTraits, typename TAllocator>
00428     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00429     suppress (TChar cSOURCE)
00430     {
00431 
00432       iterator   tIter = std::remove (begin(), end(), cSOURCE);
00433 
00434       erase (tIter, end());
00435       return *this;
00436 
00437     }  // suppress()
00438 
00439 
00440     template <typename TChar, typename TTraits, typename TAllocator>
00441     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00442     suppressFirst (TChar cSOURCE)
00443     {
00444 
00445       if ( !empty() )
00446       {
00447         const_iterator   tEnd  = end();
00448         iterator         tIter = begin();
00449 
00450         while ( tIter != tEnd )
00451         {
00452           if ( *tIter != cSOURCE )
00453           {
00454             if ( tIter != begin() )
00455             {
00456               erase (0, tIter - begin());
00457             }
00458             break;
00459           }
00460           ++tIter;
00461         }
00462       }
00463       return *this;
00464 
00465     }  // suppressFirst()
00466 
00467 
00468     template <typename TChar, typename TTraits, typename TAllocator>
00469     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00470     suppressLast (TChar cSOURCE)
00471     {
00472 
00473       if ( !empty() )
00474       {
00475         iterator   tBegin = begin();
00476         iterator   tIter  = end();
00477 
00478         while ( tBegin != tIter )
00479         {
00480           --tIter;
00481           if ( *tIter != cSOURCE )
00482           {
00483             if ( tIter != (end() - 1) )
00484             {
00485               erase (tIter - tBegin + 1);
00486             }
00487             break;
00488           }
00489         }
00490       }
00491       return *this;
00492 
00493     }  // suppressLast()
00494 
00495 
00496     template <typename TChar, typename TTraits, typename TAllocator>
00497     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00498     uppercase (void)
00499     {
00500 
00501       std::locale                TLocale;
00502       const std::ctype<TChar>&   rktCtype (std::use_facet<std::ctype<TChar> >(TLocale));
00503 
00504       for (iterator I = begin(); ( I != end() ) ;++I)
00505       {
00506         *I = rktCtype.toupper (*I);
00507       }
00508       return *this;
00509 
00510     }  // uppercase()
00511 
00512 
00513     template <typename TChar, typename TTraits, typename TAllocator>
00514     inline TBasicString<TChar, TTraits, TAllocator>& TBasicString<TChar, TTraits, TAllocator>::
00515     lowercase (void)
00516     {
00517 
00518       using std::locale;
00519 
00520       std::locale                TLocale;
00521       const std::ctype<TChar>&   rktCtype (std::use_facet<std::ctype<TChar> >(TLocale));
00522 
00523       for (iterator I = begin(); ( I != end() ) ;++I)
00524       {
00525         *I = rktCtype.tolower (*I);
00526       }
00527       return *this;
00528 
00529     }  // lowercase()
00530 
00531 
00532     //
00533     //  S E L E C T O R S
00534     //
00535 
00536     template <typename TChar, typename TTraits, typename TAllocator>
00537     inline typename TBasicString<TChar, TTraits, TAllocator>::size_type TBasicString<TChar, TTraits, TAllocator>::
00538     occurrences (const TChar* pkcSOURCE) const
00539     {
00540 
00541       register size_type   zOccurrences = 0;
00542 
00543       if ( !pkcSOURCE )
00544       {
00545         throw TConstraintException ("null string in parameters", __FILE__, __LINE__);
00546       }
00547       else
00548       {
00549         register size_type   zOffset       = 0;
00550         register size_type   zTargetLength = std::strlen (pkcSOURCE);
00551 
00552         while ( npos != (zOffset = find (pkcSOURCE, zOffset)) )
00553         {
00554           ++zOccurrences;
00555           zOffset += zTargetLength;
00556         }
00557       }
00558       return zOccurrences;
00559 
00560     }  // occurrences()
00561 
00562 
00563     template <typename TChar, typename TTraits, typename TAllocator>
00564     inline typename TBasicString<TChar, TTraits, TAllocator>::size_type TBasicString<TChar, TTraits, TAllocator>::
00565     occurrences (TChar cTARGET) const
00566     {
00567 
00568       register size_type   zOffset      = 0;
00569       register size_type   zOccurrences = 0;
00570 
00571       while ( npos != (zOffset = find (cTARGET, zOffset)) )
00572       {
00573         ++zOccurrences;
00574         ++zOffset;
00575       }
00576       return zOccurrences;
00577 
00578     }  // occurrences()
00579 
00580   }  // namespace text
00581 
00582 }  // namespace mpcl
00583 
00584 
00585 #endif  // not _MPCL_TEXT_BASIC_STRING__

Generated on Mon Oct 13 02:35:22 2003 for MPCL by doxygen1.2.18