summaryrefslogtreecommitdiffstats
path: root/cxxformat.hpp
blob: 1dd919ea11e6ac2618c8d124c69436763f75ec84 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
Copyright 2019 Mittendrein Markus

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once
#include <string>
#include <string_view>
#include <optional>
#include <cstring>
#include <stdexcept>
#include <cstdint>

namespace format {

using FormatException = std::invalid_argument;

template<typename To, typename From, typename Enable = void>
struct FormatConvert {};

template<typename To, typename From>
struct ImplicitConversion
{
	static constexpr To convert(From from)
	{
		return from;
	}
};

template<typename From>
struct FormatConvert<std::intmax_t, From, std::enable_if_t<std::is_signed_v<From> && std::is_integral_v<From>>> : ImplicitConversion<std::intmax_t, From> {};

template<typename From>
struct FormatConvert<std::intmax_t, From, std::enable_if_t<std::is_unsigned_v<From> && std::is_integral_v<From> && (sizeof(From) < sizeof(std::intmax_t))>> : ImplicitConversion<std::intmax_t, From> {};

template<typename From>
struct FormatConvert<std::uintmax_t, From, std::enable_if_t<std::is_unsigned_v<From> && std::is_integral_v<From>>> : ImplicitConversion<std::uintmax_t, From> {};

template<typename String>
struct FormatConvert<std::string_view, String, std::enable_if_t<std::is_convertible_v<String, std::string_view>>> : ImplicitConversion<std::string_view, const String &> {};

template<typename From>
struct FormatConvert<double, From, std::enable_if_t<std::is_convertible_v<From, double> && !std::is_integral_v<From>>> : ImplicitConversion<double, const From &> {};

template<typename Pointer>
struct FormatConvert<const void *, Pointer, std::enable_if_t<std::is_pointer_v<Pointer> && !std::is_array_v<std::remove_pointer_t<Pointer>>>> : ImplicitConversion<const void *, const Pointer> {};

template<typename Pointer>
struct FormatConvert<const void *, Pointer, std::enable_if_t<std::is_pointer_v<Pointer> && std::is_array_v<std::remove_pointer_t<Pointer>>>> : ImplicitConversion<const void*, const Pointer> {};

template<typename From, typename Enable = void>
struct AutoConversion
{
	static bool conversion(const From &)
	{
		throw FormatException{""};
	}
};

template<char Conversion, typename From>
struct SimpleAutoConversion
{
	static constexpr char conversion(const From &)
	{
		return Conversion;
	}
};

template<typename From>
struct AutoConversion<From, std::enable_if_t<std::is_integral_v<From> && std::is_unsigned_v<From>>> : SimpleAutoConversion<'u', From> {};

template<typename From>
struct AutoConversion<From, std::enable_if_t<std::is_integral_v<From> && std::is_signed_v<From>>> : SimpleAutoConversion<'d', From> {};

template<typename From>
struct AutoConversion<From, std::enable_if_t<std::is_convertible_v<From, double> && !std::is_integral_v<From>>> : SimpleAutoConversion<'G', From> {};

template<typename From>
struct AutoConversion<From, std::enable_if_t<std::is_convertible_v<From, std::string_view>>> : SimpleAutoConversion<'s', From> {};

template<>
struct AutoConversion<char> : SimpleAutoConversion<'c', const char> {};

namespace detail
{
	template<typename T>
	struct remove_cvref { using type = std::remove_cv_t<std::remove_reference_t<T>>; };

	template<typename T>
	using remove_cvref_t = typename remove_cvref<T>::type;

	template<typename T, bool Ok = false>
	struct StointHelper
	{
		static T convert(const std::string &, std::size_t *, int)
		{
			static_assert(Ok, "stoint: Unsupported type");
			return T{};
		}
	};

	template<typename T, T(converter)(const std::string &, std::size_t *, int)>
	struct StointHelperConverter
	{
		static T convert(const std::string &str, std::size_t *pos, int base)
		{
			return converter(str, pos, base);
		}
	};

