-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_printf_v4.c
More file actions
194 lines (171 loc) · 6.16 KB
/
simple_printf_v4.c
File metadata and controls
194 lines (171 loc) · 6.16 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
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
/*
* Copyright 2023, J. Zbiciak <joe.zbiciak@leftturnonly.info>
* Author: Joe Zbiciak <joe.zbiciak@leftturnonly.info>
* SPDX-License-Identifier: CC-BY-SA-4.0
*/
/* Assume MSB is sign bit. */
#define SIGN_BIT (ULLONG_MAX - ULLONG_MAX / 2)
static const char *const hex_digits[2] = {
"0123456789abcdef", "0123456789ABCDEF"
};
/* Prints an integer in the specified base. */
void print_integer(unsigned long long value, bool is_signed, bool is_caps,
unsigned base) {
bool is_negative = false;
char buf[24];
if (is_signed && (value & SIGN_BIT)) {
is_negative = true;
value = -value;
}
int idx = 24;
buf[--idx] = '\0';
do {
buf[--idx] = hex_digits[is_caps][value % base];
value /= base;
} while (value > 0);
if (is_negative) {
buf[--idx] = '-';
}
fputs(buf + idx, stdout);
}
/*
* Simplified printf that understands:
* -- Strings: %s
* -- Signed decimal integers: %d, %ld, %lld, %i, %li, %lli
* -- Unsigned decimal integers: %u, %lu, %llu
* -- Lowercase hexadecimal integers: %x, %lx, %llx
* -- Uppercase hexadecimal integers: %X, %lX, %llX
* -- Printing % with %%.
*/
void simple_printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
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 take a look. */
const char *initial = fmt;
int conv = *fmt++;
int longlong = 0;
/* Look for long and long long modifiers. */
while (conv == 'l') {
longlong++;
conv = *fmt++;
}
if (longlong > 2) {
/* Overly long "long long". Print the '%' and skip the conversion. */
fmt = initial;
putchar('%');
continue;
}
bool is_caps = false;
bool is_signed = true;
int base = 10;
switch (conv) {
case 's': {
/* If longlong is non-zero, this is a failed conversion. */
if (longlong > 0) {
fmt = initial;
putchar('%');
continue;
}
/* %s is a string. */
const char *s = va_arg(args, const char *);
fputs(s, stdout);
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; /* Set hex unless we fell through. */
/* 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 v;
if (is_signed) {
v = longlong == 0 ? (unsigned long long)va_arg(args, int)
: longlong == 1 ? (unsigned long long)va_arg(args, long)
: (unsigned long long)va_arg(args, long long);
} else {
v = longlong == 0 ? va_arg(args, unsigned)
: longlong == 1 ? va_arg(args, unsigned long)
: va_arg(args, unsigned long long);
}
print_integer(v, is_signed, is_caps, base);
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\n", 0);
simple_printf("Positive %%d: %d\n", 123456789);
simple_printf("Negative %%d: %d\n", -123456789);
simple_printf("Positive %%i: %i\n", 123456789);
simple_printf("Negative %%i: %i\n", -123456789);
simple_printf("Unsigned %%u: %u\n", 4000000000U);
simple_printf("Octal %%o: %o\n", 123456789);
simple_printf("Octal %%o: %o\n", -123456789);
simple_printf("Octal %%o: %o\n", 4000000000U);
simple_printf("Hex %%x: %x\n", 123456789);
simple_printf("Hex %%x: %x\n", -123456789);
simple_printf("Hex %%x: %x\n", 4000000000U);
simple_printf("Hex %%X: %X\n", 123456789);
simple_printf("Hex %%X: %X\n", -123456789);
simple_printf("Hex %%X: %X\n", 4000000000U);
simple_printf("Positive %%ld: %ld\n", 123456789L);
simple_printf("Negative %%ld: %ld\n", -123456789L);
simple_printf("Positive %%li: %li\n", 123456789L);
simple_printf("Negative %%li: %li\n", -123456789L);
simple_printf("Unsigned %%lu: %lu\n", 4000000000UL);
simple_printf("Octal %%lx: %lo\n", 123456789L);
simple_printf("Octal %%lx: %lo\n", -123456789L);
simple_printf("Octal %%lx: %lo\n", 4000000000UL);
simple_printf("Hex %%lx: %lx\n", 123456789L);
simple_printf("Hex %%lx: %lx\n", -123456789L);
simple_printf("Hex %%lx: %lx\n", 4000000000UL);
simple_printf("Hex %%lX: %lX\n", 123456789L);
simple_printf("Hex %%lX: %lX\n", -123456789L);
simple_printf("Hex %%lX: %lX\n", 4000000000UL);
simple_printf("Positive %%lld: %lld\n", 123456789123456789LL);
simple_printf("Negative %%lld: %lld\n", -123456789123456789LL);
simple_printf("Positive %%lli: %lli\n", 123456789123456789LL);
simple_printf("Negative %%lli: %lli\n", -123456789123456789LL);
simple_printf("Unsigned %%llu: %llu\n", 4000000000000000000ULL);
simple_printf("Octal %%llo: %llo\n", 123456789123456789LL);
simple_printf("Octal %%llo: %llo\n", -123456789123456789LL);
simple_printf("Octal %%llo: %llo\n", 4000000000000000000ULL);
simple_printf("Hex %%llx: %llx\n", 123456789123456789LL);
simple_printf("Hex %%llx: %llx\n", -123456789123456789LL);
simple_printf("Hex %%llx: %llx\n", 4000000000000000000ULL);
simple_printf("Hex %%llX: %llX\n", 123456789123456789LL);
simple_printf("Hex %%llX: %llX\n", -123456789123456789LL);
simple_printf("Hex %%llX: %llX\n", 4000000000000000000ULL);
}