-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrim.hpp
95 lines (76 loc) · 2.92 KB
/
trim.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
The MIT License (MIT)
Copyright (c) 2014 https://github.com/labyrinthofdreams
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef TRIM_HPP
#define TRIM_HPP
#include <algorithm>
#include <cctype>
#include <string>
#include <utility>
namespace mylib {
/**
* @brief A functor that checks if a char value is whitespace
*/
struct is_ws {
/**
* @brief Check if c is whitespace
* @param c Value to check
* @return True if whitespace, otherwise false
*/
bool operator()(const char c) const {
return std::isspace(c);
}
};
template <class InIter, class Cmp = is_ws>
std::pair<InIter, InIter> trim_until(InIter first, InIter last, Cmp ws_cmp = Cmp()) {
auto new_begin = std::find_if_not(first, last, std::cref(ws_cmp));
return {new_begin, last};
}
// Trimmed versions do not modify the argument
template <class Cmp = is_ws>
std::string ltrimmed(const std::string& in) {
auto p = trim_until(in.cbegin(), in.cend(), Cmp());
return {p.first, p.second};
}
template <class Cmp = is_ws>
std::string rtrimmed(const std::string& in) {
auto p = trim_until(in.crbegin(), in.crend(), Cmp());
return {p.second.base(), p.first.base()};
}
template <class Cmp = is_ws>
std::string trimmed(const std::string& in) {
auto p1 = trim_until(in.cbegin(), in.cend(), Cmp());
auto p2 = trim_until(in.crbegin(), in.crend(), Cmp());
return {p1.first, p2.first.base()};
}
// Trim versions modify the argument
// Copied from http://stackoverflow.com/a/217605/110397
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
} // namespace mylib
#endif