	template<>
	struct StointHelper<int> : StointHelperConverter<int, std::stoi> {};
	template<>
	struct StointHelper<long> : StointHelperConverter<long, std::stol> {};
	template<>
	struct StointHelper<long long> : StointHelperConverter<long long, std::stoll> {};
	template<>
	struct StointHelper<unsigned long> : StointHelperConverter<unsigned long, std::stoul> {};
	template<>
	struct StointHelper<unsigned long long> : StointHelperConverter<unsigned long long, std::stoull> {};

	template<typename T>
	T stoint(const std::string &str, std::size_t *pos = nullptr, int base = 10)
	{
		return StointHelper<T>::convert(str, pos, base);
	}

	template<typename... Args>
	std::string strprintf(const char *fmt, Args... args)
	{
		int size = std::snprintf(nullptr, 0, fmt, args...) + 1;
		std::string result;
		result.resize(size);
		std::snprintf(result.data(), size, fmt, args...);
		result.resize(size - 1);
		return result;
	}

	template<typename To, typename From, bool allowFallback, typename Enable = void>
	struct FormatConvertHelper
	{
		static constexpr To convert(const From &)
		{
			static_assert(allowFallback, "Impossible conversion needed");
			throw FormatException{""};
		}
	};

	template<typename T, bool allowFallback>
	struct FormatConvertHelper<T, T, allowFallback>
	{
		static constexpr const T &convert(const T &from)
		{
			return from;
		}
	};

	template<typename To, typename From, bool allowFallback>
	struct FormatConvertHelper<To, From, allowFallback, std::enable_if_t<!std::is_same_v<To, From> && std::is_same_v<remove_cvref_t<decltype(FormatConvert<To, remove_cvref_t<From>>::convert(std::declval<From>()))>, remove_cvref_t<To>>>>
	{
		static constexpr To convert(const From &from)
		{
			return FormatConvert<To, remove_cvref_t<From>>::convert(from);
		}
	};

	template<typename To, bool allowFallback = true, typename From>
	auto convert(const From &from, const char *toName, std::size_t index = 0)
	{
		try
		{
			return FormatConvertHelper<To, From, allowFallback>::convert(from);
		}
		catch (const FormatException &)
		{
			throw FormatException{"Argument#" + std::to_string(index) + " is not convertible to " + std::string{toName}};
		}
	}

	template<typename From, typename Enable = void>
	struct StringOrStringViewHelper
	{
		using type = std::string_view;
	};

	template<typename From>
	struct StringOrStringViewHelper<From, std::enable_if_t<std::is_same_v<std::string, std::decay_t<decltype(FormatConvert<std::string, From>::convert(std::declval<From>()))>>>>
	{
		using type = std::string;
	};

	template<typename From>
	using string_or_string_view = typename StringOrStringViewHelper<From>::type;

	template<bool allowPointer = false, bool allowFallback = true, typename From>
	constexpr char autoConversionSpecifier(From &&from, std::size_t index = 0)
	{
		try
		{
			if constexpr (!allowFallback)
			{
				if constexpr(!std::is_same_v<char, decltype(AutoConversion<remove_cvref_t<From>>::conversion(std::forward<From>(from)))>)
				{
					if constexpr (std::is_pointer_v<From>)
					{
						if constexpr (allowPointer)
						{
							return 'p';
						}
						else
						{
							if constexpr (!allowFallback) static_assert(allowPointer, "%v doesn't allow automatic conversions for pointers; use %V to alsow allow arbitrary pointers");
							else throw FormatException{"Argument#" + std::to_string(index) + " is not applicable for auto conversion with %v, use %V to also allow arbitrary pointers"};
						}
					}
				}
			}
			return AutoConversion<remove_cvref_t<From>>::conversion(std::forward<From>(from));
		}
		catch (const FormatException &)
		{
			if constexpr (std::is_pointer_v<From>)
			{
				if constexpr (allowPointer)
				{
					return 'p';
				}
				else
				{
					if constexpr (!allowFallback) static_assert(allowPointer, "%v doesn't allow automatic conversions for pointers; use %V to alsow allow arbitrary pointers");
					throw FormatException{"Argument#" + std::to_string(index) + " is not applicable for auto conversion with %v, use %V to also allow arbitrary pointers"};
				}
			}
			throw FormatException{"Argument#" + std::to_string(index) + " is not applicable for auto conversion (%v / %V)"};
		}
	}

