/
Format.h
736 lines (696 loc) · 22 KB
/
Format.h
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//
// std::string Format(const std::string& format, const Args& ...args);
// が定義されている。printf風に引数を与えれば良い。
// フォーマット指定が正しい場合、printf系関数とできるだけ同じ出力になるよう設計されているが、
// 指定に誤りがある場合はこの限りではない。
//
// 指定と異なる型が与えられている場合、それがキャスト可能な型であれば勝手にキャストされる。
// したがって、例えば%dにlong long intを与えたりするとintにキャストされ4バイト分切り詰められてしまう。
//
// もし厳密な型チェックを行いたいのなら、
// このファイルをincludeする前に
// #define REQUIRE_STRICT_TYPE_MATCHING
// とする。この場合、指定子、修飾子と引数型の厳密な一致が要求されるようになる。
// 例えば、%dにlong long intを与えると!TYPE_MISMATCH!が出力される(%dはint型用なので)。
// %uにintを与えても!TYPE_MISMATCH!である。unsigned intを与えなければならない。
//
// 変換指定はprintf等と同じく
// %[フラグ][最小フィールド幅][.精度][長さ修飾子]変換指定子
// である。
//
// * フラグ
// - : 左寄せ
// + : 常に符号を出力
// # : 基数を表すプレフィックスの出力等
// 0 : 0埋め
//
// * 長さ修飾子
// なし: int、unsigned int、float、double
// hh: char
// h : short
// l : longまたはfloat、double(%fと%lfはprintfにおいては全く同じなので、それに習った)
// ll: long long
// L : long double
// z : size_t
// ? : 整数または浮動少数点全てに対応
//
// * 変換指定子
// d, i : 10進符号付き整数
// u : 10進符号無し整数
// o : 8進符号無し整数
// x, X : 16進符号無し整数(Xは大文字で出力)
// e, E : 指数形式浮動小数点数(Eは大文字で出力)
// f, F : 小数形式浮動小数点数(Fは大文字で出力)
// a, A : 16進浮動小数点(Aは大文字で出力)
// b : bool値をtrueまたはfalseの文字で出力
// c : char一文字
// s : 文字列
// % : 文字の"%"
//
// ある程度printfの機能を再現したが以下のものは用意するのが面倒くさかったので対応していない。誰か作って。
// * %...pによるポインタ値
// * %...nによる出力済み文字数の格納
// * 長さ修飾子j、t
// * %...lcによるワイド文字
//
// その他、他のライブラリのFormat関数で見られた便利機能を多少追加した。
// * 整数、浮動小数点に対して使える万能の長さ修飾子"?"。
// 長さ修飾子指定が面倒くさい場合、%?dとか%?fとすると勝手に適切な型として出力する。boolにも対応している。
// * %sの文字列をstd::stringで与える機能。C++ならconst char*よりはこちらが標準なので。
// * %bでbool値をtrueまたはfalseの文字列で出力する。
//
// 既知の問題
// * %.0eのように指数表示に対して精度0を指定したとき、小数点以下が表示されてしまう。
// std::setprecision(0)が何故かstd::scientificに対して働いていないらしい。
// これが標準ライブラリの仕様なのか、それともバグなのかは不明。
//
#ifndef CUF_FORMAT_H
#define CUF_FORMAT_H
#include <sstream>
#include <cstddef>
#include <cctype>
#include <type_traits>
#include <exception>
#include <ADAPT/CUF/Template.h>
#include <ADAPT/CUF/Function.h>
namespace adapt
{
inline namespace cuf
{
namespace detail
{
inline char Parse(std::ostream& str, std::string::const_iterator& it, const std::string::const_iterator& itend)
{
while (it != itend)
{
if (*it == '0') str.fill('0'), str.setf(std::ios::internal);
else if (*it == '+') str.setf(std::ios::showpos);
else if (*it == '-') str.setf(std::ios::left);
else if (*it == '#') str.setf(std::ios::showpoint | std::ios::showbase);
else break;
++it;
}
int digit = 0;
while (it != itend && std::isdigit(*it))
{
digit = 10 * digit + *it - '0';
++it;
}
if (digit != 0) str.width(digit);
if (it != itend && *it == '.')
{
++it;
int dec = 0;
while (it != itend && std::isdigit(*it))
{
dec = 10 * dec + *it - '0';
++it;
}
if (dec >= 0) str.precision(dec);
}
char mod = 0;
if (it != itend)
{
switch (*it)
{
case 'z':
case 'L':
case '?': mod = *it++; break;
case 'l':
if (*(++it) == 'l') mod = 'm', ++it;
else mod = 'l';
break;
case 'h':
if (*(++it) == 'h') mod = 'g', ++it;
else mod = 'h';
break;
}
}
return mod;
}
#ifdef REQUIRE_STRICT_TYPE_MATCHING
//厳密な型一致を要求する。
//指定子と型が一致しない場合例外を投げる。
template <class Type, class Value, bool IsMatched = std::is_same<Type, Value>::value>
struct Write_impl
{
static void f(std::ostream& str, const Value& v) { str << v; }
};
template <>
struct Write_impl<double, float, false>
{
//doubleの場合、printfの仕様に準拠するため、
//floatからのキャストは常に許されなければならない。
static void f(std::ostream& str, double v) { str << v; }
};
#else
//型の不一致を許し、キャスト可能な場合はstatic_castする。
template <class Type, class Value, bool IsConvertible = std::is_convertible<Value, Type>::value>
struct Write_impl { static void f(std::ostream& str, const Value& v) { str << static_cast<Type>(v); } };
//boolのときだけは、阿呆なmsvc2015がキャスト時に警告を発しやがるので、特殊化して回避する。
//msvc2017以降は発さないよう修正されたらしい。
template <class Value>
struct Write_impl<bool, Value, true> { static void f(std::ostream& str, const Value& v) { str << (v != 0); } };
#endif
template <class Type, class Value>
struct Write_impl<Type, Value, false> { static void f(std::ostream&, const Value&) { throw std::exception(); } };
template <class Type, class Value>
void Write(std::ostream& str, const Value& v)
{
Write_impl<Type, Value>::f(str, v);
}
template <class Value, bool IsArithmetic = std::is_arithmetic<Value>::value, bool IsIntegral = std::is_integral<Value>::value, bool IsSigned = std::is_signed<Value>::value>
struct WriteAnyInt_impl
{
//正しくない動作。
static void f(std::ostream&, Value) { throw std::exception(); }
};
template <class Value>
struct WriteAnyInt_impl<Value, true, true, true>
{
//正しい動作。
static void f(std::ostream& str, Value v) { str << v; }
};
template <>
struct WriteAnyInt_impl<signed char, true, true, true>
{
//整数かつsignedであるような正しい動作だが、
//charなので文字出力にならないよう特殊化する。
static void f(std::ostream& str, signed char v) { str << +v; }
};
template <>
struct WriteAnyInt_impl<char, true, true, true>
{
//整数かつsignedであるような正しい動作だが、
//charなので文字出力にならないよう特殊化する。
//charとsigned charは別扱いなので特殊化も分離する。
//charは環境によってはunsignedである場合もあるが、
//その場合はIsSignedがfalseになるので型の不一致扱いとなり、この関数は呼ばれない。
static void f(std::ostream& str, char v) { str << +v; }
};
template <>
struct WriteAnyInt_impl<bool, true, true, false>
{
//boolはsignedでもunsignedでもないが、整数として扱うため特殊化する。
static void f(std::ostream& str, bool v) { str << v; }
};
#ifndef REQUIRE_STRICT_TYPE_MATCHING
template <class Value>
struct WriteAnyInt_impl<Value, true, true, false>
{
//整数だがsignedではない
static void f(std::ostream& str, Value v) { str << +static_cast<std::make_signed_t<Value>>(v); }
};
template <class Value, bool IsSigned>
struct WriteAnyInt_impl<Value, true, false, IsSigned>
{
//signedだが非整数
static void f(std::ostream& str, Value v) { str << static_cast<long long>(v); }
};
#endif
template <class Value>
void WriteAnyInt(std::ostream& str, Value v)
{
WriteAnyInt_impl<Value>::f(str, v);
}
template <class Value, bool IsArithmetic = std::is_arithmetic<Value>::value, bool IsIntegral = std::is_integral<Value>::value, bool IsUnsigned = std::is_unsigned<Value>::value>
struct WriteAnyUnsignedInt_impl
{
//正しくない動作。
static void f(std::ostream&, Value) { throw std::exception(); }
};
template <class Value>
struct WriteAnyUnsignedInt_impl<Value, true, true, true>
{
//正しい動作。
static void f(std::ostream& str, Value v) { str << v; }
};
template <>
struct WriteAnyUnsignedInt_impl<unsigned char, true, true, true>
{
//整数かつsignedであるような正しい動作だが、
//unsigned charなので文字出力にならないよう特殊化する。
static void f(std::ostream& str, unsigned char v) { str << (unsigned int)v; }
};
template <>
struct WriteAnyUnsignedInt_impl<char, true, true, true>
{
//整数かつunsignedであるような正しい動作だが、
//charなので文字出力にならないよう特殊化する。
//charがsignedかunsignedかは環境依存なので、場合によってはこれが必要になることがある。
static void f(std::ostream& str, char v) { str << +v; }
};
template <>
struct WriteAnyUnsignedInt_impl<bool, true, true, false>
{
//boolはsignedでもunsignedでもないが、整数として扱うため特殊化する。
static void f(std::ostream& str, bool v) { str << v; }
};
#ifndef REQUIRE_STRICT_TYPE_MATCHING
template <class Value>
struct WriteAnyUnsignedInt_impl<Value, true, true, false>
{
//整数だがunsignedではない
static void f(std::ostream& str, Value v) { str << +static_cast<std::make_unsigned_t<Value>>(v); }
};
template <class Value, bool IsUnsigned>
struct WriteAnyUnsignedInt_impl<Value, true, false, IsUnsigned>
{
//非整数、必然的にunsignedではない。
static void f(std::ostream& str, Value v) { str << static_cast<unsigned long long>(v); }
};
#endif
template <class Value>
void WriteAnyUnsignedInt(std::ostream& str, Value v)
{
WriteAnyUnsignedInt_impl<Value>::f(str, v);
}
template <class Value, bool IsArithmetic = std::is_arithmetic<Value>::value, bool IsFloat = std::is_floating_point<Value>::value>
struct WriteAnyFloat_impl
{
static void f(std::ostream&, Value) { throw std::exception(); }
};
template <class Value>
struct WriteAnyFloat_impl<Value, true, true>
{
static void f(std::ostream& str, Value v) { str << v; }
};
#ifndef REQUIRE_STRICT_TYPE_MATCHING
template <class Value>
struct WriteAnyFloat_impl<Value, true, false>
{
//ゆるい型一致のときは整数型からもdoubleにキャストされる。
static void f(std::ostream& str, Value v) { str << static_cast<double>(v); }
};
#endif
template <class Value>
void WriteAnyFloat(std::ostream& str, Value v)
{
WriteAnyFloat_impl<Value>::f(str, v);
}
inline void WriteAnyString(std::ostream& str, const std::string& v)
{
str << v;
}
inline void WriteAnyString(std::ostream& str, const char* v)
{
str << v;
}
inline void WriteAnyString(std::ostream&, ...)
{
throw std::exception();
}
template <class Value>
void WriteInt(std::ostringstream& str, const Value& arg, char mod)
{
switch (mod)
{
case 0: Write<int>(str, arg); break;
case 'l': Write<long>(str, arg); break;
case 'm': Write<long long>(str, arg); break;
case 'h': Write<short>(str, arg); break;
case 'g': Write<char>(str, arg); break;
case '?': WriteAnyInt(str, arg); break;
default: throw std::exception(); break;
}
}
template <class Value>
void WriteUnsignedInt(std::ostringstream& str, const Value& arg, char mod)
{
switch (mod)
{
case 'z': Write<std::size_t>(str, arg); break;
case 'm': Write<unsigned long long>(str, arg); break;
case 'l': Write<unsigned long>(str, arg); break;
case 0: Write<unsigned int>(str, arg); break;
case 'h': Write<unsigned short>(str, arg); break;
case 'g': Write<unsigned char>(str, arg); break;
case '?': WriteAnyUnsignedInt(str, arg); break;
default: throw std::exception(); break;
}
}
template <class Value>
void WriteFloat(std::ostringstream& str, const Value& arg, char mod)
{
switch (mod)
{
case 0:
case 'l': Write<double>(str, arg); break;
case 'L': Write<long double>(str, arg); break;
case '?': WriteAnyFloat(str, arg); break;
default: throw std::exception(); break;
}
}
template <class Value>
void AddArg(std::string& result, std::string::const_iterator& it, const std::string::const_iterator& itend, const Value& arg)
{
std::ostringstream str;
try
{
char mod = 0;
mod = Parse(str, it, itend);
if (it != itend)
{
char type = *it++;
switch (type)
{
case 'd':
case 'i':
str << std::dec;
WriteInt(str, arg, mod);
break;
case 'u':
str << std::dec;
WriteUnsignedInt(str, arg, mod);
break;
case 'o':
str << std::oct;
WriteUnsignedInt(str, arg, mod);
break;
case 'x':
str << std::hex;
WriteUnsignedInt(str, arg, mod);
break;
case 'X':
str << std::hex << std::uppercase;
WriteUnsignedInt(str, arg, mod);
break;
case 'f':
str << std::fixed;
WriteFloat(str, arg, mod);
break;
case 'e':
str << std::scientific;
WriteFloat(str, arg, mod);
break;
case 'E':
str << std::scientific << std::uppercase;
WriteFloat(str, arg, mod);
break;
case 'g':
str << std::defaultfloat;
WriteFloat(str, arg, mod);
break;
case 'G':
str << std::defaultfloat << std::uppercase;
WriteFloat(str, arg, mod);
break;
case 'a':
str << std::hexfloat;
WriteFloat(str, arg, mod);
break;
case 'A':
str << std::hexfloat << std::uppercase;
WriteFloat(str, arg, mod);
break;
case 's':
WriteAnyString(str, arg);
break;
case 'c':
Write<char>(str, arg);
break;
case 'b':
str << std::boolalpha;
Write<bool>(str, arg);
break;
default:
str << type;
}
}
}
catch (const std::exception&)
{
str << "!TYPE_MISMATCH!";
}
result += str.str();
}
inline void AddArg(std::string& result, std::string::const_iterator& it, const std::string::const_iterator& itend)
{
std::ostringstream str;
//char mod = 0;
/*mod = */Parse(str, it, itend);
if (it != itend)
{
str << "!NO_ARGUMENTS!";
++it;
}
result += str.str();
}
inline void Format_impl(std::string& result,
std::string::const_iterator& it, std::string::const_iterator& itend)
{
while (it != itend)
{
switch (*it)
{
case '%':
++it;
if (*it != '%')
{
if (it != itend)
AddArg(result, it, itend);
break;
}
[[fallthrough]];
default:
result += *it;
++it;
}
}
}
template <class Value, class ...Values>
void Format_impl(std::string& result,
std::string::const_iterator& it, std::string::const_iterator& itend,
const Value& value, const Values& ...values)
{
while (it != itend)
{
switch (*it)
{
case '%':
++it;
if (*it != '%')
{
if (it != itend)
AddArg(result, it, itend, value);
return Format_impl(result, it, itend, values...);
}
[[fallthrough]];
default:
result += *it;
++it;
}
}
}
}
template <class ...Values>
std::string Format(const std::string& format, const Values& ...values)
{
std::string::const_iterator it = format.begin();
std::string::const_iterator itend = format.end();
std::string result;
detail::Format_impl(result, it, itend, values...);
return result;
}
namespace detail
{
namespace print
{
constexpr size_t GetCharLen(int del)
{
return (char)(del >> 8) == '\0' ? 1 : (char)(del >> 16) == '\0' ? 2 : (char)(del >> 24) == '\0' ? 3 : 4;
}
template <int ...Char>
struct Character
{
template <size_t N>
static constexpr int GetChar() { return std::get<N>(std::make_tuple(Char...)); }
static constexpr auto Get()
{
return CatArray(Get_impl<Char>()...);
}
private:
template <int C>
static constexpr auto Get_impl()
{
return Get_impl<C>(std::make_index_sequence<GetCharLen(C)>());
}
template <int C, size_t ...Indices>
static constexpr auto Get_impl(std::index_sequence<Indices...>)
{
return std::array<char, GetCharLen(C)>{ char(C >> (8 * (GetCharLen(C) - Indices - 1)))... };
}
};
template <>
struct Character<>
{
static constexpr auto Get()
{
return std::array<char, 0>();
}
};
template <int ...Del> struct Delimiter : public Character<Del...> {};
template <int ...E> struct End : public Character<E...> {};
template <int B>
struct Flush
{};
}
template <class Type_>
struct GetFmtSpec_ { static constexpr void apply() {} };
template <class Type>
struct GetFmtSpec_<Type*> { static constexpr auto apply() { return std::array<char, 2>{ '%', 'p' }; } };
#define GET_FMT_SPEC(TYPE, ...)\
template <> constexpr auto GetFmtSpec<TYPE>() { return MakeArray(__VA_ARGS__); }
template <class Type>
constexpr auto GetFmtSpec() { return GetFmtSpec_<Type>::apply(); }
GET_FMT_SPEC(float, '%', 'f');
GET_FMT_SPEC(double, '%', 'l', 'f');
GET_FMT_SPEC(long double, '%', 'l', 'f');
GET_FMT_SPEC(char, '%', 'c');
GET_FMT_SPEC(short, '%', 'h', 'd');
GET_FMT_SPEC(int, '%', 'd');
GET_FMT_SPEC(long, '%', 'l', 'd');
GET_FMT_SPEC(long long, '%', 'l', 'l', 'd');
GET_FMT_SPEC(unsigned short, '%', 'h', 'u');
GET_FMT_SPEC(unsigned int, '%', 'u');
GET_FMT_SPEC(unsigned long, '%', 'l', 'u');
GET_FMT_SPEC(unsigned long long, '%', 'l', 'l', 'u');
GET_FMT_SPEC(const char*, '%', 's');
GET_FMT_SPEC(char*, '%', 's');
GET_FMT_SPEC(std::string, '%', 's');
#undef GET_FMT_SPEC
template <class ...Args>
constexpr auto GetOptions();
template <class Args>
struct GetOptions_impl;
template <>
struct GetOptions_impl<TypeList<>>
{
static constexpr auto apply()
{
return std::make_tuple(print::Delimiter<' '>(), print::End<'\n'>(), FalseType(), IndexConstant<0>());
}
};
template <class Head, class ...Args>
struct GetOptions_impl<TypeList<Head, Args...>>
{
static constexpr auto apply()
{
constexpr auto t = GetOptions_impl<TypeList<Args...>>::apply();
return std::make_tuple(std::get<0>(t), std::get<1>(t), std::get<2>(t), std::get<3>(t) + IndexConstant<1>());
}
};
template <int ...Int, class ...Args>
struct GetOptions_impl<TypeList<print::Delimiter<Int...>, Args...>>
{
static constexpr auto apply()
{
constexpr auto t = GetOptions_impl<TypeList<Args...>>::apply();
return std::make_tuple(print::Delimiter<Int...>(), std::get<1>(t), std::get<2>(t), std::get<3>(t));
}
};
template <int ...Int, class ...Args>
struct GetOptions_impl<TypeList<print::End<Int...>, Args...>>
{
static constexpr auto apply()
{
constexpr auto t = GetOptions_impl<TypeList<Args...>>::apply();
return std::make_tuple(std::get<0>(t), print::End<Int...>(), std::get<2>(t), std::get<3>(t));
}
};
template <bool B, class ...Args>
struct GetOptions_impl<TypeList<print::Flush<B>, Args...>>
{
static constexpr auto apply()
{
constexpr auto t = GetOptions_impl<TypeList<Args...>>::apply();
return std::make_tuple(std::get<0>(t), std::get<1>(t), BoolConstant<B>(), std::get<3>(t));
}
};
template <class ...Args>
constexpr auto GetOptions()
{
return GetOptions_impl<TypeList<Args...>>::apply();
}
template <class F>
inline void Flush(FILE* fp) { fflush(fp); }
template <>
inline void Flush<FalseType>(FILE*) {}
template <class F>
inline void Flush(std::ostream& ost) { ost << std::flush; }
template <>
inline void Flush<FalseType>(std::ostream&) {}
template <class Del, class End>
constexpr auto MakeFormatStr_rec()
{
return CatArray(End::Get(), std::array<char, 1>{ '\0' });
}
template <class Del, class End, class Head, class ...Args, std::enable_if_t<!std::is_void<decltype(GetFmtSpec<Head>())>::value, std::nullptr_t> = nullptr>
constexpr auto MakeFormatStr_rec()
{
return CatArray(Del::Get(), GetFmtSpec<Head>(), MakeFormatStr_rec<Del, End, Args...>());
}
template <class Del, class End, class Args>
struct MakeFormatStr;
template <class Del, class End, class Head, class ...Args>
struct MakeFormatStr<Del, End, TypeList<Head, Args...>>
{
static constexpr auto apply()
{
return CatArray(GetFmtSpec<Head>(), MakeFormatStr_rec<Del, End, Args...>());
}
};
struct PrintToStream
{
template <class Del, class End, class Fls, class Head>
void operator()(std::ostream& ost, Del, End, Fls, Head&& head) const
{
ost << std::forward<Head>(head) << CatArray(End::Get(), std::array<char, 1>{ '\0' }).data();
Flush<Fls>(ost);
}
template <class Del, class End, class Fls, class Head, class ...Args, std::enable_if_t<sizeof...(Args) != 0, std::nullptr_t> = nullptr>
void operator()(std::ostream& ost, Del d, End e, Fls f, Head&& head, Args&& ...args) const
{
ost << std::forward<Head>(head) << CatArray(Del::Get(), std::array<char, 1>{ '\0' }).data();
return (*this)(ost, d, e, f, std::forward<Args>(args)...);
}
};
template <class Type, std::enable_if_t<!std::is_same<std::decay_t<Type>, std::string>::value, std::nullptr_t> = nullptr>
Type&& ConvStringToCharPtr(Type&& v) { return std::forward<Type>(v); }
template <class Type, std::enable_if_t<std::is_same<std::decay_t<Type>, std::string>::value, std::nullptr_t> = nullptr>
const char* ConvStringToCharPtr(Type&& v) { return v.c_str(); }
}
namespace print
{
template <int ...N>
constexpr detail::print::Delimiter<N...> delim() { return detail::print::Delimiter<N...>(); };
template <int ...N>
constexpr detail::print::End<N...> end() { return detail::print::End<N...>(); };
template <bool B>
constexpr detail::print::Flush<B> flush() { return detail::print::Flush<B>(); };
}
template <class ...Args>
void Print(FILE* fp, Args&& ...args)
{
constexpr auto t = detail::GetOptions<std::decay_t<Args>...>();
constexpr auto d = std::get<0>(t);
constexpr auto e = std::get<1>(t);
constexpr auto f = std::get<2>(t);
constexpr size_t n = std::get<3>(t).value;
constexpr auto fmt = detail::MakeFormatStr<decltype(d), decltype(e), GetFrontTypesT<n, std::decay_t<Args>...>>::apply();
Apply(&fprintf, std::tuple_cat(std::make_tuple(fp), std::make_tuple(fmt.data()), GetFrontArgs<n>(detail::ConvStringToCharPtr(std::forward<Args>(args))...)));
detail::Flush<decltype(f)>(fp);
}
template <class ...Args>
void Print(std::ostream& ost, Args&& ...args)
{
constexpr auto t = detail::GetOptions<std::decay_t<Args>...>();
constexpr auto d = std::get<0>(t);
constexpr auto e = std::get<1>(t);
constexpr auto f = std::get<2>(t).value;
constexpr auto n = std::get<3>(t).value;
Apply(detail::PrintToStream(), std::tuple_cat(std::forward_as_tuple(ost, d, e, f), GetFrontArgs<n>(std::forward<Args>(args)...)));
}
}
}
#endif