desktop: made it possible to run Detect() multiple times in EvoSources
[barry/progweb.git] / src / trim.h
blob030b5c0880fcba93d7d8053de6f33c5041533cc5
1 // Found at:
2 // http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
4 // Note that these functions trim the same arguments passed in, and do not
5 // make copies.
7 #ifndef __BARRY_TRIM_H__
8 #define __BARRY_TRIM_H__
10 #include <algorithm>
11 #include <functional>
12 #include <locale>
14 namespace Barry { namespace Inplace {
16 // trim from start
17 static inline std::string &ltrim(std::string &s) {
18 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
19 return s;
22 // trim from end
23 static inline std::string &rtrim(std::string &s) {
24 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
25 return s;
28 // trim from both ends
29 static inline std::string &trim(std::string &s) {
30 return ltrim(rtrim(s));
33 }} // namespace Barry::Inplace
35 #endif