	std::string format(std::string_view fmt, const std::size_t argumentIndex)
	{
		const auto fmtSize = fmt.size();
		for (std::size_t i = 0; i < fmtSize; ++i)
		{
			if (fmt[i] == '%')
			{
				if (i >= fmtSize - 1)
				{
					throw FormatException{"Incomplete directive"};
				}

				if (fmt[++i] == '%')
				{
					return std::string{fmt.substr(0, i)} + format(fmt.substr(i + 1), argumentIndex);
				}

				throw FormatException{"Not enough arguments passed"};
			}
		}
		return std::string{fmt};
	}

	template<typename Arg, typename... Args>
	std::string format(std::string_view fmt, const std::size_t argumentIndex, Arg &&arg, Args &&... args)
	{
		const auto fmtSize = fmt.size();
		for (std::size_t i = 0; i < fmtSize; ++i)
		{
			auto checkFmtLength = [i, fmtSize]
			{
				if (i >= fmtSize - 1)
				{
					throw FormatException{"Incomplete directive"};
				}
			};
			const auto current = fmt[i];
			if (current == '%')
			{
				const auto directiveStart = i;
				checkFmtLength();
				std::string specification{"%"};

				const auto flag = fmt[++i];
				if (flag == '%') // handle %% here
				{
					return std::string{fmt.substr(0, directiveStart + 1)} + format(fmt.substr(i + 1), argumentIndex, std::forward<Arg>(arg), std::forward<Args>(args)...);
				}
				switch (flag)
				{
					case '#': case '0': case '-': case ' ': case '+':
						specification.push_back(flag);
						checkFmtLength();
						break;
					default:
						--i;
				}

				std::string tempNumber;
				// field width
				while (std::isdigit(fmt[++i]))
				{
					tempNumber.push_back(fmt[i]);
					checkFmtLength();
				}
				--i;
				const auto fieldWidth = tempNumber.empty() ? 0 : stoint<std::size_t>(tempNumber);
				tempNumber.clear();

				std::optional<std::size_t> precision;
				// precision
				if (fmt[++i] == '.')
				{
					checkFmtLength();
					while (std::isdigit(fmt[++i]))
					{
						tempNumber.push_back(fmt[i]);
						checkFmtLength();
					}
					precision = tempNumber.empty() ? 0 : stoint<std::size_t>(tempNumber);
				}
				--i;

				std::string conversion{fmt[++i]};
				if(conversion.front() == 'v')
				{
					conversion = autoConversionSpecifier(std::forward<Arg>(arg), argumentIndex);
				}
				else if(conversion.front() == 'V')
				{
					conversion = autoConversionSpecifier<true>(std::forward<Arg>(arg), argumentIndex);
				}

				auto continueFormatting = [directiveStart, &args..., fmt, &specification, &fieldWidth, &precision, i, conversion, argumentIndex](auto length, auto converted)
				{
					if (fieldWidth) specification += std::to_string(fieldWidth);
					if (precision) specification += '.' + std::to_string(*precision);
					if (length) specification += length;
					specification += conversion;
					return std::string{fmt.substr(0, directiveStart)} + strprintf(specification.c_str(), converted) + format(fmt.substr(i + 1), argumentIndex + 1, std::forward<Args>(args)...);
				};

				switch (conversion.back())
				{
					case 'c':
						return continueFormatting(false, convert<char>(std::forward<Arg>(arg), "character", argumentIndex));

					case 'd': case 'i':
						return continueFormatting('j', convert<std::intmax_t>(std::forward<Arg>(arg), "signed integer", argumentIndex));

					case 'u': case 'o': case 'x': case 'X':
						return continueFormatting('j', convert<std::uintmax_t>(std::forward<Arg>(arg), "unsigned integer", argumentIndex));

					case 's':
					{
						const auto& string = convert<string_or_string_view<remove_cvref_t<Arg>>>(std::forward<Arg>(arg), "string", argumentIndex);
						precision = precision ? std::min(*precision, string.size()) : string.size();
						return continueFormatting(false, string.data());
					}

					case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': case 'a': case 'A':
						return continueFormatting(false, convert<double>(std::forward<Arg>(arg), "double", argumentIndex));

					case 'p':
						return continueFormatting(false, convert<const void *>(std::forward<Arg>(arg), "pointer", argumentIndex));

					default:
						throw FormatException{std::string{"Unknown conversion specifier: \""} + conversion + "\""};
				}
			}
		}

		throw FormatException{std::to_string(argumentIndex - 1) + " placeholders found, but " + std::to_string(argumentIndex + sizeof...(args)) + " arguments passed"};
	}
}

template<bool NoThrow = false, typename String, typename... Args>
std::string format(const String &fmt, Args &&... args)
{
	const auto fmtStr = detail::convert<std::string_view, false>(fmt, "string");
	try
	{
		return detail::format(fmtStr, 1, std::forward<Args>(args)...);
	}
	catch (const FormatException &e)
	{
		const auto &message = std::string{"format(\""} + std::string{fmtStr} + "\", " + std::to_string(sizeof...(args)) + " more arguments...): " + e.what();

		if constexpr (NoThrow) return message;
		else throw FormatException(message);
	}
}

template<size_t i>
struct str_index { static constexpr size_t n = i; };

template<size_t N>
struct str
{
public:
	const char s[N + 1];
	static constexpr size_t size = N;

private:
	template<size_t... indices>
	constexpr str<N + 1> appendCharHelper(const char c, std::index_sequence<indices...>) const
	{
		return str<N + 1>{s[indices]..., c};
	}

