00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <locale>
00044
00045 namespace lass
00046 {
00047 namespace stde
00048 {
00049 namespace impl
00050 {
00051 template <typename StringType>
00052 StringType whitespace(const StringType&)
00053 {
00054 return StringType(" \t\n");
00055 }
00056 }
00057
00058
00059
00060
00061 template <typename Char, typename Traits, typename Alloc>
00062 std::basic_string<Char, Traits, Alloc>
00063 tolower(const std::basic_string<Char, Traits, Alloc>& input,
00064 const std::locale& locale)
00065 {
00066 std::basic_string<Char, Traits, Alloc> result(input);
00067 typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
00068 for (iterator i = result.begin(); i != result.end(); ++i)
00069 {
00070 *i = std::tolower<Char>(*i, locale);
00071 }
00072 return result;
00073 }
00074
00075
00076
00077
00078
00079
00080 template <typename Char, typename Traits, typename Alloc>
00081 std::basic_string<Char, Traits, Alloc>
00082 toupper(const std::basic_string<Char, Traits, Alloc>& input,
00083 const std::locale& locale)
00084 {
00085 std::basic_string<Char, Traits, Alloc> result(input);
00086 typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
00087 for (iterator i = result.begin(); i != result.end(); ++i)
00088 {
00089 *i = std::toupper<Char>(*i, locale);
00090 }
00091 return result;
00092 }
00093
00094
00095
00096
00097 template <typename Char, typename Traits, typename Alloc>
00098 std::basic_string<Char, Traits, Alloc>
00099 replace_all(const std::basic_string<Char, Traits, Alloc>& input,
00100 const std::basic_string<Char, Traits, Alloc>& to_be_replaced,
00101 const std::basic_string<Char, Traits, Alloc>& replacement)
00102 {
00103 typedef std::basic_string<Char, Traits, Alloc> string_type;
00104 typename string_type::size_type size_to_be_replaced = to_be_replaced.size();
00105 typename string_type::size_type size_replacement = replacement.size();
00106 string_type result(input);
00107
00108 typename string_type::size_type i = result.find(to_be_replaced);
00109 while (i != string_type::npos)
00110 {
00111 result.replace(i, size_to_be_replaced, replacement);
00112 i = result.find(to_be_replaced, i + size_replacement);
00113 }
00114 return result;
00115 }
00116
00117
00118
00119
00120 template <typename Char, typename Traits, typename Alloc>
00121 std::basic_string<Char, Traits, Alloc>
00122 replace_all(const std::basic_string<Char, Traits, Alloc>& input,
00123 const Char* to_be_replaced,
00124 const Char* replacement)
00125 {
00126 return replace_all(input, std::string(to_be_replaced), std::string(replacement));
00127 }
00128
00129
00130
00131
00132
00133
00134 template <typename Char, typename Traits, typename Alloc>
00135 bool begins_with(const std::basic_string<Char, Traits, Alloc>& input,
00136 const std::basic_string<Char, Traits, Alloc>& prefix)
00137 {
00138 return prefix.length() <= input.length() && std::equal(prefix.begin(), prefix.end(), input.begin());
00139 }
00140
00141
00142
00143
00144 template <typename Char, typename Traits, typename Alloc>
00145 bool begins_with(const std::basic_string<Char, Traits, Alloc>& input,
00146 const Char* prefix)
00147 {
00148 return begins_with(input, std::string(prefix));
00149 }
00150
00151
00152
00153
00154
00155
00156 template <typename Char, typename Traits, typename Alloc>
00157 bool ends_with(const std::basic_string<Char, Traits, Alloc>& input,
00158 const std::basic_string<Char, Traits, Alloc>& suffix)
00159 {
00160 return suffix.length() <= input.length() &&
00161 std::equal(suffix.begin(), suffix.end(), input.begin() + input.length() - suffix.length());
00162 }
00163
00164
00165
00166
00167 template <typename Char, typename Traits, typename Alloc>
00168 bool ends_with(const std::basic_string<Char, Traits, Alloc>& input,
00169 const Char* suffix)
00170 {
00171 return ends_with(input, std::string(suffix));
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 template <typename Char, typename Traits, typename Alloc>
00185 std::vector< std::basic_string<Char, Traits, Alloc> >
00186 split(const std::basic_string<Char, Traits, Alloc>& to_be_split)
00187 {
00188 typedef std::basic_string<Char, Traits, Alloc> string_type;
00189 typedef typename string_type::size_type size_type;
00190
00191 const string_type seperators = impl::whitespace(to_be_split);
00192 std::vector< std::basic_string<Char, Traits, Alloc> > result;
00193
00194 if (to_be_split.empty())
00195 {
00196 return result;
00197 }
00198
00199 size_type begin = 0;
00200 size_type end = to_be_split.find_first_of(seperators);
00201 while (end != string_type::npos)
00202 {
00203 result.push_back(to_be_split.substr(begin, end - begin));
00204 begin = to_be_split.find_first_not_of(seperators, end);
00205 if (begin == string_type::npos)
00206 {
00207 result.push_back(string_type());
00208 return result;
00209 }
00210 end = to_be_split.find_first_of(seperators, begin);
00211 }
00212
00213 result.push_back(to_be_split.substr(begin));
00214 return result;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 template <typename Char, typename Traits, typename Alloc>
00233 std::vector< std::basic_string<Char, Traits, Alloc> >
00234 split(const std::basic_string<Char, Traits, Alloc>& to_be_split,
00235 const std::basic_string<Char, Traits, Alloc>& seperator,
00236 size_t max_split)
00237 {
00238 typedef std::basic_string<Char, Traits, Alloc> string_type;
00239 typedef typename string_type::size_type size_type;
00240
00241 std::vector< std::basic_string<Char, Traits, Alloc> > result;
00242 if (max_split == 0)
00243 {
00244 max_split = result.max_size() - 1;
00245 }
00246
00247 const size_type seperator_size = seperator.size();
00248
00249 size_type begin = 0;
00250 while (result.size() < max_split)
00251 {
00252 const size_type end = to_be_split.find(seperator, begin);
00253 if (end == string_type::npos)
00254 {
00255 break;
00256 }
00257
00258 result.push_back(to_be_split.substr(begin, end - begin));
00259 begin = end + seperator_size;
00260 }
00261
00262 result.push_back(to_be_split.substr(begin));
00263 return result;
00264 }
00265
00266
00267
00268
00269 template <typename Char, typename Traits, typename Alloc>
00270 std::vector< std::basic_string<Char, Traits, Alloc> >
00271 split(const std::basic_string<Char, Traits, Alloc>& to_be_split,
00272 const Char* seperator,
00273 size_t max_split)
00274 {
00275 return split(to_be_split, std::string(seperator), max_split);
00276 }
00277
00278
00279
00280
00281
00282 template <typename Char, typename Traits, typename Alloc, typename InputIterator>
00283 std::basic_string<Char, Traits, Alloc>
00284 join(const std::basic_string<Char, Traits, Alloc>& joiner, InputIterator first, InputIterator last)
00285 {
00286 std::basic_ostringstream<Char, Traits, Alloc> buffer;
00287 if (first != last)
00288 {
00289 buffer << *first++;
00290 }
00291 while (first != last)
00292 {
00293 buffer << joiner << *first++;
00294 }
00295 return buffer.str();
00296 }
00297
00298
00299
00300 template <typename Char, typename Traits, typename Alloc, typename InputRange> inline
00301 std::basic_string<Char, Traits, Alloc>
00302 join_r(const std::basic_string<Char, Traits, Alloc>& joiner, const InputRange& range)
00303 {
00304 return join(joiner, range.begin(), range.end());
00305 }
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 template <typename Char, typename Traits, typename Alloc>
00316 std::basic_string<Char, Traits, Alloc>
00317 lstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00318 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00319 {
00320 typedef std::basic_string<Char, Traits, Alloc> string_type;
00321 const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
00322 return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin);
00323 }
00324
00325
00326
00327
00328
00329
00330 template <typename Char, typename Traits, typename Alloc> inline
00331 std::basic_string<Char, Traits, Alloc>
00332 lstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00333 {
00334 return lstrip(to_be_stripped, impl::whitespace(to_be_stripped));
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 template <typename Char, typename Traits, typename Alloc>
00346 std::basic_string<Char, Traits, Alloc>
00347 rstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00348 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00349 {
00350 typedef std::basic_string<Char, Traits, Alloc> string_type;
00351 const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
00352 return end == string_type::npos ? string_type() : to_be_stripped.substr(0, end + 1);
00353 }
00354
00355
00356
00357
00358
00359
00360 template <typename Char, typename Traits, typename Alloc> inline
00361 std::basic_string<Char, Traits, Alloc>
00362 rstrip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00363 {
00364 return rstrip(to_be_stripped, impl::whitespace(to_be_stripped));
00365 }
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 template <typename Char, typename Traits, typename Alloc>
00376 std::basic_string<Char, Traits, Alloc>
00377 strip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
00378 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
00379 {
00380 typedef std::basic_string<Char, Traits, Alloc> string_type;
00381 const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
00382 const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
00383 return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin, end - begin + 1);
00384 }
00385
00386
00387
00388
00389
00390
00391 template <typename Char, typename Traits, typename Alloc> inline
00392 std::basic_string<Char, Traits, Alloc>
00393 strip(const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
00394 {
00395 return strip(to_be_stripped, impl::whitespace(to_be_stripped));
00396 }
00397
00398
00399
00400 }
00401
00402 }
00403
00404