-
Notifications
You must be signed in to change notification settings - Fork 1
/
sprintf.cpp
executable file
·227 lines (202 loc) · 4.03 KB
/
sprintf.cpp
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
/***
sprintf.cpp
A version of sprintf() that doesn't suck. Allocates the memory needed for the
formatted string and returns it.
Copyright (c) 2014-2022 C. M. Street
***/
char *mysprintf(const char *fmt, ...) {
u32 len;
va_list args;
char *buf;
char *str;
const char* w;
union {
int i;
long l;
long long ll;
short s;
double g;
long double lg;
char* str;
char c;
int *pi;
} data;
va_start(args, fmt);
len = strlen(fmt) * 2 + 256UL;
w = fmt;
while (*w)
{
if ('%' == *w)
{
bool numeric = false, numericd = false;
bool decimal = false;
i32 numval;
i32 decval;
i32 lenmod = 0;
#define RESET_STATE() do { numeric = numericd = decimal = false; lenmod = decval = numval = 0; } while (0)
#define INVALID_LENMOD() ((lenmod == 3) || (lenmod > 8) || (lenmod > 4 && (lenmod & 3) > 0) || (lenmod < 0))
LAgain:
++w;
switch (*w)
{
case '%':
break;
case 0:
goto LDone;
case '*':
case '+':
case '-':
case ' ':
case '#':
goto LAgain;
case '.':
decimal = true;
goto LAgain;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (!numeric)
{
numeric = true;
numval = atoi(w);
if (numval > 2048)
return(NULL); // ???
}
else if (decimal && !numericd)
{
numericd = true;
decval = atoi(w);
if (decval > 2048)
return(NULL);
}
goto LAgain;
case 'h':
lenmod++;
if (INVALID_LENMOD())
return(NULL);
goto LAgain;
case 'l':
case 'L':
lenmod += 4;
if (INVALID_LENMOD())
return(NULL);
goto LAgain;
case 'j':
lenmod = 8;
goto LAgain;
case 'z':
if (sizeof(size_t) == sizeof(long))
lenmod = 4;
else if (sizeof(size_t) == sizeof(long long))
lenmod = 8;
goto LAgain;
case 't':
if (sizeof(ptrdiff_t) == sizeof(long))
lenmod = 4;
else if (sizeof(ptrdiff_t) == sizeof(long long))
lenmod = 8;
goto LAgain;
case 'n':
data.pi = va_arg(args, int*);
break;
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
/* integer types */
switch (lenmod)
{
case 0:
data.i = va_arg(args, int);
break;
case 1:
// short is actually stored as an int in the argument list.
data.s = va_arg(args, int);
break;
case 2:
// char is actually stored as an int in the argument list.
data.c = va_arg(args, int);
break;
case 4:
data.l = va_arg(args, long);
break;
case 8:
data.ll = va_arg(args, long long);
break;
default:
return(NULL);
}
if (numeric)
len += max_int(32, numval);
else
len += 32;
RESET_STATE();
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'a':
case 'A':
// floating point
switch (lenmod)
{
case 4:
case 8:
data.lg = va_arg(args, long double);
break;
default:
data.g = va_arg(args, double);
break;
}
if (numeric && numericd)
len += max_int(32, numval + decval + 1);
else if (numericd)
len += max_int(32, decval + 1);
else if (numeric)
len += max_int(32, numval);
else
len += 32;
RESET_STATE();
break;
case 'c':
data.c = va_arg(args, int);
len += 16;
RESET_STATE();
break;
case 's':
data.str = va_arg(args, char*);
len += (strlen(data.str) + 1);
RESET_STATE();
break;
}
}
++w;
}
LDone:
va_end(args);
buf = new char [len + 1];
if (unlikely(is_null(buf)))
return(NULL);
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
str = new char [strlen(buf) + 1];
if (unlikely(is_null(str)))
return(buf);
strcpy(str, buf);
delete [] buf;
return(str);
}
/*** end sprintf.cpp ***/