summaryrefslogtreecommitdiff
path: root/lib/compileTimeFormatter.h
blob: 6e8c8132f08e56dd09a486944524879fadce8bab (plain)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef ADHOCUTIL_COMPILE_TIME_FORMATTER_H
#define ADHOCUTIL_COMPILE_TIME_FORMATTER_H

#include <array>
#include <boost/preprocessor/variadic/size.hpp>
#include <cstring>
#include <iostream>
#include <optional>
#include <sstream> // IWYU pragma: export

namespace AdHoc {
	// Template char utils
	template<typename char_type>
	constexpr bool
	isdigit(const char_type & ch)
	{
		return (ch >= '0' && ch <= '9');
	}

	template<typename char_type>
	constexpr bool
	ispositivedigit(const char_type & ch)
	{
		return (ch >= '1' && ch <= '9');
	}

	// Template string utils
	template<const auto S>
	static constexpr auto
	strlen()
	{
		auto off = 0U;
		while (S[off]) {
			++off;
		}
		return off;
	}

	template<typename char_type>
	static constexpr auto
	strlen(const char_type * S)
	{
		auto off = 0U;
		while (S[off]) {
			++off;
		}
		return off;
	}

	template<const auto S, auto n, auto start = 0U, auto L = strlen<S>()>
	static constexpr std::optional<decltype(start)>
	strchr()
	{
		static_assert(start <= L);
		decltype(start) off = start;
		while (off < L && S[off] != n) {
			++off;
		}
		if (off == L) {
			return {};
		}
		return off;
	}

	template<const auto S, auto n, auto start = 0U, auto L = strlen<S>()>
	static constexpr decltype(L)
	strchrnul()
	{
		decltype(start) off = start;
		while (off < L && S[off] != n) {
			++off;
		}
		return off;
	}

	template<const auto S, const auto L> class FormatterDetail;

	/// Template used to apply parameters to a stream.
	template<const auto S, auto L, auto pos, typename stream, typename, auto...> struct StreamWriter {
		/// Write parameters to stream.
		template<typename... Pn>
		static void
		write(stream &, const Pn &...)
		{
			static_assert(!L, "invalid format string/arguments");
		}
	};

	/// Helper to simplify implementations of StreamWriter.
	template<const auto S, auto L, auto pos, typename stream> struct StreamWriterBase {
		/// Continue processing parameters.
		template<typename... Pn>
		static inline void
		next(stream & s, const Pn &... pn)
		{
			FormatterDetail<S, L>::template Parser<stream, pos + 1, Pn...>::run(s, pn...);
		}
	};

#define StreamWriterT(C...) \
	template<const auto S, auto L, auto pos, typename stream, auto... sn> \
	struct StreamWriter<S, L, pos, stream, void, '%', C, sn...> : \
		public StreamWriterBase<S, L, BOOST_PP_VARIADIC_SIZE(C) + pos, stream>

#define StreamWriterTP(P, C...) \
	template<const auto S, auto L, auto pos, typename stream, auto P, auto... sn> \
	struct StreamWriter<S, L, pos, stream, void, '%', C, sn...> : \
		public StreamWriterBase<S, L, BOOST_PP_VARIADIC_SIZE(C) + pos, stream>

	// Default stream writer formatter
	StreamWriterT('?') {
		template<typename P, typename... Pn>
		static inline void
		write(stream & s, const P & p, const Pn &... pn)
		{
			s << p;
			StreamWriter::next(s, pn...);
		}
	};

	// Escaped % stream writer formatter
	StreamWriterT('%') {
		template<typename... Pn>
		static inline void
		write(stream & s, const Pn &... pn)
		{
			s << '%';
			StreamWriter::next(s, pn...);
		}
	};

	template<typename stream, typename char_type>
	static inline void
	appendStream(stream & s, const char_type * p, size_t n)
	{
		s.write(p, n);
	}

	template<typename stream>
	static inline auto
	streamLength(stream & s)
	{
		return s.tellp();
	}