	template<size_t... indices>
	static constexpr str<N + 1> prependCharHelper(const char c, const str<N> &s, std::index_sequence<indices...>)
	{
		return str<N + 1>{c, s.s[indices]...};
	}

	template<auto other, size_t... ownIndices, size_t... othersIndices>
	constexpr str<N + sizeof...(othersIndices)> appendStrHelper(std::index_sequence<ownIndices...>, std::index_sequence<othersIndices...>) const
	{
		return str<N + sizeof...(othersIndices)>{s[ownIndices]..., other.s[othersIndices]...};
	}

public:
	template<size_t... indices>
	constexpr str(const char (&s)[N + 1], std::index_sequence<indices...>) : s{s[indices]...}
	{

	}

	constexpr str(const char (&s)[N + 1]) : str{s, std::make_index_sequence<N + 1>()}
	{

	}

	template<typename... Chars>
	constexpr str(Chars... chars) : s{chars..., '\0'}
	{
		static_assert(N == sizeof...(chars), "O.o");
	}

	template<size_t index>
	constexpr char get() const
	{
		static_assert(index < N, "str::get: index out of range");
		return s[index];
	}

	template<size_t offset, size_t... indices>
	constexpr str<sizeof...(indices)> extract(std::index_sequence<indices...>) const
	{
		static_assert(((indices + offset < N) && ...), "str::extract: index out of range");
		return str<sizeof...(indices)>{s[indices + offset]...};
	}

	constexpr str<N + 1> operator+(const char c) const
	{
		return appendCharHelper(c, std::make_index_sequence<N>());
	}

	template<size_t otherN>
	constexpr str<N + otherN> operator+(const str<otherN> &other) const
	{
		return appendStrHelper<other>(std::make_index_sequence<N>(), std::make_index_sequence<otherN>());
	}

	template<size_t _N>
	friend constexpr str<_N + 1> operator+(const char c, const str<_N> &other);
};

template<size_t N>
constexpr str<N + 1> operator+(const char c, const str<N> &other)
{
	return str<N>::prependCharHelper(c, other, std::make_index_sequence<N>());
}

template<size_t N>
str(const char(&)[N]) -> str<N - 1>;

template<typename... Chars>
str(Chars... chars) -> str<sizeof...(Chars)>;

namespace detail
{
	template<size_t i, size_t N>
	constexpr char get(const str<N>& s)
	{
		return s.template get<i>();
	}

	template<size_t N, size_t offset, size_t... indices>
	constexpr str<sizeof...(indices)> extractStr(const str<N> &source, std::index_sequence<indices...>)
	{
		return str<sizeof...(indices)>{get<indices + offset>(source)...};
	}

