51 template <
typename StringType>
52 StringType whitespace(
const StringType&)
54 return StringType(
" \t\n");
61template <
typename Char,
typename Traits,
typename Alloc>
62std::basic_string<Char, Traits, Alloc>
63tolower(
const std::basic_string<Char, Traits, Alloc>& input,
64 const std::locale& locale)
66 std::basic_string<Char, Traits, Alloc> result(input);
67 typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
68 for (iterator i = result.begin(); i != result.end(); ++i)
70 *i = std::tolower<Char>(*i, locale);
80template <
typename Char,
typename Traits,
typename Alloc>
81std::basic_string<Char, Traits, Alloc>
82toupper(
const std::basic_string<Char, Traits, Alloc>& input,
83 const std::locale& locale)
85 std::basic_string<Char, Traits, Alloc> result(input);
86 typedef typename std::basic_string<Char, Traits, Alloc>::iterator iterator;
87 for (iterator i = result.begin(); i != result.end(); ++i)
89 *i = std::toupper<Char>(*i, locale);
97template <
typename Char,
typename Traits,
typename Alloc>
98std::basic_string<Char, Traits, Alloc>
99replace_all(
const std::basic_string<Char, Traits, Alloc>& input,
100 const std::basic_string<Char, Traits, Alloc>& to_be_replaced,
101 const std::basic_string<Char, Traits, Alloc>& replacement)
103 typedef std::basic_string<Char, Traits, Alloc> string_type;
104 typename string_type::size_type size_to_be_replaced = to_be_replaced.size();
105 typename string_type::size_type size_replacement = replacement.size();
106 string_type result(input);
108 typename string_type::size_type i = result.find(to_be_replaced);
109 while (i != string_type::npos)
111 result.replace(i, size_to_be_replaced, replacement);
112 i = result.find(to_be_replaced, i + size_replacement);
120template <
typename Char,
typename Traits,
typename Alloc>
121std::basic_string<Char, Traits, Alloc>
123 const Char* to_be_replaced,
124 const Char* replacement)
126 typedef std::basic_string<Char, Traits, Alloc> string_type;
127 return replace_all(input, string_type(to_be_replaced), string_type(replacement));
135template <
typename Char,
typename Traits,
typename Alloc>
136bool begins_with(
const std::basic_string<Char, Traits, Alloc>& input,
137 const std::basic_string<Char, Traits, Alloc>& prefix)
139 return prefix.length() <= input.length() && std::equal(prefix.begin(), prefix.end(), input.begin());
145template <
typename Char,
typename Traits,
typename Alloc>
146bool begins_with(
const std::basic_string<Char, Traits, Alloc>& input,
149 typedef std::basic_string<Char, Traits, Alloc> string_type;
156template <
typename Char,
typename Traits,
typename Alloc>
157bool begins_with(
const std::basic_string<Char, Traits, Alloc>& input,
160 return !input.empty() && input[0] == prefix;
168template <
typename Char,
typename Traits,
typename Alloc>
169bool ends_with(
const std::basic_string<Char, Traits, Alloc>& input,
170 const std::basic_string<Char, Traits, Alloc>& suffix)
172 typedef typename std::basic_string<Char, Traits, Alloc>::difference_type
difference_type;
173 return suffix.length() <= input.length() &&
174 std::equal(suffix.begin(), suffix.end(), input.end() -
static_cast<difference_type>(suffix.length()));
180template <
typename Char,
typename Traits,
typename Alloc>
181bool ends_with(
const std::basic_string<Char, Traits, Alloc>& input,
184 typedef std::basic_string<Char, Traits, Alloc> string_type;
185 return ends_with(input, string_type(suffix));
191template <
typename Char,
typename Traits,
typename Alloc>
192bool ends_with(
const std::basic_string<Char, Traits, Alloc>& input,
195 return !input.empty() && input[input.size()-1] == suffix;
208template <
typename Char,
typename Traits,
typename Alloc>
209std::vector< std::basic_string<Char, Traits, Alloc> >
210split(
const std::basic_string<Char, Traits, Alloc>& to_be_split)
212 typedef std::basic_string<Char, Traits, Alloc> string_type;
213 typedef typename string_type::size_type
size_type;
215 const string_type seperators = impl::whitespace(to_be_split);
216 std::vector< std::basic_string<Char, Traits, Alloc> > result;
218 if (to_be_split.empty())
224 size_type end = to_be_split.find_first_of(seperators);
225 while (end != string_type::npos)
227 result.push_back(to_be_split.substr(begin, end - begin));
228 begin = to_be_split.find_first_not_of(seperators, end);
229 if (begin == string_type::npos)
231 result.push_back(string_type());
234 end = to_be_split.find_first_of(seperators, begin);
237 result.push_back(to_be_split.substr(begin));
256template <
typename Char,
typename Traits,
typename Alloc>
257std::vector< std::basic_string<Char, Traits, Alloc> >
258split(
const std::basic_string<Char, Traits, Alloc>& to_be_split,
259 const std::basic_string<Char, Traits, Alloc>& seperator,
262 typedef std::basic_string<Char, Traits, Alloc> string_type;
263 typedef typename string_type::size_type
size_type;
265 std::vector< std::basic_string<Char, Traits, Alloc> > result;
268 max_split = result.max_size() - 1;
274 while (result.size() < max_split)
276 const size_type end = to_be_split.find(seperator, begin);
277 if (end == string_type::npos)
282 result.push_back(to_be_split.substr(begin, end - begin));
283 begin = end + seperator_size;
286 result.push_back(to_be_split.substr(begin));
293template <
typename Char,
typename Traits,
typename Alloc>
294std::vector< std::basic_string<Char, Traits, Alloc> >
295split(
const std::basic_string<Char, Traits, Alloc>& to_be_split,
296 const Char* seperator,
299 typedef std::basic_string<Char, Traits, Alloc> string_type;
300 return split(to_be_split, string_type(seperator), max_split);
307template <
typename Char,
typename Traits,
typename Alloc,
typename InputIterator>
308std::basic_string<Char, Traits, Alloc>
309join(
const std::basic_string<Char, Traits, Alloc>& joiner, InputIterator first, InputIterator last)
311 std::basic_ostringstream<Char, Traits, Alloc> buffer;
316 while (first != last)
318 buffer << joiner << *first++;
325template <
typename Char,
typename Traits,
typename Alloc,
typename InputRange>
inline
326std::basic_string<Char, Traits, Alloc>
327join_r(
const std::basic_string<Char, Traits, Alloc>& joiner,
const InputRange& range)
329 return join(joiner, range.begin(), range.end());
340template <
typename Char,
typename Traits,
typename Alloc>
341std::basic_string<Char, Traits, Alloc>
342lstrip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
343 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
345 typedef std::basic_string<Char, Traits, Alloc> string_type;
346 const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
347 return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin);
355template <
typename Char,
typename Traits,
typename Alloc>
inline
356std::basic_string<Char, Traits, Alloc>
357lstrip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
359 return lstrip(to_be_stripped, impl::whitespace(to_be_stripped));
370template <
typename Char,
typename Traits,
typename Alloc>
371std::basic_string<Char, Traits, Alloc>
372rstrip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
373 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
375 typedef std::basic_string<Char, Traits, Alloc> string_type;
376 const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
377 return end == string_type::npos ? string_type() : to_be_stripped.substr(0, end + 1);
385template <
typename Char,
typename Traits,
typename Alloc>
inline
386std::basic_string<Char, Traits, Alloc>
387rstrip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
389 return rstrip(to_be_stripped, impl::whitespace(to_be_stripped));
400template <
typename Char,
typename Traits,
typename Alloc>
401std::basic_string<Char, Traits, Alloc>
402strip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped,
403 const std::basic_string<Char, Traits, Alloc>& to_be_removed)
405 typedef std::basic_string<Char, Traits, Alloc> string_type;
406 const typename string_type::size_type begin = to_be_stripped.find_first_not_of(to_be_removed);
407 const typename string_type::size_type end = to_be_stripped.find_last_not_of(to_be_removed);
408 return begin == string_type::npos ? string_type() : to_be_stripped.substr(begin, end - begin + 1);
416template <
typename Char,
typename Traits,
typename Alloc>
inline
417std::basic_string<Char, Traits, Alloc>
418strip(
const std::basic_string<Char, Traits, Alloc>& to_be_stripped)
420 return strip(to_be_stripped, impl::whitespace(to_be_stripped));
size_type size() const
returns the N, the number of elements in the list.
std::basic_string< Char, Traits, Alloc > lstrip(const std::basic_string< Char, Traits, Alloc > &to_be_stripped, const std::basic_string< Char, Traits, Alloc > &to_be_removed)
Return a copy of the string to_be_stripped with leading characters removed.
bool begins_with(const std::basic_string< Char, Traits, Alloc > &input, const std::basic_string< Char, Traits, Alloc > &prefix)
returns true if input begins with the input prefix
std::basic_string< Char, Traits, Alloc > rstrip(const std::basic_string< Char, Traits, Alloc > &to_be_stripped, const std::basic_string< Char, Traits, Alloc > &to_be_removed)
Return a copy of the string to_be_stripped with trailing characters removed.
std::basic_string< Char, Traits, Alloc > replace_all(const std::basic_string< Char, Traits, Alloc > &input, const std::basic_string< Char, Traits, Alloc > &to_be_replaced, const std::basic_string< Char, Traits, Alloc > &replacement)
replace all instances of to_be_replaced in input by replacement.
std::basic_string< Char, Traits, Alloc > tolower(const std::basic_string< Char, Traits, Alloc > &input, const std::locale &locale=std::locale())
convert std::basic_string to lower case by using user locale
std::vector< std::basic_string< Char, Traits, Alloc > > split(const std::basic_string< Char, Traits, Alloc > &to_be_split)
Reflects the Python function split without seperator argument.
std::basic_string< Char, Traits, Alloc > toupper(const std::basic_string< Char, Traits, Alloc > &input, const std::locale &locale=std::locale())
convert std::basic_string to upper case by using user locale
bool ends_with(const std::basic_string< Char, Traits, Alloc > &input, const std::basic_string< Char, Traits, Alloc > &suffix)
returns true if input ends with the input suffix
std::basic_string< Char, Traits, Alloc > strip(const std::basic_string< Char, Traits, Alloc > &to_be_stripped, const std::basic_string< Char, Traits, Alloc > &to_be_removed)
Return a copy of the string to_be_stripped with both leading and trailing characters removed.
lass extensions to the standard library
Library for Assembled Shared Sources.