	/**
	 * Compile time string formatter.
	 * @param S the format string.
	 */
	template<const auto S, const auto L> class FormatterDetail {
	private:
		using strlen_t = decltype(strlen<S>());
		template<const auto, auto, auto, typename> friend struct StreamWriterBase;

	public:
		/// The derived charater type of the format string.
		using char_type = typename std::decay<decltype(S[0])>::type;
		/**
		 * Get a string containing the result of formatting.
		 * @param pn the format arguments.
		 * @return the formatted string.
		 */
		template<typename... Pn>
		static inline auto
		get(const Pn &... pn)
		{
			std::basic_stringstream<char_type> s;
			return write(s, pn...).str();
		}
		/**
		 * Get a string containing the result of formatting.
		 * @param pn the format arguments.
		 * @return the formatted string.
		 */
		template<typename... Pn>
		inline auto
		operator()(const Pn &... pn) const
		{
			return get(pn...);
		}

		/**
		 * Write the result of formatting to the given stream.
		 * @param s the stream to write to.
		 * @param pn the format arguments.
		 * @return the stream.
		 */
		template<typename stream, typename... Pn>
		static inline stream &
		write(stream & s, const Pn &... pn)
		{
			return Parser<stream, 0U, Pn...>::run(s, pn...);
		}
		/**
		 * Write the result of formatting to the given stream.
		 * @param s the stream to write to.
		 * @param pn the format arguments.
		 * @return the stream.
		 */
		template<typename stream, typename... Pn>
		inline typename std::enable_if<std::is_base_of_v<std::basic_ostream<char_type>, stream>, stream>::type &
		operator()(stream & s, const Pn &... pn) const
		{
			return write(s, pn...);
		}

	private:
		template<typename stream, auto pos, typename... Pn> struct Parser {
			static inline stream &
			run(stream & s, const Pn &... pn)
			{
				if (pos != L) {
					constexpr auto ph = strchrnul<S, '%', pos, L>();
					if constexpr (ph != pos) {
						appendStream(s, &S[pos], ph - pos);
					}
					if constexpr (ph != L) {
						packAndWrite<ph>(s, pn...);
					}
				}
				return s;
			}
			template<strlen_t ph, strlen_t off = 0U, auto... Pck>
			static inline void
			packAndWrite(stream & s, const Pn &... pn)
			{
				if constexpr (ph + off == L || sizeof...(Pck) == 32) {
					StreamWriter<S, L, ph, stream, void, Pck...>::write(s, pn...);
				}
				else if constexpr (ph + off < L) {
					packAndWrite<ph, off + 1, Pck..., S[ph + off]>(s, pn...);
				}
			}
		};
	};

	// New C++20 implementation
	namespace support {
		template<typename CharT, std::size_t N> class basic_fixed_string : public std::array<CharT, N> {
		public:
			// cppcheck-suppress noExplicitConstructor
			constexpr basic_fixed_string(const CharT (&str)[N + 1])
			{
				for (decltype(N) x = 0; x < N; x++) {
					this->at(x) = str[x];
				}
			}
			constexpr basic_fixed_string(const CharT * str, decltype(N) len)
			{
				for (decltype(N) x = 0; x < len; x++) {
					this->at(x) = str[x];
				}
			}
		};

		template<typename CharT, std::size_t N>
		basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string<CharT, N - 1>;
	}

	template<const support::basic_fixed_string Str> class LiteralFormatter : public FormatterDetail<Str, Str.size()> {
	};

	template<const auto & S, decltype(strlen(S)) L = strlen(S)>
	class Formatter :
		public FormatterDetail<support::basic_fixed_string<typename std::decay<decltype(S[0])>::type, L>(S, L), L> {
	};

#define AdHocFormatter(name, str) using name = ::AdHoc::LiteralFormatter<str>

	template<const support::basic_fixed_string Str, typename... Pn>
	inline auto
	scprintf(const Pn &... pn)
	{
		return FormatterDetail<Str, Str.size()>::get(pn...);
	}

	template<const support::basic_fixed_string Str, typename stream, typename... Pn>
	inline auto &
	scprintf(stream & strm, const Pn &... pn)
	{
		return FormatterDetail<Str, Str.size()>::write(strm, pn...);
	}

	template<const support::basic_fixed_string Str, typename... Pn>
	inline auto &
	cprintf(const Pn &... pn)
	{
		return scprintf<Str>(std::cout, pn...);
	}
}

