-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringUtils.hpp
304 lines (219 loc) · 7.99 KB
/
stringUtils.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*!
\file stringUtils.hpp
\date 05.10.2020
\author Ildar Kasimov
This file is a single-header library which was written in C++14 standard.
The library is a bunch of helpers for std::string type.
The usage of the library is pretty simple, just copy this file into your enviornment and
predefine STR_UTILS_IMPLEMENTATION macro before its inclusion like the following
\code
#define STR_UTILS_IMPLEMENTATION
#include "stringUtils.hpp"
\endcode
*/
#pragma once
#include <type_traits>
#include <string>
#include <cctype>
#include <sstream>
#include <array>
///< Library's configs
#define STR_UTILS_DISABLE_EXCEPTIONS 1
#define STR_UTILS_ENABLE_EXPORT 1
#if STR_UTILS_DISABLE_EXCEPTIONS
#define STR_UTILS_NOEXCEPT noexcept
#else
#define STR_UTILS_NOEXCEPT
#endif
#if STR_UTILS_ENABLE_EXPORT
#if defined(_WIN32) || defined(_MSC_VER)
#if !defined(WRENCH_APIENTRY)
#define WRENCH_APIENTRY __cdecl ///< Calling convention for VS
#endif
#if !defined(WRENCH_API)
#if defined(WRENCH_DLLIMPORT)
#define WRENCH_API __declspec(dllimport)
#else
#define WRENCH_API __declspec(dllexport)
#endif
#endif
#elif defined(__GNUC__)
#if !defined(WRENCH_APIENTRY)
#define WRENCH_APIENTRY __attribute__((cdecl)) ///< Calling convention for GNUC
#endif
#if !defined(WRENCH_API)
#if defined(WRENCH_DLLIMPORT)
#define WRENCH_API
#else
#define WRENCH_API __attribute__((visibility("default")))
#endif
#endif
#else /// Unknown platform and compiler
#define WRENCH_API
#endif
#endif
namespace Wrench
{
class StringUtils
{
public:
/*!
\brief The method replaces all occurrences of {what} onto {replacement} value
\param[in] input Input string
\param[in] what A substring that should be replaced
\param[in] replacement A value that should be injected instead of {what} value
\return Transformed string which is the same as input one except occurrences of {what} substrings
*/
WRENCH_API static std::string WRENCH_APIENTRY ReplaceAll(const std::string& input, const std::string& what, const std::string& replacement);
/*!
\brief The method removes all extra whitespaces from a given string
\param[in] str A processing string
\return A processed string where a continuous sequence of whitespaces is replaced with a single one
*/
WRENCH_API static std::string WRENCH_APIENTRY RemoveExtraWhitespaces(const std::string& str) STR_UTILS_NOEXCEPT;
/*!
\brief The method remove all whitespaces from a given string
\param[in] str A processing string
\return A processed string without spaces
*/
WRENCH_API static std::string WRENCH_APIENTRY RemoveAllWhitespaces(const std::string& str) STR_UTILS_NOEXCEPT;
/*!
\brief The method splits a given string into an array of substring which are separated with given delimiters
\param[in] str An original string
\param[in] delims A string contains delimiters
\return An array of substring that are separated with delimiters
*/
WRENCH_API static std::vector<std::string> WRENCH_APIENTRY Split(const std::string& str, const std::string& delims) STR_UTILS_NOEXCEPT;
/*!
\brief The method returns an empty string
\return The method returns an empty string
*/
WRENCH_API static const std::string& WRENCH_APIENTRY GetEmptyStr() STR_UTILS_NOEXCEPT;
/*!
\brief The method checks up whether the given string stars from specified prefix string or not
\return Returns true if first parameter contains the second as a prefix
*/
WRENCH_API static bool WRENCH_APIENTRY StartsWith(const std::string& str, const std::string& prefix) STR_UTILS_NOEXCEPT;
/*!
\brief The method checks up whether the given string ends with specified suffix string or not
\return Returns true if first parameter contains the second as a prefix
*/
WRENCH_API static bool WRENCH_APIENTRY EndsWith(const std::string& str, const std::string& suffix) STR_UTILS_NOEXCEPT;
template <typename... TArgs>
static std::string Format(const std::string& formatStr, TArgs&&... args) STR_UTILS_NOEXCEPT
{
constexpr size_t argsCount = sizeof...(args);
std::array<std::string, argsCount> arguments;
_convertToStringsArray<sizeof...(args), TArgs...>(arguments, std::forward<TArgs>(args)...);
std::string formattedStr = formatStr;
std::string currArgValue;
std::string currArgPattern;
currArgPattern.reserve(5);
std::string::size_type pos = 0;
/// \note replace the following patterns {i}
for (size_t i = 0; i < argsCount; ++i)
{
currArgPattern = "{" + ToString<size_t>(i) + "}";
currArgValue = arguments[i];
while ((pos = formattedStr.find(currArgPattern)) != std::string::npos)
{
formattedStr.replace(pos, currArgPattern.length(), currArgValue);
}
}
return formattedStr;
}
template <typename T>
static std::string ToString(const T& arg) STR_UTILS_NOEXCEPT
{
std::ostringstream stream;
stream << arg;
return stream.str();
}
public:
static const std::string mEmptyStr;
private:
template <uint32_t size>
static void _convertToStringsArray(std::array<std::string, size>& outArray) STR_UTILS_NOEXCEPT {}
template <uint32_t size, typename Head, typename... Tail>
static void _convertToStringsArray(std::array<std::string, size>& outArray, Head&& firstArg, Tail&&... rest) STR_UTILS_NOEXCEPT
{
outArray[size - 1 - sizeof...(Tail)] = ToString(std::forward<Head>(firstArg));
_convertToStringsArray<size, Tail...>(outArray, std::forward<Tail>(rest)...);
}
};
#if defined(STR_UTILS_IMPLEMENTATION)
const std::string StringUtils::mEmptyStr {};
std::string StringUtils::ReplaceAll(const std::string& input, const std::string& what, const std::string& replacement)
{
std::string output = input;
std::string::size_type pos = 0;
std::string::size_type whatStrLength = what.length();
while ((pos = output.find(what)) != std::string::npos)
{
output = output.substr(0, pos) + replacement + output.substr(pos + whatStrLength);
}
return output;
}
std::string StringUtils::RemoveExtraWhitespaces(const std::string& str) STR_UTILS_NOEXCEPT
{
bool isPrevChSpace = false;
std::string processedStr{ str };
processedStr.erase(std::remove_if(processedStr.begin(), processedStr.end(), [&isPrevChSpace](char ch)
{
bool isCurrChSpace = std::isspace(ch);
bool result = isCurrChSpace && isPrevChSpace;
isPrevChSpace = isCurrChSpace;
return result;
}), processedStr.end());
return processedStr;
}
std::string StringUtils::RemoveAllWhitespaces(const std::string& str) STR_UTILS_NOEXCEPT
{
std::string processedStr{ str };
processedStr.erase(std::remove_if(processedStr.begin(), processedStr.end(), [](char ch)
{
return std::isspace(ch);
}), processedStr.end());
return processedStr;
}
std::vector<std::string> StringUtils::Split(const std::string& str, const std::string& delims) STR_UTILS_NOEXCEPT
{
std::string::size_type pos = 0;
std::string::size_type currPos = 0;
std::string currToken;
std::vector<std::string> tokens;
while ((currPos < str.length()) && ((pos = str.find_first_of(delims, currPos)) != std::string::npos))
{
currToken = std::move(str.substr(currPos, pos - currPos));
if (!currToken.empty())
{
tokens.emplace_back(std::move(currToken));
}
currPos = pos + 1;
}
/// insert last token if it wasn't pushed back before
if (currPos != str.length())
{
tokens.emplace_back(std::move(str.substr(currPos, str.length() - currPos)));
}
return std::move(tokens);
}
const std::string& StringUtils::GetEmptyStr() STR_UTILS_NOEXCEPT
{
return mEmptyStr;
}
bool StringUtils::StartsWith(const std::string& str, const std::string& prefix) STR_UTILS_NOEXCEPT
{
return str.rfind(prefix, 0) == 0;
}
bool StringUtils::EndsWith(const std::string& str, const std::string& suffix) STR_UTILS_NOEXCEPT
{
if (suffix.length() > str.length())
{
return false;
}
size_t pos = std::max<size_t>(0, str.length() - suffix.length());
return str.rfind(suffix, pos) == pos;
}
#endif
}