3 /* _PDCLIB_print( const char *, struct _PDCLIB_status_t * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
15 /* Using an integer's bits as flags for both the conversion flags and length
28 #define E_intmax 1<<10
30 #define E_ptrdiff 1<<12
31 #define E_intptr 1<<13
32 #define E_ldouble 1<<14
34 #define E_unsigned 1<<16
36 /* This macro delivers a given character to either a memory buffer or a stream,
37 depending on the contents of 'status' (struct _PDCLIB_status_t).
38 x - the character to be delivered
39 i - pointer to number of characters already delivered in this call
40 n - pointer to maximum number of characters to be delivered in this call
41 s - the buffer into which the character shall be delivered
43 #define DELIVER( x ) do { if ( status->i < status->n ) { if ( status->stream != NULL ) { status->stream->buffer[status->stream->bufidx++] = x; if ( ( status->stream->bufidx == status->stream->bufsize ) || ( ( status->stream->status & _IOLBF ) && ( x == '\n' ) ) || ( status->stream->status & _IONBF ) ) fflush( status->stream ); } else status->s[status->i] = x; } ++(status->i); } while ( 0 )
45 /* This function recursively converts a given integer value to a character
46 stream. The conversion is done under the control of a given status struct
47 and written either to a character string or a stream, depending on that
48 same status struct. The status struct also keeps the function from exceeding
49 snprintf() limits, and enables any necessary padding / prefixing of the
50 output once the number of characters to be printed is known, which happens
51 at the lowermost recursion level.
53 static void int2base( intmax_t value, struct _PDCLIB_status_t * status )
55 /* Registering the character being printed at the end of the function here
56 already so it will be taken into account when the deepestmost recursion
57 does the prefix / padding stuff.
60 if ( ( value / status->base ) != 0 )
62 /* More digits to be done - recurse deeper */
63 int2base( value / status->base, status );
67 /* We reached the last digit, the deepest point of our recursion, and
68 only now know how long the number to be printed actually is. Now we
69 have to do the sign, prefix, width, and precision padding stuff
70 before printing the numbers while we resurface from the recursion.
72 /* At worst, we need two prefix characters (hex prefix). */
73 char preface[3] = "\0";
75 if ( ( status->flags & E_alt ) && ( status->base == 16 || status->base == 8 ) )
77 /* Octal / hexadecimal prefix for "%#" conversions */
78 preface[ preidx++ ] = '0';
79 if ( status->base == 16 )
81 preface[ preidx++ ] = ( status->flags & E_lower ) ? 'x' : 'X';
86 /* Negative sign for negative values - at all times. */
87 preface[ preidx++ ] = '-';
89 else if ( ! ( status->flags & E_unsigned ) )
91 /* plus sign / extra space are only for unsigned conversions */
92 if ( status->flags & E_plus )
94 preface[ preidx++ ] = '+';
96 else if ( status->flags & E_space )
98 preface[ preidx++ ] = ' ';
102 size_t prec_pads = ( status->prec > status->this ) ? ( status->prec - status->this ) : 0;
103 if ( ! ( status->flags & ( E_minus | E_zero ) ) )
105 /* Space padding is only done if no zero padding or left alignment
106 is requested. Leave space for any prefixes determined above.
108 /* The number of characters to be printed, plus prefixes if any. */
109 /* This line contained probably the most stupid, time-wasting bug
110 I've ever perpetrated. Greetings to Samface, DevL, and all
111 sceners at Breakpoint 2006.
113 size_t characters = preidx + ( ( status->this > status->prec ) ? status->this : status->prec );
114 if ( status->width > characters )
116 for ( int i = 0; i < status->width - characters; ++i )
122 if ( status->i < status->n )
124 if ( status->stream != 0 )
127 status->stream->buffer[status->stream->bufidx++] = (char)' ',
128 if ( ( status->stream->bufidx == status->stream->bufsize )
129 || ( ( status->stream->status & 2 ) && (char)' ' == '\n' )
130 || ( status->stream->status & 4 ) )
131 fflush( status->stream )
134 else status->s[status->i] = ' ';
143 /* Now we did the padding, do the prefixes (if any). */
145 while ( preface[ preidx ] != '\0' )
147 DELIVER( preface[ preidx++ ] );
150 if ( ( ! ( status->flags & E_minus ) ) && ( status->flags & E_zero ) )
152 /* If field is not left aligned, and zero padding is requested, do
155 while ( status->this < status->width )
161 /* Do the precision padding if necessary. */
162 for ( int i = 0; i < prec_pads; ++i )
168 /* Recursion tail - print the current digit. */
170 int digit = value % status->base;
175 if ( status->flags & E_lower )
177 /* Lowercase letters. Same array used for strto...(). */
178 DELIVER( _PDCLIB_digits[ digit ] );
182 /* Uppercase letters. Array only used here, only 0-F. */
183 DELIVER( _PDCLIB_Xdigits[ digit ] );
188 const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status )
190 const char * orig_spec = spec;
191 if ( *(++spec) == '%' )
193 /* %% -> print single '%' */
197 /* Initializing status structure */
204 /* First come 0..n flags */
210 /* left-aligned output */
211 status->flags |= E_minus;
215 /* positive numbers prefixed with '+' */
216 status->flags |= E_plus;
220 /* alternative format (leading 0x for hex, 0 for octal) */
221 status->flags |= E_alt;
225 /* positive numbers prefixed with ' ' */
226 status->flags |= E_space;
230 /* right-aligned padding done with '0' instead of ' ' */
231 status->flags |= E_zero;
235 /* not a flag, exit flag parsing */
236 status->flags |= E_done;
239 } while ( ! ( status->flags & E_done ) );
241 /* Optional field width */
244 /* Retrieve width value from argument stack */
245 if ( ( status->width = va_arg( status->arg, int ) ) < 0 )
247 /* Negative value is '-' flag plus absolute value */
248 status->flags |= E_minus;
255 /* If a width is given, strtol() will return its value. If not given,
256 strtol() will return zero. In both cases, endptr will point to the
257 rest of the conversion specifier - just what we need.
259 status->width = (int)strtol( spec, (char**)&spec, 10 );
262 /* Optional precision */
268 /* Retrieve precision value from argument stack. A negative value
269 is as if no precision is given - as precision is initalized to
270 EOF (negative), there is no need for testing for negative here.
272 status->prec = va_arg( status->arg, int );
277 status->prec = (int)strtol( spec, &endptr, 10 );
278 if ( spec == endptr )
280 /* Decimal point but no number - bad conversion specifier. */
285 /* Having a precision cancels out any zero flag. */
286 status->flags ^= E_zero;
289 /* Optional length modifier
290 We step one character ahead in any case, and step back only if we find
291 there has been no length modifier (or step ahead another character if it
292 has been "hh" or "ll").
300 status->flags |= E_char;
306 status->flags |= E_short;
312 /* ll -> long long */
313 status->flags |= E_llong;
319 status->flags |= E_long;
323 /* j -> intmax_t, which might or might not be long long */
324 status->flags |= E_intmax;
327 /* z -> size_t, which might or might not be unsigned int */
328 status->flags |= E_size;
331 /* t -> ptrdiff_t, which might or might not be long */
332 status->flags |= E_ptrdiff;
335 /* L -> long double */
336 status->flags |= E_ldouble;
343 /* Conversion specifier */
353 status->flags |= E_unsigned;
357 status->flags |= E_unsigned;
361 status->flags |= ( E_lower | E_unsigned );
365 status->flags |= E_unsigned;
378 /* TODO: Flags, wide chars. */
379 DELIVER( va_arg( status->arg, int ) );
382 /* TODO: Flags, wide chars. */
384 char * s = va_arg( status->arg, char * );
392 /* TODO: E_long -> E_intptr */
394 status->flags |= ( E_lower | E_unsigned | E_alt | E_long );
398 int * val = va_arg( status->arg, int * );
403 /* No conversion specifier. Bad conversion. */
407 /* Do the actual output based on our findings */
408 if ( status->base != 0 )
410 /* Integer conversions */
411 /* TODO: Check for invalid flag combinations. */
412 if ( status->flags & E_unsigned )
415 switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_size ) )
418 value = (uintmax_t)(unsigned char)va_arg( status->arg, int );
421 value = (uintmax_t)(unsigned short)va_arg( status->arg, int );
424 value = (uintmax_t)va_arg( status->arg, unsigned int );
427 value = (uintmax_t)va_arg( status->arg, unsigned long );
430 value = (uintmax_t)va_arg( status->arg, unsigned long long );
433 value = (uintmax_t)va_arg( status->arg, size_t );
437 if ( ( value / status->base ) != 0 )
439 int2base( (intmax_t)(value / status->base), status );
441 int digit = value % status->base;
446 if ( status->flags & E_lower )
448 DELIVER( _PDCLIB_digits[ digit ] );
452 DELIVER( _PDCLIB_Xdigits[ digit ] );
457 switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_intmax ) )
460 int2base( (intmax_t)(char)va_arg( status->arg, int ), status );
463 int2base( (intmax_t)(short)va_arg( status->arg, int ), status );
466 int2base( (intmax_t)va_arg( status->arg, int ), status );
469 int2base( (intmax_t)va_arg( status->arg, long ), status );
472 int2base( (intmax_t)va_arg( status->arg, long long ), status );
475 int2base( (intmax_t)va_arg( status->arg, ptrdiff_t ), status );
478 int2base( va_arg( status->arg, intmax_t ), status );
482 if ( status->flags & E_minus )
484 while ( status->this < status->width )
490 if ( status->i >= status->n )
492 status->s[status->n - 1] = '\0';
499 #include <_PDCLIB_test.h>
504 static int testprintf( char * buffer, size_t n, const char * format, ... )
506 /* Members: base, flags, n, i, this, s, width, prec, stream, arg */
507 struct _PDCLIB_status_t status = { 0, 0, n, 0, 0, buffer, 0, 0, NULL, NULL };
508 memset( buffer, '\0', 100 );
509 va_start( status.arg, format );
510 if ( *(_PDCLIB_print( format, &status )) != '\0' )
512 printf( "_PDCLIB_print() did not return end-of-specifier on '%s'.\n", format );
515 va_end( status.arg );
522 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
523 TESTCASE( strcmp( buffer, "-128" ) == 0 );
524 TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
525 TESTCASE( strcmp( buffer, "127" ) == 0 );
526 TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
527 TESTCASE( strcmp( buffer, "0" ) == 0 );
528 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
529 TESTCASE( strcmp( buffer, "-32768" ) == 0 );
530 TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
531 TESTCASE( strcmp( buffer, "32767" ) == 0 );
532 TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
533 TESTCASE( strcmp( buffer, "0" ) == 0 );
534 TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
535 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
536 TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
537 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
538 TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
539 TESTCASE( strcmp( buffer, "0" ) == 0 );
540 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
541 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
542 TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
543 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
544 TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
545 TESTCASE( strcmp( buffer, "0" ) == 0 );
546 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
547 TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
548 TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
549 TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
550 TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
551 TESTCASE( strcmp( buffer, "0" ) == 0 );
552 TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
553 TESTCASE( strcmp( buffer, "255" ) == 0 );
554 TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
555 TESTCASE( strcmp( buffer, "255" ) == 0 );
556 TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
557 TESTCASE( strcmp( buffer, "65535" ) == 0 );
558 TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
559 TESTCASE( strcmp( buffer, "65535" ) == 0 );
560 TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
561 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
562 TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
563 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
564 TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
565 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
566 TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
567 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
568 TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
569 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
570 TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
571 TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
572 TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
573 TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
574 TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
575 TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
576 TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
577 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
578 TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
579 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
580 TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
581 TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
582 TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
583 TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
584 TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
585 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
586 TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
587 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
588 TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
589 TESTCASE( strcmp( buffer, "+0" ) == 0 );
590 TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
591 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
592 TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
593 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
594 TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
595 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
596 TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
597 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
598 TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
599 TESTCASE( strcmp( buffer, " 0" ) == 0 );
600 TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
601 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
602 TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
603 TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
604 TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
605 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
606 TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
607 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
608 TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
609 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
610 TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
611 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
612 TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
613 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
614 TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
615 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
616 TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
617 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
618 TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
619 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
620 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
621 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
622 TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
623 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
624 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
625 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
626 TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
627 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
628 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
629 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
630 TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
631 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
632 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
633 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
634 TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
635 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
636 TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
637 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
638 TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
639 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
640 TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
641 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
642 TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
643 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
644 TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
645 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
646 TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
647 TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
648 TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
649 TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
650 TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
651 TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
652 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
653 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
654 TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
655 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
656 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
657 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
658 TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
659 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
660 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
661 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
662 TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
663 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
664 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
665 TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
666 TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
667 TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
668 TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
669 TESTCASE( strcmp( buffer, "2147483" ) == 0 );
670 TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
671 TESTCASE( strcmp( buffer, "-214748" ) == 0 );
672 TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
673 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
674 TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
675 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
676 TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
677 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
678 TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
679 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
680 TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
681 TESTCASE( strcmp( buffer, "21474836" ) == 0 );
682 TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
683 TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
684 TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
685 TESTCASE( strcmp( buffer, "214748364" ) == 0 );
686 TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
687 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
688 TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
689 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
690 TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
691 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
692 TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
693 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
694 TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
695 TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
696 TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
697 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
698 TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
699 TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
700 TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
701 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
702 TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
703 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
704 TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
705 TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
706 TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
707 TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
708 TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
709 TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
710 TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
711 TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
712 TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
713 TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
714 TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
715 TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
716 TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
717 TESTCASE( strcmp( buffer, " 00000000002147483647" ) == 0 );
718 TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
719 TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
720 TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
721 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
722 TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
723 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
724 TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
725 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
726 TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
727 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
728 TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
729 TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
730 TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
731 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
732 TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
733 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
734 TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
735 TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
736 TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
737 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
738 TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
739 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
740 TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
741 TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
742 TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
743 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
744 TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
745 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
746 TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
747 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
748 TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
749 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
750 TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
751 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
752 TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
753 TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
754 TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
755 TESTCASE( strcmp( buffer, "x" ) == 0 );
756 TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
757 TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
758 TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
759 TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );