Debian Codegraph @

src/mpt/string/utility.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_STRING_UTILITY_HPP
4 #define MPT_STRING_UTILITY_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/detect/mfc.hpp"
11 #include "mpt/string/types.hpp"
12
13 #include <string>
14 #include <vector>
15
16 #include <cstddef>
17
18 #if MPT_DETECTED_MFC
19 // cppcheck-suppress missingInclude
20 #include <afx.h>
21 #endif // MPT_DETECTED_MFC
22
23
24 namespace mpt {
25 inline namespace MPT_INLINE_NS {
26
27
28
29 template <typename Tchar>
30 struct char_constants { };
31
32 template <>
33 struct char_constants<char> {
34 static inline constexpr char null = '\0';
35 static inline constexpr char tab = '\t';
36 static inline constexpr char lf = '\n';
37 static inline constexpr char cr = '\r';
38 static inline constexpr char space = ' ';
39 static inline constexpr char plus = '+';
40 static inline constexpr char comma = ',';
41 static inline constexpr char minus = '-';
42 static inline constexpr char number0 = '0';
43 static inline constexpr char number1 = '1';
44 static inline constexpr char number9 = '9';
45 static inline constexpr char A = 'A';
46 static inline constexpr char Z = 'Z';
47 static inline constexpr char a = 'a';
48 static inline constexpr char z = 'z';
49 };
50
51 #if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
52 template <>
53 struct char_constants<wchar_t> {
54 static inline constexpr wchar_t null = L'\0';
55 static inline constexpr wchar_t tab = L'\t';
56 static inline constexpr wchar_t lf = L'\n';
57 static inline constexpr wchar_t cr = L'\r';
58 static inline constexpr wchar_t space = L' ';
59 static inline constexpr wchar_t plus = L'+';
60 static inline constexpr wchar_t comma = L',';
61 static inline constexpr wchar_t minus = L'-';
62 static inline constexpr wchar_t number0 = L'0';
63 static inline constexpr wchar_t number1 = L'1';
64 static inline constexpr wchar_t number9 = L'9';
65 static inline constexpr wchar_t A = L'A';
66 static inline constexpr wchar_t Z = L'Z';
67 static inline constexpr wchar_t a = L'a';
68 static inline constexpr wchar_t z = L'z';
69 };
70 #endif // !MPT_COMPILER_QUIRK_NO_WCHAR
71
72 #if MPT_CXX_AT_LEAST(20)
73 template <>
74 struct char_constants<char8_t> {
75 static inline constexpr char8_t null = u8'\0';
76 static inline constexpr char8_t tab = u8'\t';
77 static inline constexpr char8_t lf = u8'\n';
78 static inline constexpr char8_t cr = u8'\r';
79 static inline constexpr char8_t space = u8' ';
80 static inline constexpr char8_t plus = u8'+';
81 static inline constexpr char8_t comma = u8',';
82 static inline constexpr char8_t minus = u8'-';
83 static inline constexpr char8_t number0 = u8'0';
84 static inline constexpr char8_t number1 = u8'1';
85 static inline constexpr char8_t number9 = u8'9';
86 static inline constexpr char8_t A = u8'A';
87 static inline constexpr char8_t Z = u8'Z';
88 static inline constexpr char8_t a = u8'a';
89 static inline constexpr char8_t z = u8'z';
90 };
91 #endif
92
93 template <>
94 struct char_constants<char16_t> {
95 static inline constexpr char16_t null = u'\0';
96 static inline constexpr char16_t tab = u'\t';
97 static inline constexpr char16_t lf = u'\n';
98 static inline constexpr char16_t cr = u'\r';
99 static inline constexpr char16_t space = u' ';
100 static inline constexpr char16_t plus = u'+';
101 static inline constexpr char16_t comma = u',';
102 static inline constexpr char16_t minus = u'-';
103 static inline constexpr char16_t number0 = u'0';
104 static inline constexpr char16_t number1 = u'1';
105 static inline constexpr char16_t number9 = u'9';
106 static inline constexpr char16_t A = u'A';
107 static inline constexpr char16_t Z = u'Z';
108 static inline constexpr char16_t a = u'a';
109 static inline constexpr char16_t z = u'z';
110 };
111
112 template <>
113 struct char_constants<char32_t> {
114 static inline constexpr char32_t null = U'\0';
115 static inline constexpr char32_t tab = U'\t';
116 static inline constexpr char32_t lf = U'\n';
117 static inline constexpr char32_t cr = U'\r';
118 static inline constexpr char32_t space = U' ';
119 static inline constexpr char32_t plus = U'+';
120 static inline constexpr char32_t comma = U',';
121 static inline constexpr char32_t minus = U'-';
122 static inline constexpr char32_t number0 = U'0';
123 static inline constexpr char32_t number1 = U'1';
124 static inline constexpr char32_t number9 = U'9';
125 static inline constexpr char32_t A = U'A';
126 static inline constexpr char32_t Z = U'Z';
127 static inline constexpr char32_t a = U'a';
128 static inline constexpr char32_t z = U'z';
129 };
130
131
132 // string_traits abstract the API of underlying string classes, in particular they allow adopting to CString without having to specialize for CString explicitly
133
134 template <typename Tstring>
135 struct string_traits {
136
137 using string_type = Tstring;
138 using size_type = typename string_type::size_type;
139 using char_type = typename string_type::value_type;
140 using unsigned_char_type = typename std::make_unsigned<char_type>::type;
141
142 static inline std::size_t length(const string_type & str) {
143 return str.length();
144 }
145
146 static inline void reserve(string_type & str, std::size_t size) {
147 str.reserve(size);
148 }
149
150 static inline void set_at(string_type & str, size_type pos, char_type c) {
151 str.data()[pos] = c;
152 }
153
154 static inline void append(string_type & str, const string_type & a) {
155 str.append(a);
156 }
157 static inline void append(string_type & str, string_type && a) {
158 str.append(std::move(a));
159 }
160 static inline void append(string_type & str, std::size_t count, char_type c) {
161 str.append(count, c);
162 }
163 static inline void append(string_type & str, char_type c) {
164 str.append(1, c);
165 }
166
167 static inline string_type pad(string_type str, std::size_t left, std::size_t right) {
168 str.insert(str.begin(), left, char_constants<char_type>::space);
169 str.insert(str.end(), right, char_constants<char_type>::space);
170 return str;
171 }
172 };
173
174 #if MPT_DETECTED_MFC
175
176 template <>
177 struct string_traits<CStringA> {
178
179 using string_type = CStringA;
180 using size_type = int;
181 using char_type = typename CStringA::XCHAR;
182 using unsigned_char_type = typename std::make_unsigned<char_type>::type;
183
184 static inline size_type length(const string_type & str) {
185 return str.GetLength();
186 }
187
188 static inline void reserve(string_type & str, size_type size) {
189 str.Preallocate(size);
190 }
191
192 static inline void set_at(string_type & str, size_type pos, char_type c) {
193 str.SetAt(pos, c);
194 }
195
196 static inline void append(string_type & str, const string_type & a) {
197 str.Append(a);
198 }
199 static inline void append(string_type & str, size_type count, char_type c) {
200 while (count--) {
201 str.AppendChar(c);
202 }
203 }
204 static inline void append(string_type & str, char_type c) {
205 str.AppendChar(c);
206 }
207
208 static inline string_type pad(const string_type & str, size_type left, size_type right) {
209 string_type tmp;
210 while (left--) {
211 tmp.AppendChar(char_constants<char_type>::space);
212 }
213 tmp += str;
214 while (right--) {
215 tmp.AppendChar(char_constants<char_type>::space);
216 }
217 return tmp;
218 }
219 };
220
221 template <>
222 struct string_traits<CStringW> {
223
224 using string_type = CStringW;
225 using size_type = int;
226 using char_type = typename CStringW::XCHAR;
227 using unsigned_char_type = typename std::make_unsigned<char_type>::type;
228
229 static inline size_type length(const string_type & str) {
230 return str.GetLength();
231 }
232
233 static inline void reserve(string_type & str, size_type size) {
234 str.Preallocate(size);
235 }
236
237 static inline void set_at(string_type & str, size_type pos, char_type c) {
238 str.SetAt(pos, c);
239 }
240
241 static inline void append(string_type & str, const string_type & a) {
242 str.Append(a);
243 }
244 static inline void append(string_type & str, size_type count, char_type c) {
245 while (count--) {
246 str.AppendChar(c);
247 }
248 }
249 static inline void append(string_type & str, char_type c) {
250 str.AppendChar(c);
251 }
252
253 static inline string_type pad(const string_type & str, size_type left, size_type right) {
254 string_type tmp;
255 while (left--) {
256 tmp.AppendChar(char_constants<char_type>::space);
257 }
258 tmp += str;
259 while (right--) {
260 tmp.AppendChar(char_constants<char_type>::space);
261 }
262 return tmp;
263 }
264 };
265
266 #endif // MPT_DETECTED_MFC
267
268
269 template <typename Tchar>
270 constexpr bool is_any_line_ending(Tchar c) noexcept {
271 return (c == char_constants<Tchar>::cr) || (c == char_constants<Tchar>::lf);
272 }
273
274
275 template <typename Tstring>
276 inline Tstring default_whitespace() {
277 Tstring result;
278 result.reserve(4);
279 result.push_back(char_constants<typename Tstring::value_type>::space);
280 result.push_back(char_constants<typename Tstring::value_type>::lf);
281 result.push_back(char_constants<typename Tstring::value_type>::cr);
282 result.push_back(char_constants<typename Tstring::value_type>::tab);
283 return result;
284 }
285
286
287 // Remove whitespace at start of string
288 template <typename Tstring>
289 inline Tstring trim_left(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
290 typename Tstring::size_type pos = str.find_first_not_of(whitespace);
291 if (pos != Tstring::npos) {
292 str.erase(str.begin(), str.begin() + pos);
293 } else if (pos == Tstring::npos && str.length() > 0 && str.find_last_of(whitespace) == str.length() - 1) {
294 return Tstring();
295 }
296 return str;
297 }
298
299 // Remove whitespace at end of string
300 template <typename Tstring>
301 inline Tstring trim_right(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
302 typename Tstring::size_type pos = str.find_last_not_of(whitespace);
303 if (pos != Tstring::npos) {
304 str.erase(str.begin() + pos + 1, str.end());
305 } else if (pos == Tstring::npos && str.length() > 0 && str.find_first_of(whitespace) == 0) {
306 return Tstring();
307 }
308 return str;
309 }
310
311 // Remove whitespace at start and end of string
312 template <typename Tstring>
313 inline Tstring trim(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
314 return trim_right(trim_left(str, whitespace), whitespace);
315 }
316
317
318 template <typename Tstring, typename Tmatch>
319 inline bool starts_with(const Tstring & str, const Tmatch & match) {
320 return (str.find(typename mpt::make_string_type<Tmatch>::type{match}) == 0);
321 }
322
323 template <typename Tstring, typename Tmatch>
324 inline bool ends_with(const Tstring & str, const Tmatch & match) {
325 return (str.rfind(typename mpt::make_string_type<Tmatch>::type{match}) == (str.length() - typename mpt::make_string_type<Tmatch>::type{match}.length()));
326 }
327
328
329 template <typename Tstring, typename Treplace>
330 inline Tstring replace(Tstring str, const Treplace & old_str, const Treplace & new_str) {
331 std::size_t pos = 0;
332 while ((pos = str.find(typename mpt::make_string_type<Treplace>::type{old_str}, pos)) != Tstring::npos) {
333 str.replace(pos, typename mpt::make_string_type<Treplace>::type{old_str}.length(), typename mpt::make_string_type<Treplace>::type{new_str});
334 pos += typename mpt::make_string_type<Treplace>::type{new_str}.length();
335 }
336 return str;
337 }
338
339
340 template <typename Tstring>
341 inline Tstring truncate(Tstring str, std::size_t max_len) {
342 if (str.length() > max_len) {
343 str.resize(max_len);
344 }
345 return str;
346 }
347
348
349 template <typename Tchar>
350 inline constexpr Tchar to_lower_ascii(Tchar c) noexcept {
351 if (char_constants<Tchar>::A <= c && c <= char_constants<Tchar>::Z) {
352 c += char_constants<Tchar>::a - char_constants<Tchar>::A;
353 }
354 return c;
355 }
356
357 template <typename Tchar>
358 inline constexpr Tchar to_upper_ascii(Tchar c) noexcept {
359 if (char_constants<Tchar>::a <= c && c <= char_constants<Tchar>::z) {
360 c -= char_constants<Tchar>::a - char_constants<Tchar>::A;
361 }
362 return c;
363 }
364
365
366 template <typename Tstring>
367 inline std::vector<Tstring> split(const Tstring & str, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) {
368 std::vector<Tstring> vals;
369 std::size_t pos = 0;
370 while (str.find(sep, pos) != std::string::npos) {
371 vals.push_back(str.substr(pos, str.find(sep, pos) - pos));
372 pos = str.find(sep, pos) + sep.length();
373 }
374 if (!vals.empty() || (str.substr(pos).length() > 0)) {
375 vals.push_back(str.substr(pos));
376 }
377 return vals;
378 }
379
380
381 template <typename Tstring>
382 inline Tstring join(const std::vector<Tstring> & vals, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) {
383 Tstring str;
384 for (std::size_t i = 0; i < vals.size(); ++i) {
385 if (i > 0) {
386 str += sep;
387 }
388 str += vals[i];
389 }
390 return str;
391 }
392
393
394
395 } // namespace MPT_INLINE_NS
396 } // namespace mpt
397
398
399
400 #endif // MPT_STRING_UTILITY_HPP