	template<size_t start, size_t end = std::string::npos, bool endIsIndex = false, size_t N>
	constexpr str<end == std::string::npos ? N - start : (endIsIndex ? end + 1 - start : end)> substr(const str<N> &s)
	{
		static_assert(!endIsIndex || end < N, "substr: end index / substr-length is out of range");
		if constexpr (!endIsIndex && end == 0) return str<0>{};
		else if constexpr (end == std::string::npos) return substr<start, N - 1, true>(s);
		else if constexpr (!endIsIndex) return substr<start, start + end - 1, true>(s);
		else return s.template extract<start>(std::make_index_sequence<end - start + 1>());
	}

	template<typename T, auto s, size_t i = 0, bool allowSign = true, T parsedPart = 0>
	constexpr T strToInt()
	{
		if constexpr (i == s.size) return parsedPart;
		else
		{
			constexpr char c = get<i>(s);
			if constexpr (c == '-')
			{
				static_assert(allowSign, "strToInt: invalid character found");
				static_assert(std::is_signed_v<T>, "strToInt: negative sign found when converting to an unsigned type");
				return -strToInt<T, s, i + 1, false>();
			}
			else if constexpr (c == '+')
			{
				static_assert(allowSign, "strToInt: invalid character found");
				return strToInt<T, s, i + 1, false>();
			}
			else
			{
				static_assert(c >= '0' && c <= '9', "strToInt: invalid character found");
				return strToInt<T, s, i + 1, false, parsedPart * T{10} + T{c - '0'}>();
			}
		}
	}

	template<typename T, T N>
	constexpr size_t decimalLength()
	{
		if constexpr (N < 0) return 1 + decimalLength<T, -N>();
		else if constexpr (N < 10) return 1;
		else return 1 + decimalLength<T, N / 10>();
	}

	template<typename T, T N>
	constexpr str<decimalLength<T, N>()> toStr()
	{
		if constexpr (N < 0) return '-' + toStr<T, -N>();
		else if constexpr (N < 10)
		{
			return str<1>{'0' + N};
		}
		else return toStr<T, N / 10>() + char(N % 10 + '0');
	}

	template<auto fmt, size_t i, size_t argumentIndex>
	std::string format_s()
	{
		if constexpr (i == fmt.size) return "";
		else if constexpr (get<i>(fmt) == '%')
		{
			if constexpr (get<i + 1>(fmt) == '%') return substr<0, i, true>(fmt).s + format_s<substr<i + 2>(fmt), 0, argumentIndex>();
			else
			{
				static_assert(get<i + 1>(fmt) == '%', "Not enough arguments passed");
				throw FormatException{"Not enough arguments passed"};
			}
		}
		else return format_s<fmt, i + 1, argumentIndex>();
	}

	template<auto fmt, size_t i>
	constexpr std::pair<char, size_t> getFlag()
	{
		constexpr char next = get<i>(fmt);
		if constexpr (next == '#' || next == '0' || next == '-' || next == ' ' || next == '+') return {next, i + 1};
		else return {0, i};
	}

	template<auto fmt, size_t i>
	constexpr size_t getNumberEnd()
	{
		if constexpr (std::isdigit(get<i>(fmt))) return getNumberEnd<fmt, i + 1>();
		else return i;
	}

	template<char conversion, size_t argumentIndex, typename Arg>
	constexpr char checkReplaceAutoConversion(Arg &&arg)
	{
		if constexpr (conversion == 'v' || conversion == 'V') return autoConversionSpecifier<conversion == 'V', false>(std::forward<Arg>(arg), argumentIndex);
		else return conversion;
	}

	template<bool havePrecision, auto fmt, size_t iAfterFieldWidth, size_t iAfterPrecision, typename String>
	size_t determineStringPrecision(const String& string)
	{
		if constexpr (havePrecision) return std::min(strToInt<size_t, substr<iAfterFieldWidth + 1, iAfterPrecision - 1, true>(fmt)>(), string.size());
		else return string.size();
	}

	template<auto fmt, char conversion, size_t i, size_t iAfterPrecision, char length>
	constexpr auto makeDirective()
	{
		if constexpr (length) return substr<i, iAfterPrecision - 1, true>(fmt) + length + conversion;
		else return substr<i, iAfterPrecision - 1, true>(fmt) + conversion;
	}

