Repository navigation
Expand file tree
/
Copy pathstring_utils.h
More file actions
189 lines (159 loc) · 3.92 KB
/
Copy pathstring_utils.h
File metadata and controls
189 lines (159 loc) · 3.92 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* \file src/string_utils.h
* \author Jan Milík <milikjan@fit.cvut.cz>
* \date 2013-04-29
*
* \brief
*/
#ifndef STRING_UTILS_GXVJSRP9
#define STRING_UTILS_GXVJSRP9
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cctype>
#include <cstring>
#include <locale>
namespace cppapp {
template<class T>
inline size_t length(T value)
{
return value.size();
}
template<>
inline size_t length<const char *>(const char *value)
{
return strlen(value);
}
/**
* \brief Contains utility methods for manipulating strings.
*/
struct Strings {
/**
* \brief Returns length of a STL string.
*
* \returns length of a STL string
*/
static inline size_t length(const std::string &s)
{
return s.size();
}
/**
* \brief Returns length of a C-string.
*
* \returns length of a C-string
*/
static inline size_t length(const char *s)
{
return strlen(s);
}
/**
* \brief Trim whitespace from the left end of a string.
*/
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;
}
/**
* \brief Trim whitespace from the right end of a string.
*/
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;
}
/**
* \brief Trims whitespace from both ends of a string.
*/
static inline std::string &trim(std::string &s)
{
return ltrim(rtrim(s));
}
/**
* \brief Converts a string to lowercase.
*/
static inline std::string toLower(const std::string &s)
{
std::string result = s;
std::transform(s.begin(), s.end(), result.begin(), ::tolower);
return result;
}
/**
* \brief Converts a string to uppercase.
*/
static inline std::string toUpper(const std::string &s)
{
std::string result = s;
std::transform(s.begin(), s.end(), result.begin(), ::toupper);
return result;
}
/**
* \brief Splits a string into parts by a separator.
*
* \param value the string to be split
* \param separator string to split the \c value by
* \param output pointer to a vector to which the
* resulting string parts will be stored
*
* \code
* std::vector<std::string> output;
* split("a,b,c", ",", &output);
* // vector contents: ["a", "b", "c"]
*
* split("abc", ",", &output);
* // vector contents: ["abc"]
* \endcode
*/
static inline void split(std::string value, std::string separator, std::vector<std::string> *output)
{
size_t pos = 0;
while (pos < value.length()) {
size_t found = value.find(separator, pos);
if (found == std::string::npos) {
output->push_back(value.substr(pos, value.length() - pos));
return;
}
output->push_back(value.substr(pos, found - pos));
pos = found + separator.length();
}
}
/**
* \brief Returns \c true if \c needle occurs within \c haystack.
*
* \param haystack string to be searched for occurences of \c needle
* \param needle string to search for in \c haystack
* \returns \c true if \c needle occurs in \c haystack, \c false otherwise
*/
template<class T>
static inline bool contains(const std::string &haystack, T needle)
{
size_t pos = haystack.find(needle);
return (pos != std::string::npos);
}
/**
* \brief Returns \c true if a string \c value ends with \c suffix.
*
* \param value string whose end will be compared to \c suffix
* \param suffix string to which is compared to the end of \c value
* \returns \c true if the suffix of \c value is equal to \c suffix, \c false otherwise
*/
template<class T>
static inline bool endsWith(const std::string &value, T suffix)
{
int l = length(suffix);
return (value.compare(value.size() - l, l, suffix) == 0);
}
};
} // namespace cppapp
#endif /* end of include guard: STRING_UTILS_GXVJSRP9 */