-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_printf_v5.c
More file actions
656 lines (576 loc) · 25.9 KB
/
simple_printf_v5.c
File metadata and controls
656 lines (576 loc) · 25.9 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
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/*
* Copyright 2023, J. Zbiciak <joe.zbiciak@leftturnonly.info>
* Author: Joe Zbiciak <joe.zbiciak@leftturnonly.info>
* SPDX-License-Identifier: CC-BY-SA-4.0
*/
/* Operand sizes: */ /* Mod diouxX conversions cs convs */
enum { /* ---- ---------------------------------- -------- */
kSizeChar, /* hh signed char, unsigned char */
kSizeShort, /* h short, unsigned short */
kSizeDefault, /* none int, unsigned int, double, char */
kSizeLong, /* l long, unsigned long, wchar_t */
kSizeLongLong, /* ll long long int, unsigned long long */
kSizeMax, /* j intmax_t */
kSizeSizeT, /* z size_t */
kSizePtrDiffT /* t ptrdiff_t */
};
/* Sign display: */ /* Flag Non-negative values Negative values */
enum { /* ----- -------------------- --------------------- */
kSignDefault, /* none Nothing '-' */
kSignAlways, /* + '+' '-' */
kSignSpace /* space ' ' '-' */
};
/* Assume MSB is sign bit. */
#define SIGN_BIT (ULLONG_MAX - ULLONG_MAX / 2)
/*
* Buffer size for rendering integers. This should be enough for a 128-bit
* intmax_t, with sign or 0x prefix and terminating null, with some extra room.
* My current platform only has a 64-bit intmax_t, however, so 128-bit is
* not tested.
*/
#define INT_BUF_SIZE (48)
static const char *const hex_digits[2] = {
"0123456789abcdef", "0123456789ABCDEF"
};
/*
* Converts an integer in the specified base, stored at the _end_ of buf[].
* Returns the index of the first character.
*/
static int conv_integer(
unsigned long long value, int sign, bool is_signed, bool is_caps,
bool is_alt, int prec, bool soft_prec, unsigned base, char *buf) {
bool is_negative = false;
int idx = INT_BUF_SIZE;
buf[--idx] = '\0';
/* Print nothing if precision and value are both 0. */
if (!soft_prec && prec == 0 && value == 0) {
return INT_BUF_SIZE - 1;
}
/* Remember negative numbers in signed conversions. Convert to positive. */
if (is_signed && (value & SIGN_BIT)) {
is_negative = true;
value = -value;
}
/* Convert the digits, starting with the least significant. */
do {
buf[--idx] = hex_digits[is_caps][value % base];
value /= base;
} while (value > 0);
/*
* If our precision actually came from the width field, adjust it based on
* other things we might print before the padding zeros.
*/
if (soft_prec) {
if (is_alt && base == 16) { prec -= 2; }
if (is_alt && base == 8 && buf[idx] != '0') { prec--; }
if (is_negative || (is_signed && sign != kSignDefault)) { prec--; }
if (prec < 1) { prec = 1; }
}
/*
* Compute index for padding zeros, out to precision. Bound the number of
* leading zeros we support to what fits in our buffer. This does break
* standards compliance, as it wants us to support up to 4095 characters in
* a conversion.
*/
int prec_idx = prec < INT_BUF_SIZE - 1 ? INT_BUF_SIZE - prec : 1;
/* Leave room for "0x" if alternate-form hex. Hex is never signed. */
if (prec_idx < 3 && is_alt && base == 16) {
prec_idx = 3;
}
/* Leave room for "0" if alternate-form octal. Octal is never signed. */
if (prec_idx < 3 && is_alt && base == 8) {
prec_idx = 2;
}
/* Leave enough room for a leading sign if we need it. */
if (prec_idx < 2 && (is_negative || (is_signed && sign != kSignDefault))) {
prec_idx = 2;
}
/* Add leading zeros out to precision index. */
while (idx >= prec_idx) {
buf[--idx] = '0';
}
/* If we're alternate-form octal, add a leading 0 if needed. */
if (is_alt && base == 8 && buf[idx] != '0') {
buf[--idx] = '0';
}
/* If we're alternate-form hex, add a leading "0x" or "0X". */
if (is_alt && base == 16) {
buf[--idx] = is_caps ? 'X' : 'x';
buf[--idx] = '0';
}
/*
* If we're negative add a negative sign. Otherwise, if this is a signed
* conversion, add a '+' or ' ' if directed.
*/
if (is_negative) {
buf[--idx] = '-';
} else if (is_signed && sign != kSignDefault) {
buf[--idx] = sign == kSignAlways ? '+' : ' ';
}
return idx;
}
/* Prints a string in a particular width field. */
static void print_string(const char *s, int len, int width, bool left_justify) {
if (!left_justify && len < width) {
for (int i = width - len; i > 0; --i) {
fputc(' ', stdout);
}
}
fwrite(s, 1, len, stdout);
if (left_justify && len < width) {
for (int i = width - len; i > 0; --i) {
fputc(' ', stdout);
}
}
}
/* Gets a signed argument of the specified size. */
static unsigned long long get_signed_integer(va_list *args, int size) {
switch (size) {
case kSizeChar: { return (signed char )va_arg(*args, int); }
case kSizeShort: { return (signed short)va_arg(*args, int); }
case kSizeDefault: { return va_arg(*args, int); }
case kSizeLong: { return va_arg(*args, long); }
case kSizeLongLong: { return va_arg(*args, long long); }
case kSizeMax: { return va_arg(*args, intmax_t); }
case kSizePtrDiffT: { return va_arg(*args, ptrdiff_t); }
case kSizeSizeT: {
/*
* C doesn't give a name to the signed type that corresponds to
* size_t, but that's what we need to extract here. Guess among
* long long, long, and int based on size.
*/
if (sizeof(size_t) == sizeof(unsigned long long)) {
return va_arg(*args, long long);
} else if (sizeof(size_t) == sizeof(unsigned long)) {
return va_arg(*args, long);
} else {
return va_arg(*args, int);
}
}
}
/* Unknown size. Fall back to int. */
return va_arg(*args, int);
}
/* Gets an unsigned argument of the specified size. */
static unsigned long long get_unsigned_integer(va_list *args, int size) {
switch (size) {
case kSizeChar: { return (unsigned char)va_arg(*args, int); }
case kSizeShort: { return (unsigned short)va_arg(*args,int); }
case kSizeDefault: { return va_arg(*args, unsigned int); }
case kSizeLong: { return va_arg(*args, unsigned long); }
case kSizeLongLong: { return va_arg(*args, unsigned long long); }
case kSizeMax: { return va_arg(*args, uintmax_t); }
case kSizeSizeT: { return va_arg(*args, size_t); }
case kSizePtrDiffT: {
/*
* C doesn't give a name to the unsigned type that corresponds to
* ptrdiff_t, but that's what we need to extract here. Guess
* among unsigned long long, unsigned long, and unsigned based on
* size.
*/
if (sizeof(ptrdiff_t) == sizeof(long long)) {
return va_arg(*args, unsigned long long);
} else if (sizeof(size_t) == sizeof(long)) {
return va_arg(*args, unsigned long);
} else {
return va_arg(*args, unsigned);
}
break;
}
}
/* Unknown size. Fall back to unsigned. */
return va_arg(*args, unsigned);
}
/*
* Implements simplified printf that understands:
* -- Strings: "s"
* -- Characters: "c"
* -- Integers:
* -- Lengths: "hh", "h", "l", "ll", "j", "z", "t" and default int.
* -- Signed decimal: "d", "i"
* -- Unsigned decimal: "u"
* -- Octal: "o"
* -- Hexadecimal: "x", "X"
* -- Flags:
* -- "#" for alternate form on "o", "x", and "X" conversions.
* -- " " and "+" for sign on "d", "i" conversions.
* -- "0" for leading zeros on integer conversions.
* -- "-" for left-justified fields.
* -- Width and precision specifiers:
* -- Supports dynamic values via "*".
* -- Max integer precision is limited by INT_BUF_MAX.
* -- Printing "%" with "%%".
*
* Not supported:
* -- Floating point.
* -- Returning length of printed string.
* -- Reporting length of printed string with "n".
* -- Printing to a stream other than stdout.
* -- Printing to a buffer.
* -- Converting pointers with "p".
* -- Wide characters ("%lc").
* -- Wide character strings ("%ls").
*
* I guess it's no longer so simple...
*/
void simple_printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char buf[INT_BUF_SIZE];
for (int ch = *fmt++; ch; ch = *fmt++) {
/* If it's not %, just print the character. */
if (ch != '%') {
fputc(ch, stdout);
continue;
}
/* It's (potentially) a conversion. Let's look. */
const char *initial = fmt; /* Start of conversion specifier. */
int conv = *fmt++; /* Fetch first character of conversion spec. */
bool leading_zero = false; /* Leading-zero flag. */
bool left_justify = false; /* Left justification flag. */
bool is_alt = false; /* Alternate form flag. */
int sign = kSignDefault; /* Sign display flag. */
int size = kSizeDefault; /* Operand size. */
bool default_width = true; /* Use the default width. */
int width = 0; /* User-provided width of the field. */
bool default_prec = true; /* Use the default precision. */
int prec = 0; /* User-provided precision of the field. */
bool is_caps = false; /* Print hex values in capital letters. */
bool is_signed = true; /* Perform a signed integer conversion. */
int base = 10; /* Default radix is decimal. */
/* Check for flags. Flags can appear in any order. */
for (bool done_flags = false; !done_flags;) {
switch (conv) {
case '0': { leading_zero = true; conv = *fmt++; continue; }
case '-': { left_justify = true; conv = *fmt++; continue; }
case '+': { sign = kSignAlways; conv = *fmt++; continue; }
case '#': { is_alt = true; conv = *fmt++; continue; }
case ' ': {
if (sign == kSignDefault) {
/* ' ' only takes effect if '+' isn't also provided. */
sign = kSignSpace;
}
conv = *fmt++;
continue;
}
default: {
done_flags = true;
}
}
}
/* Check for width. */
if (conv == '*') { /* Width provided as an int argument. */
default_width = false;
width = va_arg(args, int);
if (width < 0) { /* Negative width specifies left justification. */
left_justify = true;
width = -width;
}
conv = *fmt++;
} else { /* Width is a decimal number in the format string. */
while (isdigit(conv)) {
default_width = false;
width = width * 10 + (conv - '0');
conv = *fmt++;
}
}
/* Check for precision. Always preceded by a "." */
if (conv == '.') {
default_prec = false;
conv = *fmt++;
if (conv == '*') { /* Precision provided as an int argument. */
prec = va_arg(args, int);
if (prec < 0) { prec = 0; } /* Negative precision acts like 0. */
conv = *fmt++;
} else { /* Precision is a decimal number in the format string. */
while (isdigit(conv)) {
prec = prec * 10 + (conv - '0');
conv = *fmt++;
}
}
}
/* Check for a size modifier: "hh", "h", "l", "ll", "j", "z", "t". */
switch (conv) {
case 'h': {
size = kSizeShort; /* "h" is short. */
if (*fmt == 'h') { /* "hh" is char. */
size = kSizeChar;
fmt++;
}
conv = *fmt++;
break;
}
case 'l': {
size = kSizeLong; /* "l" is long. */
if (*fmt == 'l') { /* "ll" is long long. */
size = kSizeLongLong;
fmt++;
}
conv = *fmt++;
break;
}
case 'j': { size = kSizeMax; conv = *fmt++; break; }
case 'z': { size = kSizeSizeT; conv = *fmt++; break; }
case 't': { size = kSizePtrDiffT; conv = *fmt++; break; }
}
/* Now look for the actual conversion. */
switch (conv) {
case 'c': {
/* For now, we don't support %lc. */
if (size != kSizeDefault) {
fmt = initial;
putchar('%');
break;
}
buf[0] = va_arg(args, int);
print_string(buf, 1, width, left_justify);
break;
}
case 's': {
/* For now, we don't support %ls. */
if (size != kSizeDefault) {
fmt = initial;
putchar('%');
break;
}
/* %s is a string. */
if (default_prec) {
prec = INT_MAX;
}
const char *const s = va_arg(args, const char *);
const char *const e = memchr(s, '\0', prec);
const int len = !e ? prec : e - s;
print_string(s, len, width, left_justify);
break;
}
case 'o': /* %o is an unsigned octal integer. */
base = 8;
/* FALLTHROUGH_INTENDED */
case 'X': /* %X is an unsigned hexadecimal integer in caps. */
is_caps = true;
/* FALLTHROUGH_INTENDED */
case 'x': /* %x is an unsigned hexadecimal integer in lowercase. */
base = base == 10 ? 16 : base; /* Hex unless fallthrough from octal. */
/* FALLTHROUGH_INTENDED */
case 'u': /* %u is an unsigned decimal integer. */
is_signed = false;
/* FALLTHROUGH_INTENDED */
case 'i': /* %d and %i are signed decimal integers. */
case 'd': {
unsigned long long val = is_signed ? get_signed_integer(&args, size)
: get_unsigned_integer(&args, size);
bool soft_prec = false;
if (default_prec) {
/*
* If provided an explicit width but no precision and asked to zero
* pad, treat the width like a "soft" precision that can be eaten
* into by the sign and a radix prefix if needed.
*/
if (leading_zero && !default_width && !left_justify) {
prec = width;
soft_prec = true;
} else {
prec = 1;
}
}
int idx = conv_integer(val, sign, is_signed, is_caps, is_alt, prec,
soft_prec, base, buf);
int len = INT_BUF_SIZE - idx - 1;
print_string(buf + idx, len, width, left_justify);
break;
}
case '%': {
/* %% prints '%' */
putchar('%');
break;
}
default: {
/* Not a valid conversion. Print the '%' and back up. */
putchar('%');
fmt = initial;
break;
}
}
}
va_end(args);
}
int main() {
simple_printf("Hello %s, the answer is %d.\n", "world", 42);
simple_printf("Zero: %d %i %o %x %X char: '%c'\n", 0, 0, 0, 0, 0, '*');
simple_printf("\nIntegers: signed char / unsigned char\n");
simple_printf("Positive %%hhd %%#hhd: %hhd %#hhd\n", 123456789, 123456789);
simple_printf("Negative %%hhd %%#hhd: %hhd %#hhd\n", -123456789, -123456789);
simple_printf("Positive %%hhi %%#hhi: %hhi %#hhi\n", 123456789, 123456789);
simple_printf("Negative %%hhi %%#hhi: %hhi %#hhi\n", -123456789, -123456789);
simple_printf("Unsigned %%hhu %%#hhu: %hhu %#hhu\n", 4000000000U,4000000000U);
simple_printf("Octal %%hho %%#hho: %hho %#hho\n", 123456789, 123456789);
simple_printf("Octal %%hho %%#hho: %hho %#hho\n", -123456789, -123456789);
simple_printf("Octal %%hho %%#hho: %hho %#hho\n", 4000000000U,4000000000U);
simple_printf("Hex %%hhx %%#hhx: %hhx %#hhx\n", 123456789, 123456789);
simple_printf("Hex %%hhx %%#hhx: %hhx %#hhx\n", -123456789, -123456789);
simple_printf("Hex %%hhx %%#hhx: %hhx %#hhx\n", 4000000000U,4000000000U);
simple_printf("Hex %%hhX %%#hhX: %hhX %#hhX\n", 123456789, 123456789);
simple_printf("Hex %%hhX %%#hhX: %hhX %#hhX\n", -123456789, -123456789);
simple_printf("Hex %%hhX %%#hhX: %hhX %#hhX\n", 4000000000U,4000000000U);
simple_printf("\nIntegers: short / unsigned short\n");
simple_printf("Positive %%hd %%#hd: %hd %#hd\n", 123456789, 123456789);
simple_printf("Negative %%hd %%#hd: %hd %#hd\n", -123456789, -123456789);
simple_printf("Positive %%hi %%#hi: %hi %#hi\n", 123456789, 123456789);
simple_printf("Negative %%hi %%#hi: %hi %#hi\n", -123456789, -123456789);
simple_printf("Unsigned %%hu %%#hu: %hu %#hu\n", 4000000000U,4000000000U);
simple_printf("Octal %%ho %%#ho: %ho %#ho\n", 123456789, 123456789);
simple_printf("Octal %%ho %%#ho: %ho %#ho\n", -123456789, -123456789);
simple_printf("Octal %%ho %%#ho: %ho %#ho\n", 4000000000U,4000000000U);
simple_printf("Hex %%hx %%#hx: %hx %#hx\n", 123456789, 123456789);
simple_printf("Hex %%hx %%#hx: %hx %#hx\n", -123456789, -123456789);
simple_printf("Hex %%hx %%#hx: %hx %#hx\n", 4000000000U,4000000000U);
simple_printf("Hex %%hX %%#hX: %hX %#hX\n", 123456789, 123456789);
simple_printf("Hex %%hX %%#hX: %hX %#hX\n", -123456789, -123456789);
simple_printf("Hex %%hX %%#hX: %hX %#hX\n", 4000000000U,4000000000U);
simple_printf("\nIntegers: int / unsigned int\n");
simple_printf("Positive %%d %%#d: %d %#d\n", 123456789, 123456789);
simple_printf("Negative %%d %%#d: %d %#d\n", -123456789, -123456789);
simple_printf("Positive %%i %%#i: %i %#i\n", 123456789, 123456789);
simple_printf("Negative %%i %%#i: %i %#i\n", -123456789, -123456789);
simple_printf("Unsigned %%u %%#u: %u %#u\n", 4000000000U,4000000000U);
simple_printf("Octal %%o %%#o: %o %#o\n", 123456789, 123456789);
simple_printf("Octal %%o %%#o: %o %#o\n", -123456789, -123456789);
simple_printf("Octal %%o %%#o: %o %#o\n", 4000000000U,4000000000U);
simple_printf("Hex %%x %%#x: %x %#x\n", 123456789, 123456789);
simple_printf("Hex %%x %%#x: %x %#x\n", -123456789, -123456789);
simple_printf("Hex %%x %%#x: %x %#x\n", 4000000000U,4000000000U);
simple_printf("Hex %%X %%#X: %X %#X\n", 123456789, 123456789);
simple_printf("Hex %%X %%#X: %X %#X\n", -123456789, -123456789);
simple_printf("Hex %%X %%#X: %X %#X\n", 4000000000U,4000000000U);
simple_printf("\nIntegers: long / unsigned long\n");
simple_printf("Positive %%ld %%#ld: %ld %#ld\n", 123456789, 123456789);
simple_printf("Negative %%ld %%#ld: %ld %#ld\n", -123456789, -123456789);
simple_printf("Positive %%li %%#li: %li %#li\n", 123456789, 123456789);
simple_printf("Negative %%li %%#li: %li %#li\n", -123456789, -123456789);
simple_printf("Unsigned %%lu %%#lu: %lu %#lu\n", 4000000000U,4000000000U);
simple_printf("Octal %%lx %%#lx: %lo %#lo\n", 123456789, 123456789);
simple_printf("Octal %%lx %%#lx: %lo %#lo\n", -123456789, -123456789);
simple_printf("Octal %%lx %%#lx: %lo %#lo\n", 4000000000U,4000000000U);
simple_printf("Hex %%lx %%#lx: %lx %#lx\n", 123456789, 123456789);
simple_printf("Hex %%lx %%#lx: %lx %#lx\n", -123456789, -123456789);
simple_printf("Hex %%lx %%#lx: %lx %#lx\n", 4000000000U,4000000000U);
simple_printf("Hex %%lX %%#lX: %lX %#lX\n", 123456789, 123456789);
simple_printf("Hex %%lX %%#lX: %lX %#lX\n", -123456789, -123456789);
simple_printf("Hex %%lX %%#lX: %lX %#lX\n", 4000000000U,4000000000U);
simple_printf("\nIntegers: long long / unsigned long long\n");
simple_printf("Positive %%lld %%#lld: %lld %#lld\n", 123456789123456789LL,
123456789123456789LL);
simple_printf("Negative %%lld %%#lld: %lld %#lld\n", -123456789123456789LL,
-123456789123456789LL);
simple_printf("Positive %%lli %%#lli: %lli %#lli\n", 123456789123456789LL,
123456789123456789LL);
simple_printf("Negative %%lli %%#lli: %lli %#lli\n", -123456789123456789LL,
-123456789123456789LL);
simple_printf("Unsigned %%llu %%#llu: %llu %#llu\n", 4000000000000000000ULL,
4000000000000000000ULL);
simple_printf("Octal %%llo %%#llo: %llo %#llo\n", 123456789123456789LL,
123456789123456789LL);
simple_printf("Octal %%llo %%#llo: %llo %#llo\n", -123456789123456789LL,
-123456789123456789LL);
simple_printf("Octal %%llo %%#llo: %llo %#llo\n", 4000000000000000000ULL,
4000000000000000000ULL);
simple_printf("Hex %%llx %%#llx: %llx %#llx\n", 123456789123456789LL,
123456789123456789LL);
simple_printf("Hex %%llx %%#llx: %llx %#llx\n", -123456789123456789LL,
-123456789123456789LL);
simple_printf("Hex %%llx %%#llx: %llx %#llx\n", 4000000000000000000ULL,
4000000000000000000ULL);
simple_printf("Hex %%llX %%#llX: %llX %#llX\n", 123456789123456789LL,
123456789123456789LL);
simple_printf("Hex %%llX %%#llX: %llX %#llX\n", -123456789123456789LL,
-123456789123456789LL);
simple_printf("Hex %%llX %%#llX: %llX %#llX\n", 4000000000000000000ULL,
4000000000000000000ULL);
simple_printf("\nIntegers: size_t\n");
simple_printf("Positive %%zd: %jd (expected: -1)\n", SIZE_MAX);
simple_printf("Unsigned %%zu: %ju\n", SIZE_MAX);
simple_printf("Octal %%zo: %jo\n", SIZE_MAX);
simple_printf("Hex %%zx: %jx\n", SIZE_MAX);
simple_printf("Hex %%zX: %jX\n", SIZE_MAX);
simple_printf("\nIntegers: ptrdiff_t\n");
simple_printf("Positive %%td: %jd\n", PTRDIFF_MAX);
simple_printf("Negative %%td: %jd\n", PTRDIFF_MIN);
simple_printf("Unsigned %%tu: %ju\n", PTRDIFF_MAX);
simple_printf("Octal %%to: %jo\n", PTRDIFF_MAX);
simple_printf("Hex %%tx: %jx\n", PTRDIFF_MAX);
simple_printf("Hex %%tX: %jX\n", PTRDIFF_MAX);
simple_printf("\nIntegers: intmax_t / uintmax_t\n");
simple_printf("Positive %%jd: %jd\n", INTMAX_MAX);
simple_printf("Negative %%jd: %jd\n", INTMAX_MIN);
simple_printf("Unsigned %%ju: %ju\n", UINTMAX_MAX);
simple_printf("Octal %%jo: %jo\n", UINTMAX_MAX);
simple_printf("Hex %%jx: %jx\n", UINTMAX_MAX);
simple_printf("Hex %%jX: %jX\n", UINTMAX_MAX);
simple_printf("Positive %%#jd: %#jd\n", INTMAX_MAX);
simple_printf("Negative %%#jd: %#jd\n", INTMAX_MIN);
simple_printf("Unsigned %%#ju: %#ju\n", UINTMAX_MAX);
simple_printf("Octal %%#jo: %#jo\n", UINTMAX_MAX);
simple_printf("Hex %%#jx: %#jx\n", UINTMAX_MAX);
simple_printf("Hex %%#jX: %#jX\n", UINTMAX_MAX);
simple_printf("\nField width & precision:\n");
simple_printf("%%c: [%c] %%-10c: [%-10c] %%10c: [%10c]\n",
'*', '*', '*');
simple_printf("%%s: [%s] %%-10s: [%-10s] %%10s: [%10s]\n",
"Hello", "Hello", "Hello");
simple_printf("%%.2s: [%.2s] %%-10.2s: [%-10.2s] %%10.2s: "
"[%10.2s]\n", "Hello", "Hello", "Hello");
simple_printf("%%d: [%d] %%-10d: [%-10d] %%10d: [%10d]\n",
12345, 12345, 12345);
simple_printf("%%d: [%d] %%-10d: [%-10d] %%10d: [%10d]\n",
-1234, -1234, -1234);
simple_printf("%% d: [% d] %% -10d: [% -10d] %% 10d: [% 10d]\n",
1234, 1234, 1234);
simple_printf("%% d: [% d] %% -10d: [% -10d] %% 10d: [% 10d]\n",
-1234, -1234, -1234);
simple_printf("%%+d: [%+d] %%+-10d: [%+-10d] %%+10d: [%+10d]\n",
1234, 1234, 1234);
simple_printf("%%+d: [%+d] %%+-10d: [%+-10d] %%+10d: [%+10d]\n",
-1234, -1234, -1234);
simple_printf("%% .7d: [% .7d] %% -10.7d: [% -10.7d] %% 10.7d: [% 10.7d]\n",
1234, 1234, 1234);
simple_printf("%% .7d: [% .7d] %% -10.7d: [% -10.7d] %% 10.7d: [% 10.7d]\n",
-1234, -1234, -1234);
simple_printf("%%+.7d: [%+.7d] %%+-10.7d: [%+-10.7d] %%+10.7d: [%+10.7d]\n",
1234, 1234, 1234);
simple_printf("%%+.7d: [%+.7d] %%+-10.7d: [%+-10.7d] %%+10.7d: [%+10.7d]\n",
-1234, -1234, -1234);
simple_printf("%%07d: [%07d] %%-07d: [%-07d] %%07d: [%07d]\n",
1234, 1234, 1234);
simple_printf("%%07d: [%07d] %%-07d: [%-07d] %%07d: [%07d]\n",
-1234, -1234, -1234);
simple_printf("%% 07d: [% 07d] %% -07d: [% -07d] %% 07d: [% 07d]\n",
1234, 1234, 1234);
simple_printf("%% 07d: [% 07d] %% -07d: [% -07d] %% 07d: [% 07d]\n",
-1234, -1234, -1234);
simple_printf("%%+07d: [%+07d] %%+-07d: [%+-07d] %%+07d: [%+07d]\n",
1234, 1234, 1234);
simple_printf("%%+07d: [%+07d] %%+-07d: [%+-07d] %%+07d: [%+07d]\n",
-1234, -1234, -1234);
simple_printf("\nWidth from '*', string \"x\":\n");
for (int i = -10; i <= 10; ++i) {
simple_printf("%%*s, * == %+3d: [%*s]\n", i, i, "x");
}
simple_printf("\nPrecision from '*', string \"01234567\":\n");
for (int i = -10; i <= 10; ++i) {
simple_printf("%%.*s, * == %+3d: [%.*s]\n", i, i, "01234567");
}
simple_printf("\nZero precision zeros should print nothing: "
"[%%.d%%.i%%.u%%.o%%.x%%.X] -> [%.d%.i%.u%.o%.x%.X]\n",
0, 0, 0, 0, 0, 0);
simple_printf("Zero width zeros should print something: "
"[%%*d%%*i%%*u%%*o%%*x%%*X] -> \[%*d%*i%*u%*o%*x%*X]\n",
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}