1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2010 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* This file can be parametrized with the following macros:
20 VASNPRINTF The name of the function being defined.
21 FCHAR_T The element type of the format string.
22 DCHAR_T The element type of the destination (result) string.
23 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
24 in the format string are ASCII. MUST be set if
25 FCHAR_T and DCHAR_T are not the same type.
26 DIRECTIVE Structure denoting a format directive.
28 DIRECTIVES Structure denoting the set of format directives of a
29 format string. Depends on FCHAR_T.
30 PRINTF_PARSE Function that parses a format string.
32 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
33 DCHAR_SET memset like function for DCHAR_T[] arrays.
34 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
35 SNPRINTF The system's snprintf (or similar) function.
36 This may be either snprintf or swprintf.
37 TCHAR_T The element type of the argument and result string
38 of the said SNPRINTF function. This may be either
39 char or wchar_t. The code exploits that
40 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
41 alignof (TCHAR_T) <= alignof (DCHAR_T).
42 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
43 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
44 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
45 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
46 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
48 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
49 This must come before <config.h> because <config.h> may include
50 <features.h>, and once <features.h> has been included, it's too late. */
52 # define _GNU_SOURCE 1
64 # if WIDE_CHAR_VERSION
65 # include "vasnwprintf.h"
67 # include "vasnprintf.h"
71 #include <locale.h> /* localeconv() */
72 #include <stdio.h> /* snprintf(), sprintf() */
73 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
74 #include <string.h> /* memcpy(), strlen() */
75 #include <errno.h> /* errno */
76 #include <limits.h> /* CHAR_BIT */
77 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
79 # include <langinfo.h>
82 # if WIDE_CHAR_VERSION
83 # include "wprintf-parse.h"
85 # include "printf-parse.h"
89 /* Checked size_t computations. */
92 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
97 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
99 # include "isnand-nolibm.h"
102 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
104 # include "isnanl-nolibm.h"
108 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
110 # include "isnand-nolibm.h"
111 # include "printf-frexp.h"
114 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
116 # include "isnanl-nolibm.h"
117 # include "printf-frexpl.h"
121 /* Default parameters. */
123 # if WIDE_CHAR_VERSION
124 # define VASNPRINTF vasnwprintf
125 # define FCHAR_T wchar_t
126 # define DCHAR_T wchar_t
127 # define TCHAR_T wchar_t
128 # define DCHAR_IS_TCHAR 1
129 # define DIRECTIVE wchar_t_directive
130 # define DIRECTIVES wchar_t_directives
131 # define PRINTF_PARSE wprintf_parse
132 # define DCHAR_CPY wmemcpy
133 # define DCHAR_SET wmemset
135 # define VASNPRINTF vasnprintf
136 # define FCHAR_T char
137 # define DCHAR_T char
138 # define TCHAR_T char
139 # define DCHAR_IS_TCHAR 1
140 # define DIRECTIVE char_directive
141 # define DIRECTIVES char_directives
142 # define PRINTF_PARSE printf_parse
143 # define DCHAR_CPY memcpy
144 # define DCHAR_SET memset
147 #if WIDE_CHAR_VERSION
148 /* TCHAR_T is wchar_t. */
149 # define USE_SNPRINTF 1
150 # if HAVE_DECL__SNWPRINTF
151 /* On Windows, the function swprintf() has a different signature than
152 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
153 instead. The mingw function snwprintf() has fewer bugs than the
154 MSVCRT function _snwprintf(), so prefer that. */
155 # if defined __MINGW32__
156 # define SNPRINTF snwprintf
158 # define SNPRINTF _snwprintf
162 # define SNPRINTF swprintf
165 /* TCHAR_T is char. */
166 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
167 But don't use it on BeOS, since BeOS snprintf produces no output if the
168 size argument is >= 0x3000000.
169 Also don't use it on Linux libc5, since there snprintf with size = 1
170 writes any output without bounds, like sprintf. */
171 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
172 # define USE_SNPRINTF 1
174 # define USE_SNPRINTF 0
176 # if HAVE_DECL__SNPRINTF
177 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
178 function _snprintf(), so prefer that. */
179 # if defined __MINGW32__
180 # define SNPRINTF snprintf
181 /* Here we need to call the native snprintf, not rpl_snprintf. */
184 # define SNPRINTF _snprintf
188 # define SNPRINTF snprintf
189 /* Here we need to call the native snprintf, not rpl_snprintf. */
193 /* Here we need to call the native sprintf, not rpl_sprintf. */
196 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
197 warnings in this file. Use -Dlint to suppress them. */
199 # define IF_LINT(Code) Code
201 # define IF_LINT(Code) /* empty */
204 /* Avoid some warnings from "gcc -Wshadow".
205 This file doesn't use the exp() and remainder() functions. */
209 #define remainder rem
211 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
212 # if (HAVE_STRNLEN && !defined _AIX)
213 # define local_strnlen strnlen
215 # ifndef local_strnlen_defined
216 # define local_strnlen_defined 1
218 local_strnlen (const char *string, size_t maxlen)
220 const char *end = memchr (string, '\0', maxlen);
221 return end ? (size_t) (end - string) : maxlen;
227 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
229 # define local_wcslen wcslen
231 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
232 a dependency towards this library, here is a local substitute.
233 Define this substitute only once, even if this file is included
234 twice in the same compilation unit. */
235 # ifndef local_wcslen_defined
236 # define local_wcslen_defined 1
238 local_wcslen (const wchar_t *s)
242 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
250 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
252 # define local_wcsnlen wcsnlen
254 # ifndef local_wcsnlen_defined
255 # define local_wcsnlen_defined 1
257 local_wcsnlen (const wchar_t *s, size_t maxlen)
261 for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
269 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
270 /* Determine the decimal-point character according to the current locale. */
271 # ifndef decimal_point_char_defined
272 # define decimal_point_char_defined 1
274 decimal_point_char (void)
277 /* Determine it in a multithread-safe way. We know nl_langinfo is
278 multithread-safe on glibc systems and MacOS X systems, but is not required
279 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
280 localeconv() is rarely multithread-safe. */
281 # if HAVE_NL_LANGINFO && (__GLIBC__ || (defined __APPLE__ && defined __MACH__))
282 point = nl_langinfo (RADIXCHAR);
285 sprintf (pointbuf, "%#.0f", 1.0);
286 point = &pointbuf[1];
288 point = localeconv () -> decimal_point;
290 /* The decimal point is always a single byte: either '.' or ','. */
291 return (point[0] != '\0' ? point[0] : '.');
296 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
298 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
300 is_infinite_or_zero (double x)
302 return isnand (x) || x + x == x;
307 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
309 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
311 is_infinite_or_zerol (long double x)
313 return isnanl (x) || x + x == x;
318 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
320 /* Converting 'long double' to decimal without rare rounding bugs requires
321 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
322 (and slower) algorithms. */
324 typedef unsigned int mp_limb_t;
325 # define GMP_LIMB_BITS 32
326 typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
328 typedef unsigned long long mp_twolimb_t;
329 # define GMP_TWOLIMB_BITS 64
330 typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
332 /* Representation of a bignum >= 0. */
336 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
339 /* Compute the product of two bignums >= 0.
340 Return the allocated memory in case of success, NULL in case of memory
341 allocation failure. */
343 multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
350 if (src1.nlimbs <= src2.nlimbs)
364 /* Now 0 <= len1 <= len2. */
367 /* src1 or src2 is zero. */
369 dest->limbs = (mp_limb_t *) malloc (1);
373 /* Here 1 <= len1 <= len2. */
379 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
382 for (k = len2; k > 0; )
384 for (i = 0; i < len1; i++)
386 mp_limb_t digit1 = p1[i];
387 mp_twolimb_t carry = 0;
388 for (j = 0; j < len2; j++)
390 mp_limb_t digit2 = p2[j];
391 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
393 dp[i + j] = (mp_limb_t) carry;
394 carry = carry >> GMP_LIMB_BITS;
396 dp[i + len2] = (mp_limb_t) carry;
399 while (dlen > 0 && dp[dlen - 1] == 0)
407 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
408 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
410 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
412 Return the allocated memory in case of success, NULL in case of memory
413 allocation failure. */
415 divide (mpn_t a, mpn_t b, mpn_t *q)
418 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
419 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
420 If m<n, then q:=0 and r:=a.
421 If m>=n=1, perform a single-precision division:
424 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
425 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
426 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
427 Normalise [q[m-1],...,q[0]], yields q.
428 If m>=n>1, perform a multiple-precision division:
429 We have a/b < beta^(m-n+1).
430 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
431 Shift a and b left by s bits, copying them. r:=a.
432 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
433 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
435 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
436 In case of overflow (q* >= beta) set q* := beta-1.
437 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
438 and c3 := b[n-2] * q*.
439 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
440 occurred. Furthermore 0 <= c3 < beta^2.
441 If there was overflow and
442 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
443 the next test can be skipped.}
444 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
445 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
447 Put r := r - b * q* * beta^j. In detail:
448 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
449 hence: u:=0, for i:=0 to n-1 do
451 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
452 u:=u div beta (+ 1, if carry in subtraction)
454 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
456 the carry u does not overflow.}
457 If a negative carry occurs, put q* := q* - 1
458 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
460 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
461 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
463 The room for q[j] can be allocated at the memory location of r[n+j].
464 Finally, round-to-even:
465 Shift r left by 1 bit.
466 If r > b or if r = b and q[0] is odd, q := q+1.
468 const mp_limb_t *a_ptr = a.limbs;
469 size_t a_len = a.nlimbs;
470 const mp_limb_t *b_ptr = b.limbs;
471 size_t b_len = b.nlimbs;
473 mp_limb_t *tmp_roomptr = NULL;
479 /* Allocate room for a_len+2 digits.
480 (Need a_len+1 digits for the real division and 1 more digit for the
481 final rounding of q.) */
482 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
487 while (a_len > 0 && a_ptr[a_len - 1] == 0)
494 /* Division by zero. */
496 if (b_ptr[b_len - 1] == 0)
502 /* Here m = a_len >= 0 and n = b_len > 0. */
506 /* m<n: trivial case. q=0, r := copy of a. */
509 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
510 q_ptr = roomptr + a_len;
515 /* n=1: single precision division.
516 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
520 mp_limb_t den = b_ptr[0];
521 mp_limb_t remainder = 0;
522 const mp_limb_t *sourceptr = a_ptr + a_len;
523 mp_limb_t *destptr = q_ptr + a_len;
525 for (count = a_len; count > 0; count--)
528 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
529 *--destptr = num / den;
530 remainder = num % den;
532 /* Normalise and store r. */
535 r_ptr[0] = remainder;
542 if (q_ptr[q_len - 1] == 0)
548 /* n>1: multiple precision division.
549 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
550 beta^(m-n-1) <= a/b < beta^(m-n+1). */
554 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
582 /* 0 <= s < GMP_LIMB_BITS.
583 Copy b, shifting it left by s bits. */
586 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
587 if (tmp_roomptr == NULL)
593 const mp_limb_t *sourceptr = b_ptr;
594 mp_limb_t *destptr = tmp_roomptr;
595 mp_twolimb_t accu = 0;
597 for (count = b_len; count > 0; count--)
599 accu += (mp_twolimb_t) *sourceptr++ << s;
600 *destptr++ = (mp_limb_t) accu;
601 accu = accu >> GMP_LIMB_BITS;
603 /* accu must be zero, since that was how s was determined. */
609 /* Copy a, shifting it left by s bits, yields r.
611 At the beginning: r = roomptr[0..a_len],
612 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
616 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
621 const mp_limb_t *sourceptr = a_ptr;
622 mp_limb_t *destptr = r_ptr;
623 mp_twolimb_t accu = 0;
625 for (count = a_len; count > 0; count--)
627 accu += (mp_twolimb_t) *sourceptr++ << s;
628 *destptr++ = (mp_limb_t) accu;
629 accu = accu >> GMP_LIMB_BITS;
631 *destptr++ = (mp_limb_t) accu;
633 q_ptr = roomptr + b_len;
634 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
636 size_t j = a_len - b_len; /* m-n */
637 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
638 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
639 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
640 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
641 /* Division loop, traversed m-n+1 times.
642 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
647 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
649 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
651 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
652 | r_ptr[j + b_len - 1];
653 q_star = num / b_msd;
658 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
659 q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
660 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
661 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
662 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
664 If yes, jump directly to the subtraction loop.
665 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
666 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
667 if (r_ptr[j + b_len] > b_msd
668 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
669 /* r[j+n] >= b[n-1]+1 or
670 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
675 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
677 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
678 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
679 mp_twolimb_t c3 = /* b[n-2] * q* */
680 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
681 /* While c2 < c3, increase c2 and decrease c3.
682 Consider c3-c2. While it is > 0, decrease it by
683 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
684 this can happen only twice. */
687 q_star = q_star - 1; /* q* := q* - 1 */
688 if (c3 - c2 > b_msdd)
689 q_star = q_star - 1; /* q* := q* - 1 */
695 /* Subtract r := r - b * q* * beta^j. */
698 const mp_limb_t *sourceptr = b_ptr;
699 mp_limb_t *destptr = r_ptr + j;
700 mp_twolimb_t carry = 0;
702 for (count = b_len; count > 0; count--)
704 /* Here 0 <= carry <= q*. */
707 + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
708 + (mp_limb_t) ~(*destptr);
709 /* Here 0 <= carry <= beta*q* + beta-1. */
710 *destptr++ = ~(mp_limb_t) carry;
711 carry = carry >> GMP_LIMB_BITS; /* <= q* */
713 cr = (mp_limb_t) carry;
715 /* Subtract cr from r_ptr[j + b_len], then forget about
717 if (cr > r_ptr[j + b_len])
719 /* Subtraction gave a carry. */
720 q_star = q_star - 1; /* q* := q* - 1 */
723 const mp_limb_t *sourceptr = b_ptr;
724 mp_limb_t *destptr = r_ptr + j;
727 for (count = b_len; count > 0; count--)
729 mp_limb_t source1 = *sourceptr++;
730 mp_limb_t source2 = *destptr;
731 *destptr++ = source1 + source2 + carry;
734 ? source1 >= (mp_limb_t) ~source2
735 : source1 > (mp_limb_t) ~source2);
738 /* Forget about the carry and about r[j+n]. */
741 /* q* is determined. Store it as q[j]. */
750 if (q_ptr[q_len - 1] == 0)
752 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
753 b is shifted left by s bits. */
754 /* Shift r right by s bits. */
757 mp_limb_t ptr = r_ptr + r_len;
758 mp_twolimb_t accu = 0;
760 for (count = r_len; count > 0; count--)
762 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
763 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
764 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
769 while (r_len > 0 && r_ptr[r_len - 1] == 0)
772 /* Compare r << 1 with b. */
780 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
781 | (i < r_len ? r_ptr[i] << 1 : 0);
782 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
792 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
797 for (i = 0; i < q_len; i++)
798 if (++(q_ptr[i]) != 0)
803 if (tmp_roomptr != NULL)
810 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
812 Destroys the contents of a.
813 Return the allocated memory - containing the decimal digits in low-to-high
814 order, terminated with a NUL character - in case of success, NULL in case
815 of memory allocation failure. */
817 convert_to_decimal (mpn_t a, size_t extra_zeroes)
819 mp_limb_t *a_ptr = a.limbs;
820 size_t a_len = a.nlimbs;
821 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
822 size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
823 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
827 for (; extra_zeroes > 0; extra_zeroes--)
831 /* Divide a by 10^9, in-place. */
832 mp_limb_t remainder = 0;
833 mp_limb_t *ptr = a_ptr + a_len;
835 for (count = a_len; count > 0; count--)
838 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
839 *ptr = num / 1000000000;
840 remainder = num % 1000000000;
842 /* Store the remainder as 9 decimal digits. */
843 for (count = 9; count > 0; count--)
845 *d_ptr++ = '0' + (remainder % 10);
846 remainder = remainder / 10;
849 if (a_ptr[a_len - 1] == 0)
852 /* Remove leading zeroes. */
853 while (d_ptr > c_ptr && d_ptr[-1] == '0')
855 /* But keep at least one zero. */
858 /* Terminate the string. */
864 # if NEED_PRINTF_LONG_DOUBLE
866 /* Assuming x is finite and >= 0:
867 write x as x = 2^e * m, where m is a bignum.
868 Return the allocated memory in case of success, NULL in case of memory
869 allocation failure. */
871 decode_long_double (long double x, int *ep, mpn_t *mp)
878 /* Allocate memory for result. */
879 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
880 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
883 /* Split into exponential part and mantissa. */
884 y = frexpl (x, &exp);
885 if (!(y >= 0.0L && y < 1.0L))
887 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
888 latter is an integer. */
889 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
890 I'm not sure whether it's safe to cast a 'long double' value between
891 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
892 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
894 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
895 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
898 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
901 if (!(y >= 0.0L && y < 1.0L))
903 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
906 if (!(y >= 0.0L && y < 1.0L))
908 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
913 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
916 if (!(y >= 0.0L && y < 1.0L))
918 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
922 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
925 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
928 if (!(y >= 0.0L && y < 1.0L))
930 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
933 if (!(y >= 0.0L && y < 1.0L))
935 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
937 #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
943 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
946 *ep = exp - LDBL_MANT_BIT;
952 # if NEED_PRINTF_DOUBLE
954 /* Assuming x is finite and >= 0:
955 write x as x = 2^e * m, where m is a bignum.
956 Return the allocated memory in case of success, NULL in case of memory
957 allocation failure. */
959 decode_double (double x, int *ep, mpn_t *mp)
966 /* Allocate memory for result. */
967 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
968 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
971 /* Split into exponential part and mantissa. */
973 if (!(y >= 0.0 && y < 1.0))
975 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
976 latter is an integer. */
977 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
978 I'm not sure whether it's safe to cast a 'double' value between
979 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
980 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
982 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
983 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
986 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
989 if (!(y >= 0.0 && y < 1.0))
991 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
994 if (!(y >= 0.0 && y < 1.0))
996 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1001 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
1004 if (!(y >= 0.0 && y < 1.0))
1006 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
1010 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1013 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1016 if (!(y >= 0.0 && y < 1.0))
1018 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1021 if (!(y >= 0.0 && y < 1.0))
1023 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1028 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
1031 *ep = exp - DBL_MANT_BIT;
1037 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1038 Returns the decimal representation of round (x * 10^n).
1039 Return the allocated memory - containing the decimal digits in low-to-high
1040 order, terminated with a NUL character - in case of success, NULL in case
1041 of memory allocation failure. */
1043 scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1046 size_t extra_zeroes;
1049 mp_limb_t *pow5_ptr;
1051 unsigned int s_limbs;
1052 unsigned int s_bits;
1060 /* x = 2^e * m, hence
1061 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1062 = round (2^s * 5^n * m). */
1065 /* Factor out a common power of 10 if possible. */
1068 extra_zeroes = (s < n ? s : n);
1072 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1073 Before converting to decimal, we need to compute
1074 z = round (2^s * 5^n * m). */
1075 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1076 sign. 2.322 is slightly larger than log(5)/log(2). */
1077 abs_n = (n >= 0 ? n : -n);
1078 abs_s = (s >= 0 ? s : -s);
1079 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1080 + abs_s / GMP_LIMB_BITS + 1)
1081 * sizeof (mp_limb_t));
1082 if (pow5_ptr == NULL)
1087 /* Initialize with 1. */
1090 /* Multiply with 5^|n|. */
1093 static mp_limb_t const small_pow5[13 + 1] =
1095 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1096 48828125, 244140625, 1220703125
1099 for (n13 = 0; n13 <= abs_n; n13 += 13)
1101 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1103 mp_twolimb_t carry = 0;
1104 for (j = 0; j < pow5_len; j++)
1106 mp_limb_t digit2 = pow5_ptr[j];
1107 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1108 pow5_ptr[j] = (mp_limb_t) carry;
1109 carry = carry >> GMP_LIMB_BITS;
1112 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1115 s_limbs = abs_s / GMP_LIMB_BITS;
1116 s_bits = abs_s % GMP_LIMB_BITS;
1117 if (n >= 0 ? s >= 0 : s <= 0)
1119 /* Multiply with 2^|s|. */
1122 mp_limb_t *ptr = pow5_ptr;
1123 mp_twolimb_t accu = 0;
1125 for (count = pow5_len; count > 0; count--)
1127 accu += (mp_twolimb_t) *ptr << s_bits;
1128 *ptr++ = (mp_limb_t) accu;
1129 accu = accu >> GMP_LIMB_BITS;
1133 *ptr = (mp_limb_t) accu;
1140 for (count = pow5_len; count > 0;)
1143 pow5_ptr[s_limbs + count] = pow5_ptr[count];
1145 for (count = s_limbs; count > 0;)
1148 pow5_ptr[count] = 0;
1150 pow5_len += s_limbs;
1152 pow5.limbs = pow5_ptr;
1153 pow5.nlimbs = pow5_len;
1156 /* Multiply m with pow5. No division needed. */
1157 z_memory = multiply (m, pow5, &z);
1161 /* Divide m by pow5 and round. */
1162 z_memory = divide (m, pow5, &z);
1167 pow5.limbs = pow5_ptr;
1168 pow5.nlimbs = pow5_len;
1172 Multiply m with pow5, then divide by 2^|s|. */
1176 tmp_memory = multiply (m, pow5, &numerator);
1177 if (tmp_memory == NULL)
1183 /* Construct 2^|s|. */
1185 mp_limb_t *ptr = pow5_ptr + pow5_len;
1187 for (i = 0; i < s_limbs; i++)
1189 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1190 denominator.limbs = ptr;
1191 denominator.nlimbs = s_limbs + 1;
1193 z_memory = divide (numerator, denominator, &z);
1199 Multiply m with 2^s, then divide by pow5. */
1202 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1203 * sizeof (mp_limb_t));
1204 if (num_ptr == NULL)
1211 mp_limb_t *destptr = num_ptr;
1214 for (i = 0; i < s_limbs; i++)
1219 const mp_limb_t *sourceptr = m.limbs;
1220 mp_twolimb_t accu = 0;
1222 for (count = m.nlimbs; count > 0; count--)
1224 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1225 *destptr++ = (mp_limb_t) accu;
1226 accu = accu >> GMP_LIMB_BITS;
1229 *destptr++ = (mp_limb_t) accu;
1233 const mp_limb_t *sourceptr = m.limbs;
1235 for (count = m.nlimbs; count > 0; count--)
1236 *destptr++ = *sourceptr++;
1238 numerator.limbs = num_ptr;
1239 numerator.nlimbs = destptr - num_ptr;
1241 z_memory = divide (numerator, pow5, &z);
1248 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1250 if (z_memory == NULL)
1252 digits = convert_to_decimal (z, extra_zeroes);
1257 # if NEED_PRINTF_LONG_DOUBLE
1259 /* Assuming x is finite and >= 0, and n is an integer:
1260 Returns the decimal representation of round (x * 10^n).
1261 Return the allocated memory - containing the decimal digits in low-to-high
1262 order, terminated with a NUL character - in case of success, NULL in case
1263 of memory allocation failure. */
1265 scale10_round_decimal_long_double (long double x, int n)
1269 void *memory = decode_long_double (x, &e, &m);
1270 return scale10_round_decimal_decoded (e, m, memory, n);
1275 # if NEED_PRINTF_DOUBLE
1277 /* Assuming x is finite and >= 0, and n is an integer:
1278 Returns the decimal representation of round (x * 10^n).
1279 Return the allocated memory - containing the decimal digits in low-to-high
1280 order, terminated with a NUL character - in case of success, NULL in case
1281 of memory allocation failure. */
1283 scale10_round_decimal_double (double x, int n)
1287 void *memory = decode_double (x, &e, &m);
1288 return scale10_round_decimal_decoded (e, m, memory, n);
1293 # if NEED_PRINTF_LONG_DOUBLE
1295 /* Assuming x is finite and > 0:
1296 Return an approximation for n with 10^n <= x < 10^(n+1).
1297 The approximation is usually the right n, but may be off by 1 sometimes. */
1299 floorlog10l (long double x)
1306 /* Split into exponential part and mantissa. */
1307 y = frexpl (x, &exp);
1308 if (!(y >= 0.0L && y < 1.0L))
1314 while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1316 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1317 exp -= GMP_LIMB_BITS;
1319 if (y < (1.0L / (1 << 16)))
1321 y *= 1.0L * (1 << 16);
1324 if (y < (1.0L / (1 << 8)))
1326 y *= 1.0L * (1 << 8);
1329 if (y < (1.0L / (1 << 4)))
1331 y *= 1.0L * (1 << 4);
1334 if (y < (1.0L / (1 << 2)))
1336 y *= 1.0L * (1 << 2);
1339 if (y < (1.0L / (1 << 1)))
1341 y *= 1.0L * (1 << 1);
1345 if (!(y >= 0.5L && y < 1.0L))
1347 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1350 if (z < 0.70710678118654752444)
1352 z *= 1.4142135623730950488;
1355 if (z < 0.8408964152537145431)
1357 z *= 1.1892071150027210667;
1360 if (z < 0.91700404320467123175)
1362 z *= 1.0905077326652576592;
1365 if (z < 0.9576032806985736469)
1367 z *= 1.0442737824274138403;
1370 /* Now 0.95 <= z <= 1.01. */
1372 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1373 Four terms are enough to get an approximation with error < 10^-7. */
1374 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1375 /* Finally multiply with log(2)/log(10), yields an approximation for
1377 l *= 0.30102999566398119523;
1378 /* Round down to the next integer. */
1379 return (int) l + (l < 0 ? -1 : 0);
1384 # if NEED_PRINTF_DOUBLE
1386 /* Assuming x is finite and > 0:
1387 Return an approximation for n with 10^n <= x < 10^(n+1).
1388 The approximation is usually the right n, but may be off by 1 sometimes. */
1390 floorlog10 (double x)
1397 /* Split into exponential part and mantissa. */
1398 y = frexp (x, &exp);
1399 if (!(y >= 0.0 && y < 1.0))
1405 while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1407 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1408 exp -= GMP_LIMB_BITS;
1410 if (y < (1.0 / (1 << 16)))
1412 y *= 1.0 * (1 << 16);
1415 if (y < (1.0 / (1 << 8)))
1417 y *= 1.0 * (1 << 8);
1420 if (y < (1.0 / (1 << 4)))
1422 y *= 1.0 * (1 << 4);
1425 if (y < (1.0 / (1 << 2)))
1427 y *= 1.0 * (1 << 2);
1430 if (y < (1.0 / (1 << 1)))
1432 y *= 1.0 * (1 << 1);
1436 if (!(y >= 0.5 && y < 1.0))
1438 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1441 if (z < 0.70710678118654752444)
1443 z *= 1.4142135623730950488;
1446 if (z < 0.8408964152537145431)
1448 z *= 1.1892071150027210667;
1451 if (z < 0.91700404320467123175)
1453 z *= 1.0905077326652576592;
1456 if (z < 0.9576032806985736469)
1458 z *= 1.0442737824274138403;
1461 /* Now 0.95 <= z <= 1.01. */
1463 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1464 Four terms are enough to get an approximation with error < 10^-7. */
1465 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1466 /* Finally multiply with log(2)/log(10), yields an approximation for
1468 l *= 0.30102999566398119523;
1469 /* Round down to the next integer. */
1470 return (int) l + (l < 0 ? -1 : 0);
1475 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1476 a single '1' digit. */
1478 is_borderline (const char *digits, size_t precision)
1480 for (; precision > 0; precision--, digits++)
1486 return *digits == '\0';
1491 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
1493 /* Use a different function name, to make it possible that the 'wchar_t'
1494 parametrization and the 'char' parametrization get compiled in the same
1495 translation unit. */
1496 # if WIDE_CHAR_VERSION
1497 # define MAX_ROOM_NEEDED wmax_room_needed
1499 # define MAX_ROOM_NEEDED max_room_needed
1502 /* Returns the number of TCHAR_T units needed as temporary space for the result
1503 of sprintf or SNPRINTF of a single conversion directive. */
1504 static inline size_t
1505 MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
1506 arg_type type, int flags, size_t width, int has_precision,
1507 size_t precision, int pad_ourselves)
1513 case 'd': case 'i': case 'u':
1514 # if HAVE_LONG_LONG_INT
1515 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1517 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1518 * 0.30103 /* binary -> decimal */
1520 + 1; /* turn floor into ceil */
1523 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1525 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1526 * 0.30103 /* binary -> decimal */
1528 + 1; /* turn floor into ceil */
1531 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1532 * 0.30103 /* binary -> decimal */
1534 + 1; /* turn floor into ceil */
1535 if (tmp_length < precision)
1536 tmp_length = precision;
1537 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1538 tmp_length = xsum (tmp_length, tmp_length);
1539 /* Add 1, to account for a leading sign. */
1540 tmp_length = xsum (tmp_length, 1);
1544 # if HAVE_LONG_LONG_INT
1545 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1547 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1548 * 0.333334 /* binary -> octal */
1550 + 1; /* turn floor into ceil */
1553 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1555 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1556 * 0.333334 /* binary -> octal */
1558 + 1; /* turn floor into ceil */
1561 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1562 * 0.333334 /* binary -> octal */
1564 + 1; /* turn floor into ceil */
1565 if (tmp_length < precision)
1566 tmp_length = precision;
1567 /* Add 1, to account for a leading sign. */
1568 tmp_length = xsum (tmp_length, 1);
1572 # if HAVE_LONG_LONG_INT
1573 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1575 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1576 * 0.25 /* binary -> hexadecimal */
1578 + 1; /* turn floor into ceil */
1581 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1583 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1584 * 0.25 /* binary -> hexadecimal */
1586 + 1; /* turn floor into ceil */
1589 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1590 * 0.25 /* binary -> hexadecimal */
1592 + 1; /* turn floor into ceil */
1593 if (tmp_length < precision)
1594 tmp_length = precision;
1595 /* Add 2, to account for a leading sign or alternate form. */
1596 tmp_length = xsum (tmp_length, 2);
1600 if (type == TYPE_LONGDOUBLE)
1602 (unsigned int) (LDBL_MAX_EXP
1603 * 0.30103 /* binary -> decimal */
1604 * 2 /* estimate for FLAG_GROUP */
1606 + 1 /* turn floor into ceil */
1607 + 10; /* sign, decimal point etc. */
1610 (unsigned int) (DBL_MAX_EXP
1611 * 0.30103 /* binary -> decimal */
1612 * 2 /* estimate for FLAG_GROUP */
1614 + 1 /* turn floor into ceil */
1615 + 10; /* sign, decimal point etc. */
1616 tmp_length = xsum (tmp_length, precision);
1619 case 'e': case 'E': case 'g': case 'G':
1621 12; /* sign, decimal point, exponent etc. */
1622 tmp_length = xsum (tmp_length, precision);
1626 if (type == TYPE_LONGDOUBLE)
1628 (unsigned int) (LDBL_DIG
1629 * 0.831 /* decimal -> hexadecimal */
1631 + 1; /* turn floor into ceil */
1634 (unsigned int) (DBL_DIG
1635 * 0.831 /* decimal -> hexadecimal */
1637 + 1; /* turn floor into ceil */
1638 if (tmp_length < precision)
1639 tmp_length = precision;
1640 /* Account for sign, decimal point etc. */
1641 tmp_length = xsum (tmp_length, 12);
1645 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1646 if (type == TYPE_WIDE_CHAR)
1647 tmp_length = MB_CUR_MAX;
1655 if (type == TYPE_WIDE_STRING)
1657 # if WIDE_CHAR_VERSION
1658 /* ISO C says about %ls in fwprintf:
1659 "If the precision is not specified or is greater than the size
1660 of the array, the array shall contain a null wide character."
1661 So if there is a precision, we must not use wcslen. */
1662 const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
1665 tmp_length = local_wcsnlen (arg, precision);
1667 tmp_length = local_wcslen (arg);
1669 /* ISO C says about %ls in fprintf:
1670 "If a precision is specified, no more than that many bytes are
1671 written (including shift sequences, if any), and the array
1672 shall contain a null wide character if, to equal the multibyte
1673 character sequence length given by the precision, the function
1674 would need to access a wide character one past the end of the
1676 So if there is a precision, we must not use wcslen. */
1677 /* This case has already been handled separately in VASNPRINTF. */
1684 # if WIDE_CHAR_VERSION
1685 /* ISO C says about %s in fwprintf:
1686 "If the precision is not specified or is greater than the size
1687 of the converted array, the converted array shall contain a
1688 null wide character."
1689 So if there is a precision, we must not use strlen. */
1690 /* This case has already been handled separately in VASNPRINTF. */
1693 /* ISO C says about %s in fprintf:
1694 "If the precision is not specified or greater than the size of
1695 the array, the array shall contain a null character."
1696 So if there is a precision, we must not use strlen. */
1697 const char *arg = ap->arg[arg_index].a.a_string;
1700 tmp_length = local_strnlen (arg, precision);
1702 tmp_length = strlen (arg);
1709 (unsigned int) (sizeof (void *) * CHAR_BIT
1710 * 0.25 /* binary -> hexadecimal */
1712 + 1 /* turn floor into ceil */
1713 + 2; /* account for leading 0x */
1722 # if ENABLE_UNISTDIO
1723 /* Padding considers the number of characters, therefore the number of
1724 elements after padding may be
1725 > max (tmp_length, width)
1727 <= tmp_length + width. */
1728 tmp_length = xsum (tmp_length, width);
1730 /* Padding considers the number of elements, says POSIX. */
1731 if (tmp_length < width)
1736 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1744 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1745 const FCHAR_T *format, va_list args)
1750 if (PRINTF_PARSE (format, &d, &a) < 0)
1751 /* errno is already set. */
1759 if (PRINTF_FETCHARGS (args, &a) < 0)
1767 size_t buf_neededlength;
1769 TCHAR_T *buf_malloced;
1773 /* Output string accumulator. */
1778 /* Allocate a small buffer that will hold a directive passed to
1779 sprintf or snprintf. */
1781 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1783 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
1785 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
1786 buf_malloced = NULL;
1791 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
1792 if (size_overflow_p (buf_memsize))
1793 goto out_of_memory_1;
1794 buf = (TCHAR_T *) malloc (buf_memsize);
1796 goto out_of_memory_1;
1800 if (resultbuf != NULL)
1803 allocated = *lengthp;
1812 result is either == resultbuf or == NULL or malloc-allocated.
1813 If length > 0, then result != NULL. */
1815 /* Ensures that allocated >= needed. Aborts through a jump to
1816 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1817 #define ENSURE_ALLOCATION(needed) \
1818 if ((needed) > allocated) \
1820 size_t memory_size; \
1823 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1824 if ((needed) > allocated) \
1825 allocated = (needed); \
1826 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1827 if (size_overflow_p (memory_size)) \
1828 goto out_of_memory; \
1829 if (result == resultbuf || result == NULL) \
1830 memory = (DCHAR_T *) malloc (memory_size); \
1832 memory = (DCHAR_T *) realloc (result, memory_size); \
1833 if (memory == NULL) \
1834 goto out_of_memory; \
1835 if (result == resultbuf && length > 0) \
1836 DCHAR_CPY (memory, result, length); \
1840 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1842 if (cp != dp->dir_start)
1844 size_t n = dp->dir_start - cp;
1845 size_t augmented_length = xsum (length, n);
1847 ENSURE_ALLOCATION (augmented_length);
1848 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1849 need that the format string contains only ASCII characters
1850 if FCHAR_T and DCHAR_T are not the same type. */
1851 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1853 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1854 length = augmented_length;
1859 result[length++] = (unsigned char) *cp++;
1866 /* Execute a single directive. */
1867 if (dp->conversion == '%')
1869 size_t augmented_length;
1871 if (!(dp->arg_index == ARG_NONE))
1873 augmented_length = xsum (length, 1);
1874 ENSURE_ALLOCATION (augmented_length);
1875 result[length] = '%';
1876 length = augmented_length;
1880 if (!(dp->arg_index != ARG_NONE))
1883 if (dp->conversion == 'n')
1885 switch (a.arg[dp->arg_index].type)
1887 case TYPE_COUNT_SCHAR_POINTER:
1888 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1890 case TYPE_COUNT_SHORT_POINTER:
1891 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
1893 case TYPE_COUNT_INT_POINTER:
1894 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
1896 case TYPE_COUNT_LONGINT_POINTER:
1897 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1899 #if HAVE_LONG_LONG_INT
1900 case TYPE_COUNT_LONGLONGINT_POINTER:
1901 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1909 /* The unistdio extensions. */
1910 else if (dp->conversion == 'U')
1912 arg_type type = a.arg[dp->arg_index].type;
1913 int flags = dp->flags;
1921 if (dp->width_start != dp->width_end)
1923 if (dp->width_arg_index != ARG_NONE)
1927 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1929 arg = a.arg[dp->width_arg_index].a.a_int;
1932 /* "A negative field width is taken as a '-' flag
1933 followed by a positive field width." */
1935 width = (unsigned int) (-arg);
1942 const FCHAR_T *digitp = dp->width_start;
1945 width = xsum (xtimes (width, 10), *digitp++ - '0');
1946 while (digitp != dp->width_end);
1953 if (dp->precision_start != dp->precision_end)
1955 if (dp->precision_arg_index != ARG_NONE)
1959 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1961 arg = a.arg[dp->precision_arg_index].a.a_int;
1962 /* "A negative precision is taken as if the precision
1972 const FCHAR_T *digitp = dp->precision_start + 1;
1975 while (digitp != dp->precision_end)
1976 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1983 case TYPE_U8_STRING:
1985 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1986 const uint8_t *arg_end;
1991 /* Use only PRECISION characters, from the left. */
1994 for (; precision > 0; precision--)
1996 int count = u8_strmblen (arg_end);
2001 if (!(result == resultbuf || result == NULL))
2003 if (buf_malloced != NULL)
2004 free (buf_malloced);
2015 /* Use the entire string, and count the number of
2021 int count = u8_strmblen (arg_end);
2026 if (!(result == resultbuf || result == NULL))
2028 if (buf_malloced != NULL)
2029 free (buf_malloced);
2040 /* Use the entire string. */
2041 arg_end = arg + u8_strlen (arg);
2042 /* The number of characters doesn't matter. */
2046 if (has_width && width > characters
2047 && !(dp->flags & FLAG_LEFT))
2049 size_t n = width - characters;
2050 ENSURE_ALLOCATION (xsum (length, n));
2051 DCHAR_SET (result + length, ' ', n);
2055 # if DCHAR_IS_UINT8_T
2057 size_t n = arg_end - arg;
2058 ENSURE_ALLOCATION (xsum (length, n));
2059 DCHAR_CPY (result + length, arg, n);
2064 DCHAR_T *converted = result + length;
2065 size_t converted_len = allocated - length;
2067 /* Convert from UTF-8 to locale encoding. */
2069 u8_conv_to_encoding (locale_charset (),
2070 iconveh_question_mark,
2071 arg, arg_end - arg, NULL,
2072 converted, &converted_len);
2074 /* Convert from UTF-8 to UTF-16/UTF-32. */
2076 U8_TO_DCHAR (arg, arg_end - arg,
2077 converted, &converted_len);
2079 if (converted == NULL)
2081 int saved_errno = errno;
2082 if (!(result == resultbuf || result == NULL))
2084 if (buf_malloced != NULL)
2085 free (buf_malloced);
2087 errno = saved_errno;
2090 if (converted != result + length)
2092 ENSURE_ALLOCATION (xsum (length, converted_len));
2093 DCHAR_CPY (result + length, converted, converted_len);
2096 length += converted_len;
2100 if (has_width && width > characters
2101 && (dp->flags & FLAG_LEFT))
2103 size_t n = width - characters;
2104 ENSURE_ALLOCATION (xsum (length, n));
2105 DCHAR_SET (result + length, ' ', n);
2111 case TYPE_U16_STRING:
2113 const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
2114 const uint16_t *arg_end;
2119 /* Use only PRECISION characters, from the left. */
2122 for (; precision > 0; precision--)
2124 int count = u16_strmblen (arg_end);
2129 if (!(result == resultbuf || result == NULL))
2131 if (buf_malloced != NULL)
2132 free (buf_malloced);
2143 /* Use the entire string, and count the number of
2149 int count = u16_strmblen (arg_end);
2154 if (!(result == resultbuf || result == NULL))
2156 if (buf_malloced != NULL)
2157 free (buf_malloced);
2168 /* Use the entire string. */
2169 arg_end = arg + u16_strlen (arg);
2170 /* The number of characters doesn't matter. */
2174 if (has_width && width > characters
2175 && !(dp->flags & FLAG_LEFT))
2177 size_t n = width - characters;
2178 ENSURE_ALLOCATION (xsum (length, n));
2179 DCHAR_SET (result + length, ' ', n);
2183 # if DCHAR_IS_UINT16_T
2185 size_t n = arg_end - arg;
2186 ENSURE_ALLOCATION (xsum (length, n));
2187 DCHAR_CPY (result + length, arg, n);
2192 DCHAR_T *converted = result + length;
2193 size_t converted_len = allocated - length;
2195 /* Convert from UTF-16 to locale encoding. */
2197 u16_conv_to_encoding (locale_charset (),
2198 iconveh_question_mark,
2199 arg, arg_end - arg, NULL,
2200 converted, &converted_len);
2202 /* Convert from UTF-16 to UTF-8/UTF-32. */
2204 U16_TO_DCHAR (arg, arg_end - arg,
2205 converted, &converted_len);
2207 if (converted == NULL)
2209 int saved_errno = errno;
2210 if (!(result == resultbuf || result == NULL))
2212 if (buf_malloced != NULL)
2213 free (buf_malloced);
2215 errno = saved_errno;
2218 if (converted != result + length)
2220 ENSURE_ALLOCATION (xsum (length, converted_len));
2221 DCHAR_CPY (result + length, converted, converted_len);
2224 length += converted_len;
2228 if (has_width && width > characters
2229 && (dp->flags & FLAG_LEFT))
2231 size_t n = width - characters;
2232 ENSURE_ALLOCATION (xsum (length, n));
2233 DCHAR_SET (result + length, ' ', n);
2239 case TYPE_U32_STRING:
2241 const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
2242 const uint32_t *arg_end;
2247 /* Use only PRECISION characters, from the left. */
2250 for (; precision > 0; precision--)
2252 int count = u32_strmblen (arg_end);
2257 if (!(result == resultbuf || result == NULL))
2259 if (buf_malloced != NULL)
2260 free (buf_malloced);
2271 /* Use the entire string, and count the number of
2277 int count = u32_strmblen (arg_end);
2282 if (!(result == resultbuf || result == NULL))
2284 if (buf_malloced != NULL)
2285 free (buf_malloced);
2296 /* Use the entire string. */
2297 arg_end = arg + u32_strlen (arg);
2298 /* The number of characters doesn't matter. */
2302 if (has_width && width > characters
2303 && !(dp->flags & FLAG_LEFT))
2305 size_t n = width - characters;
2306 ENSURE_ALLOCATION (xsum (length, n));
2307 DCHAR_SET (result + length, ' ', n);
2311 # if DCHAR_IS_UINT32_T
2313 size_t n = arg_end - arg;
2314 ENSURE_ALLOCATION (xsum (length, n));
2315 DCHAR_CPY (result + length, arg, n);
2320 DCHAR_T *converted = result + length;
2321 size_t converted_len = allocated - length;
2323 /* Convert from UTF-32 to locale encoding. */
2325 u32_conv_to_encoding (locale_charset (),
2326 iconveh_question_mark,
2327 arg, arg_end - arg, NULL,
2328 converted, &converted_len);
2330 /* Convert from UTF-32 to UTF-8/UTF-16. */
2332 U32_TO_DCHAR (arg, arg_end - arg,
2333 converted, &converted_len);
2335 if (converted == NULL)
2337 int saved_errno = errno;
2338 if (!(result == resultbuf || result == NULL))
2340 if (buf_malloced != NULL)
2341 free (buf_malloced);
2343 errno = saved_errno;
2346 if (converted != result + length)
2348 ENSURE_ALLOCATION (xsum (length, converted_len));
2349 DCHAR_CPY (result + length, converted, converted_len);
2352 length += converted_len;
2356 if (has_width && width > characters
2357 && (dp->flags & FLAG_LEFT))
2359 size_t n = width - characters;
2360 ENSURE_ALLOCATION (xsum (length, n));
2361 DCHAR_SET (result + length, ' ', n);
2372 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2373 else if (dp->conversion == 's'
2374 # if WIDE_CHAR_VERSION
2375 && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2377 && a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2381 /* The normal handling of the 's' directive below requires
2382 allocating a temporary buffer. The determination of its
2383 length (tmp_length), in the case when a precision is
2384 specified, below requires a conversion between a char[]
2385 string and a wchar_t[] wide string. It could be done, but
2386 we have no guarantee that the implementation of sprintf will
2387 use the exactly same algorithm. Without this guarantee, it
2388 is possible to have buffer overrun bugs. In order to avoid
2389 such bugs, we implement the entire processing of the 's'
2390 directive ourselves. */
2391 int flags = dp->flags;
2399 if (dp->width_start != dp->width_end)
2401 if (dp->width_arg_index != ARG_NONE)
2405 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2407 arg = a.arg[dp->width_arg_index].a.a_int;
2410 /* "A negative field width is taken as a '-' flag
2411 followed by a positive field width." */
2413 width = (unsigned int) (-arg);
2420 const FCHAR_T *digitp = dp->width_start;
2423 width = xsum (xtimes (width, 10), *digitp++ - '0');
2424 while (digitp != dp->width_end);
2431 if (dp->precision_start != dp->precision_end)
2433 if (dp->precision_arg_index != ARG_NONE)
2437 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2439 arg = a.arg[dp->precision_arg_index].a.a_int;
2440 /* "A negative precision is taken as if the precision
2450 const FCHAR_T *digitp = dp->precision_start + 1;
2453 while (digitp != dp->precision_end)
2454 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2459 # if WIDE_CHAR_VERSION
2460 /* %s in vasnwprintf. See the specification of fwprintf. */
2462 const char *arg = a.arg[dp->arg_index].a.a_string;
2463 const char *arg_end;
2468 /* Use only as many bytes as needed to produce PRECISION
2469 wide characters, from the left. */
2472 memset (&state, '\0', sizeof (mbstate_t));
2476 for (; precision > 0; precision--)
2480 count = mbrlen (arg_end, MB_CUR_MAX, &state);
2482 count = mblen (arg_end, MB_CUR_MAX);
2485 /* Found the terminating NUL. */
2489 /* Invalid or incomplete multibyte character. */
2490 if (!(result == resultbuf || result == NULL))
2492 if (buf_malloced != NULL)
2493 free (buf_malloced);
2504 /* Use the entire string, and count the number of wide
2508 memset (&state, '\0', sizeof (mbstate_t));
2516 count = mbrlen (arg_end, MB_CUR_MAX, &state);
2518 count = mblen (arg_end, MB_CUR_MAX);
2521 /* Found the terminating NUL. */
2525 /* Invalid or incomplete multibyte character. */
2526 if (!(result == resultbuf || result == NULL))
2528 if (buf_malloced != NULL)
2529 free (buf_malloced);
2540 /* Use the entire string. */
2541 arg_end = arg + strlen (arg);
2542 /* The number of characters doesn't matter. */
2546 if (has_width && width > characters
2547 && !(dp->flags & FLAG_LEFT))
2549 size_t n = width - characters;
2550 ENSURE_ALLOCATION (xsum (length, n));
2551 DCHAR_SET (result + length, ' ', n);
2555 if (has_precision || has_width)
2557 /* We know the number of wide characters in advance. */
2561 memset (&state, '\0', sizeof (mbstate_t));
2563 ENSURE_ALLOCATION (xsum (length, characters));
2564 for (remaining = characters; remaining > 0; remaining--)
2569 count = mbrtowc (&wc, arg, arg_end - arg, &state);
2571 count = mbtowc (&wc, arg, arg_end - arg);
2574 /* mbrtowc not consistent with mbrlen, or mbtowc
2575 not consistent with mblen. */
2577 result[length++] = wc;
2580 if (!(arg == arg_end))
2587 memset (&state, '\0', sizeof (mbstate_t));
2589 while (arg < arg_end)
2594 count = mbrtowc (&wc, arg, arg_end - arg, &state);
2596 count = mbtowc (&wc, arg, arg_end - arg);
2599 /* mbrtowc not consistent with mbrlen, or mbtowc
2600 not consistent with mblen. */
2602 ENSURE_ALLOCATION (xsum (length, 1));
2603 result[length++] = wc;
2608 if (has_width && width > characters
2609 && (dp->flags & FLAG_LEFT))
2611 size_t n = width - characters;
2612 ENSURE_ALLOCATION (xsum (length, n));
2613 DCHAR_SET (result + length, ' ', n);
2618 /* %ls in vasnprintf. See the specification of fprintf. */
2620 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
2621 const wchar_t *arg_end;
2623 # if !DCHAR_IS_TCHAR
2624 /* This code assumes that TCHAR_T is 'char'. */
2625 typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
2634 /* Use only as many wide characters as needed to produce
2635 at most PRECISION bytes, from the left. */
2636 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2638 memset (&state, '\0', sizeof (mbstate_t));
2642 while (precision > 0)
2644 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2648 /* Found the terminating null wide character. */
2650 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2651 count = wcrtomb (cbuf, *arg_end, &state);
2653 count = wctomb (cbuf, *arg_end);
2657 /* Cannot convert. */
2658 if (!(result == resultbuf || result == NULL))
2660 if (buf_malloced != NULL)
2661 free (buf_malloced);
2666 if (precision < count)
2669 characters += count;
2679 /* Use the entire string, and count the number of
2681 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2683 memset (&state, '\0', sizeof (mbstate_t));
2689 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2693 /* Found the terminating null wide character. */
2695 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2696 count = wcrtomb (cbuf, *arg_end, &state);
2698 count = wctomb (cbuf, *arg_end);
2702 /* Cannot convert. */
2703 if (!(result == resultbuf || result == NULL))
2705 if (buf_malloced != NULL)
2706 free (buf_malloced);
2712 characters += count;
2718 /* Use the entire string. */
2719 arg_end = arg + local_wcslen (arg);
2720 /* The number of bytes doesn't matter. */
2725 # if !DCHAR_IS_TCHAR
2726 /* Convert the string into a piece of temporary memory. */
2727 tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
2731 TCHAR_T *tmpptr = tmpsrc;
2733 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2735 memset (&state, '\0', sizeof (mbstate_t));
2737 for (remaining = characters; remaining > 0; )
2739 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2744 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2745 count = wcrtomb (cbuf, *arg, &state);
2747 count = wctomb (cbuf, *arg);
2750 /* Inconsistency. */
2752 memcpy (tmpptr, cbuf, count);
2757 if (!(arg == arg_end))
2761 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2763 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2764 iconveh_question_mark,
2770 int saved_errno = errno;
2772 if (!(result == resultbuf || result == NULL))
2774 if (buf_malloced != NULL)
2775 free (buf_malloced);
2777 errno = saved_errno;
2785 # if ENABLE_UNISTDIO
2786 /* Outside POSIX, it's preferrable to compare the width
2787 against the number of _characters_ of the converted
2789 w = DCHAR_MBSNLEN (result + length, characters);
2791 /* The width is compared against the number of _bytes_
2792 of the converted value, says POSIX. */
2797 /* w doesn't matter. */
2800 if (has_width && width > w
2801 && !(dp->flags & FLAG_LEFT))
2803 size_t n = width - w;
2804 ENSURE_ALLOCATION (xsum (length, n));
2805 DCHAR_SET (result + length, ' ', n);
2810 if (has_precision || has_width)
2812 /* We know the number of bytes in advance. */
2814 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2816 memset (&state, '\0', sizeof (mbstate_t));
2818 ENSURE_ALLOCATION (xsum (length, characters));
2819 for (remaining = characters; remaining > 0; )
2821 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2826 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2827 count = wcrtomb (cbuf, *arg, &state);
2829 count = wctomb (cbuf, *arg);
2832 /* Inconsistency. */
2834 memcpy (result + length, cbuf, count);
2839 if (!(arg == arg_end))
2844 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2846 memset (&state, '\0', sizeof (mbstate_t));
2848 while (arg < arg_end)
2850 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2855 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2856 count = wcrtomb (cbuf, *arg, &state);
2858 count = wctomb (cbuf, *arg);
2862 /* Cannot convert. */
2863 if (!(result == resultbuf || result == NULL))
2865 if (buf_malloced != NULL)
2866 free (buf_malloced);
2871 ENSURE_ALLOCATION (xsum (length, count));
2872 memcpy (result + length, cbuf, count);
2878 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2879 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2881 length += tmpdst_len;
2884 if (has_width && width > w
2885 && (dp->flags & FLAG_LEFT))
2887 size_t n = width - w;
2888 ENSURE_ALLOCATION (xsum (length, n));
2889 DCHAR_SET (result + length, ' ', n);
2896 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2897 else if ((dp->conversion == 'a' || dp->conversion == 'A')
2898 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2900 # if NEED_PRINTF_DOUBLE
2901 || a.arg[dp->arg_index].type == TYPE_DOUBLE
2903 # if NEED_PRINTF_LONG_DOUBLE
2904 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2910 arg_type type = a.arg[dp->arg_index].type;
2911 int flags = dp->flags;
2917 DCHAR_T tmpbuf[700];
2924 if (dp->width_start != dp->width_end)
2926 if (dp->width_arg_index != ARG_NONE)
2930 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2932 arg = a.arg[dp->width_arg_index].a.a_int;
2935 /* "A negative field width is taken as a '-' flag
2936 followed by a positive field width." */
2938 width = (unsigned int) (-arg);
2945 const FCHAR_T *digitp = dp->width_start;
2948 width = xsum (xtimes (width, 10), *digitp++ - '0');
2949 while (digitp != dp->width_end);
2956 if (dp->precision_start != dp->precision_end)
2958 if (dp->precision_arg_index != ARG_NONE)
2962 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2964 arg = a.arg[dp->precision_arg_index].a.a_int;
2965 /* "A negative precision is taken as if the precision
2975 const FCHAR_T *digitp = dp->precision_start + 1;
2978 while (digitp != dp->precision_end)
2979 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2984 /* Allocate a temporary buffer of sufficient size. */
2985 if (type == TYPE_LONGDOUBLE)
2987 (unsigned int) ((LDBL_DIG + 1)
2988 * 0.831 /* decimal -> hexadecimal */
2990 + 1; /* turn floor into ceil */
2993 (unsigned int) ((DBL_DIG + 1)
2994 * 0.831 /* decimal -> hexadecimal */
2996 + 1; /* turn floor into ceil */
2997 if (tmp_length < precision)
2998 tmp_length = precision;
2999 /* Account for sign, decimal point etc. */
3000 tmp_length = xsum (tmp_length, 12);
3002 if (tmp_length < width)
3005 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3007 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3011 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3013 if (size_overflow_p (tmp_memsize))
3014 /* Overflow, would lead to out of memory. */
3016 tmp = (DCHAR_T *) malloc (tmp_memsize);
3018 /* Out of memory. */
3024 if (type == TYPE_LONGDOUBLE)
3026 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3027 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3031 if (dp->conversion == 'A')
3033 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3037 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3043 DECL_LONG_DOUBLE_ROUNDING
3045 BEGIN_LONG_DOUBLE_ROUNDING ();
3047 if (signbit (arg)) /* arg < 0.0L or negative zero */
3055 else if (flags & FLAG_SHOWSIGN)
3057 else if (flags & FLAG_SPACE)
3060 if (arg > 0.0L && arg + arg == arg)
3062 if (dp->conversion == 'A')
3064 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3068 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3074 long double mantissa;
3077 mantissa = printf_frexpl (arg, &exponent);
3085 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
3087 /* Round the mantissa. */
3088 long double tail = mantissa;
3091 for (q = precision; ; q--)
3093 int digit = (int) tail;
3097 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
3106 for (q = precision; q > 0; q--)
3112 *p++ = dp->conversion - 'A' + 'X';
3117 digit = (int) mantissa;
3120 if ((flags & FLAG_ALT)
3121 || mantissa > 0.0L || precision > 0)
3123 *p++ = decimal_point_char ();
3124 /* This loop terminates because we assume
3125 that FLT_RADIX is a power of 2. */
3126 while (mantissa > 0.0L)
3129 digit = (int) mantissa;
3134 : dp->conversion - 10);
3138 while (precision > 0)
3145 *p++ = dp->conversion - 'A' + 'P';
3146 # if WIDE_CHAR_VERSION
3148 static const wchar_t decimal_format[] =
3149 { '%', '+', 'd', '\0' };
3150 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3155 if (sizeof (DCHAR_T) == 1)
3157 sprintf ((char *) p, "%+d", exponent);
3165 sprintf (expbuf, "%+d", exponent);
3166 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3172 END_LONG_DOUBLE_ROUNDING ();
3180 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3181 double arg = a.arg[dp->arg_index].a.a_double;
3185 if (dp->conversion == 'A')
3187 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3191 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3198 if (signbit (arg)) /* arg < 0.0 or negative zero */
3206 else if (flags & FLAG_SHOWSIGN)
3208 else if (flags & FLAG_SPACE)
3211 if (arg > 0.0 && arg + arg == arg)
3213 if (dp->conversion == 'A')
3215 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3219 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3228 mantissa = printf_frexp (arg, &exponent);
3236 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
3238 /* Round the mantissa. */
3239 double tail = mantissa;
3242 for (q = precision; ; q--)
3244 int digit = (int) tail;
3248 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
3257 for (q = precision; q > 0; q--)
3263 *p++ = dp->conversion - 'A' + 'X';
3268 digit = (int) mantissa;
3271 if ((flags & FLAG_ALT)
3272 || mantissa > 0.0 || precision > 0)
3274 *p++ = decimal_point_char ();
3275 /* This loop terminates because we assume
3276 that FLT_RADIX is a power of 2. */
3277 while (mantissa > 0.0)
3280 digit = (int) mantissa;
3285 : dp->conversion - 10);
3289 while (precision > 0)
3296 *p++ = dp->conversion - 'A' + 'P';
3297 # if WIDE_CHAR_VERSION
3299 static const wchar_t decimal_format[] =
3300 { '%', '+', 'd', '\0' };
3301 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3306 if (sizeof (DCHAR_T) == 1)
3308 sprintf ((char *) p, "%+d", exponent);
3316 sprintf (expbuf, "%+d", exponent);
3317 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3327 /* The generated string now extends from tmp to p, with the
3328 zero padding insertion point being at pad_ptr. */
3329 if (has_width && p - tmp < width)
3331 size_t pad = width - (p - tmp);
3332 DCHAR_T *end = p + pad;
3334 if (flags & FLAG_LEFT)
3336 /* Pad with spaces on the right. */
3337 for (; pad > 0; pad--)
3340 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3342 /* Pad with zeroes. */
3347 for (; pad > 0; pad--)
3352 /* Pad with spaces on the left. */
3357 for (; pad > 0; pad--)
3365 size_t count = p - tmp;
3367 if (count >= tmp_length)
3368 /* tmp_length was incorrectly calculated - fix the
3372 /* Make room for the result. */
3373 if (count >= allocated - length)
3375 size_t n = xsum (length, count);
3377 ENSURE_ALLOCATION (n);
3380 /* Append the result. */
3381 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3388 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3389 else if ((dp->conversion == 'f' || dp->conversion == 'F'
3390 || dp->conversion == 'e' || dp->conversion == 'E'
3391 || dp->conversion == 'g' || dp->conversion == 'G'
3392 || dp->conversion == 'a' || dp->conversion == 'A')
3394 # if NEED_PRINTF_DOUBLE
3395 || a.arg[dp->arg_index].type == TYPE_DOUBLE
3396 # elif NEED_PRINTF_INFINITE_DOUBLE
3397 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
3398 /* The systems (mingw) which produce wrong output
3399 for Inf, -Inf, and NaN also do so for -0.0.
3400 Therefore we treat this case here as well. */
3401 && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
3403 # if NEED_PRINTF_LONG_DOUBLE
3404 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3405 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3406 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3407 /* Some systems produce wrong output for Inf,
3408 -Inf, and NaN. Some systems in this category
3409 (IRIX 5.3) also do so for -0.0. Therefore we
3410 treat this case here as well. */
3411 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
3415 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3416 arg_type type = a.arg[dp->arg_index].type;
3418 int flags = dp->flags;
3424 DCHAR_T tmpbuf[700];
3431 if (dp->width_start != dp->width_end)
3433 if (dp->width_arg_index != ARG_NONE)
3437 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3439 arg = a.arg[dp->width_arg_index].a.a_int;
3442 /* "A negative field width is taken as a '-' flag
3443 followed by a positive field width." */
3445 width = (unsigned int) (-arg);
3452 const FCHAR_T *digitp = dp->width_start;
3455 width = xsum (xtimes (width, 10), *digitp++ - '0');
3456 while (digitp != dp->width_end);
3463 if (dp->precision_start != dp->precision_end)
3465 if (dp->precision_arg_index != ARG_NONE)
3469 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3471 arg = a.arg[dp->precision_arg_index].a.a_int;
3472 /* "A negative precision is taken as if the precision
3482 const FCHAR_T *digitp = dp->precision_start + 1;
3485 while (digitp != dp->precision_end)
3486 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3491 /* POSIX specifies the default precision to be 6 for %f, %F,
3492 %e, %E, but not for %g, %G. Implementations appear to use
3493 the same default precision also for %g, %G. But for %a, %A,
3494 the default precision is 0. */
3496 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3499 /* Allocate a temporary buffer of sufficient size. */
3500 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3501 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3502 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3503 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3504 # elif NEED_PRINTF_LONG_DOUBLE
3505 tmp_length = LDBL_DIG + 1;
3506 # elif NEED_PRINTF_DOUBLE
3507 tmp_length = DBL_DIG + 1;
3511 if (tmp_length < precision)
3512 tmp_length = precision;
3513 # if NEED_PRINTF_LONG_DOUBLE
3514 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3515 if (type == TYPE_LONGDOUBLE)
3517 if (dp->conversion == 'f' || dp->conversion == 'F')
3519 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3520 if (!(isnanl (arg) || arg + arg == arg))
3522 /* arg is finite and nonzero. */
3523 int exponent = floorlog10l (arg < 0 ? -arg : arg);
3524 if (exponent >= 0 && tmp_length < exponent + precision)
3525 tmp_length = exponent + precision;
3529 # if NEED_PRINTF_DOUBLE
3530 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3531 if (type == TYPE_DOUBLE)
3533 if (dp->conversion == 'f' || dp->conversion == 'F')
3535 double arg = a.arg[dp->arg_index].a.a_double;
3536 if (!(isnand (arg) || arg + arg == arg))
3538 /* arg is finite and nonzero. */
3539 int exponent = floorlog10 (arg < 0 ? -arg : arg);
3540 if (exponent >= 0 && tmp_length < exponent + precision)
3541 tmp_length = exponent + precision;
3545 /* Account for sign, decimal point etc. */
3546 tmp_length = xsum (tmp_length, 12);
3548 if (tmp_length < width)
3551 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3553 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3557 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3559 if (size_overflow_p (tmp_memsize))
3560 /* Overflow, would lead to out of memory. */
3562 tmp = (DCHAR_T *) malloc (tmp_memsize);
3564 /* Out of memory. */
3571 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3572 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3573 if (type == TYPE_LONGDOUBLE)
3576 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3580 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3582 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3586 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3592 DECL_LONG_DOUBLE_ROUNDING
3594 BEGIN_LONG_DOUBLE_ROUNDING ();
3596 if (signbit (arg)) /* arg < 0.0L or negative zero */
3604 else if (flags & FLAG_SHOWSIGN)
3606 else if (flags & FLAG_SPACE)
3609 if (arg > 0.0L && arg + arg == arg)
3611 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3613 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3617 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3622 # if NEED_PRINTF_LONG_DOUBLE
3625 if (dp->conversion == 'f' || dp->conversion == 'F')
3631 scale10_round_decimal_long_double (arg, precision);
3634 END_LONG_DOUBLE_ROUNDING ();
3637 ndigits = strlen (digits);
3639 if (ndigits > precision)
3643 *p++ = digits[ndigits];
3645 while (ndigits > precision);
3648 /* Here ndigits <= precision. */
3649 if ((flags & FLAG_ALT) || precision > 0)
3651 *p++ = decimal_point_char ();
3652 for (; precision > ndigits; precision--)
3657 *p++ = digits[ndigits];
3663 else if (dp->conversion == 'e' || dp->conversion == 'E')
3671 if ((flags & FLAG_ALT) || precision > 0)
3673 *p++ = decimal_point_char ();
3674 for (; precision > 0; precision--)
3685 exponent = floorlog10l (arg);
3690 scale10_round_decimal_long_double (arg,
3691 (int)precision - exponent);
3694 END_LONG_DOUBLE_ROUNDING ();
3697 ndigits = strlen (digits);
3699 if (ndigits == precision + 1)
3701 if (ndigits < precision
3702 || ndigits > precision + 2)
3703 /* The exponent was not guessed
3704 precisely enough. */
3707 /* None of two values of exponent is
3708 the right one. Prevent an endless
3712 if (ndigits == precision)
3718 /* Here ndigits = precision+1. */
3719 if (is_borderline (digits, precision))
3721 /* Maybe the exponent guess was too high
3722 and a smaller exponent can be reached
3723 by turning a 10...0 into 9...9x. */
3725 scale10_round_decimal_long_double (arg,
3726 (int)precision - exponent + 1);
3727 if (digits2 == NULL)
3730 END_LONG_DOUBLE_ROUNDING ();
3733 if (strlen (digits2) == precision + 1)
3742 /* Here ndigits = precision+1. */
3744 *p++ = digits[--ndigits];
3745 if ((flags & FLAG_ALT) || precision > 0)
3747 *p++ = decimal_point_char ();
3751 *p++ = digits[ndigits];
3758 *p++ = dp->conversion; /* 'e' or 'E' */
3759 # if WIDE_CHAR_VERSION
3761 static const wchar_t decimal_format[] =
3762 { '%', '+', '.', '2', 'd', '\0' };
3763 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3768 if (sizeof (DCHAR_T) == 1)
3770 sprintf ((char *) p, "%+.2d", exponent);
3778 sprintf (expbuf, "%+.2d", exponent);
3779 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3784 else if (dp->conversion == 'g' || dp->conversion == 'G')
3788 /* precision >= 1. */
3791 /* The exponent is 0, >= -4, < precision.
3792 Use fixed-point notation. */
3794 size_t ndigits = precision;
3795 /* Number of trailing zeroes that have to be
3798 (flags & FLAG_ALT ? 0 : precision - 1);
3802 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3804 *p++ = decimal_point_char ();
3805 while (ndigits > nzeroes)
3821 exponent = floorlog10l (arg);
3826 scale10_round_decimal_long_double (arg,
3827 (int)(precision - 1) - exponent);
3830 END_LONG_DOUBLE_ROUNDING ();
3833 ndigits = strlen (digits);
3835 if (ndigits == precision)
3837 if (ndigits < precision - 1
3838 || ndigits > precision + 1)
3839 /* The exponent was not guessed
3840 precisely enough. */
3843 /* None of two values of exponent is
3844 the right one. Prevent an endless
3848 if (ndigits < precision)
3854 /* Here ndigits = precision. */
3855 if (is_borderline (digits, precision - 1))
3857 /* Maybe the exponent guess was too high
3858 and a smaller exponent can be reached
3859 by turning a 10...0 into 9...9x. */
3861 scale10_round_decimal_long_double (arg,
3862 (int)(precision - 1) - exponent + 1);
3863 if (digits2 == NULL)
3866 END_LONG_DOUBLE_ROUNDING ();
3869 if (strlen (digits2) == precision)
3878 /* Here ndigits = precision. */
3880 /* Determine the number of trailing zeroes
3881 that have to be dropped. */
3883 if ((flags & FLAG_ALT) == 0)
3884 while (nzeroes < ndigits
3885 && digits[nzeroes] == '0')
3888 /* The exponent is now determined. */
3890 && exponent < (long)precision)
3892 /* Fixed-point notation:
3893 max(exponent,0)+1 digits, then the
3894 decimal point, then the remaining
3895 digits without trailing zeroes. */
3898 size_t count = exponent + 1;
3899 /* Note: count <= precision = ndigits. */
3900 for (; count > 0; count--)
3901 *p++ = digits[--ndigits];
3902 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3904 *p++ = decimal_point_char ();
3905 while (ndigits > nzeroes)
3908 *p++ = digits[ndigits];
3914 size_t count = -exponent - 1;
3916 *p++ = decimal_point_char ();
3917 for (; count > 0; count--)
3919 while (ndigits > nzeroes)
3922 *p++ = digits[ndigits];
3928 /* Exponential notation. */
3929 *p++ = digits[--ndigits];
3930 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3932 *p++ = decimal_point_char ();
3933 while (ndigits > nzeroes)
3936 *p++ = digits[ndigits];
3939 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3940 # if WIDE_CHAR_VERSION
3942 static const wchar_t decimal_format[] =
3943 { '%', '+', '.', '2', 'd', '\0' };
3944 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3949 if (sizeof (DCHAR_T) == 1)
3951 sprintf ((char *) p, "%+.2d", exponent);
3959 sprintf (expbuf, "%+.2d", exponent);
3960 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3972 /* arg is finite. */
3978 if (dp->conversion == 'f' || dp->conversion == 'F')
3981 if ((flags & FLAG_ALT) || precision > 0)
3983 *p++ = decimal_point_char ();
3984 for (; precision > 0; precision--)
3988 else if (dp->conversion == 'e' || dp->conversion == 'E')
3991 if ((flags & FLAG_ALT) || precision > 0)
3993 *p++ = decimal_point_char ();
3994 for (; precision > 0; precision--)
3997 *p++ = dp->conversion; /* 'e' or 'E' */
4002 else if (dp->conversion == 'g' || dp->conversion == 'G')
4005 if (flags & FLAG_ALT)
4008 (precision > 0 ? precision - 1 : 0);
4009 *p++ = decimal_point_char ();
4010 for (; ndigits > 0; --ndigits)
4014 else if (dp->conversion == 'a' || dp->conversion == 'A')
4017 *p++ = dp->conversion - 'A' + 'X';
4020 if ((flags & FLAG_ALT) || precision > 0)
4022 *p++ = decimal_point_char ();
4023 for (; precision > 0; precision--)
4026 *p++ = dp->conversion - 'A' + 'P';
4035 END_LONG_DOUBLE_ROUNDING ();
4038 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4042 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4044 double arg = a.arg[dp->arg_index].a.a_double;
4048 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4050 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
4054 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
4061 if (signbit (arg)) /* arg < 0.0 or negative zero */
4069 else if (flags & FLAG_SHOWSIGN)
4071 else if (flags & FLAG_SPACE)
4074 if (arg > 0.0 && arg + arg == arg)
4076 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4078 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
4082 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
4087 # if NEED_PRINTF_DOUBLE
4090 if (dp->conversion == 'f' || dp->conversion == 'F')
4096 scale10_round_decimal_double (arg, precision);
4099 ndigits = strlen (digits);
4101 if (ndigits > precision)
4105 *p++ = digits[ndigits];
4107 while (ndigits > precision);
4110 /* Here ndigits <= precision. */
4111 if ((flags & FLAG_ALT) || precision > 0)
4113 *p++ = decimal_point_char ();
4114 for (; precision > ndigits; precision--)
4119 *p++ = digits[ndigits];
4125 else if (dp->conversion == 'e' || dp->conversion == 'E')
4133 if ((flags & FLAG_ALT) || precision > 0)
4135 *p++ = decimal_point_char ();
4136 for (; precision > 0; precision--)
4147 exponent = floorlog10 (arg);
4152 scale10_round_decimal_double (arg,
4153 (int)precision - exponent);
4156 ndigits = strlen (digits);
4158 if (ndigits == precision + 1)
4160 if (ndigits < precision
4161 || ndigits > precision + 2)
4162 /* The exponent was not guessed
4163 precisely enough. */
4166 /* None of two values of exponent is
4167 the right one. Prevent an endless
4171 if (ndigits == precision)
4177 /* Here ndigits = precision+1. */
4178 if (is_borderline (digits, precision))
4180 /* Maybe the exponent guess was too high
4181 and a smaller exponent can be reached
4182 by turning a 10...0 into 9...9x. */
4184 scale10_round_decimal_double (arg,
4185 (int)precision - exponent + 1);
4186 if (digits2 == NULL)
4191 if (strlen (digits2) == precision + 1)
4200 /* Here ndigits = precision+1. */
4202 *p++ = digits[--ndigits];
4203 if ((flags & FLAG_ALT) || precision > 0)
4205 *p++ = decimal_point_char ();
4209 *p++ = digits[ndigits];
4216 *p++ = dp->conversion; /* 'e' or 'E' */
4217 # if WIDE_CHAR_VERSION
4219 static const wchar_t decimal_format[] =
4220 /* Produce the same number of exponent digits
4221 as the native printf implementation. */
4222 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4223 { '%', '+', '.', '3', 'd', '\0' };
4225 { '%', '+', '.', '2', 'd', '\0' };
4227 SNPRINTF (p, 6 + 1, decimal_format, exponent);
4233 static const char decimal_format[] =
4234 /* Produce the same number of exponent digits
4235 as the native printf implementation. */
4236 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4241 if (sizeof (DCHAR_T) == 1)
4243 sprintf ((char *) p, decimal_format, exponent);
4251 sprintf (expbuf, decimal_format, exponent);
4252 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4258 else if (dp->conversion == 'g' || dp->conversion == 'G')
4262 /* precision >= 1. */
4265 /* The exponent is 0, >= -4, < precision.
4266 Use fixed-point notation. */
4268 size_t ndigits = precision;
4269 /* Number of trailing zeroes that have to be
4272 (flags & FLAG_ALT ? 0 : precision - 1);
4276 if ((flags & FLAG_ALT) || ndigits > nzeroes)
4278 *p++ = decimal_point_char ();
4279 while (ndigits > nzeroes)
4295 exponent = floorlog10 (arg);
4300 scale10_round_decimal_double (arg,
4301 (int)(precision - 1) - exponent);
4304 ndigits = strlen (digits);
4306 if (ndigits == precision)
4308 if (ndigits < precision - 1
4309 || ndigits > precision + 1)
4310 /* The exponent was not guessed
4311 precisely enough. */
4314 /* None of two values of exponent is
4315 the right one. Prevent an endless
4319 if (ndigits < precision)
4325 /* Here ndigits = precision. */
4326 if (is_borderline (digits, precision - 1))
4328 /* Maybe the exponent guess was too high
4329 and a smaller exponent can be reached
4330 by turning a 10...0 into 9...9x. */
4332 scale10_round_decimal_double (arg,
4333 (int)(precision - 1) - exponent + 1);
4334 if (digits2 == NULL)
4339 if (strlen (digits2) == precision)
4348 /* Here ndigits = precision. */
4350 /* Determine the number of trailing zeroes
4351 that have to be dropped. */
4353 if ((flags & FLAG_ALT) == 0)
4354 while (nzeroes < ndigits
4355 && digits[nzeroes] == '0')
4358 /* The exponent is now determined. */
4360 && exponent < (long)precision)
4362 /* Fixed-point notation:
4363 max(exponent,0)+1 digits, then the
4364 decimal point, then the remaining
4365 digits without trailing zeroes. */
4368 size_t count = exponent + 1;
4369 /* Note: count <= precision = ndigits. */
4370 for (; count > 0; count--)
4371 *p++ = digits[--ndigits];
4372 if ((flags & FLAG_ALT) || ndigits > nzeroes)
4374 *p++ = decimal_point_char ();
4375 while (ndigits > nzeroes)
4378 *p++ = digits[ndigits];
4384 size_t count = -exponent - 1;
4386 *p++ = decimal_point_char ();
4387 for (; count > 0; count--)
4389 while (ndigits > nzeroes)
4392 *p++ = digits[ndigits];
4398 /* Exponential notation. */
4399 *p++ = digits[--ndigits];
4400 if ((flags & FLAG_ALT) || ndigits > nzeroes)
4402 *p++ = decimal_point_char ();
4403 while (ndigits > nzeroes)
4406 *p++ = digits[ndigits];
4409 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
4410 # if WIDE_CHAR_VERSION
4412 static const wchar_t decimal_format[] =
4413 /* Produce the same number of exponent digits
4414 as the native printf implementation. */
4415 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4416 { '%', '+', '.', '3', 'd', '\0' };
4418 { '%', '+', '.', '2', 'd', '\0' };
4420 SNPRINTF (p, 6 + 1, decimal_format, exponent);
4426 static const char decimal_format[] =
4427 /* Produce the same number of exponent digits
4428 as the native printf implementation. */
4429 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4434 if (sizeof (DCHAR_T) == 1)
4436 sprintf ((char *) p, decimal_format, exponent);
4444 sprintf (expbuf, decimal_format, exponent);
4445 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4458 /* arg is finite. */
4464 if (dp->conversion == 'f' || dp->conversion == 'F')
4467 if ((flags & FLAG_ALT) || precision > 0)
4469 *p++ = decimal_point_char ();
4470 for (; precision > 0; precision--)
4474 else if (dp->conversion == 'e' || dp->conversion == 'E')
4477 if ((flags & FLAG_ALT) || precision > 0)
4479 *p++ = decimal_point_char ();
4480 for (; precision > 0; precision--)
4483 *p++ = dp->conversion; /* 'e' or 'E' */
4485 /* Produce the same number of exponent digits as
4486 the native printf implementation. */
4487 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4493 else if (dp->conversion == 'g' || dp->conversion == 'G')
4496 if (flags & FLAG_ALT)
4499 (precision > 0 ? precision - 1 : 0);
4500 *p++ = decimal_point_char ();
4501 for (; ndigits > 0; --ndigits)
4513 /* The generated string now extends from tmp to p, with the
4514 zero padding insertion point being at pad_ptr. */
4515 if (has_width && p - tmp < width)
4517 size_t pad = width - (p - tmp);
4518 DCHAR_T *end = p + pad;
4520 if (flags & FLAG_LEFT)
4522 /* Pad with spaces on the right. */
4523 for (; pad > 0; pad--)
4526 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4528 /* Pad with zeroes. */
4533 for (; pad > 0; pad--)
4538 /* Pad with spaces on the left. */
4543 for (; pad > 0; pad--)
4551 size_t count = p - tmp;
4553 if (count >= tmp_length)
4554 /* tmp_length was incorrectly calculated - fix the
4558 /* Make room for the result. */
4559 if (count >= allocated - length)
4561 size_t n = xsum (length, count);
4563 ENSURE_ALLOCATION (n);
4566 /* Append the result. */
4567 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4576 arg_type type = a.arg[dp->arg_index].type;
4577 int flags = dp->flags;
4578 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4582 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4586 #if NEED_PRINTF_UNBOUNDED_PRECISION
4589 # define prec_ourselves 0
4591 #if NEED_PRINTF_FLAG_LEFTADJUST
4592 # define pad_ourselves 1
4593 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4596 # define pad_ourselves 0
4599 unsigned int prefix_count;
4600 int prefixes[2] IF_LINT (= { 0 });
4603 TCHAR_T tmpbuf[700];
4607 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4610 if (dp->width_start != dp->width_end)
4612 if (dp->width_arg_index != ARG_NONE)
4616 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4618 arg = a.arg[dp->width_arg_index].a.a_int;
4621 /* "A negative field width is taken as a '-' flag
4622 followed by a positive field width." */
4624 width = (unsigned int) (-arg);
4631 const FCHAR_T *digitp = dp->width_start;
4634 width = xsum (xtimes (width, 10), *digitp++ - '0');
4635 while (digitp != dp->width_end);
4641 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4644 if (dp->precision_start != dp->precision_end)
4646 if (dp->precision_arg_index != ARG_NONE)
4650 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4652 arg = a.arg[dp->precision_arg_index].a.a_int;
4653 /* "A negative precision is taken as if the precision
4663 const FCHAR_T *digitp = dp->precision_start + 1;
4666 while (digitp != dp->precision_end)
4667 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
4673 /* Decide whether to handle the precision ourselves. */
4674 #if NEED_PRINTF_UNBOUNDED_PRECISION
4675 switch (dp->conversion)
4677 case 'd': case 'i': case 'u':
4679 case 'x': case 'X': case 'p':
4680 prec_ourselves = has_precision && (precision > 0);
4688 /* Decide whether to perform the padding ourselves. */
4689 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4690 switch (dp->conversion)
4692 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4693 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4694 to perform the padding after this conversion. Functions
4695 with unistdio extensions perform the padding based on
4696 character count rather than element count. */
4699 # if NEED_PRINTF_FLAG_ZERO
4700 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4706 pad_ourselves = prec_ourselves;
4712 /* Allocate a temporary buffer of sufficient size for calling
4715 MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
4716 flags, width, has_precision, precision,
4719 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4723 size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4725 if (size_overflow_p (tmp_memsize))
4726 /* Overflow, would lead to out of memory. */
4728 tmp = (TCHAR_T *) malloc (tmp_memsize);
4730 /* Out of memory. */
4735 /* Construct the format string for calling snprintf or
4739 #if NEED_PRINTF_FLAG_GROUPING
4740 /* The underlying implementation doesn't support the ' flag.
4741 Produce no grouping characters in this case; this is
4742 acceptable because the grouping is locale dependent. */
4744 if (flags & FLAG_GROUP)
4747 if (flags & FLAG_LEFT)
4749 if (flags & FLAG_SHOWSIGN)
4751 if (flags & FLAG_SPACE)
4753 if (flags & FLAG_ALT)
4757 if (flags & FLAG_ZERO)
4759 if (dp->width_start != dp->width_end)
4761 size_t n = dp->width_end - dp->width_start;
4762 /* The width specification is known to consist only
4763 of standard ASCII characters. */
4764 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4766 memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4771 const FCHAR_T *mp = dp->width_start;
4773 *fbp++ = (unsigned char) *mp++;
4778 if (!prec_ourselves)
4780 if (dp->precision_start != dp->precision_end)
4782 size_t n = dp->precision_end - dp->precision_start;
4783 /* The precision specification is known to consist only
4784 of standard ASCII characters. */
4785 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4787 memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4792 const FCHAR_T *mp = dp->precision_start;
4794 *fbp++ = (unsigned char) *mp++;
4802 #if HAVE_LONG_LONG_INT
4803 case TYPE_LONGLONGINT:
4804 case TYPE_ULONGLONGINT:
4805 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4818 case TYPE_WIDE_CHAR:
4821 case TYPE_WIDE_STRING:
4825 case TYPE_LONGDOUBLE:
4831 #if NEED_PRINTF_DIRECTIVE_F
4832 if (dp->conversion == 'F')
4836 *fbp = dp->conversion;
4838 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4843 /* On glibc2 systems from glibc >= 2.3 - probably also older
4844 ones - we know that snprintf's returns value conforms to
4845 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4846 Therefore we can avoid using %n in this situation.
4847 On glibc2 systems from 2004-10-18 or newer, the use of %n
4848 in format strings in writable memory may crash the program
4849 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4850 in this situation. */
4851 /* On native Win32 systems (such as mingw), we can avoid using
4853 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4854 snprintf does not write more than the specified number
4855 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4856 '4', '5', '6' into buf, not '4', '5', '\0'.)
4857 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4858 allows us to recognize the case of an insufficient
4859 buffer size: it returns -1 in this case.
4860 On native Win32 systems (such as mingw) where the OS is
4861 Windows Vista, the use of %n in format strings by default
4862 crashes the program. See
4863 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4864 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4865 So we should avoid %n in this situation. */
4872 /* Construct the arguments for calling snprintf or sprintf. */
4874 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4876 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4878 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4880 if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4882 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4884 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4888 /* The SNPRINTF result is appended after result[0..length].
4889 The latter is an array of DCHAR_T; SNPRINTF appends an
4890 array of TCHAR_T to it. This is possible because
4891 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4892 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4893 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4894 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4895 where an snprintf() with maxlen==1 acts like sprintf(). */
4896 ENSURE_ALLOCATION (xsum (length,
4897 (2 + TCHARS_PER_DCHAR - 1)
4898 / TCHARS_PER_DCHAR));
4899 /* Prepare checking whether snprintf returns the count
4901 *(TCHAR_T *) (result + length) = '\0';
4910 size_t maxlen = allocated - length;
4911 /* SNPRINTF can fail if its second argument is
4913 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4914 maxlen = INT_MAX / TCHARS_PER_DCHAR;
4915 maxlen = maxlen * TCHARS_PER_DCHAR;
4916 # define SNPRINTF_BUF(arg) \
4917 switch (prefix_count) \
4920 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4925 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4927 prefixes[0], arg, &count); \
4930 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4932 prefixes[0], prefixes[1], arg, \
4939 # define SNPRINTF_BUF(arg) \
4940 switch (prefix_count) \
4943 count = sprintf (tmp, buf, arg); \
4946 count = sprintf (tmp, buf, prefixes[0], arg); \
4949 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4962 int arg = a.arg[dp->arg_index].a.a_schar;
4968 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4974 int arg = a.arg[dp->arg_index].a.a_short;
4980 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4986 int arg = a.arg[dp->arg_index].a.a_int;
4992 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4998 long int arg = a.arg[dp->arg_index].a.a_longint;
5004 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
5008 #if HAVE_LONG_LONG_INT
5009 case TYPE_LONGLONGINT:
5011 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
5015 case TYPE_ULONGLONGINT:
5017 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
5024 double arg = a.arg[dp->arg_index].a.a_double;
5028 case TYPE_LONGDOUBLE:
5030 long double arg = a.arg[dp->arg_index].a.a_longdouble;
5036 int arg = a.arg[dp->arg_index].a.a_char;
5041 case TYPE_WIDE_CHAR:
5043 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
5050 const char *arg = a.arg[dp->arg_index].a.a_string;
5055 case TYPE_WIDE_STRING:
5057 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
5064 void *arg = a.arg[dp->arg_index].a.a_pointer;
5073 /* Portability: Not all implementations of snprintf()
5074 are ISO C 99 compliant. Determine the number of
5075 bytes that snprintf() has produced or would have
5079 /* Verify that snprintf() has NUL-terminated its
5082 && ((TCHAR_T *) (result + length)) [count] != '\0')
5084 /* Portability hack. */
5085 if (retcount > count)
5090 /* snprintf() doesn't understand the '%n'
5094 /* Don't use the '%n' directive; instead, look
5095 at the snprintf() return value. */
5101 /* Look at the snprintf() return value. */
5104 # if !HAVE_SNPRINTF_RETVAL_C99
5105 /* HP-UX 10.20 snprintf() is doubly deficient:
5106 It doesn't understand the '%n' directive,
5107 *and* it returns -1 (rather than the length
5108 that would have been required) when the
5109 buffer is too small.
5110 But a failure at this point can also come
5111 from other reasons than a too small buffer,
5112 such as an invalid wide string argument to
5113 the %ls directive, or possibly an invalid
5114 floating-point argument. */
5116 MAX_ROOM_NEEDED (&a, dp->arg_index,
5117 dp->conversion, type, flags,
5118 width, has_precision,
5119 precision, pad_ourselves);
5121 if (maxlen < tmp_length)
5123 /* Make more room. But try to do through
5124 this reallocation only once. */
5125 size_t bigger_need =
5128 TCHARS_PER_DCHAR - 1)
5129 / TCHARS_PER_DCHAR);
5130 /* And always grow proportionally.
5131 (There may be several arguments, each
5132 needing a little more room than the
5134 size_t bigger_need2 =
5135 xsum (xtimes (allocated, 2), 12);
5136 if (bigger_need < bigger_need2)
5137 bigger_need = bigger_need2;
5138 ENSURE_ALLOCATION (bigger_need);
5149 /* Attempt to handle failure. */
5152 /* SNPRINTF or sprintf failed. Save and use the errno
5153 that it has set, if any. */
5154 int saved_errno = errno;
5156 if (!(result == resultbuf || result == NULL))
5158 if (buf_malloced != NULL)
5159 free (buf_malloced);
5164 : (dp->conversion == 'c' || dp->conversion == 's'
5171 /* Handle overflow of the allocated buffer.
5172 If such an overflow occurs, a C99 compliant snprintf()
5173 returns a count >= maxlen. However, a non-compliant
5174 snprintf() function returns only count = maxlen - 1. To
5175 cover both cases, test whether count >= maxlen - 1. */
5176 if ((unsigned int) count + 1 >= maxlen)
5178 /* If maxlen already has attained its allowed maximum,
5179 allocating more memory will not increase maxlen.
5180 Instead of looping, bail out. */
5181 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5185 /* Need at least (count + 1) * sizeof (TCHAR_T)
5186 bytes. (The +1 is for the trailing NUL.)
5187 But ask for (count + 2) * sizeof (TCHAR_T)
5188 bytes, so that in the next round, we likely get
5189 maxlen > (unsigned int) count + 1
5190 and so we don't get here again.
5191 And allocate proportionally, to avoid looping
5192 eternally if snprintf() reports a too small
5196 ((unsigned int) count + 2
5197 + TCHARS_PER_DCHAR - 1)
5198 / TCHARS_PER_DCHAR),
5199 xtimes (allocated, 2));
5201 ENSURE_ALLOCATION (n);
5207 #if NEED_PRINTF_UNBOUNDED_PRECISION
5210 /* Handle the precision. */
5213 (TCHAR_T *) (result + length);
5217 size_t prefix_count;
5221 /* Put the additional zeroes after the sign. */
5223 && (*prec_ptr == '-' || *prec_ptr == '+'
5224 || *prec_ptr == ' '))
5226 /* Put the additional zeroes after the 0x prefix if
5227 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5229 && prec_ptr[0] == '0'
5230 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
5233 move = count - prefix_count;
5234 if (precision > move)
5236 /* Insert zeroes. */
5237 size_t insert = precision - move;
5243 (count + insert + TCHARS_PER_DCHAR - 1)
5244 / TCHARS_PER_DCHAR);
5245 length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5246 ENSURE_ALLOCATION (n);
5247 length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5248 prec_ptr = (TCHAR_T *) (result + length);
5251 prec_end = prec_ptr + count;
5252 prec_ptr += prefix_count;
5254 while (prec_end > prec_ptr)
5257 prec_end[insert] = prec_end[0];
5263 while (prec_end > prec_ptr);
5271 if (count >= tmp_length)
5272 /* tmp_length was incorrectly calculated - fix the
5278 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5279 if (dp->conversion == 'c' || dp->conversion == 's')
5281 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5283 The result string is not certainly ASCII. */
5284 const TCHAR_T *tmpsrc;
5287 /* This code assumes that TCHAR_T is 'char'. */
5288 typedef int TCHAR_T_verify
5289 [2 * (sizeof (TCHAR_T) == 1) - 1];
5291 tmpsrc = (TCHAR_T *) (result + length);
5296 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5297 iconveh_question_mark,
5303 int saved_errno = errno;
5304 if (!(result == resultbuf || result == NULL))
5306 if (buf_malloced != NULL)
5307 free (buf_malloced);
5309 errno = saved_errno;
5312 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
5313 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5319 /* The result string is ASCII.
5320 Simple 1:1 conversion. */
5322 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5323 no-op conversion, in-place on the array starting
5324 at (result + length). */
5325 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
5328 const TCHAR_T *tmpsrc;
5333 if (result == resultbuf)
5335 tmpsrc = (TCHAR_T *) (result + length);
5336 /* ENSURE_ALLOCATION will not move tmpsrc
5337 (because it's part of resultbuf). */
5338 ENSURE_ALLOCATION (xsum (length, count));
5342 /* ENSURE_ALLOCATION will move the array
5343 (because it uses realloc(). */
5344 ENSURE_ALLOCATION (xsum (length, count));
5345 tmpsrc = (TCHAR_T *) (result + length);
5349 ENSURE_ALLOCATION (xsum (length, count));
5351 tmpdst = result + length;
5352 /* Copy backwards, because of overlapping. */
5355 for (n = count; n > 0; n--)
5356 *--tmpdst = (unsigned char) *--tmpsrc;
5361 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5362 /* Make room for the result. */
5363 if (count > allocated - length)
5365 /* Need at least count elements. But allocate
5368 xmax (xsum (length, count), xtimes (allocated, 2));
5370 ENSURE_ALLOCATION (n);
5374 /* Here count <= allocated - length. */
5376 /* Perform padding. */
5377 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5378 if (pad_ourselves && has_width)
5381 # if ENABLE_UNISTDIO
5382 /* Outside POSIX, it's preferrable to compare the width
5383 against the number of _characters_ of the converted
5385 w = DCHAR_MBSNLEN (result + length, count);
5387 /* The width is compared against the number of _bytes_
5388 of the converted value, says POSIX. */
5393 size_t pad = width - w;
5395 /* Make room for the result. */
5396 if (xsum (count, pad) > allocated - length)
5398 /* Need at least count + pad elements. But
5399 allocate proportionally. */
5401 xmax (xsum3 (length, count, pad),
5402 xtimes (allocated, 2));
5406 ENSURE_ALLOCATION (n);
5409 ENSURE_ALLOCATION (n);
5412 /* Here count + pad <= allocated - length. */
5415 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5416 DCHAR_T * const rp = result + length;
5418 DCHAR_T * const rp = tmp;
5420 DCHAR_T *p = rp + count;
5421 DCHAR_T *end = p + pad;
5423 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5424 if (dp->conversion == 'c'
5425 || dp->conversion == 's')
5426 /* No zero-padding for string directives. */
5431 pad_ptr = (*rp == '-' ? rp + 1 : rp);
5432 /* No zero-padding of "inf" and "nan". */
5433 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
5434 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
5437 /* The generated string now extends from rp to p,
5438 with the zero padding insertion point being at
5441 count = count + pad; /* = end - rp */
5443 if (flags & FLAG_LEFT)
5445 /* Pad with spaces on the right. */
5446 for (; pad > 0; pad--)
5449 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
5451 /* Pad with zeroes. */
5456 for (; pad > 0; pad--)
5461 /* Pad with spaces on the left. */
5466 for (; pad > 0; pad--)
5474 /* Here still count <= allocated - length. */
5476 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5477 /* The snprintf() result did fit. */
5479 /* Append the sprintf() result. */
5480 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5487 #if NEED_PRINTF_DIRECTIVE_F
5488 if (dp->conversion == 'F')
5490 /* Convert the %f result to upper case for %F. */
5491 DCHAR_T *rp = result + length;
5493 for (rc = count; rc > 0; rc--, rp++)
5494 if (*rp >= 'a' && *rp <= 'z')
5495 *rp = *rp - 'a' + 'A';
5502 #undef pad_ourselves
5503 #undef prec_ourselves
5508 /* Add the final NUL. */
5509 ENSURE_ALLOCATION (xsum (length, 1));
5510 result[length] = '\0';
5512 if (result != resultbuf && length + 1 < allocated)
5514 /* Shrink the allocated memory if possible. */
5517 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
5522 if (buf_malloced != NULL)
5523 free (buf_malloced);
5526 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5527 says that snprintf() fails with errno = EOVERFLOW in this case, but
5528 that's only because snprintf() returns an 'int'. This function does
5529 not have this limitation. */
5534 if (!(result == resultbuf || result == NULL))
5536 if (buf_malloced != NULL)
5537 free (buf_malloced);
5544 if (!(result == resultbuf || result == NULL))
5546 if (buf_malloced != NULL)
5547 free (buf_malloced);
5555 #undef MAX_ROOM_NEEDED
5556 #undef TCHARS_PER_DCHAR
5564 #undef DCHAR_IS_TCHAR