string_algorithm.tpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00030 #include <sstream>
00031 
00032 /*----------------------------------------------------------------------------*/
00039 template<typename StreamType, typename StringType>
00040 StreamType& claw::text::getline( StreamType& is, StringType& str )
00041 {
00042   std::getline( is, str );
00043 
00044   if ( !str.empty() )
00045     if ( str[ str.size() - 1 ] == typename StringType::value_type('\r') )
00046       str.erase( str.size() - 1 );
00047 
00048   return is;
00049 } // getline()
00050 
00051 /*----------------------------------------------------------------------------*/
00057 template<class StringType>
00058 void claw::text::trim_left( StringType& str,
00059                             const typename StringType::value_type* const s )
00060 {
00061   typename StringType::size_type p = str.find_first_not_of(s);
00062 
00063   if (p != StringType::npos)
00064     str = str.substr(p);
00065 } // trim_left()
00066 
00067 /*----------------------------------------------------------------------------*/
00073 template<class StringType>
00074 void claw::text::trim_right( StringType& str,
00075                              const typename StringType::value_type* const s )
00076 {
00077   typename StringType::size_type p = str.find_last_not_of(s);
00078 
00079   if (p != StringType::npos)
00080     str = str.substr( 0, p+1 );
00081 } // trim_right()
00082 
00083 /*----------------------------------------------------------------------------*/
00089 template<class StringType>
00090 void claw::text::trim( StringType& str,
00091                        const typename StringType::value_type* const s )
00092 {
00093   typename StringType::size_type first = str.find_first_not_of(s);
00094   typename StringType::size_type last  = str.find_last_not_of(s);
00095 
00096   if (first != StringType::npos)
00097     str = str.substr( first, last - first + 1 );
00098 } // trim()
00099 
00100 /*----------------------------------------------------------------------------*/
00113 template<class StringType>
00114 void claw::text::squeeze( StringType& str,
00115                           const typename StringType::value_type* const s )
00116 {
00117   typedef typename StringType::size_type size_type;
00118 
00119   size_type first(0);
00120 
00121   do
00122     {
00123       first = str.find_first_of(s, first);
00124 
00125       if ( first != StringType::npos )
00126         {
00127           size_type last = str.find_first_not_of(str[first], first+1);
00128 
00129           if ( last == StringType::npos )
00130             str = str.substr(0, first+1);
00131           else if ( last - first > 1 )
00132             str = str.substr(0, first+1) + str.substr(last);
00133 
00134           ++first;
00135         }
00136     }
00137   while ( (first != StringType::npos) && (first != str.length()) );
00138 } // squeeze()
00139 
00140 /*----------------------------------------------------------------------------*/
00145 template<typename T, class StringType>
00146 bool claw::text::is_of_type( const StringType& str )
00147 {
00148   std::basic_istringstream< typename StringType::value_type,
00149     typename StringType::traits_type,
00150     typename StringType::allocator_type > iss(str);
00151 
00152   T val;
00153   bool result = false;
00154 
00155   if ( iss >> val )
00156     result = iss.eof();
00157 
00158   return result;
00159 } // is_of_type()
00160 
00161 /*----------------------------------------------------------------------------*/
00169 template<class BackInsertion, class StringType>
00170 void claw::text::split( BackInsertion& sequence, const StringType& str,
00171       const typename StringType::value_type sep )
00172 {
00173   StringType line;
00174   std::basic_istringstream< typename StringType::value_type,
00175     typename StringType::traits_type,
00176     typename StringType::allocator_type > iss(str);
00177 
00178   while ( std::getline(iss, line, sep) )
00179     sequence.push_back(line);
00180 } // split()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1