#include <boost/assert.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/comma_if.hpp>
#include <boost/preprocessor/if.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <iomanip>
#include <type_traits>

namespace AdHoc {
#define BASICCONV(PARAMTYPE, OP, ...) \
	StreamWriterT(__VA_ARGS__) { \
		template<typename... Pn> \
		static inline void \
		write(stream & s, const PARAMTYPE & p, const Pn &... pn) \
		{ \
			OP; \
			s.copyfmt(std::ios(nullptr)); \
			StreamWriter::next(s, pn...); \
		} \
	}

	// Integers (d, i, o, u, x, X)
#define INTCONV(BASE, OP, CONV) \
	BASICCONV(BASE, OP, CONV); \
	BASICCONV(short BASE, OP, 'h', CONV); \
	BASICCONV(long BASE, OP, 'l', CONV); \
	BASICCONV(long long BASE, OP, 'l', 'l', CONV);
	INTCONV(int, s << std::dec << p, 'i');
	INTCONV(int, s << std::dec << p, 'd');
	INTCONV(unsigned int, s << std::oct << p, 'o');
	INTCONV(unsigned int, s << std::dec << p, 'u');
	INTCONV(unsigned int, s << std::nouppercase << std::hex << p, 'x');
	INTCONV(unsigned int, s << std::uppercase << std::hex << p, 'X');
#undef INTCONV

	BASICCONV(intmax_t, s << std::dec << p, 'j', 'd');
	BASICCONV(uintmax_t, s << std::dec << p, 'j', 'u');
	BASICCONV(ssize_t, s << std::dec << p, 'z', 'd');
	BASICCONV(size_t, s << std::dec << p, 'z', 'u');
	BASICCONV(short int, s << std::dec << p, 'h', 'h', 'i'); // char
	BASICCONV(short int, s << std::dec << p, 'h', 'h', 'd'); // char
	BASICCONV(unsigned char, s << std::dec << p, 'h', 'h', 'u');
	BASICCONV(unsigned char, s << std::oct << p, 'h', 'h', 'o');
	BASICCONV(unsigned char, s << std::nouppercase << std::hex << p, 'h', 'h', 'x');
	BASICCONV(unsigned char, s << std::uppercase << std::hex << p, 'h', 'h', 'X');

	// Floating point (a, A, e, E, f, F, g, G)
#define FPCONV(BASE, OP, CONV) \
	BASICCONV(BASE, OP, CONV); \
	BASICCONV(long BASE, OP, 'L', CONV);
	FPCONV(double, s << std::nouppercase << std::hexfloat << p, 'a');
	FPCONV(double, s << std::uppercase << std::hexfloat << p, 'A');
	FPCONV(double, s << std::nouppercase << std::scientific << p, 'e');
	FPCONV(double, s << std::uppercase << std::scientific << p, 'E');
	FPCONV(double, s << std::nouppercase << std::fixed << p, 'f');
	FPCONV(double, s << std::uppercase << std::fixed << p, 'F');
	FPCONV(double, s << std::nouppercase << std::defaultfloat << p, 'g');
	FPCONV(double, s << std::uppercase << std::defaultfloat << p, 'G');
#undef FPCONV

	BASICCONV(std::string_view, s << p, 's');
	BASICCONV(std::wstring_view, s << p, 'l', 's');
	BASICCONV(char, s << p, 'c');
	BASICCONV(wchar_t, s << p, 'l', 'c');
#undef BASICCONV
	StreamWriterT('p') {
		template<typename Obj, typename... Pn>
		static inline void
		write(stream & s, Obj * const ptr, const Pn &... pn)
		{
			s << std::showbase << std::hex << (long unsigned int)ptr;
			s.copyfmt(std::ios(nullptr));
			StreamWriter::next(s, pn...);
		}
		template<typename Ptr, typename... Pn>
		static inline void
		write(stream & s, const Ptr & ptr, const Pn &... pn)
		{
			write(s, ptr.get(), pn...);
		}
	};

