aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/mpt/string/utility.hpp
blob: e2238a09f623f8fbeff007fc12112449b0bd2760 (plain) (blame)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */

#ifndef MPT_STRING_UTILITY_HPP
#define MPT_STRING_UTILITY_HPP



#include "mpt/base/detect.hpp"
#include "mpt/base/namespace.hpp"
#include "mpt/detect/mfc.hpp"

#include <string>
#include <vector>

#include <cstddef>

#if MPT_DETECTED_MFC
// cppcheck-suppress missingInclude
#include <afx.h>
#endif // MPT_DETECTED_MFC


namespace mpt {
inline namespace MPT_INLINE_NS {



// string_traits abstract the API of underlying string classes, in particular they allow adopting to CString without having to specialize for CString explicitly

template <typename Tstring>
struct string_traits {

	using string_type = Tstring;
	using size_type = typename string_type::size_type;
	using char_type = typename string_type::value_type;

	static inline std::size_t length(const string_type & str) {
		return str.length();
	}

	static inline void reserve(string_type & str, std::size_t size) {
		str.reserve(size);
	}

	static inline string_type & append(string_type & str, const string_type & a) {
		return str.append(a);
	}
	static inline string_type & append(string_type & str, string_type && a) {
		return str.append(std::move(a));
	}
	static inline string_type & append(string_type & str, std::size_t count, char_type c) {
		return str.append(count, c);
	}

	static inline string_type pad(string_type str, std::size_t left, std::size_t right) {
		str.insert(str.begin(), left, char_type(' '));
		str.insert(str.end(), right, char_type(' '));
		return str;
	}
};

#if MPT_DETECTED_MFC

template <>
struct string_traits<CStringA> {

	using string_type = CStringA;
	using size_type = int;
	using char_type = typename CStringA::XCHAR;

	static inline size_type length(const string_type & str) {
		return str.GetLength();
	}

	static inline void reserve(string_type & str, size_type size) {
		str.Preallocate(size);
	}

	static inline string_type & append(string_type & str, const string_type & a) {
		str += a;
		return str;
	}
	static inline string_type & append(string_type & str, size_type count, char_type c) {
		while (count--) {
			str.AppendChar(c);
		}
		return str;
	}

	static inline string_type pad(const string_type & str, size_type left, size_type right) {
		string_type tmp;
		while (left--) {
			tmp.AppendChar(char_type(' '));
		}
		tmp += str;
		while (right--) {
			tmp.AppendChar(char_type(' '));
		}
		return tmp;
	}
};

template <>
struct string_traits<CStringW> {

	using string_type = CStringW;
	using size_type = int;
	using char_type = typename CStringW::XCHAR;

	static inline size_type length(const string_type & str) {
		return str.GetLength();
	}

	static inline void reserve(string_type & str, size_type size) {
		str.Preallocate(size);
	}

	static inline string_type & append(string_type & str, const string_type & a) {
		str += a;
		return str;
	}
	static inline string_type & append(string_type & str, size_type count, char_type c) {
		while (count--) {
			str.AppendChar(c);
		}
		return str;
	}

	static inline string_type pad(const string_type & str, size_type left, size_type right) {
		string_type tmp;
		while (left--) {
			tmp.AppendChar(char_type(' '));
		}
		tmp += str;
		while (right--) {
			tmp.AppendChar(char_type(' '));
		}
		return tmp;
	}
};

#endif // MPT_DETECTED_MFC


template <typename Tchar>
struct char_constants {
	static inline constexpr Tchar space = ' ';
	static inline constexpr Tchar a = 'a';
	static inline constexpr Tchar z = 'z';
	static inline constexpr Tchar A = 'A';
	static inline constexpr Tchar Z = 'Z';
	static inline constexpr Tchar lf = '\n';
	static inline constexpr Tchar cr = '\r';
	static inline constexpr Tchar tab = '\t';
	static inline constexpr Tchar comma = ',';
};

template <>
struct char_constants<char> {
	static inline constexpr char space = ' ';
	static inline constexpr char a = 'a';
	static inline constexpr char z = 'z';
	static inline constexpr char A = 'A';
	static inline constexpr char Z = 'Z';
	static inline constexpr char lf = '\n';
	static inline constexpr char cr = '\r';
	static inline constexpr char tab = '\t';
	static inline constexpr char comma = ',';
};

#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
template <>
struct char_constants<wchar_t> {
	static inline constexpr wchar_t space = L' ';
	static inline constexpr wchar_t a = L'a';
	static inline constexpr wchar_t z = L'z';
	static inline constexpr wchar_t A = L'A';
	static inline constexpr wchar_t Z = L'Z';
	static inline constexpr wchar_t lf = L'\n';
	static inline constexpr wchar_t cr = L'\r';
	static inline constexpr wchar_t tab = L'\t';
	static inline constexpr wchar_t comma = L',';
};
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR

#if MPT_CXX_AT_LEAST(20)
template <>
struct char_constants<char8_t> {
	static inline constexpr char8_t space = u8' ';
	static inline constexpr char8_t a = u8'a';
	static inline constexpr char8_t z = u8'z';
	static inline constexpr char8_t A = u8'A';
	static inline constexpr char8_t Z = u8'Z';
	static inline constexpr char8_t lf = u8'\n';
	static inline constexpr char8_t cr = u8'\r';
	static inline constexpr char8_t tab = u8'\t';
	static inline constexpr char8_t comma = u8',';
};
#endif

template <>
struct char_constants<char16_t> {
	static inline constexpr char16_t space = u' ';
	static inline constexpr char16_t a = u'a';
	static inline constexpr char16_t z = u'z';
	static inline constexpr char16_t A = u'A';
	static inline constexpr char16_t Z = u'Z';
	static inline constexpr char16_t lf = u'\n';
	static inline constexpr char16_t cr = u'\r';
	static inline constexpr char16_t tab = u'\t';
	static inline constexpr char16_t comma = u',';
};

template <>
struct char_constants<char32_t> {
	static inline constexpr char32_t space = U' ';
	static inline constexpr char32_t a = U'a';
	static inline constexpr char32_t z = U'z';
	static inline constexpr char32_t A = U'A';
	static inline constexpr char32_t Z = U'Z';
	static inline constexpr char32_t lf = U'\n';
	static inline constexpr char32_t cr = U'\r';
	static inline constexpr char32_t tab = U'\t';
	static inline constexpr char32_t comma = U',';
};


template <typename Tchar>
constexpr bool is_any_line_ending(Tchar c) noexcept {
	return (c == char_constants<Tchar>::cr) || (c == char_constants<Tchar>::lf);
}


template <typename Tstring>
inline Tstring default_whitespace() {
	Tstring result;
	result.reserve(4);
	result.push_back(char_constants<typename Tstring::value_type>::space);
	result.push_back(char_constants<typename Tstring::value_type>::lf);
	result.push_back(char_constants<typename Tstring::value_type>::cr);
	result.push_back(char_constants<typename Tstring::value_type>::tab);
	return result;
}


// Remove whitespace at start of string
template <typename Tstring>
inline Tstring trim_left(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
	typename Tstring::size_type pos = str.find_first_not_of(whitespace);
	if (pos != Tstring::npos) {
		str.erase(str.begin(), str.begin() + pos);
	} else if (pos == Tstring::npos && str.length() > 0 && str.find_last_of(whitespace) == str.length() - 1) {
		return Tstring();
	}
	return str;
}

// Remove whitespace at end of string
template <typename Tstring>
inline Tstring trim_right(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
	typename Tstring::size_type pos = str.find_last_not_of(whitespace);
	if (pos != Tstring::npos) {
		str.erase(str.begin() + pos + 1, str.end());
	} else if (pos == Tstring::npos && str.length() > 0 && str.find_first_of(whitespace) == 0) {
		return Tstring();
	}
	return str;
}

// Remove whitespace at start and end of string
template <typename Tstring>
inline Tstring trim(Tstring str, const Tstring & whitespace = default_whitespace<Tstring>()) {
	return trim_right(trim_left(str, whitespace), whitespace);
}


template <typename Tstring, typename Tmatch>
inline bool starts_with(const Tstring & str, const Tmatch & match) {
	return (str.find(typename mpt::make_string_type<Tmatch>::type{match}) == 0);
}

template <typename Tstring, typename Tmatch>
inline bool ends_with(const Tstring & str, const Tmatch & match) {
	return (str.rfind(typename mpt::make_string_type<Tmatch>::type{match}) == (str.length() - typename mpt::make_string_type<Tmatch>::type{match}.length()));
}


template <typename Tstring, typename Treplace>
inline Tstring replace(Tstring str, const Treplace & old_str, const Treplace & new_str) {
	std::size_t pos = 0;
	while ((pos = str.find(typename mpt::make_string_type<Treplace>::type{old_str}, pos)) != Tstring::npos) {
		str.replace(pos, typename mpt::make_string_type<Treplace>::type{old_str}.length(), typename mpt::make_string_type<Treplace>::type{new_str});
		pos += typename mpt::make_string_type<Treplace>::type{new_str}.length();
	}
	return str;
}


template <typename Tstring>
inline Tstring truncate(Tstring str, std::size_t max_len) {
	if (str.length() > max_len) {
		str.resize(max_len);
	}
	return str;
}


template <typename Tchar>
inline constexpr Tchar to_lower_ascii(Tchar c) noexcept {
	if (char_constants<Tchar>::A <= c && c <= char_constants<Tchar>::Z) {
		c += char_constants<Tchar>::a - char_constants<Tchar>::A;
	}
	return c;
}

template <typename Tchar>
inline constexpr Tchar to_upper_ascii(Tchar c) noexcept {
	if (char_constants<Tchar>::a <= c && c <= char_constants<Tchar>::z) {
		c -= char_constants<Tchar>::a - char_constants<Tchar>::A;
	}
	return c;
}


template <typename Tstring>
inline std::vector<Tstring> split(const Tstring & str, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) {
	std::vector<Tstring> vals;
	std::size_t pos = 0;
	while (str.find(sep, pos) != std::string::npos) {
		vals.push_back(str.substr(pos, str.find(sep, pos) - pos));
		pos = str.find(sep, pos) + sep.length();
	}
	if (!vals.empty() || (str.substr(pos).length() > 0)) {
		vals.push_back(str.substr(pos));
	}
	return vals;
}


template <typename Tstring>
inline Tstring join(const std::vector<Tstring> & vals, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) {
	Tstring str;
	for (std::size_t i = 0; i < vals.size(); ++i) {
		if (i > 0) {
			str += sep;
		}
		str += vals[i];
	}
	return str;
}



} // namespace MPT_INLINE_NS
} // namespace mpt



#endif // MPT_STRING_UTILITY_HPP