3 /* vsnprintf( char *, size_t, const char *, va_list ap )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
14 int vsnprintf( char * s, size_t n, const char * format, _PDCLIB_va_list arg )
16 /* TODO: This function should interpret format as multibyte characters. */
17 /* Members: base, flags, n, i, this, s, width, prec, stream, arg */
18 struct _PDCLIB_status_t status;
28 va_copy( status.arg, arg );
29 // = { 0, 0, n, 0, 0, s, 0, 0, NULL };
30 while ( *format != '\0' )
33 if ( ( *format != '%' ) || ( ( rc = _PDCLIB_print( format, &status ) ) == format ) )
35 /* No conversion specifier, print verbatim */
36 s[ status.i++ ] = *(format++);
40 /* Continue parsing after conversion specifier */
52 #include <_PDCLIB_test.h>
59 static int testprintf( char * s, size_t n, const char * format, ... )
63 va_start( arg, format );
64 i = vsnprintf( s, n, format, arg );
72 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
73 TESTCASE( strcmp( buffer, "-128" ) == 0 );
74 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
75 TESTCASE( strcmp( buffer, "127" ) == 0 );
76 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
77 TESTCASE( strcmp( buffer, "0" ) == 0 );
78 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
79 TESTCASE( strcmp( buffer, "-32768" ) == 0 );
80 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
81 TESTCASE( strcmp( buffer, "32767" ) == 0 );
82 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
83 TESTCASE( strcmp( buffer, "0" ) == 0 );
84 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
85 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
86 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
87 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
88 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
89 TESTCASE( strcmp( buffer, "0" ) == 0 );
90 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
91 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
92 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
93 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
94 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
95 TESTCASE( strcmp( buffer, "0" ) == 0 );
96 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
97 TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
98 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
99 TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
100 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
101 TESTCASE( strcmp( buffer, "0" ) == 0 );
102 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
103 TESTCASE( strcmp( buffer, "255" ) == 0 );
104 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
105 TESTCASE( strcmp( buffer, "255" ) == 0 );
106 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
107 TESTCASE( strcmp( buffer, "65535" ) == 0 );
108 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
109 TESTCASE( strcmp( buffer, "65535" ) == 0 );
110 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
111 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
112 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
113 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
114 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
115 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
116 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
117 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
118 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
119 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
120 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
121 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
122 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
123 TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
124 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
125 TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
126 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
127 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
128 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
129 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
130 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
131 TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
132 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
133 TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
134 /* TODO: This test case is broken, doesn't test what it was intended to. */
135 TESTCASE( testprintf( buffer, 100, "%.0#o", 0 ) == 5 );
136 TESTCASE( strcmp( buffer, "%.0#o" ) == 0 );
137 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
138 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
139 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
140 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
141 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
142 TESTCASE( strcmp( buffer, "+0" ) == 0 );
143 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
144 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
145 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
146 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
147 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
148 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
149 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
150 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
151 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
152 TESTCASE( strcmp( buffer, " 0" ) == 0 );
153 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
154 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
155 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
156 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
157 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
158 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
159 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
160 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
161 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
162 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
163 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
164 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
165 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
166 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
167 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
168 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
169 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
170 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
171 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
172 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
173 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
174 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
175 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
176 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
177 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
178 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
179 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
180 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
181 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
182 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
183 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
184 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
185 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
186 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
187 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
188 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
189 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
190 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
191 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
192 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
193 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
194 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
195 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
196 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
197 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
198 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
199 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
200 TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
201 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
202 TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
203 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
204 TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
205 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
206 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
207 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
208 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
209 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
210 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
211 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
212 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
213 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
214 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
215 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
216 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
217 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
218 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
219 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
220 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
221 TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
222 TESTCASE( strcmp( buffer, "2147483" ) == 0 );
223 TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
224 TESTCASE( strcmp( buffer, "-214748" ) == 0 );
225 TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
226 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
227 TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
228 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
229 TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
230 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
231 TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
232 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
233 TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
234 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
235 TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
236 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
237 TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
238 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
239 TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
240 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
241 TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
242 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
243 TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
244 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
245 TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
246 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
247 TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
248 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
249 TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
250 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
251 TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
252 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
253 TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
254 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
255 TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
256 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
257 TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
258 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
259 TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
260 TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
261 TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
262 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
263 TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
264 TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
265 TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
266 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
267 TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
268 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
269 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
270 TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
271 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
272 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
273 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
274 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
275 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
276 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
277 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
278 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
279 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
280 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
281 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
282 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
283 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
284 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
285 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
286 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
287 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
288 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
289 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
290 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
291 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
292 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
293 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
294 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
295 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
296 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
297 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
298 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
299 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
300 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
301 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
302 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
303 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
304 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
305 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
306 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
307 TESTCASE( testprintf( buffer, 100, "- %d", INT_MAX ) == 12 );
308 TESTCASE( strcmp( buffer, "- 2147483647" ) == 0 );
309 TESTCASE( testprintf( buffer, 100, "- %d %% %d", INT_MAX, INT_MIN ) == 26 );
310 TESTCASE( strcmp( buffer, "- 2147483647 % -2147483648" ) == 0 );
311 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
312 TESTCASE( strcmp( buffer, "x" ) == 0 );
313 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
314 TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
315 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
316 TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
319 TESTCASE( testprintf( buffer, 100, "123456%n789%n", &val1, &val2 ) == 9 );
320 TESTCASE( strcmp( buffer, "123456789" ) == 0 );
321 TESTCASE( val1 == 6 );
322 TESTCASE( val2 == 9 );