File: StringConcatenable.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (277 lines) | stat: -rw-r--r-- 6,779 bytes parent folder | download
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
#pragma once

#include "GrowableArray.h"
#include "String.h"

namespace Death { namespace Containers {
//###==##====#=====--==~--~=~- --- -- -  -  -   -

	namespace Implementation
	{
		template<typename T> struct StringConcatenable;

		template<typename T>
		using StringConcatenableEx = StringConcatenable<std::remove_cv_t<std::remove_reference_t<T>>>;

		template<typename A, typename B> struct ConvertToTypeHelper
		{ typedef A ConvertTo; };
		template<typename T> struct ConvertToTypeHelper<T, String>
		{ typedef String ConvertTo; };
	}

	/** @brief Resulting base class of a deferred string concatenation */
	template<typename Builder, typename T>
	struct StringBuilderBase
	{
	protected:
		/** @brief Returns the resulting string */
		T resolved() const {
			return *static_cast<const Builder*>(this);
		}
	};

	/** @brief Resulting template class of a deferred string concatenation */
	template<typename A, typename B>
	class StringBuilder : public StringBuilderBase<StringBuilder<A, B>,
		typename Implementation::ConvertToTypeHelper<
		typename Implementation::StringConcatenableEx<A>::ConvertTo,
		typename Implementation::StringConcatenableEx<B>::ConvertTo
		>::ConvertTo
	>
	{
	public:
		StringBuilder(A&& a_, B&& b_) : a(Death::forward<A>(a_)), b(Death::forward<B>(b_)) {}

		StringBuilder(StringBuilder&&) = default;
		StringBuilder(const StringBuilder&) = default;
		~StringBuilder() = default;

	private:
		typedef Implementation::StringConcatenable<StringBuilder<A, B>> Concatenable;

		template<typename T>
		T convertTo() const
		{
			const std::size_t length = Concatenable::size(*this);
			T s{NoInit, length};
			auto d = s.data();
			Concatenable::appendTo(*this, d);
			return s;
		}

	public:
		typedef typename Concatenable::ConvertTo ConvertTo;

		operator ConvertTo() const {
			return convertTo<ConvertTo>();
		}

		/** @brief Returns total length in characters */
		std::size_t size() const {
			return Concatenable::size(*this);
		}

		/** @brief First part */
		A a;
		/** @brief Second part */
		B b;

	private:
		StringBuilder& operator=(StringBuilder&&) = delete;
		StringBuilder& operator=(const StringBuilder&) = delete;
	};

	namespace Implementation
	{
		template<>
		struct StringConcatenable<char>
		{
			typedef char type;
			typedef String ConvertTo;

			static std::size_t size(const char) {
				return 1;
			}
			static inline void appendTo(const char c, char*& out) {
				*out++ = c;
			}
		};

		template<>
		struct StringConcatenable<String>
		{
			typedef String type;
			typedef String ConvertTo;

			static std::size_t size(const String& a) {
				return a.size();
			}
			static inline void appendTo(const String& a, char*& out) {
				const std::size_t n = a.size();
				if (n != 0) {
					std::memcpy(out, a.data(), n);
				}
				out += n;
			}
		};

		template<>
		struct StringConcatenable<StringView>
		{
			typedef StringView type;
			typedef String ConvertTo;

			static std::size_t size(StringView a) {
				return a.size();
			}
			static inline void appendTo(StringView a, char*& out) {
				const std::size_t n = a.size();
				if (n != 0) {
					std::memcpy(out, a.data(), n);
				}
				out += n;
			}
		};

		template<>
		struct StringConcatenable<MutableStringView> : StringConcatenable<StringView>
		{
			typedef MutableStringView type;
		};

		template<std::size_t N>
		struct StringConcatenable<const char[N]>
		{
			typedef const char type[N];
			typedef String ConvertTo;

			static std::size_t size(const char a[N]) {
				for (std::size_t i = 0; i < N; i++) {
					if (a[i] == '\0') {
						return i;
					}
				}
				return N;
			}
			static inline void appendTo(const char a[N], char*& out) {
				for (std::size_t i = 0; i < N; i++) {
					if (a[i] == '\0') {
						break;
					}
					*out++ = a[i];
				}
			}
		};

		template<std::size_t N>
		struct StringConcatenable<char[N]> : StringConcatenable<const char[N]>
		{
			typedef char type[N];
		};

		template<>
		struct StringConcatenable<const char*>
		{
			typedef const char* type;
			typedef String ConvertTo;

			static std::size_t size(const char* a) {
				return std::strlen(a);
			}
			static inline void appendTo(const char* a, char*& out) {
				while (*a != '\0') {
					*out++ = *a++;
				}
			}
		};

		template<>
		struct StringConcatenable<char*> : StringConcatenable<const char*>
		{
			typedef char* type;
		};

		template<>
		struct StringConcatenable<ArrayView<const char>>
		{
			typedef ArrayView<const char> type;
			typedef String ConvertTo;

			static std::size_t size(const ArrayView<const char>& a) {
				return a.size();
			}
			static inline void appendTo(const ArrayView<const char>& a, char*& out) {
				const char* d = a.data();
				const char* const end = a.end();
				while (d != end) {
					*out++ = *d++;
				}
			}
		};

		template<typename A, typename B>
		struct StringConcatenable<StringBuilder<A, B>>
		{
			typedef StringBuilder<A, B> type;
			using ConvertTo = typename Implementation::ConvertToTypeHelper<
				typename StringConcatenableEx<A>::ConvertTo,
				typename StringConcatenableEx<B>::ConvertTo
			>::ConvertTo;

			static std::size_t size(const type& c) {
				return StringConcatenableEx<A>::size(c.a) + StringConcatenableEx<B>::size(c.b);
			}

			template<typename T>
			static inline void appendTo(const type& c, T*& out) {
				StringConcatenableEx<A>::appendTo(c.a, out);
				StringConcatenableEx<B>::appendTo(c.b, out);
			}
		};
	}

#ifndef DOXYGEN_GENERATING_OUTPUT
	template<typename A, typename B,
		typename = std::void_t<typename Implementation::StringConcatenableEx<A>::type, typename Implementation::StringConcatenableEx<B>::type>>
		auto operator+(A&& a, B&& b)
	{
		return StringBuilder<A, B>(Death::forward<A>(a), Death::forward<B>(b));
	}

	template<typename A, typename B>
	Array<char>& operator+=(Array<char>& a, const StringBuilder<A, B>& b)
	{
		std::size_t prevLength = a.size();
		std::size_t length = prevLength + Implementation::StringConcatenable<StringBuilder<A, B>>::size(b);
		std::size_t doubleCapacity = 2 * arrayCapacity(a);

		arrayReserve(a, length < doubleCapacity ? doubleCapacity : length);

		char* it = a.data() + prevLength;
		Implementation::StringConcatenable<StringBuilder<A, B>>::appendTo(b, it);

		arrayResize(a, NoInit, length);
		return a;
	}

	template<typename A, typename B>
	String& operator+=(String& a, const StringBuilder<A, B>& b)
	{
		std::size_t prevLength = a.size();
		std::size_t length = prevLength + Implementation::StringConcatenable<StringBuilder<A, B>>::size(b);

		String result{NoInit, length};
		char* it = result.data();

		if (prevLength != 0) {
			std::memcpy(it, a.data(), prevLength);
		}
		it += prevLength;
		Implementation::StringConcatenable<StringBuilder<A, B>>::appendTo(b, it);

		a = move(result);
		return a;
	}
#endif

}}