-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.h
More file actions
299 lines (256 loc) · 8.85 KB
/
string.h
File metadata and controls
299 lines (256 loc) · 8.85 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
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
#pragma once
#include <algorithm>
#include <cassert>
#include <cctype>
#include <codecvt>
#include <cwctype>
#include <locale>
#include <string>
#include <sstream>
#include <stdarg.h>
#include <ext/core/defines.h>
#if defined(__GNUC__) // linux
#define _Printf_format_string_
#else
#include <sal.h> // _Printf_format_string_
#endif
namespace std {
template <typename CharType>
void string_trim_all(std::basic_string<CharType, std::char_traits<CharType>, std::allocator<CharType>>& text)
{
auto isNotSpace = [](CharType ch)
{
if (ch < 0 || ch > 255)
return true;
if constexpr (std::is_same_v<CharType, wchar_t>)
return !std::iswspace(ch);
else
return !std::isspace(ch);
};
const auto indexFirstNotSpace = std::distance(text.begin(), std::find_if(text.begin(), text.end(), isNotSpace));
const auto indexLastNotSpace = std::distance(text.rbegin(), std::find_if(text.rbegin(), text.rend(), isNotSpace));
text = text.substr(indexFirstNotSpace, text.length() - indexLastNotSpace - indexFirstNotSpace);
}
template <typename CharType>
void string_trim_left(std::basic_string<CharType, std::char_traits<CharType>, std::allocator<CharType>>& text)
{
auto isNotSpace = [](CharType ch)
{
if (ch < 0 || ch > 255)
return true;
if constexpr (std::is_same_v<CharType, wchar_t>)
return !std::iswspace(ch);
else
return !std::isspace(ch);
};
text.erase(text.begin(), std::find_if(text.begin(), text.end(), isNotSpace));
}
template <typename CharType>
auto string_trim_left(std::basic_string_view<CharType, std::char_traits<CharType>>& text)
{
auto isNotSpace = [](CharType ch)
{
if (ch < 0 || ch > 255)
return true;
if constexpr (std::is_same_v<CharType, wchar_t>)
return !std::iswspace(ch);
else
return !std::isspace(ch);
};
return text.substr(std::distance(text.begin(), std::find_if(text.begin(), text.end(), isNotSpace)));
}
template <typename CharType>
void string_trim_right(std::basic_string<CharType, std::char_traits<CharType>, std::allocator<CharType>>& text)
{
auto isNotSpace = [](CharType ch)
{
if (ch < 0 || ch > 255)
return true;
if constexpr (std::is_same_v<CharType, wchar_t>)
return !std::iswspace(ch);
else
return !std::isspace(ch);
};
const auto charsToRemove = std::distance(text.rbegin(), std::find_if(text.rbegin(), text.rend(), isNotSpace));
text.resize(text.size() - charsToRemove);
}
template <typename CharType>
auto string_trim_right(const std::basic_string_view<CharType, std::char_traits<CharType>>& text)
{
auto isNotSpace = [](CharType ch)
{
if (ch < 0 || ch > 255)
return true;
if constexpr (std::is_same_v<CharType, wchar_t>)
return !std::iswspace(ch);
else
return !std::isspace(ch);
};
return text.substr(0, text.size() - std::distance(text.rbegin(), std::find_if(text.rbegin(), text.rend(), isNotSpace)));
}
[[nodiscard]] inline std::wstring widen(const char* str)
{
#if defined(_WIN32) || defined(__CYGWIN__) // windows
const auto length = strlen(str);
std::wstring result;
result.resize(length);
// do not forget about std::locale::global(std::locale("")); for some characters
const auto& facet = use_facet<std::ctype<wchar_t>>(std::wostringstream().getloc());
std::transform(str, str + length, result.begin(), [&facet](const char ch)
{
return facet.widen(ch);
});
return result;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(str);
#endif
}
[[nodiscard]] inline std::wstring widen(const std::string& str)
{
#if defined(_WIN32) || defined(__CYGWIN__) // windows
std::wstring result;
result.reserve(str.size());
// do not forget about std::locale::global(std::locale("")); for some characters
const auto& facet = use_facet<std::ctype<wchar_t>>(std::wostringstream().getloc());
for (char ch : str)
{
result.append(1, facet.widen(ch));
}
return result;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(str);
#endif
}
[[nodiscard]] inline std::string narrow(const wchar_t* str)
{
#if defined(_WIN32) || defined(__CYGWIN__) // windows
const auto length = wcslen(str);
std::string result;
result.resize(length);
// do not forget about std::locale::global(std::locale("")); for some characters
const auto& facet = use_facet<std::ctype<wchar_t>>(std::ostringstream().getloc());
std::transform(str, str + length, result.begin(), [&facet](const wchar_t ch)
{
return facet.narrow(ch, '\0');
});
return result;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(str);
#endif
}
[[nodiscard]] inline std::string narrow(const std::wstring& str)
{
#if defined(_WIN32) || defined(__CYGWIN__) // windows
std::string result;
result.reserve(str.size());
// do not forget about std::locale::global(std::locale("")); for some characters
const auto& facet = use_facet<std::ctype<wchar_t>>(std::ostringstream().getloc());
for (wchar_t ch : str)
{
result.append(1, facet.narrow(ch, '\0'));
}
return result;
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(str);
#endif
}
[[nodiscard]] inline std::string string_sprintf(_Printf_format_string_ const char* format, ...) EXT_THROWS()
{
va_list _ArgList;
va_start(_ArgList, format);
const int size_s = std::vsnprintf(nullptr, 0, format, _ArgList) + 1; // + '\0'
va_end(_ArgList);
if (size_s <= 0) { assert(false); throw std::runtime_error("Error during formatting."); }
const auto size = static_cast<size_t>(size_s);
std::string string(size, {});
va_start(_ArgList, format);
std::vsnprintf(string.data(), size, format, _ArgList);
va_end(_ArgList);
string.pop_back(); // - '\0'
return string;
}
[[nodiscard]] inline std::wstring string_swprintf(_Printf_format_string_ const wchar_t* format, ...) EXT_THROWS()
{
#if defined(__GNUC__) // linux
// swprintf doesn't work properly on linux, we will use string_sprintf
std::wstring formatStr(format);
for (size_t i = 0, length = formatStr.size(); i + 1 < length; ++i) {
if (formatStr[i] == L'%') {
auto& symb = formatStr[++i];
switch (symb) {
case L's':
symb = L'S';
break;
case L'S':
symb = L's';
break;
}
}
}
auto formatBuffer = narrow(formatStr);
va_list _ArgList;
va_start(_ArgList, format);
const int size_s = std::vsnprintf(nullptr, 0, formatBuffer.c_str(), _ArgList) + 1; // + '\0'
va_end(_ArgList);
if (size_s <= 0) { assert(false); throw std::runtime_error("Error during formatting."); }
const auto size = static_cast<size_t>(size_s);
std::string string(size, {});
va_start(_ArgList, format);
std::vsnprintf(string.data(), size, formatBuffer.c_str(), _ArgList);
va_end(_ArgList);
string.pop_back(); // - '\0'
return widen(string);
#else
va_list _ArgList;
va_start(_ArgList, format);
const int size_s = _vswprintf_c_l(nullptr, 0, format, NULL, _ArgList) + 1; // + '\0'
va_end(_ArgList);
if (size_s <= 0) { assert(false); throw std::runtime_error("Error during formatting."); }
const auto size = static_cast<size_t>(size_s);
std::wstring string(size, {});
va_start(_ArgList, format);
_vswprintf_c_l(string.data(), size, format, NULL, _ArgList);
va_end(_ArgList);
string.pop_back(); // - '\0'
return string;
#endif // not linux
}
inline auto& operator<<(std::ostringstream& stream, const wchar_t* text)
{
return stream << narrow(text);
}
inline auto& operator<<(std::wostringstream& stream, const char* text)
{
return stream << widen(text);
}
template <typename StreamCharType, typename CharType>
auto& operator<<(std::basic_ostringstream<StreamCharType>& stream, const std::basic_string<CharType>& string)
{
return stream << string.c_str();
}
template <typename StreamCharType, typename CharType>
auto& operator<<(std::basic_ostringstream<StreamCharType>& stream, const std::basic_string_view<CharType>& string)
{
return stream << string.data();
}
inline auto& operator<<(std::ostream& stream, const wchar_t* text)
{
return stream << narrow(text);
}
inline auto& operator<<(std::wostream& stream, const char* text)
{
return stream << widen(text);
}
inline auto& operator<<(std::ostream& stream, const std::wstring& text)
{
return stream << narrow(text).c_str();
}
inline auto& operator<<(std::wostream& stream, const std::string& text)
{
return stream << widen(text).c_str();
}
} // namespace std