	template<auto fmt, size_t i, size_t argumentIndex, typename Arg, typename... Args>
	std::string format_s(Arg &&arg, Args &&...args)
	{
		static_assert(i < fmt.size, "Too many arguments passed");
		if constexpr (i == fmt.size)
		{
			throw FormatException{std::to_string(argumentIndex - 1) + " placeholders found, but " + std::to_string(argumentIndex + sizeof...(args)) + " arguments passed"};
		}
		else if constexpr (get<i>(fmt) == '%')
		{
			if constexpr (get<i + 1>(fmt) == '%') return substr<0, i, true>(fmt).s + format_s<substr<i + 2>(fmt), 0, argumentIndex>(std::forward<Arg>(arg), std::forward<Args>(args)...);

			constexpr auto flagData = getFlag<fmt, i + 1>();
			constexpr char flag = flagData.first;
			constexpr char iAfterFlag = flagData.second;
			constexpr size_t iAfterFieldWidth = getNumberEnd<fmt, iAfterFlag>();

			constexpr auto havePrecision = get<iAfterFieldWidth>(fmt) == '.';
			constexpr size_t iAfterPrecision = [havePrecision, iAfterFieldWidth]() -> size_t { if constexpr (havePrecision) return getNumberEnd<fmt, iAfterFieldWidth + 1>(); else return iAfterFieldWidth; }();

			constexpr char conversion = checkReplaceAutoConversion<get<iAfterPrecision>(fmt), argumentIndex>(std::forward<Arg>(arg));

			if constexpr (conversion == 'c') return substr<0, i>(fmt).s + strprintf(makeDirective<fmt, conversion, i, iAfterPrecision, 0>().s, convert<char, false>(std::forward<Arg>(arg), "character", argumentIndex)) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			else if constexpr (conversion == 'd' || conversion == 'i') return substr<0, i>(fmt).s + strprintf(makeDirective<fmt, conversion, i, iAfterPrecision, 'j'>().s, convert<std::intmax_t, false>(std::forward<Arg>(arg), "signed integer", argumentIndex)) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			else if constexpr (conversion == 'u' || conversion == 'o' || conversion == 'x' || conversion == 'X') return substr<0, i>(fmt).s + strprintf(makeDirective<fmt, conversion, i, iAfterPrecision, 'j'>().s, convert<std::uintmax_t, false>(std::forward<Arg>(arg), "unsigned integer", argumentIndex)) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			else if constexpr (conversion == 'e' || conversion == 'E' || conversion == 'f' || conversion == 'F' || conversion == 'g' || conversion == 'G' || conversion == 'a' || conversion == 'A') return substr<0, i>(fmt).s + strprintf(makeDirective<fmt, conversion, i, iAfterPrecision, 0>().s, convert<double, false>(std::forward<Arg>(arg), "double", argumentIndex)) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			else if constexpr (conversion == 'p') return substr<0, i>(fmt).s + strprintf(makeDirective<fmt, conversion, i, iAfterPrecision, 0>().s, convert<const void *, false>(std::forward<Arg>(arg), "pointer", argumentIndex)) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			else if constexpr (conversion == 's')
			{
				const auto& string = convert<string_or_string_view<remove_cvref_t<Arg>>, false>(std::forward<Arg>(arg), "string", argumentIndex);
				return substr<0, i>(fmt).s + strprintf((str{"%."}.s + std::to_string(determineStringPrecision<havePrecision, fmt, iAfterFieldWidth, iAfterPrecision>(string)) + 's').c_str(), string.data()) + format_s<substr<iAfterPrecision + 1>(fmt), 0, argumentIndex + 1>(std::forward<Args>(args)...);
			}
			else
			{
				static_assert(fmt.size == 0, "Unknown conversion specifier!");
				throw FormatException{std::string{"Unknown conversion specifier: \""} + conversion + "\""};
			}
		}
		else return format_s<fmt, i + 1, argumentIndex>(std::forward<Arg>(arg), std::forward<Args>(args)...);
	}
}

template<str fmt, typename... Args>
std::string format(Args &&...args)
{
	return detail::format_s<fmt, 0, 1>(std::forward<Args>(args)...);
}
}