-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPrintln.cpp
More file actions
685 lines (542 loc) · 20 KB
/
Println.cpp
File metadata and controls
685 lines (542 loc) · 20 KB
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
// =====================================================================================
// Println.cpp // std::print // std::println
// =====================================================================================
module;
#include <cstdint>
module modern_cpp:println;
// defines for custom formatting
// #define StdFormatter_01_Basic_Formatter_API
// #define StdFormatter_02_Parsing_Format_String
// #define StdFormatter_03_Delegating_Formatting_to_Standard_Formatters
// #define StdFormatter_04_Inheriting_From_Standard_Formatters
// #define StdFormatter_05_Using_Standard_Formatters_for_Strings
// #define StdFormatter_06_Using_Standard_Formatters_for_StdVector
// #define StdFormatter_07_Custom_Parsing_01
// #define StdFormatter_07_Custom_Parsing_02
// #define StdFormatter_07_Custom_Parsing_03
namespace StdPrintln {
static void test_01()
{
std::println("Hello, world!");
}
static void test_02()
{
int value{ 123 };
std::println("Value: {}", value);
}
static void test_03()
{
int firstValue{ 123 };
int secondValue{ 456 };
std::println("First Value: {}, Second Value: {}", firstValue, secondValue);
std::println("First Value: {0}, Second Value: {1}", firstValue, secondValue);
}
// fill and align
static void test_04()
{
int value{ 123 };
std::println("{}", value); // "123"
std::println("{0}", value); // "123"
std::println("{:10}", value); // " 123"
std::println("!{:_<10}!", value); // "!123_______!"
std::println("!{:_>10}!", value); // "!_______123!"
}
// fill, align and precision
static void test_05()
{
int value{ 123 };
std::println("{}", value); // "123"
std::println("{:d}", value); // "123"
std::println("{:010}", value); // "0000000123"
std::println("{:010d}", value); // "0000000123"
std::println("{:0}", value); // "123"
std::println("{:+}", value); // "+123"
std::println("{:+}", -value); // "-123"
std::println("{:+10}", value); // " +123"
std::println("{:+10}", -value); // " -123"
std::println("{:+010}", value); // "+000000123"
std::println("{:+010}", -value); // "-000000123"
}
// fill, align and precision
static void test_06()
{
float pi{ 3.1415926535f };
std::println("{}", pi); // "3.1415927"
std::println("{0}", pi); // "3.1415927"
std::println("{:15f}", pi); // " 3.141593" (width = 15)
std::println("{:{}f}", pi, 15); // " 3.141593" (width = 15)
std::println("{:.12f}", pi); // "3.141592741013" (precision = 12)
std::println("{:.{}f}", pi, 3); // "3.142" (precision = 3)
std::println("{:15.12f}", pi); // " 3.141592741013" (width = 15, precision = 12)
std::println("{:{}.{}f}", pi, 15, 12); // " 3.141592741013" (width = 15, precision = 12)
}
// fill, align and precision
static void test_07()
{
double pi{ 3.1415926535f };
std::println("{}", pi); // "3.1415927"
std::println("{0}", pi); // "3.1415927"
std::println("{:15g}", pi); // " 3.141593" (width = 15)
std::println("{:{}g}", pi, 15); // " 3.141593" (width = 15)
std::println("{:.12g}", pi); // "3.141592741013" (precision = 12)
std::println("{:.{}g}", pi, 3); // "3.142" (precision = 3)
std::println("{:15.12g}", pi); // " 3.141592741013" (width = 15, precision = 12)
std::println("{:{}.{}g}", pi, 15, 12); // " 3.141592741013" (width = 15, precision = 12)
}
// Sign, #, and different formats
static void test_08()
{
std::println("Hexadecimal: {:x}", 6); // "Hexadecimal: 6"
std::println("Hexadecimal: {:x}", 30); // "Hexadecimal: 1e"
std::println("Hexadecimal: {:X}", 30); // "Hexadecimal: 1E"
std::println("Hexadecimal: {:#x}", 30); // "Hexadecimal: 0x1e"
std::println("Hexadecimal: {:#X}", 30); // "Hexadecimal: 0X1E"
std::println("Hexadecimal: {:15x}", 6); // "Hexadecimal: 6" (width = 15)
std::println("Hexadecimal: {:#15X}", 30); // "Hexadecimal: 0X1E" (width = 15)
std::println("Octal: {:o} ", 12); // "Octal: 14"
std::println("Octal: {:#o} ", 12); // "Octal: 014"
std::println("Octal: {:#o}", 4); // "Octal: 04"
std::println("Binary: {:b} ", 31); // "Binary: 11111"
std::println("Binary: {:#b} ", 31); // "Binary: 0b11111"
std::println("Binary: {:#B}", 7); // "Binary: 0B111"
std::println("Binary: {:#15b} ", 31); // "Binary: 0b11111" (width = 15)
std::println("Binary: {:#15B}", 7); // "Binary: 0B111" (width = 15)
}
// printing address of a variable
static void test_09()
{
int n = 123;
int* ip = &n;
std::println("&n: {:#X}", reinterpret_cast<intptr_t>(ip));
std::println("&n: {:#x}", reinterpret_cast<intptr_t>(&n));
}
}
// =====================================================================================
// =====================================================================================
namespace Formatting_Examples_Revised
{
class SimpleClass
{
private:
int m_value;
public:
SimpleClass() : SimpleClass{ 0 } {}
explicit SimpleClass(int value) : m_value{ value } {}
int getValue() const { return m_value; }
};
}
#ifdef StdFormatter_01_Basic_Formatter_API
// Basic Formatter API
namespace std
{
using namespace Formatting_Examples_Revised;
template<>
struct formatter<SimpleClass>
{
// parse the format string for this type
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin(); // should return position of '}' (hopefully)
}
// format by always writing its value:
auto format(const SimpleClass& obj, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{}", obj.getValue());
}
};
}
static void test_10()
{
using namespace Formatting_Examples_Revised;
SimpleClass obj{ 123 };
std::println("Value: {}", obj);
std::println("Two Values: {0} - {0}", obj);
}
#endif // StdFormatter_01_Basic_Formatter_API
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_02_Parsing_Format_String
namespace std
{
using namespace Formatting_Examples_Revised;
template<>
class formatter<SimpleClass>
{
private:
int m_width; // specified width of the field
public:
constexpr formatter() : m_width{ 0 } {}
// parse the format string for this type
constexpr auto parse(std::format_parse_context& ctx)
{
auto pos{ ctx.begin() };
while (pos != ctx.end() and *pos != '}') {
if (*pos < '0' or *pos > '9') {
throw std::format_error{ std::format("invalid format '{}'", *pos) };
}
m_width = m_width * 10 + (*pos - '0'); // new digit for the width
++pos;
}
return pos; // should return position of '}'
}
// format by always writing its value
auto format(const SimpleClass& obj, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{:{}}", obj.getValue(), m_width);
}
};
}
static void test_11()
{
using namespace Formatting_Examples_Revised;
try {
SimpleClass obj{ 123 };
std::println("{}", obj.getValue());
std::println("Value: {}", obj);
std::println("Twice: {0} {0}", obj);
std::println("With width: '{:6}'", obj);
std::println("Twice with width: '{0:6}' = '{1:6}'", obj, obj);
}
catch (std::format_error& e) {
std::println(std::cerr, "Format Error: {}", e.what());
}
}
#endif // StdFormatter_02_Parsing_Format_String
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_03_Delegating_Formatting_to_Standard_Formatters
namespace std
{
using namespace Formatting_Examples_Revised;
// delegating formatting to standard formatters
template<>
class formatter<SimpleClass>
{
private:
// use a standard int formatter that does the work:
std::formatter<int> m_formatter;
public:
// delegate parsing to the standard formatter:
constexpr auto parse(std::format_parse_context& ctx) {
return m_formatter.parse(ctx);
}
// delegate formatting of the value to the standard formatter:
auto format(const SimpleClass& obj, std::format_context& ctx) const {
return m_formatter.format(obj.getValue(), ctx);
}
};
}
static void test_12()
{
using namespace Formatting_Examples_Revised;
try {
SimpleClass obj{ 123 };
std::println("{}", obj.getValue());
std::println("Value: {}", obj);
std::println("Twice: {0} {0}", obj);
std::println("With width: '{:>20}'", obj);
std::println("With all: '{:.^20}'", obj);
}
catch (std::format_error& e) {
std::println(std::cerr, "Format Error: {}", e.what());
}
}
#endif // StdFormatter_03_Delegating_Formatting_to_Standard_Formatters
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_04_Inheriting_From_Standard_Formatters
namespace std
{
using namespace Formatting_Examples_Revised;
// inheriting From Standard Formatters
template<>
struct std::formatter<SimpleClass> : std::formatter<int>
{
auto format(const SimpleClass& obj, std::format_context& ctx) const {
// delegate formatting of the value to the standard formatter
return std::formatter<int>::format(obj.getValue(), ctx);
}
};
}
static void test_13()
{
using namespace Formatting_Examples_Revised;
try {
SimpleClass obj{ 123 };
std::println("{}", obj.getValue());
std::println("Value: {}", obj);
std::println("Twice: {0} {0}", obj);
std::println("With width: '{:>20}'", obj);
std::println("With all: '{:.^20}'", obj);
}
catch (std::format_error& e) {
std::println(std::cerr, "Format Error: {}", e.what());
}
}
#endif // StdFormatter_04_Inheriting_From_Standard_Formatters
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_05_Using_Standard_Formatters_for_Strings
namespace Formatting_Examples_Revised
{
enum class Color { red, green, blue };
}
namespace std
{
using namespace Formatting_Examples_Revised;
// formatter for user defined enum type Color
template<>
struct std::formatter<Color> : public std::formatter<std::string>
{
auto format(Color col, format_context& ctx) const {
std::string value{};
switch (col)
{
case Color::red:
value = "red";
break;
case Color::green:
value = "green";
break;
case Color::blue:
value = "blue";
break;
default:
value = std::format("Color{}", static_cast<int>(col));
break;
}
// delegate the rest of formatting to the string formatter
return std::formatter<std::string>::format(value, ctx);
}
};
}
static void test_14()
{
using namespace Formatting_Examples_Revised;
// using user-provided formatter for enum Color
for (auto val : { Color::red, Color::green, Color::blue, Color{ 123 } })
{
std::println("Color {:_>8} has value 0X{:02X}", val, static_cast<int>(val));
}
}
#endif // StdFormatter_05_Using_Standard_Formatters_for_Strings
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_06_Using_Standard_Formatters_for_StdVector
namespace std
{
using namespace Formatting_Examples_Revised;
// formatter for std::vector
template <typename T>
struct std::formatter<std::vector<T>> : std::formatter<std::string_view>
{
constexpr auto parse(format_parse_context& ctx) {
return ctx.begin();
}
auto format(const std::vector<T>& vec, std::format_context& ctx) const {
std::string tmp{};
const auto fmt_str = [&]() {
if constexpr (std::is_integral<T>::value) {
return "{:+5}";
}
else if constexpr (std::is_floating_point<T>::value) {
return "{:+5.2}";
}
else {
return "{}";
}
}();
const auto header = [&]() {
if (std::is_same<T, int>::value) {
return "std::vector<int>";
}
else if (std::is_same<T, long>::value) {
return "std::vector<long>";
}
else if (std::is_same<T, short>::value) {
return "std::vector<short>";
}
else if (std::is_same<T, float>::value) {
return "std::vector<float>";
}
else if (std::is_same<T, double>::value) {
return "std::vector<double>";
}
else {
return "std::vector<>";
}
}();
std::format_to(std::back_inserter(tmp), "{} - ", header);
T lastElem = vec.back();
std::for_each(
vec.begin(),
std::prev(vec.end()),
[&](const auto& elem) {
std::format_to(std::back_inserter(tmp), "{}, ", elem);
}
);
std::format_to(std::back_inserter(tmp), "{}", lastElem);
return std::formatter<string_view>::format(tmp, ctx);
}
};
}
static void test_15()
{
std::vector<int> intVec = { 1, 2, 3, 4, 5 };
std::println("{}", intVec);
std::vector<double> doublesVec = { 1.5, 2.5, 3.5, 4.5, 5.5 };
std::println("{}", doublesVec);
}
#endif // StdFormatter_06_Using_Standard_Formatters_for_StdVector
// ===========================================================================
// ===========================================================================
namespace Formatting_Examples_Again_Revised
{
class Color
{
private:
std::uint8_t m_red;
std::uint8_t m_green;
std::uint8_t m_blue;
public:
Color() : Color{ 0, 0, 0 } {}
Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue)
: m_red{ red }, m_green{ green }, m_blue{ blue }
{}
std::uint8_t getRed() const { return m_red; }
std::uint8_t getGreen() const { return m_green; }
std::uint8_t getBlue() const { return m_blue; }
};
}
#ifdef StdFormatter_07_Custom_Parsing_01
namespace std
{
using namespace Formatting_Examples_Again_Revised;
// formatter for class Color
template<>
struct std::formatter<Color> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const Color& col, std::format_context& ctx) const {
return
std::format_to(ctx.out(), "[{}, {}, {}]", col.getRed(), col.getGreen(), col.getBlue());
}
};
}
static void test_16()
{
using namespace Formatting_Examples_Again_Revised;
std::println("Color {}", Color{ 100, 200, 255 });
}
#endif // StdFormatter_07_Custom_Parsing_01
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_07_Custom_Parsing_02
namespace std
{
using namespace Formatting_Examples_Again_Revised;
// formatter for class Color
template<>
struct std::formatter<Color> : std::formatter<string_view>
{
auto format(const Color& col, std::format_context& ctx) const {
std::string tmp{};
std::format_to(std::back_inserter(tmp), "({}, {}, {})",
col.getRed(), col.getGreen(), col.getBlue());
return std::formatter<string_view>::format(tmp, ctx);
}
};
}
static void test_17()
{
using namespace Formatting_Examples_Again_Revised;
std::println("Color {}", Color{ 100, 200, 255 });
// std::println("col {:h}\n", Color{ 100, 200, 255 }); // does not compile
}
#endif // StdFormatter_07_Custom_Parsing_02
// ===========================================================================
// ===========================================================================
#ifdef StdFormatter_07_Custom_Parsing_03
namespace std
{
using namespace Formatting_Examples_Again_Revised;
// formatter for class Color
template <>
class std::formatter<Color>
{
private:
bool m_isHex;
public:
constexpr formatter() : m_isHex{ false } {}
constexpr auto parse(std::format_parse_context& ctx)
{
auto pos{ ctx.begin() };
while (pos != ctx.end() and *pos != '}') {
if (*pos == 'h' or *pos == 'H')
m_isHex = true;
++pos;
}
return pos; // should return position of '}'
}
auto format(const Color& col, std::format_context& ctx) const {
if (m_isHex) {
uint32_t val{ static_cast<uint32_t>(col.getRed() << 16 | col.getGreen() << 8 | col.getBlue()) };
return std::format_to(ctx.out(), "#{:X}", val);
}
else {
return std::format_to(ctx.out(), "[{}, {}, {}]", col.getRed(), col.getGreen(), col.getBlue());
}
};
};
}
static void test_18()
{
using namespace Formatting_Examples_Again_Revised;
Color color{ 100, 200, 255 };
std::println("Color {}", color);
std::println("Color {:h}", color);
}
#endif // StdFormatter_07_Custom_Parsing_03
// ===========================================================================
// ===========================================================================
void main_println()
{
using namespace StdPrintln;
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
test_07();
test_08();
test_09();
#ifdef StdFormatter_01_Basic_Formatter_API
test_10();
#endif
#ifdef StdFormatter_02_Parsing_Format_String
test_11();
#endif
#ifdef StdFormatter_03_Delegating_Formatting_to_Standard_Formatters
test_12();
#endif
#ifdef StdFormatter_04_Inheriting_From_Standard_Formatters
test_13();
#endif
#ifdef StdFormatter_05_Using_Standard_Formatters_for_Strings
test_14();
#endif
#ifdef StdFormatter_06_Using_Standard_Formatters_for_StdVector
test_15();
#endif
#ifdef StdFormatter_07_Custom_Parsing_01
test_16();
#endif
#ifdef StdFormatter_07_Custom_Parsing_02
test_17();
#endif
#ifdef StdFormatter_07_Custom_Parsing_03
test_18();
#endif
}
// =====================================================================================
// End-of-File
// =====================================================================================