	StreamWriterT('m') {
		template<typename... Pn>
		static inline void
		write(stream & s, const Pn &... pn)
		{
			s << strerror(errno);
			s.copyfmt(std::ios(nullptr));
			StreamWriter::next(s, pn...);
		}
	};
	StreamWriterT('n') {
		template<typename... Pn>
		static inline void
		write(stream & s, int * n, const Pn &... pn)
		{
			BOOST_ASSERT_MSG(n, "%n conversion requires non-null parameter");
			*n = streamLength(s);
			s.copyfmt(std::ios(nullptr));
			StreamWriter::next(s, pn...);
		}
	};

	////
	// Width/precision embedded in format string
	template<auto... chs>
	constexpr auto
	decdigits()
	{
		static_assert((isdigit(chs) && ... && true));
		int n = 0;
		(
				[&n](auto ch) {
					n = (n * 10) + (ch - '0');
				}(chs),
				...);
		return n;
	}
#define AUTON(z, n, data) \
	BOOST_PP_COMMA_IF(n) \
	auto BOOST_PP_CAT(data, n)
#define NS(z, n, data) \
	BOOST_PP_COMMA_IF(n) \
	BOOST_PP_CAT(data, n)
#define ISDIGIT(z, n, data) &&isdigit(BOOST_PP_CAT(data, BOOST_PP_ADD(n, 1)))
#define FMTWIDTH(unused, d, data) \
	template<const auto S, auto L, auto pos, typename stream, BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), AUTON, n), auto nn, \
			auto... sn> \
	struct StreamWriter<S, L, pos, stream, \
			typename std::enable_if<ispositivedigit(n0) BOOST_PP_REPEAT(d, ISDIGIT, n) && !isdigit(nn)>::type, '%', \
			BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n), nn, sn...> { \
		template<typename... Pn> \
		static inline void \
		write(stream & s, const Pn &... pn) \
		{ \
			constexpr auto p = decdigits<BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n)>(); \
			s << std::setw(p); \
			StreamWriter<S, L, pos + BOOST_PP_ADD(d, 1), stream, void, '%', nn, sn...>::write(s, pn...); \
		} \
	};
	BOOST_PP_REPEAT(6, FMTWIDTH, void);
#define FMTPRECISION(unused, d, data) \
	template<const auto S, auto L, auto pos, typename stream, BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), AUTON, n), auto nn, \
			auto... sn> \
	struct StreamWriter<S, L, pos, stream, \
			typename std::enable_if<isdigit(n0) BOOST_PP_REPEAT(d, ISDIGIT, n) && !isdigit(nn)>::type, '%', '.', \
			BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n), nn, sn...> { \
		template<typename... Pn> \
		static inline void \
		write(stream & s, const Pn &... pn) \
		{ \
			constexpr auto p = decdigits<BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n)>(); \
			s << std::setprecision(p); \
			StreamWriter<S, L, pos + BOOST_PP_ADD(d, 2), stream, void, '%', nn, sn...>::write(s, pn...); \
		} \
	};
	BOOST_PP_REPEAT(6, FMTPRECISION, void);
#undef AUTON
#undef NS
#undef ISDIGIT
#undef FMTWIDTH

	StreamWriterT('.', '*') {
		template<typename... Pn>
		static inline void
		write(stream & s, int l, const Pn &... pn)
		{
			s << std::setw(l);
			StreamWriter<S, L, pos + 2, stream, void, '%', sn...>::write(s, pn...);
		}
	};
	StreamWriterT('.', '*', 's') {
		template<typename... Pn>
		static inline void
		write(stream & s, int l, const std::string_view & p, const Pn &... pn)
		{
			s << p.substr(0, l);
			s.copyfmt(std::ios(nullptr));
			StreamWriter::next(s, pn...);
		}
	};

		// Flags
#define FLAGCONV(OP, ...) \
	StreamWriterT(__VA_ARGS__) { \
		template<typename... Pn> \
		static inline void \
		write(stream & s, const Pn &... pn) \
		{ \
			OP; \
			StreamWriter<S, L, pos + 1, stream, void, '%', sn...>::write(s, pn...); \
		} \
	};
	FLAGCONV(s << std::showbase, '#');
	FLAGCONV(s << std::setfill('0'), '0');
	FLAGCONV(s << std::left, '-');
	FLAGCONV(s << std::showpos, '+');
	FLAGCONV(s << std::setfill(' '), ' ');
#undef FLAGCONV
}

#endif