-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_printf_v6.c
More file actions
860 lines (742 loc) · 31.5 KB
/
simple_printf_v6.c
File metadata and controls
860 lines (742 loc) · 31.5 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
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
853
854
855
856
857
858
859
860
#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
*/
/*
* Some conversions need the "signed integer type corresponding to size_t."
* The language spec doesn't name the type, so try to determine it.
*/
#if ULLONG_MAX == SIZE_MAX
typedef long long ssize_type;
#elif ULONG_MAX == SIZE_MAX
typedef long ssize_type;
#elif UINT_MAX == SIZE_MAX
typedef int ssize_type;
#else
#error Could not determine signed type corresponding to size_t.
#endif
/*
* Some conversions need the "unsigned integer type corresponding to ptrdiff_t."
* The language spec doesn't name the type, so try to determine it.
*/
#if LLONG_MAX == PTRDIFF_MAX
typedef unsigned long long uptrdiff_type;
#elif LONG_MAX == PTRDIFF_MAX
typedef unsigned long uptrdiff_type;
#elif INT_MAX == PTRDIFF_MAX
typedef unsigned uptrdiff_type;
#else
#error Could not determine unsigned type corresponding to ptrdiff_t.
#endif
/* 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 */
kSizeIntMaxT, /* 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 ' ' '-' */
};
/* Abstracts away how text gets output, and how much was actually output. */
struct printer {
union {
FILE *file;
char *buf;
};
size_t max;
size_t total;
void (*copy)(struct printer *p, const char *s, size_t length);
void (*fill)(struct printer *p, char c, size_t length);
void (*putc)(struct printer *p, char c);
};
/* 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(
uintmax_t 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, and not alternate. */
if (!soft_prec && prec == 0 && value == 0 && !is_alt) {
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(
struct printer *p, const char *s, int len, int width, bool left_justify) {
if (!left_justify && len < width) {
p->fill(p, ' ', width - len);
}
p->copy(p, s, len);
if (left_justify && len < width) {
p->fill(p, ' ', width - len);
}
}
/* Gets a signed argument of the specified size. */
static uintmax_t 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 kSizeIntMaxT: { return va_arg(*args, intmax_t); }
case kSizeSizeT: { return va_arg(*args, ssize_type); }
case kSizePtrDiffT: { return va_arg(*args, ptrdiff_t); }
}
/* Unknown size. Fall back to int. */
return va_arg(*args, int);
}
/* Gets an unsigned argument of the specified size. */
static uintmax_t 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 kSizeIntMaxT: { return va_arg(*args, uintmax_t); }
case kSizeSizeT: { return va_arg(*args, size_t); }
case kSizePtrDiffT: { return va_arg(*args, uptrdiff_type); }
}
/* Unknown size. Fall back to unsigned. */
return va_arg(*args, unsigned);
}
/* Stores the current character count to the appropriate sort of pointer. */
static void store_character_count(va_list *args, int size, size_t total) {
/* The size modifier determines the data type of the pointer argument. */
switch (size) {
case kSizeChar: { *va_arg(*args, signed char *) = total; break; }
case kSizeShort: { *va_arg(*args, short *) = total; break; }
case kSizeDefault: { *va_arg(*args, int *) = total; break; }
case kSizeLong: { *va_arg(*args, long *) = total; break; }
case kSizeLongLong: { *va_arg(*args, long long *) = total; break; }
case kSizeIntMaxT: { *va_arg(*args, intmax_t *) = total; break; }
case kSizeSizeT: { *va_arg(*args, size_t *) = total; break; }
case kSizePtrDiffT: { *va_arg(*args, ptrdiff_t *) = total; break; }
/* Unknown: Guess int. */
default: { *va_arg(*args, int *) = total; break; }
}
}
/*
* 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"
* -- Pointers: "p".
* -- 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 "%%".
* -- Reporting length of printed string with "n".
* -- Returning length of printed string.
* -- Printing to a stream other than stdout.
* -- Printing to a buffer.
*
* Not supported:
* -- Floating point.
* -- Wide characters ("%lc").
* -- Wide character strings ("%ls").
*
* I guess it's no longer so simple...
*/
static void printf_core(struct printer *p, const char *fmt, va_list args) {
char buf[INT_BUF_SIZE];
const char *pfmt = NULL;
for (int ch = *fmt++; ch; ch = *fmt++) {
/*
* If it's not %, just print the character. The span [pfmt, fmt) holds
* the fixed characters.
*/
if (ch != '%') {
if (!pfmt) {
pfmt = fmt - 1;
}
continue;
}
/* Output any batched up non-conversion characters in format. */
if (pfmt) {
p->copy(p, pfmt, fmt - pfmt - 1);
pfmt = NULL;
}
/* It's (potentially) a conversion. Let's take a 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 = kSizeIntMaxT; 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;
p->putc(p, '%');
break;
}
buf[0] = va_arg(args, int);
print_string(p, buf, 1, width, left_justify);
break;
}
case 's': {
/* For now, we don't support %ls. */
if (size != kSizeDefault) {
fmt = initial;
p->putc(p, '%');
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(p, 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': {
uintmax_t 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(p, buf + idx, len, width, left_justify);
break;
}
case 'p': {
/* Print pointers like %#x with an appropriate length modifier. */
void *ptr = va_arg(args, void *);
uintmax_t val = (uintptr_t)ptr;
int idx = conv_integer(val, false, false, false, true, 1, false,
16, buf);
int len = INT_BUF_SIZE - idx - 1;
print_string(p, buf + idx, len, width, left_justify);
break;
}
case 'n': {
store_character_count(&args, size, p->total);
break;
}
case '%': {
/* %% prints '%' */
p->putc(p, '%');
break;
}
default: {
/* Not a valid conversion. Print the '%' and back up. */
p->putc(p, '%');
fmt = initial;
break;
}
}
}
if (pfmt) {
p->copy(p, pfmt, fmt - pfmt - 1);
pfmt = NULL;
}
}
/* Copies a string to a file. */
static void printer_file_copy(struct printer *p, const char *s, size_t len) {
p->total += len;
fwrite(s, 1, len, p->file);
}
/* Writes a block of fill characters to a file. */
static void printer_file_fill(struct printer *p, char c, size_t len) {
char buf[32];
memset(buf, c, sizeof(buf));
p->total += len;
while (len >= sizeof(buf)) {
fwrite(buf, 1, sizeof(buf), p->file);
len -= sizeof(buf);
}
if (len > 0) {
fwrite(buf, 1, len, p->file);
}
}
/* Copies a character to a file. */
static void printer_file_putc(struct printer *p, char c) {
p->total++;
fputc(c, p->file);
}
/* Wrapper around printf_core for printing to stdout. */
int simple_printf(const char *fmt, ...) {
struct printer p = {
.file = stdout,
.total = 0,
.copy = printer_file_copy,
.fill = printer_file_fill,
.putc = printer_file_putc
};
va_list args;
va_start(args, fmt);
printf_core(&p, fmt, args);
va_end(args);
return p.total;
}
/* Copies a string to a buffer. */
static void printer_buf_copy(struct printer *p, const char *s, size_t len) {
if (p->total >= p->max) {
p->total += len;
return;
}
size_t avail = p->max - p->total;
char *target = p->buf + p->total;
p->total += len;
if (len > avail) { len = avail; } /* Limit our output to what's allowed. */
memcpy(target, s, len);
}
/* Writes a block of fill characters to a buffer. */
static void printer_buf_fill(struct printer *p, char c, size_t len) {
if (p->total >= p->max) {
p->total += len;
return;
}
size_t avail = p->max - p->total;
char *target = p->buf + p->total;
p->total += len;
if (len > avail) { len = avail; } /* Limit our output to what's allowed. */
memset(target, c, len);
}
/* Copies a character to a buffer. */
static void printer_buf_putc(struct printer *p, char c) {
if (p->total >= p->max) {
p->total++;
return;
}
p->buf[p->total++] = c;
}
/* Wrapper around printf_core for printing to a buffer. */
int simple_snprintf(char *buf, size_t max, const char *fmt, ...) {
struct printer p = {
.buf = buf,
.max = max > 0 ? max - 1 : 0, /* Save room for null! */
.total = 0,
.copy = printer_buf_copy,
.fill = printer_buf_fill,
.putc = printer_buf_putc
};
va_list args;
va_start(args, fmt);
printf_core(&p, fmt, args);
va_end(args);
/* Null terminate result. */
size_t end = p.total < p.max ? p.total : p.max;
p.buf[end] = '\0';
return p.total; /* Total converted characters, possibly more than max. */
}
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);
int x;
simple_printf("Pointer: (void *)&x = %p\n", (void *)&x);
simple_printf("\nsimple_snprintf with various size buffers:\n");
for (int i = 50; i >= 0; i -= 5) {
char buf[50];
x = simple_snprintf(buf, i, "This is a test: %.16llX%.16llX",
0xDEADBEEFDEADBEEFULL, 0xABCDABCDABCDABCDULL);
simple_printf("x=%d, buf=[%s]\n", x, buf);
}
simple_printf("\nTesting %%n with different widths.\n");
char hh0 = -99, hh1 = -99;
short h0 = -9999, h1 = -9999;
int i0 = -9999, i1 = -9999;
long l0 = -9999, l1 = -9999;
long long ll0 = -9999, ll1 = -9999;
intmax_t j0 = -9999, j1 = -9999;
ssize_type z0 = -9999, z1 = -9999;
ptrdiff_t t0 = -9999, t1 = -9999;
simple_printf(
"ABCDE%hhn%hn%n%ln%lln%jn%zn%tnFGHIJ%hhn%hn%n%ln%lln%jn%zn%tn\n",
&hh0, &h0, &i0, &l0, &ll0, &j0, &z0, &t0, /* all should be 5 */
&hh1, &h1, &i1, &l1, &ll1, &j1, &z1, &t1); /* all should be 10 */
simple_printf(
"hh0=%d, h0=%d, i0=%d, l0=%ld, ll0=%lld, j0=%jd, z0=%zd, t0=%td\n",
hh0, h0, i0, l0, ll0, j0, z0, t0);
simple_printf(
"hh1=%d, h1=%d, i1=%d, l1=%ld, ll1=%lld, j1=%jd, z1=%zd, t1=%td\n",
hh1, h1, i1, l1, ll1, j1, z1, t1);
}