lib: Fixing WinCE build of configfilewin32.cc now that it uses i18n.
[barry.git] / src / trim.h
blob2c2f615e53b85534cf56ed2becfce869536d9637
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 <stdlib.h>
11 #include <algorithm>
12 #include <functional>
13 #include <locale>
15 namespace Barry { namespace Inplace {
17 // Windows CE defines std::isspace(int) as a macro to a function with
18 // two arguments with one prefilled, so a wrapper function is needed.
19 static inline int isSpaceChar(int c) {
20 return std::isspace(c);
23 // trim from start
24 static inline std::string &ltrim(std::string &s) {
25 std::string::iterator end(std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(isSpaceChar))));
26 s.erase(s.begin(), end);
27 return s;
30 // trim from end
31 static inline std::string &rtrim(std::string &s) {
32 std::string::reverse_iterator start(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(isSpaceChar))));
33 s.erase(start.base(), s.end());
34 return s;
37 // trim from both ends
38 static inline std::string &trim(std::string &s) {
39 return ltrim(rtrim(s));
42 }} // namespace Barry::Inplace
44 #endif