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
|
#ifndef __UTIL_SPRINTF_H
#define __UTIL_SPRINTF_H
#include <stdarg.h>
#include <stdint.h>
#include <builtins.h>
class Console;
namespace Util
{
class ConsoleTraits
{
public:
enum trait { NONE, HEX, DEC, };
};
template <typename _T, ConsoleTraits::trait _S = ConsoleTraits::NONE>
class ConsoleDisplay
{
public:
template <typename _F>
ALWAYS_INLINE
static size_t display(_F& c, _T value) { return 0; };
};
template <ConsoleTraits::trait _S>
class ConsoleDisplay<char*, _S>
{
public:
template <typename _F>
ALWAYS_INLINE
static size_t display(_F& c, char* value)
{
size_t count = 0;
while(*value != '\0')
{
c(*value);
value++;
count++;
}
return count;
}
};
template <>
class ConsoleDisplay<char, ConsoleTraits::NONE>
{
public:
template <typename _F>
ALWAYS_INLINE
static size_t display(_F& c, char value)
{
c(value);
return 1;
}
};
template <typename _T>
class ConsoleDisplay<_T, ConsoleTraits::DEC>
{
public:
template <typename _F>
ALWAYS_INLINE
static size_t display(_F& c, _T value)
{
size_t count = 0;
if (value == 0)
{
c('0'); count++;
}
else if (value < 0)
{
c('-'); count++;
value *= -1;
count += subdisplay(c, value);
}
else
count += subdisplay(c, value);
return count;
}
template <typename _F>
static size_t subdisplay(_F& c, _T value)
{
size_t count = 0;
if (value != 0)
{
count += subdisplay(c, value / 10);
c('0' + (value % 10));
count++;
}
return count;
}
};
template<typename _T>
class ConsoleDisplay<_T, ConsoleTraits::HEX>
{
public:
template <typename _F>
ALWAYS_INLINE
static size_t display(_F& c, _T value)
{
size_t length = sizeof(_T) * 2;
subdisplay(c, value, length);
return length;
}
template <typename _F>
static void subdisplay(_F& c, _T value, size_t length)
{
if (length == 0) return;
subdisplay(c, value / 16, length-1);
char nibble = value % 16;
if (nibble >= 0x0a)
c('A' + (nibble - 0x0a));
else
c('0' + nibble);
}
};
template<typename _F>
size_t vasprintf(_F output_func, const char* fmt_str, va_list& args)
{
size_t count = 0;
bool format = false;
int size = 0;
while('\0' != *fmt_str)
{
if (('%' == *fmt_str) || (format))
switch (*fmt_str)
{
case '%':
{
if (format)
{
count +=
ConsoleDisplay<char>::display(output_func,
'%');
format = false;
}
else
{
format = true;
size = 2;
}
break;
}
case 'c':
{
format = false;
count += ConsoleDisplay<char>
::display(output_func, (char)va_arg(args,int));
break;
}
case 'h':
{
size--;
break;
}
case 'l':
{
size++;
break;
}
case 'z': // size_t or ssize_t
{
size = 4;
break;
}
case 'd': // decimal
{
format = false;
switch(size)
{
case 0:
count += ConsoleDisplay<char,
ConsoleTraits::DEC>
::display(output_func,
(char)va_arg(args,int));
break;
case 1:
count += ConsoleDisplay<short,
ConsoleTraits::DEC>
::display(output_func,
(short)va_arg(args,int));
break;
case 2:
count += ConsoleDisplay<int,
ConsoleTraits::DEC>
::display(output_func,
va_arg(args,int));
break;
case 3:
case 4:
count += ConsoleDisplay<long,
ConsoleTraits::DEC>
::display(output_func,
va_arg(args,long));
break;
}
break;
}
case 'u': // unsigned decimal
{
format = false;
switch(size)
{
case 0:
count += ConsoleDisplay<unsigned char,
ConsoleTraits::DEC>
::display(output_func,
(unsigned char)
va_arg(args,unsigned int));
break;
case 1:
count += ConsoleDisplay<unsigned short,
ConsoleTraits::DEC>
::display(output_func,
(unsigned short)
va_arg(args,unsigned int));
break;
case 2:
count += ConsoleDisplay<unsigned int,
ConsoleTraits::DEC>
::display(output_func,
va_arg(args,unsigned int));
break;
case 3:
case 4:
count += ConsoleDisplay<unsigned long,
ConsoleTraits::DEC>
::display(output_func,
va_arg(args,unsigned long));
break;
}
break;
}
case 'x': // unsigned hex
case 'X':
{
format = false;
switch(size)
{
case 0:
count += ConsoleDisplay<unsigned char,
ConsoleTraits::HEX>
::display(output_func,
(unsigned char)
va_arg(args,unsigned int));
break;
case 1:
count += ConsoleDisplay<unsigned short,
ConsoleTraits::HEX>
::display(output_func,
(unsigned short)
va_arg(args,unsigned int));
break;
case 2:
count += ConsoleDisplay<unsigned int,
ConsoleTraits::HEX>
::display(output_func,
va_arg(args,unsigned int));
break;
case 3:
case 4:
count += ConsoleDisplay<unsigned long,
ConsoleTraits::HEX>
::display(output_func,
va_arg(args,unsigned long));
break;
}
break;
}
case 's': // string
{
format = false;
count += ConsoleDisplay<char*>
::display(output_func,
(char*) va_arg(args,void*));
break;
}
case 'p': // pointer
{
format = false;
count += ConsoleDisplay<char>
::display(output_func,
'0');
count += ConsoleDisplay<char>
::display(output_func,
'x');
count += ConsoleDisplay<unsigned long,
ConsoleTraits::HEX>
::display(output_func,
va_arg(args,unsigned long));
break;
}
}
else
count += ConsoleDisplay<char>::display(output_func, *fmt_str);
fmt_str++;
}
return count;
}
};
#endif
|