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