menu: added new Keywords tag to .desktop files
[barry.git] / src / trim.h
blobd30ff05649f6b25607e45df9ca4563c13c284fc3
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>
14 #include <cctype>
16 namespace Barry { namespace Inplace {
18 // Windows CE defines std::isspace(int) as a macro to a function with
19 // two arguments with one prefilled, so a wrapper function is needed.
20 static inline int isSpaceChar(int c) {
21 return std::isspace(c);
24 // trim from start
25 static inline std::string &ltrim(std::string &s) {
26 std::string::iterator end(std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(isSpaceChar))));
27 s.erase(s.begin(), end);
28 return s;
31 // trim from end
32 static inline std::string &rtrim(std::string &s) {
33 std::string::reverse_iterator start(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(isSpaceChar))));
34 s.erase(start.base(), s.end());
35 return s;
38 // trim from both ends
39 static inline std::string &trim(std::string &s) {
40 return ltrim(rtrim(s));
43 }} // namespace Barry::Inplace
45 #endif