/
Printf.h
852 lines (726 loc) · 23.2 KB
/
Printf.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#ifndef PRINTF_20160922_H_
#define PRINTF_20160922_H_
#include "Formatters.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <stdexcept>
#include <string>
#define CXX11_PRINTF_EXTENSIONS
namespace cxx11 {
struct format_error : std::runtime_error {
format_error(const char *what_arg) : std::runtime_error(what_arg) {
};
};
namespace detail {
enum class Modifiers {
MOD_NONE,
MOD_CHAR,
MOD_SHORT,
MOD_LONG,
MOD_LONG_LONG,
MOD_LONG_DOUBLE,
MOD_INTMAX_T,
MOD_SIZE_T,
MOD_PTRDIFF_T
};
struct Flags {
uint8_t justify : 1;
uint8_t sign : 1;
uint8_t space : 1;
uint8_t prefix : 1;
uint8_t padding : 1;
uint8_t reserved : 3;
};
static_assert(sizeof(Flags) == sizeof(uint8_t), "");
// NOTE(eteran): by placing this in a class, it allows us to do things like specialization a lot easier
template <unsigned int Divisor>
struct itoa_helper;
template <>
struct itoa_helper<10> {
static constexpr int Divisor = 10;
public:
//------------------------------------------------------------------------------
// Name: format
// Desc: returns the value of d as a C-string, formatted based on Divisor,
// and flags. places the length of the resultant string in *rlen
//------------------------------------------------------------------------------
template <class T, size_t N>
static const char *format(char (&buf)[N], T d, int width, Flags flags, const char *alphabet, size_t *rlen) {
typename std::make_unsigned<T>::type ud = d;
char *p = buf + N;
*--p = '\0';
// reserve space for leading chars as needed
// and if necessary negate the value in ud
if (d < 0) {
ud = -d;
width -= 1;
} else if (flags.space) {
width -= 1;
} else if (flags.sign) {
width -= 1;
}
// Divide UD by Divisor until UD == 0.
int digits = 0;
for (; ud; ud /= Divisor) {
const int remainder = (ud % Divisor);
*--p = alphabet[remainder];
++digits;
}
// add in any necessary padding
if (flags.padding) {
while (width-- > digits) {
*--p = '0';
}
}
// add the prefix as needed
if (d < 0) {
*--p = '-';
} else if (flags.space) {
*--p = ' ';
} else if (flags.sign) {
*--p = '+';
}
*rlen = (buf + N) - p;
return p;
}
};
// Specialization for base 16 so we can make some assumptions
template <>
struct itoa_helper<16> {
static constexpr int Shift = 4;
static constexpr int Mask = 0x0f;
public:
//------------------------------------------------------------------------------
// Name: format
// Desc: returns the value of d as a C-string, formatted based on Divisor,
// and flags. places the length of the resultant string in *rlen
//------------------------------------------------------------------------------
template <class T, size_t N>
static const char *format(char (&buf)[N], T d, int width, Flags flags, const char *alphabet, size_t *rlen) {
typename std::make_unsigned<T>::type ud = d;
char *p = buf + N;
*--p = '\0';
// add the prefix as needed
if (flags.prefix) {
width -= 2;
}
// Divide UD by Divisor until UD == 0.
int digits = 0;
for (; ud; ud >>= Shift) {
const int remainder = (ud & Mask);
*--p = alphabet[remainder];
++digits;
}
// add in any necessary padding
if (flags.padding) {
while (width-- > digits) {
*--p = '0';
}
}
// add the prefix as needed
if (flags.prefix) {
*--p = alphabet[16];
*--p = '0';
}
*rlen = (buf + N) - p;
return p;
}
};
// Specialization for base 8 so we can make some assumptions
template <>
struct itoa_helper<8> {
static constexpr int Shift = 3;
static constexpr int Mask = 0x07;
public:
//------------------------------------------------------------------------------
// Name: format
// Desc: returns the value of d as a C-string, formatted based on Divisor,
// and flags. places the length of the resultant string in *rlen
//------------------------------------------------------------------------------
template <class T, size_t N>
static const char *format(char (&buf)[N], T d, int width, Flags flags, const char *alphabet, size_t *rlen) {
typename std::make_unsigned<T>::type ud = d;
char *p = buf + N;
*--p = '\0';
// add the prefix as needed
if (flags.prefix) {
width -= 1;
}
// Divide UD by Divisor until UD == 0.
int digits = 0;
for (; ud; ud >>= Shift) {
const int remainder = (ud & Mask);
*--p = alphabet[remainder];
++digits;
}
// add in any necessary padding
if (flags.padding) {
while (width-- > digits) {
*--p = '0';
}
}
// add the prefix as needed
if (flags.prefix) {
*--p = '0';
}
*rlen = (buf + N) - p;
return p;
}
};
// Specialization for base 2 so we can make some assumptions
template <>
struct itoa_helper<2> {
static constexpr int Shift = 1;
static constexpr int Mask = 0x01;
public:
//------------------------------------------------------------------------------
// Name: format
// Desc: returns the value of d as a C-string, formatted based on Divisor,
// and flags. places the length of the resultant string in *rlen
//------------------------------------------------------------------------------
template <class T, size_t N>
static const char *format(char (&buf)[N], T d, int width, Flags flags, const char *alphabet, size_t *rlen) {
typename std::make_unsigned<T>::type ud = d;
char *p = buf + N;
*--p = '\0';
// add the prefix as needed
if (flags.prefix) {
width -= 2;
}
// Divide UD by Divisor until UD == 0.
int digits = 0;
for (; ud; ud >>= Shift) {
const int remainder = (ud & Mask);
*--p = alphabet[remainder];
++digits;
}
// add in any necessary padding
if (flags.padding) {
while (width-- > digits) {
*--p = '0';
}
}
// add the prefix as needed
if (flags.prefix) {
*--p = 'b';
*--p = '0';
}
*rlen = (buf + N) - p;
return p;
}
};
//------------------------------------------------------------------------------
// Name: itoa
// Desc: as a minor optimization, let's determine a few things up front and pass
// them as template parameters enabling some more aggressive optimizations
// when the division can use more efficient operations
//------------------------------------------------------------------------------
template <class T, size_t N>
const char *itoa(char (&buf)[N], char base, int precision, T d, int width, Flags flags, size_t *rlen) {
if (d == 0 && precision == 0) {
*buf = '\0';
*rlen = 0;
return buf;
}
// NOTE(eteran): we include the x/X, here as an easy way to put the
// upper/lower case prefix for hex numbers
static const char alphabet_l[] = "0123456789abcdefx";
static const char alphabet_u[] = "0123456789ABCDEFX";
switch (base) {
case 'i':
case 'd':
case 'u':
return itoa_helper<10>::format(buf, d, width, flags, alphabet_l, rlen);
#ifdef CXX11_PRINTF_EXTENSIONS
case 'b':
return itoa_helper<2>::format(buf, d, width, flags, alphabet_l, rlen);
#endif
case 'X':
return itoa_helper<16>::format(buf, d, width, flags, alphabet_u, rlen);
case 'x':
return itoa_helper<16>::format(buf, d, width, flags, alphabet_l, rlen);
case 'o':
return itoa_helper<8>::format(buf, d, width, flags, alphabet_l, rlen);
default:
return itoa_helper<10>::format(buf, d, width, flags, alphabet_l, rlen);
}
}
//------------------------------------------------------------------------------
// Name: output_string
// Desc: prints a string to the Context object, taking into account padding flags
// Note: ch is the current format specifier
//------------------------------------------------------------------------------
template <class Context>
void output_string(char ch, const char *s_ptr, int precision, long int width, Flags flags, int len, Context &ctx) {
if ((ch == 's' && precision >= 0 && precision < len)) {
len = precision;
}
// if not left justified padding goes first...
if (!flags.justify) {
// spaces go before the prefix...
while (width-- > len) {
ctx.write(' ');
}
}
// output the string
// NOTE(eteran): len is at most strlen, possible is less
// so we can just loop len times
width -= len;
ctx.write(s_ptr, len);
// if left justified padding goes last...
if (flags.justify) {
while (width-- > 0) {
ctx.write(' ');
}
}
}
// NOTE(eteran): Here is some code to fetch arguments of specific types. We also need a few
// default handlers, this code should never really be encountered, but
// but we need it to keep the linker happy.
#ifdef CXX11_PRINTF_EXTENSIONS
inline std::string formatted_object(std::string obj) {
return obj;
}
template <class T>
std::string to_string(T) {
throw format_error("No to_string found for this object type");
}
template <class T>
std::string formatted_object(T obj) {
using std::to_string;
using detail::to_string;
return to_string(obj);
}
#endif
template <class T>
const char *formatted_string(T s, typename std::enable_if<std::is_convertible<T, const char *>::value>::type * = 0) {
return s;
}
template <class T>
const char *formatted_string(T s, typename std::enable_if<!std::is_convertible<T, const char *>::value>::type * = 0) {
(void)s;
throw format_error("Non-String Argument For String Format");
}
template <class R, class T>
R formatted_pointer(T p, typename std::enable_if<std::is_convertible<T, const void *>::value>::type * = 0) {
return reinterpret_cast<R>(reinterpret_cast<uintptr_t>(p));
}
template <class R, class T>
R formatted_pointer(T p, typename std::enable_if<!std::is_convertible<T, const void *>::value>::type * = 0) {
(void)p;
throw format_error("Non-Pointer Argument For Pointer Format");
}
template <class R, class T>
R formatted_integer(T n, typename std::enable_if<std::is_integral<T>::value>::type * = 0) {
return static_cast<R>(n);
}
template <class R, class T>
R formatted_integer(T n, typename std::enable_if<!std::is_integral<T>::value>::type * = 0) {
(void)n;
throw format_error("Non-Integer Argument For Integer Format");
}
//------------------------------------------------------------------------------
// Name: process_format
// Desc: default handler that should never be called at runtime
//------------------------------------------------------------------------------
template <class Context>
int process_format(Context &ctx, const char *format, Flags flags, long int width, long int precision, Modifiers modifier) {
(void)ctx;
(void)format;
(void)flags;
(void)width;
(void)precision;
(void)modifier;
throw format_error("Should Never Happen");
}
//------------------------------------------------------------------------------
// Name: get_modifier
// Desc: default handler that should never be called at runtime
//------------------------------------------------------------------------------
template <class Context>
int get_modifier(Context &ctx, const char *format, Flags flags, long int width, long int precision) {
(void)ctx;
(void)format;
(void)flags;
(void)width;
(void)precision;
throw format_error("Should Never Happen");
}
//------------------------------------------------------------------------------
// Name: get_precision
// Desc: default handler that should never be called at runtime
//------------------------------------------------------------------------------
template <class Context>
int get_precision(Context &ctx, const char *format, Flags flags, long int width) {
(void)ctx;
(void)format;
(void)flags;
(void)width;
throw format_error("Should Never Happen");
}
//------------------------------------------------------------------------------
// Name: process_format
// Desc: prints the next argument to the Context taking into account the flags,
// width, precision, and modifiers collected along the way. Then will
// recursively continue processing the string
//------------------------------------------------------------------------------
template <class Context, class T, class... Ts>
int process_format(Context &ctx, const char *format, Flags flags, long int width, long int precision, Modifiers modifier, const T &arg, const Ts &... ts) {
// enough to contain a 64-bit number in bin notation + optional prefix
char num_buf[67];
size_t slen;
const char *s_ptr = nullptr;
char ch = *format;
switch (ch) {
case 'e':
case 'E':
case 'f':
case 'F':
case 'a':
case 'A':
case 'g':
case 'G':
// TODO(eteran): implement float formatting... for now, just consume the argument
return Printf(ctx, format + 1, ts...);
case 'p':
precision = 1;
ch = 'x';
flags.prefix = 1;
// NOTE(eteran): GNU printf prints "(nil)" for NULL pointers, we print 0x0
s_ptr = itoa(num_buf, ch, precision, formatted_pointer<uintptr_t>(arg), width, flags, &slen);
output_string(ch, s_ptr, precision, width, flags, slen, ctx);
return Printf(ctx, format + 1, ts...);
case 'x':
case 'X':
case 'u':
case 'o':
#ifdef CXX11_PRINTF_EXTENSIONS
case 'b': // extension, BINARY mode
#endif
if (precision < 0) {
precision = 1;
}
switch (modifier) {
case Modifiers::MOD_CHAR:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<unsigned char>(arg), width, flags, &slen);
break;
case Modifiers::MOD_SHORT:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<unsigned short int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_LONG:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<unsigned long int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_LONG_LONG:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<unsigned long long int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_INTMAX_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<uintmax_t>(arg), width, flags, &slen);
break;
case Modifiers::MOD_SIZE_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<size_t>(arg), width, flags, &slen);
break;
case Modifiers::MOD_PTRDIFF_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<std::make_unsigned<ptrdiff_t>::type>(arg), width, flags, &slen);
break;
default:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<unsigned int>(arg), width, flags, &slen);
break;
}
output_string(ch, s_ptr, precision, width, flags, slen, ctx);
return Printf(ctx, format + 1, ts...);
case 'i':
case 'd':
if (precision < 0) {
precision = 1;
}
switch (modifier) {
case Modifiers::MOD_CHAR:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<signed char>(arg), width, flags, &slen);
break;
case Modifiers::MOD_SHORT:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<short int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_LONG:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<long int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_LONG_LONG:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<long long int>(arg), width, flags, &slen);
break;
case Modifiers::MOD_INTMAX_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<intmax_t>(arg), width, flags, &slen);
break;
case Modifiers::MOD_SIZE_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<std::make_signed<size_t>::type>(arg), width, flags, &slen);
break;
case Modifiers::MOD_PTRDIFF_T:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<ptrdiff_t>(arg), width, flags, &slen);
break;
default:
s_ptr = itoa(num_buf, ch, precision, formatted_integer<int>(arg), width, flags, &slen);
break;
}
output_string(ch, s_ptr, precision, width, flags, slen, ctx);
return Printf(ctx, format + 1, ts...);
case 'c':
// char is promoted to an int when pushed on the stack
num_buf[0] = formatted_integer<char>(arg);
num_buf[1] = '\0';
s_ptr = num_buf;
output_string('c', s_ptr, precision, width, flags, 1, ctx);
return Printf(ctx, format + 1, ts...);
case 's':
s_ptr = formatted_string(arg);
if (!s_ptr) {
s_ptr = "(null)";
}
output_string('s', s_ptr, precision, width, flags, strlen(s_ptr), ctx);
return Printf(ctx, format + 1, ts...);
#ifdef CXX11_PRINTF_EXTENSIONS
case '?': {
std::string s = formatted_object(arg);
output_string('s', s.data(), precision, width, flags, s.size(), ctx);
}
return Printf(ctx, format + 1, ts...);
#endif
case 'n':
switch (modifier) {
case Modifiers::MOD_CHAR:
*formatted_pointer<signed char *>(arg) = ctx.written;
break;
case Modifiers::MOD_SHORT:
*formatted_pointer<short int *>(arg) = ctx.written;
break;
case Modifiers::MOD_LONG:
*formatted_pointer<long int *>(arg) = ctx.written;
break;
case Modifiers::MOD_LONG_LONG:
*formatted_pointer<long long int *>(arg) = ctx.written;
break;
case Modifiers::MOD_INTMAX_T:
*formatted_pointer<intmax_t *>(arg) = ctx.written;
break;
case Modifiers::MOD_SIZE_T:
*formatted_pointer<std::make_signed<size_t>::type *>(arg) = ctx.written;
break;
case Modifiers::MOD_PTRDIFF_T:
*formatted_pointer<ptrdiff_t *>(arg) = ctx.written;
break;
default:
*formatted_pointer<int *>(arg) = ctx.written;
break;
}
return Printf(ctx, format + 1, ts...);
default:
ctx.write('%');
// FALL THROUGH
case '\0':
case '%':
ctx.write(ch);
break;
}
return Printf(ctx, format + 1, ts...);
}
//------------------------------------------------------------------------------
// Name: get_modifier
// Desc: gets the modifier, if any, from the format string, then calls
// process_format
//------------------------------------------------------------------------------
template <class Context, class T, class... Ts>
int get_modifier(Context &ctx, const char *format, Flags flags, long int width, long int precision, const T &arg, const Ts &... ts) {
Modifiers modifier = Modifiers::MOD_NONE;
switch (*format) {
case 'h':
modifier = Modifiers::MOD_SHORT;
++format;
if (*format == 'h') {
modifier = Modifiers::MOD_CHAR;
++format;
}
break;
case 'l':
modifier = Modifiers::MOD_LONG;
++format;
if (*format == 'l') {
modifier = Modifiers::MOD_LONG_LONG;
++format;
}
break;
case 'L':
modifier = Modifiers::MOD_LONG_DOUBLE;
++format;
break;
case 'j':
modifier = Modifiers::MOD_INTMAX_T;
++format;
break;
case 'z':
modifier = Modifiers::MOD_SIZE_T;
++format;
break;
case 't':
modifier = Modifiers::MOD_PTRDIFF_T;
++format;
break;
default:
break;
}
return process_format(ctx, format, flags, width, precision, modifier, arg, ts...);
}
//------------------------------------------------------------------------------
// Name: get_precision
// Desc: gets the precision, if any, either from the format string or as an arg
// as needed, then calls get_modifier
//------------------------------------------------------------------------------
template <class Context, class T, class... Ts>
int get_precision(Context &ctx, const char *format, Flags flags, long int width, const T &arg, const Ts &... ts) {
// default to non-existant
long int p = -1;
if (*format == '.') {
++format;
if (*format == '*') {
++format;
// pull an int off the stack for processing
p = formatted_integer<long int>(arg);
return get_modifier(ctx, format, flags, width, p, ts...);
} else {
char *endptr;
p = strtol(format, &endptr, 10);
format = endptr;
return get_modifier(ctx, format, flags, width, p, arg, ts...);
}
}
return get_modifier(ctx, format, flags, width, p, arg, ts...);
}
//------------------------------------------------------------------------------
// Name: get_width
// Desc: gets the width if any, either from the format string or as an arg as
// needed, then calls get_precision
//------------------------------------------------------------------------------
template <class Context, class T, class... Ts>
int get_width(Context &ctx, const char *format, Flags flags, const T &arg, const Ts &... ts) {
int width = 0;
if (*format == '*') {
++format;
// pull an int off the stack for processing
width = formatted_integer<long int>(arg);
return get_precision(ctx, format, flags, width, ts...);
} else {
char *endptr;
width = strtol(format, &endptr, 10);
format = endptr;
return get_precision(ctx, format, flags, width, arg, ts...);
}
}
//------------------------------------------------------------------------------
// Name: get_flags
// Desc: gets the flags, if any, from the format string, then calls get_width
//------------------------------------------------------------------------------
template <class Context, class... Ts>
int get_flags(Context &ctx, const char *format, const Ts &... ts) {
Flags f = {0, 0, 0, 0, 0, 0};
bool done = false;
// skip past the % char
++format;
while (!done) {
char ch = *format++;
switch (ch) {
case '-':
// justify, overrides padding
f.justify = 1;
f.padding = 0;
break;
case '+':
// sign, overrides space
f.sign = 1;
f.space = 0;
break;
case ' ':
if (!f.sign) {
f.space = 1;
}
break;
case '#':
f.prefix = 1;
break;
case '0':
if (!f.justify) {
f.padding = 1;
}
break;
default:
done = true;
--format;
}
}
return get_width(ctx, format, f, ts...);
}
}
//------------------------------------------------------------------------------
// Name: Printf
// Desc: 0 argument version of Printf. Asserts on any format character found
//------------------------------------------------------------------------------
template <class Context>
int Printf(Context &ctx, const char *format) {
assert(format);
for (; *format; ++format) {
if (*format != '%' || *++format == '%') {
ctx.write(*format);
continue;
}
throw format_error("Bad Format");
}
// this will usually null terminate the string
ctx.done();
// return the amount of bytes that should have been written if there was sufficient space
return ctx.written;
}
//------------------------------------------------------------------------------
// Name: Printf
// Desc: 1+ argument version of Printf.
//------------------------------------------------------------------------------
template <class Context, class... Ts>
int Printf(Context &ctx, const char *format, const Ts &... ts) {
assert(format);
while (*format != '\0') {
if (*format == '%') {
// %[flag][width][.precision][length]char
// this recurses into get_width -> get_precision -> get_length -> process_format
return detail::get_flags(ctx, format, ts...);
} else {
ctx.write(*format);
}
++format;
}
// clean up any trailing stuff
return Printf(ctx, format + 1, ts...);
}
//------------------------------------------------------------------------------
// Name: snprintf
// Desc: implementation of what snprintf compatible interface
//------------------------------------------------------------------------------
template <class... Ts>
int sprintf(std::ostream &os, const char *format, const Ts &... ts) {
ostream_writer ctx(os);
return Printf(ctx, format, ts...);
}
//------------------------------------------------------------------------------
// Name: sprintf
// Desc: implementation of what s[n]printf compatible interface
//------------------------------------------------------------------------------
template <class... Ts>
int sprintf(char *str, size_t size, const char *format, const Ts &... ts) {
buffer_writer ctx(str, size);
return Printf(ctx, format, ts...);
}
//------------------------------------------------------------------------------
// Name: printf
// Desc: implementation of what printf compatible interface
//------------------------------------------------------------------------------
template <class... Ts>
int printf(const char *format, const Ts &... ts) {
stdout_writer ctx;
return Printf(ctx, format, ts...);
}
}
#endif