Simple SFML GUI  0.2a
trim.hpp
1 /*
2  * trim.hpp
3  * Author: Evan Teran
4  * https://stackoverflow.com/a/217605
5  */
6 
7 #ifndef INCLUDE_TRIM_HPP_
8 #define INCLUDE_TRIM_HPP_
9 
10 #include <algorithm>
11 #include <functional>
12 #include <cctype>
13 #include <locale>
14 
15 // trim from start
16 static inline std::wstring& ltrim(std::wstring& s) {
17  s.erase(
18  s.begin(),
19  std::find_if(
20  s.begin(),
21  s.end(),
22  std::not1(std::ptr_fun<int, int>(std::isspace))
23  )
24  );
25  return s;
26 }
27 
28 // trim from end
29 static inline std::wstring& rtrim(std::wstring& s) {
30  s.erase(
31  std::find_if(
32  s.rbegin(),
33  s.rend(),
34  std::not1(std::ptr_fun<int, int>(std::isspace))
35  ).base(),
36  s.end()
37  );
38  return s;
39 }
40 
41 // trim from both ends
42 static inline std::wstring& trim(std::wstring& s) {
43  return ltrim(rtrim(s));
44 }
45 
46 
47 
48 // trim from start
49 static inline std::string& ltrim(std::string& s) {
50  s.erase(
51  s.begin(),
52  std::find_if(
53  s.begin(),
54  s.end(),
55  std::not1(std::ptr_fun<int, int>(std::isspace))
56  )
57  );
58  return s;
59 }
60 
61 // trim from end
62 static inline std::string& rtrim(std::string& s) {
63  s.erase(
64  std::find_if(
65  s.rbegin(),
66  s.rend(),
67  std::not1(std::ptr_fun<int, int>(std::isspace))
68  ).base(),
69  s.end()
70  );
71  return s;
72 }
73 
74 // trim from both ends
75 static inline std::string& trim(std::string& s) {
76  return ltrim(rtrim(s));
77 }
78 
79 #endif /* INCLUDE_TRIM_HPP_ */