-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstringutils.h
280 lines (246 loc) · 6.77 KB
/
stringutils.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-FileCopyrightText: 2013-2024 Technical University of Munich
//
// SPDX-License-Identifier: BSD-3-Clause
//
// SPDX-FileContributor: Sebastian Rettenberger
#ifndef UTILS_STRINGUTILS_H_
#define UTILS_STRINGUTILS_H_
#include <algorithm>
#include <cctype>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
/**
* A collection of useful utility functions
*/
namespace utils {
/**
* A collection of useful string functions based on std::string
*/
class StringUtils {
public:
/**
* Replaces first occurrence of from in str with to
*
* Taken from
* http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string
*/
static auto replace(std::string& str, const std::string& from, const std::string& to) -> bool {
const size_t startPos = str.find(from);
if (startPos == std::string::npos) {
return false;
}
str.replace(startPos, from.length(), to);
return true;
}
/**
* Replaces last occurrence of from in str with to
*/
static auto replaceLast(std::string& str, const std::string& from, const std::string& to)
-> bool {
const size_t startPos = str.rfind(from);
if (startPos == std::string::npos) {
return false;
}
str.replace(startPos, from.length(), to);
return true;
}
static auto startsWith(const std::string& str, const std::string& prefix) -> bool {
return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
}
/**
* Checks a string for specific suffix
*
* Taken from:
* http://stackoverflow.com/questions/20446201/how-to-check-if-string-ends-with-txt
*/
static auto endsWith(const std::string& str, const std::string& suffix) -> bool {
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
static auto padLeft(const std::string& str, std::size_t size, char padchar) -> std::string {
if (str.size() >= size) {
return str;
} else {
std::stringstream stream;
const std::size_t padlen = size - str.size();
for (std::size_t i = 0; i < padlen; ++i) {
stream << padchar;
}
stream << str;
return stream.str();
}
}
static auto padRight(const std::string& str, std::size_t size, char padchar) -> std::string {
if (str.size() >= size) {
return str;
} else {
std::stringstream stream;
stream << str;
const std::size_t padlen = size - str.size();
for (std::size_t i = 0; i < padlen; ++i) {
stream << padchar;
}
return stream.str();
}
}
/**
* Converts arbitrary datatypes (all datatypes which support the << stream
* operator) into std::string
*/
template <typename T>
static auto toString(T&& value) -> std::string {
std::ostringstream ss;
ss << std::forward<T>(value);
return ss.str();
}
/**
* Converts strings to arbitrary datatypes (using the << stream operator)
*
* @param str The string that should be converted
*/
template <typename T>
static auto parseInternal(const std::string& str) -> T {
T result;
std::istringstream(str) >> result;
return result;
}
/**
* Converts strings to arbitrary datatypes
*
* @param str The string that should be converted
* @param advanced True of advanced conversions should be enabled
*/
template <typename T>
static auto parse(const std::string& str) -> T {
// By default the advanced mode is disabled for all datatypes
return parseInternal<T>(str);
}
template <typename T>
static auto parseArray(const std::string& str) -> std::vector<T> {
std::vector<T> elems;
std::istringstream f(str);
std::string s;
while (std::getline(f, s, ':')) {
elems.push_back(parse<T>(s));
}
return elems;
}
/**
* Converts null terminated string to upper case
*/
static void toUpper(char* s) {
for (size_t i = 0; s[i] != '\0'; ++i) {
s[i] = std::toupper(s[i]);
}
}
/**
* Converts std::string to upper case
*/
static void toUpper(std::string& str) {
for (auto& chr : str) {
chr = std::toupper(chr);
}
}
/**
* Converts null terminated string to lower case
*/
static void toLower(char* s) {
for (size_t i = 0; s[i] != '\0'; ++i) {
s[i] = std::tolower(s[i]);
}
}
/**
* Converts std::string to lower case
*/
static void toLower(std::string& str) {
for (auto& chr : str) {
chr = std::tolower(chr);
}
}
/**
* Trims from start
*
* Taken from
* http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
*/
static auto ltrim(std::string& s) -> std::string& {
const auto notSpace = [](unsigned char ch) { return std::isspace(ch) == 0; };
s.erase(s.begin(), std::find_if(s.begin(), s.end(), notSpace));
return s;
}
/**
* Trims from end
*
* Taken from
* http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
*/
static auto rtrim(std::string& s) -> std::string& {
const auto notSpace = [](unsigned char ch) { return std::isspace(ch) == 0; };
s.erase(std::find_if(s.rbegin(), s.rend(), notSpace).base(), s.end());
return s;
}
/**
* Trims from both ends
*
* Taken from
* http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
*/
static auto trim(std::string& s) -> std::string& { return ltrim(rtrim(s)); }
/**
* Join vector
*
* Taken from
* http://dracoater.blogspot.com/2010/03/joining-vector-and-splitting-string-in.html
*/
template <typename T>
static auto join(const std::vector<T>& v, const std::string& token) -> std::string {
std::ostringstream result;
for (auto i = v.begin(); i != v.end(); ++i) {
if (i != v.begin()) {
result << token;
}
result << *i;
}
return result.str();
}
/**
* Split a string
*
* Taken from
* http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
*/
static auto split(const std::string& str, char delim) -> std::vector<std::string> {
std::vector<std::string> elems;
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
};
template <>
inline auto StringUtils::parse(const std::string& str) -> std::string {
return str;
}
template <>
inline auto StringUtils::parse(const std::string& str) -> const char* {
return str.c_str();
}
template <>
inline auto StringUtils::parse(const std::string& str) -> bool {
std::string s = str;
toLower(s);
trim(s);
if (s == "on" || s == "yes" || s == "true") {
return true;
}
if (s == "off" || s == "no" || s == "false") {
return false;
}
return parseInternal<bool>(str);
}
} // namespace utils
#endif // UTILS_STRINGUTILS_H_