string_algorithm.tpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
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 }
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 }
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 }
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 }
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 }
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 }