1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* vsprintf with automatic memory allocation.
4 Copyright (C) 1999, 2002-2008 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* This file can be parametrized with the following macros:
21 VASNPRINTF The name of the function being defined.
22 FCHAR_T The element type of the format string.
23 DCHAR_T The element type of the destination (result) string.
24 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
25 in the format string are ASCII. MUST be set if
26 FCHAR_T and DCHAR_T are not the same type.
27 DIRECTIVE Structure denoting a format directive.
29 DIRECTIVES Structure denoting the set of format directives of a
30 format string. Depends on FCHAR_T.
31 PRINTF_PARSE Function that parses a format string.
33 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
34 DCHAR_SET memset like function for DCHAR_T[] arrays.
35 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
36 SNPRINTF The system's snprintf (or similar) function.
37 This may be either snprintf or swprintf.
38 TCHAR_T The element type of the argument and result string
39 of the said SNPRINTF function. This may be either
40 char or wchar_t. The code exploits that
41 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
42 alignof (TCHAR_T) <= alignof (DCHAR_T).
43 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
44 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
45 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
46 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
47 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
49 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
50 This must come before <config.h> because <config.h> may include
51 <features.h>, and once <features.h> has been included, it's too late. */
53 # define _GNU_SOURCE 1
65 # if WIDE_CHAR_VERSION
66 # include "vasnwprintf.h"
68 # include "vasnprintf.h"
72 #include <locale.h> /* localeconv() */
73 #include <stdio.h> /* snprintf(), sprintf() */
74 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
75 #include <string.h> /* memcpy(), strlen() */
76 #include <errno.h> /* errno */
77 #include <limits.h> /* CHAR_BIT */
78 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
80 # include <langinfo.h>
83 # if WIDE_CHAR_VERSION
84 # include "wprintf-parse.h"
86 # include "printf-parse.h"
90 /* Checked size_t computations. */
93 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
98 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
100 # include "isnand-nolibm.h"
103 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
105 # include "isnanl-nolibm.h"
109 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
111 # include "isnand-nolibm.h"
112 # include "printf-frexp.h"
115 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
117 # include "isnanl-nolibm.h"
118 # include "printf-frexpl.h"
124 # define local_wcslen wcslen
126 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
127 a dependency towards this library, here is a local substitute.
128 Define this substitute only once, even if this file is included
129 twice in the same compilation unit. */
130 # ifndef local_wcslen_defined
131 # define local_wcslen_defined 1
133 local_wcslen (const wchar_t *s)
137 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
145 /* Default parameters. */
147 # if WIDE_CHAR_VERSION
148 # define VASNPRINTF vasnwprintf
149 # define FCHAR_T wchar_t
150 # define DCHAR_T wchar_t
151 # define TCHAR_T wchar_t
152 # define DCHAR_IS_TCHAR 1
153 # define DIRECTIVE wchar_t_directive
154 # define DIRECTIVES wchar_t_directives
155 # define PRINTF_PARSE wprintf_parse
156 # define DCHAR_CPY wmemcpy
158 # define VASNPRINTF vasnprintf
159 # define FCHAR_T char
160 # define DCHAR_T char
161 # define TCHAR_T char
162 # define DCHAR_IS_TCHAR 1
163 # define DIRECTIVE char_directive
164 # define DIRECTIVES char_directives
165 # define PRINTF_PARSE printf_parse
166 # define DCHAR_CPY memcpy
169 #if WIDE_CHAR_VERSION
170 /* TCHAR_T is wchar_t. */
171 # define USE_SNPRINTF 1
172 # if HAVE_DECL__SNWPRINTF
173 /* On Windows, the function swprintf() has a different signature than
174 on Unix; we use the _snwprintf() function instead. */
175 # define SNPRINTF _snwprintf
178 # define SNPRINTF swprintf
181 /* TCHAR_T is char. */
182 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
183 But don't use it on BeOS, since BeOS snprintf produces no output if the
184 size argument is >= 0x3000000.
185 Also don't use it on Linux libc5, since there snprintf with size = 1
186 writes any output without bounds, like sprintf. */
187 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
188 # define USE_SNPRINTF 1
190 # define USE_SNPRINTF 0
192 # if HAVE_DECL__SNPRINTF
194 # define SNPRINTF _snprintf
197 # define SNPRINTF snprintf
198 /* Here we need to call the native snprintf, not rpl_snprintf. */
202 /* Here we need to call the native sprintf, not rpl_sprintf. */
205 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
206 warnings in this file. Use -Dlint to suppress them. */
208 # define IF_LINT(Code) Code
210 # define IF_LINT(Code) /* empty */
213 /* Avoid some warnings from "gcc -Wshadow".
214 This file doesn't use the exp() and remainder() functions. */
218 #define remainder rem
220 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
221 /* Determine the decimal-point character according to the current locale. */
222 # ifndef decimal_point_char_defined
223 # define decimal_point_char_defined 1
225 decimal_point_char ()
228 /* Determine it in a multithread-safe way. We know nl_langinfo is
229 multithread-safe on glibc systems, but is not required to be multithread-
230 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
231 is rarely multithread-safe. */
232 # if HAVE_NL_LANGINFO && __GLIBC__
233 point = nl_langinfo (RADIXCHAR);
236 sprintf (pointbuf, "%#.0f", 1.0);
237 point = &pointbuf[1];
239 point = localeconv () -> decimal_point;
241 /* The decimal point is always a single byte: either '.' or ','. */
242 return (point[0] != '\0' ? point[0] : '.');
247 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
249 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
251 is_infinite_or_zero (double x)
253 return isnand (x) || x + x == x;
258 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
260 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
262 is_infinite_or_zerol (long double x)
264 return isnanl (x) || x + x == x;
269 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
271 /* Converting 'long double' to decimal without rare rounding bugs requires
272 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
273 (and slower) algorithms. */
275 typedef unsigned int mp_limb_t;
276 # define GMP_LIMB_BITS 32
277 typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
279 typedef unsigned long long mp_twolimb_t;
280 # define GMP_TWOLIMB_BITS 64
281 typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
283 /* Representation of a bignum >= 0. */
287 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
290 /* Compute the product of two bignums >= 0.
291 Return the allocated memory in case of success, NULL in case of memory
292 allocation failure. */
294 multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
301 if (src1.nlimbs <= src2.nlimbs)
315 /* Now 0 <= len1 <= len2. */
318 /* src1 or src2 is zero. */
320 dest->limbs = (mp_limb_t *) malloc (1);
324 /* Here 1 <= len1 <= len2. */
330 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
333 for (k = len2; k > 0; )
335 for (i = 0; i < len1; i++)
337 mp_limb_t digit1 = p1[i];
338 mp_twolimb_t carry = 0;
339 for (j = 0; j < len2; j++)
341 mp_limb_t digit2 = p2[j];
342 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
344 dp[i + j] = (mp_limb_t) carry;
345 carry = carry >> GMP_LIMB_BITS;
347 dp[i + len2] = (mp_limb_t) carry;
350 while (dlen > 0 && dp[dlen - 1] == 0)
358 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
359 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
361 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
363 Return the allocated memory in case of success, NULL in case of memory
364 allocation failure. */
366 divide (mpn_t a, mpn_t b, mpn_t *q)
369 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
370 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
371 If m<n, then q:=0 and r:=a.
372 If m>=n=1, perform a single-precision division:
375 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
376 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
377 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
378 Normalise [q[m-1],...,q[0]], yields q.
379 If m>=n>1, perform a multiple-precision division:
380 We have a/b < beta^(m-n+1).
381 s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
382 Shift a and b left by s bits, copying them. r:=a.
383 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
384 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
386 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
387 In case of overflow (q* >= beta) set q* := beta-1.
388 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
389 and c3 := b[n-2] * q*.
390 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
391 occurred. Furthermore 0 <= c3 < beta^2.
392 If there was overflow and
393 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
394 the next test can be skipped.}
395 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
396 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
398 Put r := r - b * q* * beta^j. In detail:
399 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
400 hence: u:=0, for i:=0 to n-1 do
402 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
403 u:=u div beta (+ 1, if carry in subtraction)
405 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
407 the carry u does not overflow.}
408 If a negative carry occurs, put q* := q* - 1
409 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
411 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
412 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
414 The room for q[j] can be allocated at the memory location of r[n+j].
415 Finally, round-to-even:
416 Shift r left by 1 bit.
417 If r > b or if r = b and q[0] is odd, q := q+1.
419 const mp_limb_t *a_ptr = a.limbs;
420 size_t a_len = a.nlimbs;
421 const mp_limb_t *b_ptr = b.limbs;
422 size_t b_len = b.nlimbs;
424 mp_limb_t *tmp_roomptr = NULL;
430 /* Allocate room for a_len+2 digits.
431 (Need a_len+1 digits for the real division and 1 more digit for the
432 final rounding of q.) */
433 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
438 while (a_len > 0 && a_ptr[a_len - 1] == 0)
445 /* Division by zero. */
447 if (b_ptr[b_len - 1] == 0)
453 /* Here m = a_len >= 0 and n = b_len > 0. */
457 /* m<n: trivial case. q=0, r := copy of a. */
460 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
461 q_ptr = roomptr + a_len;
466 /* n=1: single precision division.
467 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
471 mp_limb_t den = b_ptr[0];
472 mp_limb_t remainder = 0;
473 const mp_limb_t *sourceptr = a_ptr + a_len;
474 mp_limb_t *destptr = q_ptr + a_len;
476 for (count = a_len; count > 0; count--)
479 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
480 *--destptr = num / den;
481 remainder = num % den;
483 /* Normalise and store r. */
486 r_ptr[0] = remainder;
493 if (q_ptr[q_len - 1] == 0)
499 /* n>1: multiple precision division.
500 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
501 beta^(m-n-1) <= a/b < beta^(m-n+1). */
505 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
533 /* 0 <= s < GMP_LIMB_BITS.
534 Copy b, shifting it left by s bits. */
537 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
538 if (tmp_roomptr == NULL)
544 const mp_limb_t *sourceptr = b_ptr;
545 mp_limb_t *destptr = tmp_roomptr;
546 mp_twolimb_t accu = 0;
548 for (count = b_len; count > 0; count--)
550 accu += (mp_twolimb_t) *sourceptr++ << s;
551 *destptr++ = (mp_limb_t) accu;
552 accu = accu >> GMP_LIMB_BITS;
554 /* accu must be zero, since that was how s was determined. */
560 /* Copy a, shifting it left by s bits, yields r.
562 At the beginning: r = roomptr[0..a_len],
563 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
567 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
572 const mp_limb_t *sourceptr = a_ptr;
573 mp_limb_t *destptr = r_ptr;
574 mp_twolimb_t accu = 0;
576 for (count = a_len; count > 0; count--)
578 accu += (mp_twolimb_t) *sourceptr++ << s;
579 *destptr++ = (mp_limb_t) accu;
580 accu = accu >> GMP_LIMB_BITS;
582 *destptr++ = (mp_limb_t) accu;
584 q_ptr = roomptr + b_len;
585 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
587 size_t j = a_len - b_len; /* m-n */
588 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
589 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
590 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
591 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
592 /* Division loop, traversed m-n+1 times.
593 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
598 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
600 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
602 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
603 | r_ptr[j + b_len - 1];
604 q_star = num / b_msd;
609 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
610 q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
611 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
612 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
613 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
615 If yes, jump directly to the subtraction loop.
616 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
617 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
618 if (r_ptr[j + b_len] > b_msd
619 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
620 /* r[j+n] >= b[n-1]+1 or
621 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
626 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
628 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
629 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
630 mp_twolimb_t c3 = /* b[n-2] * q* */
631 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
632 /* While c2 < c3, increase c2 and decrease c3.
633 Consider c3-c2. While it is > 0, decrease it by
634 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
635 this can happen only twice. */
638 q_star = q_star - 1; /* q* := q* - 1 */
639 if (c3 - c2 > b_msdd)
640 q_star = q_star - 1; /* q* := q* - 1 */
646 /* Subtract r := r - b * q* * beta^j. */
649 const mp_limb_t *sourceptr = b_ptr;
650 mp_limb_t *destptr = r_ptr + j;
651 mp_twolimb_t carry = 0;
653 for (count = b_len; count > 0; count--)
655 /* Here 0 <= carry <= q*. */
658 + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
659 + (mp_limb_t) ~(*destptr);
660 /* Here 0 <= carry <= beta*q* + beta-1. */
661 *destptr++ = ~(mp_limb_t) carry;
662 carry = carry >> GMP_LIMB_BITS; /* <= q* */
664 cr = (mp_limb_t) carry;
666 /* Subtract cr from r_ptr[j + b_len], then forget about
668 if (cr > r_ptr[j + b_len])
670 /* Subtraction gave a carry. */
671 q_star = q_star - 1; /* q* := q* - 1 */
674 const mp_limb_t *sourceptr = b_ptr;
675 mp_limb_t *destptr = r_ptr + j;
678 for (count = b_len; count > 0; count--)
680 mp_limb_t source1 = *sourceptr++;
681 mp_limb_t source2 = *destptr;
682 *destptr++ = source1 + source2 + carry;
685 ? source1 >= (mp_limb_t) ~source2
686 : source1 > (mp_limb_t) ~source2);
689 /* Forget about the carry and about r[j+n]. */
692 /* q* is determined. Store it as q[j]. */
701 if (q_ptr[q_len - 1] == 0)
703 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
704 b is shifted left by s bits. */
705 /* Shift r right by s bits. */
708 mp_limb_t ptr = r_ptr + r_len;
709 mp_twolimb_t accu = 0;
711 for (count = r_len; count > 0; count--)
713 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
714 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
715 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
720 while (r_len > 0 && r_ptr[r_len - 1] == 0)
723 /* Compare r << 1 with b. */
731 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
732 | (i < r_len ? r_ptr[i] << 1 : 0);
733 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
743 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
748 for (i = 0; i < q_len; i++)
749 if (++(q_ptr[i]) != 0)
754 if (tmp_roomptr != NULL)
761 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
763 Destroys the contents of a.
764 Return the allocated memory - containing the decimal digits in low-to-high
765 order, terminated with a NUL character - in case of success, NULL in case
766 of memory allocation failure. */
768 convert_to_decimal (mpn_t a, size_t extra_zeroes)
770 mp_limb_t *a_ptr = a.limbs;
771 size_t a_len = a.nlimbs;
772 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
773 size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
774 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
778 for (; extra_zeroes > 0; extra_zeroes--)
782 /* Divide a by 10^9, in-place. */
783 mp_limb_t remainder = 0;
784 mp_limb_t *ptr = a_ptr + a_len;
786 for (count = a_len; count > 0; count--)
789 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
790 *ptr = num / 1000000000;
791 remainder = num % 1000000000;
793 /* Store the remainder as 9 decimal digits. */
794 for (count = 9; count > 0; count--)
796 *d_ptr++ = '0' + (remainder % 10);
797 remainder = remainder / 10;
800 if (a_ptr[a_len - 1] == 0)
803 /* Remove leading zeroes. */
804 while (d_ptr > c_ptr && d_ptr[-1] == '0')
806 /* But keep at least one zero. */
809 /* Terminate the string. */
815 # if NEED_PRINTF_LONG_DOUBLE
817 /* Assuming x is finite and >= 0:
818 write x as x = 2^e * m, where m is a bignum.
819 Return the allocated memory in case of success, NULL in case of memory
820 allocation failure. */
822 decode_long_double (long double x, int *ep, mpn_t *mp)
829 /* Allocate memory for result. */
830 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
831 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
834 /* Split into exponential part and mantissa. */
835 y = frexpl (x, &exp);
836 if (!(y >= 0.0L && y < 1.0L))
838 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
839 latter is an integer. */
840 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
841 I'm not sure whether it's safe to cast a 'long double' value between
842 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
843 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
845 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
846 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
849 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
852 if (!(y >= 0.0L && y < 1.0L))
854 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
857 if (!(y >= 0.0L && y < 1.0L))
859 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
864 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
867 if (!(y >= 0.0L && y < 1.0L))
869 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
873 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
876 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
879 if (!(y >= 0.0L && y < 1.0L))
881 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
884 if (!(y >= 0.0L && y < 1.0L))
886 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
888 #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
894 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
897 *ep = exp - LDBL_MANT_BIT;
903 # if NEED_PRINTF_DOUBLE
905 /* Assuming x is finite and >= 0:
906 write x as x = 2^e * m, where m is a bignum.
907 Return the allocated memory in case of success, NULL in case of memory
908 allocation failure. */
910 decode_double (double x, int *ep, mpn_t *mp)
917 /* Allocate memory for result. */
918 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
919 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
922 /* Split into exponential part and mantissa. */
924 if (!(y >= 0.0 && y < 1.0))
926 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
927 latter is an integer. */
928 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
929 I'm not sure whether it's safe to cast a 'double' value between
930 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
931 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
933 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
934 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
937 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
940 if (!(y >= 0.0 && y < 1.0))
942 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
945 if (!(y >= 0.0 && y < 1.0))
947 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
952 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
955 if (!(y >= 0.0 && y < 1.0))
957 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
961 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
964 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
967 if (!(y >= 0.0 && y < 1.0))
969 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
972 if (!(y >= 0.0 && y < 1.0))
974 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
979 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
982 *ep = exp - DBL_MANT_BIT;
988 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
989 Returns the decimal representation of round (x * 10^n).
990 Return the allocated memory - containing the decimal digits in low-to-high
991 order, terminated with a NUL character - in case of success, NULL in case
992 of memory allocation failure. */
994 scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1000 mp_limb_t *pow5_ptr;
1002 unsigned int s_limbs;
1003 unsigned int s_bits;
1011 /* x = 2^e * m, hence
1012 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1013 = round (2^s * 5^n * m). */
1016 /* Factor out a common power of 10 if possible. */
1019 extra_zeroes = (s < n ? s : n);
1023 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1024 Before converting to decimal, we need to compute
1025 z = round (2^s * 5^n * m). */
1026 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1027 sign. 2.322 is slightly larger than log(5)/log(2). */
1028 abs_n = (n >= 0 ? n : -n);
1029 abs_s = (s >= 0 ? s : -s);
1030 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1031 + abs_s / GMP_LIMB_BITS + 1)
1032 * sizeof (mp_limb_t));
1033 if (pow5_ptr == NULL)
1038 /* Initialize with 1. */
1041 /* Multiply with 5^|n|. */
1044 static mp_limb_t const small_pow5[13 + 1] =
1046 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1047 48828125, 244140625, 1220703125
1050 for (n13 = 0; n13 <= abs_n; n13 += 13)
1052 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1054 mp_twolimb_t carry = 0;
1055 for (j = 0; j < pow5_len; j++)
1057 mp_limb_t digit2 = pow5_ptr[j];
1058 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1059 pow5_ptr[j] = (mp_limb_t) carry;
1060 carry = carry >> GMP_LIMB_BITS;
1063 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1066 s_limbs = abs_s / GMP_LIMB_BITS;
1067 s_bits = abs_s % GMP_LIMB_BITS;
1068 if (n >= 0 ? s >= 0 : s <= 0)
1070 /* Multiply with 2^|s|. */
1073 mp_limb_t *ptr = pow5_ptr;
1074 mp_twolimb_t accu = 0;
1076 for (count = pow5_len; count > 0; count--)
1078 accu += (mp_twolimb_t) *ptr << s_bits;
1079 *ptr++ = (mp_limb_t) accu;
1080 accu = accu >> GMP_LIMB_BITS;
1084 *ptr = (mp_limb_t) accu;
1091 for (count = pow5_len; count > 0;)
1094 pow5_ptr[s_limbs + count] = pow5_ptr[count];
1096 for (count = s_limbs; count > 0;)
1099 pow5_ptr[count] = 0;
1101 pow5_len += s_limbs;
1103 pow5.limbs = pow5_ptr;
1104 pow5.nlimbs = pow5_len;
1107 /* Multiply m with pow5. No division needed. */
1108 z_memory = multiply (m, pow5, &z);
1112 /* Divide m by pow5 and round. */
1113 z_memory = divide (m, pow5, &z);
1118 pow5.limbs = pow5_ptr;
1119 pow5.nlimbs = pow5_len;
1123 Multiply m with pow5, then divide by 2^|s|. */
1127 tmp_memory = multiply (m, pow5, &numerator);
1128 if (tmp_memory == NULL)
1134 /* Construct 2^|s|. */
1136 mp_limb_t *ptr = pow5_ptr + pow5_len;
1138 for (i = 0; i < s_limbs; i++)
1140 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1141 denominator.limbs = ptr;
1142 denominator.nlimbs = s_limbs + 1;
1144 z_memory = divide (numerator, denominator, &z);
1150 Multiply m with 2^s, then divide by pow5. */
1153 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1154 * sizeof (mp_limb_t));
1155 if (num_ptr == NULL)
1162 mp_limb_t *destptr = num_ptr;
1165 for (i = 0; i < s_limbs; i++)
1170 const mp_limb_t *sourceptr = m.limbs;
1171 mp_twolimb_t accu = 0;
1173 for (count = m.nlimbs; count > 0; count--)
1175 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1176 *destptr++ = (mp_limb_t) accu;
1177 accu = accu >> GMP_LIMB_BITS;
1180 *destptr++ = (mp_limb_t) accu;
1184 const mp_limb_t *sourceptr = m.limbs;
1186 for (count = m.nlimbs; count > 0; count--)
1187 *destptr++ = *sourceptr++;
1189 numerator.limbs = num_ptr;
1190 numerator.nlimbs = destptr - num_ptr;
1192 z_memory = divide (numerator, pow5, &z);
1199 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1201 if (z_memory == NULL)
1203 digits = convert_to_decimal (z, extra_zeroes);
1208 # if NEED_PRINTF_LONG_DOUBLE
1210 /* Assuming x is finite and >= 0, and n is an integer:
1211 Returns the decimal representation of round (x * 10^n).
1212 Return the allocated memory - containing the decimal digits in low-to-high
1213 order, terminated with a NUL character - in case of success, NULL in case
1214 of memory allocation failure. */
1216 scale10_round_decimal_long_double (long double x, int n)
1220 void *memory = decode_long_double (x, &e, &m);
1221 return scale10_round_decimal_decoded (e, m, memory, n);
1226 # if NEED_PRINTF_DOUBLE
1228 /* Assuming x is finite and >= 0, and n is an integer:
1229 Returns the decimal representation of round (x * 10^n).
1230 Return the allocated memory - containing the decimal digits in low-to-high
1231 order, terminated with a NUL character - in case of success, NULL in case
1232 of memory allocation failure. */
1234 scale10_round_decimal_double (double x, int n)
1238 void *memory = decode_double (x, &e, &m);
1239 return scale10_round_decimal_decoded (e, m, memory, n);
1244 # if NEED_PRINTF_LONG_DOUBLE
1246 /* Assuming x is finite and > 0:
1247 Return an approximation for n with 10^n <= x < 10^(n+1).
1248 The approximation is usually the right n, but may be off by 1 sometimes. */
1250 floorlog10l (long double x)
1257 /* Split into exponential part and mantissa. */
1258 y = frexpl (x, &exp);
1259 if (!(y >= 0.0L && y < 1.0L))
1265 while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1267 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1268 exp -= GMP_LIMB_BITS;
1270 if (y < (1.0L / (1 << 16)))
1272 y *= 1.0L * (1 << 16);
1275 if (y < (1.0L / (1 << 8)))
1277 y *= 1.0L * (1 << 8);
1280 if (y < (1.0L / (1 << 4)))
1282 y *= 1.0L * (1 << 4);
1285 if (y < (1.0L / (1 << 2)))
1287 y *= 1.0L * (1 << 2);
1290 if (y < (1.0L / (1 << 1)))
1292 y *= 1.0L * (1 << 1);
1296 if (!(y >= 0.5L && y < 1.0L))
1298 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1301 if (z < 0.70710678118654752444)
1303 z *= 1.4142135623730950488;
1306 if (z < 0.8408964152537145431)
1308 z *= 1.1892071150027210667;
1311 if (z < 0.91700404320467123175)
1313 z *= 1.0905077326652576592;
1316 if (z < 0.9576032806985736469)
1318 z *= 1.0442737824274138403;
1321 /* Now 0.95 <= z <= 1.01. */
1323 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1324 Four terms are enough to get an approximation with error < 10^-7. */
1325 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1326 /* Finally multiply with log(2)/log(10), yields an approximation for
1328 l *= 0.30102999566398119523;
1329 /* Round down to the next integer. */
1330 return (int) l + (l < 0 ? -1 : 0);
1335 # if NEED_PRINTF_DOUBLE
1337 /* Assuming x is finite and > 0:
1338 Return an approximation for n with 10^n <= x < 10^(n+1).
1339 The approximation is usually the right n, but may be off by 1 sometimes. */
1341 floorlog10 (double x)
1348 /* Split into exponential part and mantissa. */
1349 y = frexp (x, &exp);
1350 if (!(y >= 0.0 && y < 1.0))
1356 while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1358 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1359 exp -= GMP_LIMB_BITS;
1361 if (y < (1.0 / (1 << 16)))
1363 y *= 1.0 * (1 << 16);
1366 if (y < (1.0 / (1 << 8)))
1368 y *= 1.0 * (1 << 8);
1371 if (y < (1.0 / (1 << 4)))
1373 y *= 1.0 * (1 << 4);
1376 if (y < (1.0 / (1 << 2)))
1378 y *= 1.0 * (1 << 2);
1381 if (y < (1.0 / (1 << 1)))
1383 y *= 1.0 * (1 << 1);
1387 if (!(y >= 0.5 && y < 1.0))
1389 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1392 if (z < 0.70710678118654752444)
1394 z *= 1.4142135623730950488;
1397 if (z < 0.8408964152537145431)
1399 z *= 1.1892071150027210667;
1402 if (z < 0.91700404320467123175)
1404 z *= 1.0905077326652576592;
1407 if (z < 0.9576032806985736469)
1409 z *= 1.0442737824274138403;
1412 /* Now 0.95 <= z <= 1.01. */
1414 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1415 Four terms are enough to get an approximation with error < 10^-7. */
1416 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1417 /* Finally multiply with log(2)/log(10), yields an approximation for
1419 l *= 0.30102999566398119523;
1420 /* Round down to the next integer. */
1421 return (int) l + (l < 0 ? -1 : 0);
1426 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1427 a single '1' digit. */
1429 is_borderline (const char *digits, size_t precision)
1431 for (; precision > 0; precision--, digits++)
1437 return *digits == '\0';
1443 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1444 const FCHAR_T *format, va_list args)
1449 if (PRINTF_PARSE (format, &d, &a) < 0)
1450 /* errno is already set. */
1458 if (PRINTF_FETCHARGS (args, &a) < 0)
1466 size_t buf_neededlength;
1468 TCHAR_T *buf_malloced;
1472 /* Output string accumulator. */
1477 /* Allocate a small buffer that will hold a directive passed to
1478 sprintf or snprintf. */
1480 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1482 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
1484 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
1485 buf_malloced = NULL;
1490 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
1491 if (size_overflow_p (buf_memsize))
1492 goto out_of_memory_1;
1493 buf = (TCHAR_T *) malloc (buf_memsize);
1495 goto out_of_memory_1;
1499 if (resultbuf != NULL)
1502 allocated = *lengthp;
1511 result is either == resultbuf or == NULL or malloc-allocated.
1512 If length > 0, then result != NULL. */
1514 /* Ensures that allocated >= needed. Aborts through a jump to
1515 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1516 #define ENSURE_ALLOCATION(needed) \
1517 if ((needed) > allocated) \
1519 size_t memory_size; \
1522 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1523 if ((needed) > allocated) \
1524 allocated = (needed); \
1525 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1526 if (size_overflow_p (memory_size)) \
1527 goto out_of_memory; \
1528 if (result == resultbuf || result == NULL) \
1529 memory = (DCHAR_T *) malloc (memory_size); \
1531 memory = (DCHAR_T *) realloc (result, memory_size); \
1532 if (memory == NULL) \
1533 goto out_of_memory; \
1534 if (result == resultbuf && length > 0) \
1535 DCHAR_CPY (memory, result, length); \
1539 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1541 if (cp != dp->dir_start)
1543 size_t n = dp->dir_start - cp;
1544 size_t augmented_length = xsum (length, n);
1546 ENSURE_ALLOCATION (augmented_length);
1547 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1548 need that the format string contains only ASCII characters
1549 if FCHAR_T and DCHAR_T are not the same type. */
1550 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1552 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1553 length = augmented_length;
1558 result[length++] = (unsigned char) *cp++;
1565 /* Execute a single directive. */
1566 if (dp->conversion == '%')
1568 size_t augmented_length;
1570 if (!(dp->arg_index == ARG_NONE))
1572 augmented_length = xsum (length, 1);
1573 ENSURE_ALLOCATION (augmented_length);
1574 result[length] = '%';
1575 length = augmented_length;
1579 if (!(dp->arg_index != ARG_NONE))
1582 if (dp->conversion == 'n')
1584 switch (a.arg[dp->arg_index].type)
1586 case TYPE_COUNT_SCHAR_POINTER:
1587 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1589 case TYPE_COUNT_SHORT_POINTER:
1590 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
1592 case TYPE_COUNT_INT_POINTER:
1593 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
1595 case TYPE_COUNT_LONGINT_POINTER:
1596 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1598 #if HAVE_LONG_LONG_INT
1599 case TYPE_COUNT_LONGLONGINT_POINTER:
1600 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1608 /* The unistdio extensions. */
1609 else if (dp->conversion == 'U')
1611 arg_type type = a.arg[dp->arg_index].type;
1612 int flags = dp->flags;
1620 if (dp->width_start != dp->width_end)
1622 if (dp->width_arg_index != ARG_NONE)
1626 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1628 arg = a.arg[dp->width_arg_index].a.a_int;
1631 /* "A negative field width is taken as a '-' flag
1632 followed by a positive field width." */
1634 width = (unsigned int) (-arg);
1641 const FCHAR_T *digitp = dp->width_start;
1644 width = xsum (xtimes (width, 10), *digitp++ - '0');
1645 while (digitp != dp->width_end);
1652 if (dp->precision_start != dp->precision_end)
1654 if (dp->precision_arg_index != ARG_NONE)
1658 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1660 arg = a.arg[dp->precision_arg_index].a.a_int;
1661 /* "A negative precision is taken as if the precision
1671 const FCHAR_T *digitp = dp->precision_start + 1;
1674 while (digitp != dp->precision_end)
1675 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1682 case TYPE_U8_STRING:
1684 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1685 const uint8_t *arg_end;
1690 /* Use only PRECISION characters, from the left. */
1693 for (; precision > 0; precision--)
1695 int count = u8_strmblen (arg_end);
1700 if (!(result == resultbuf || result == NULL))
1702 if (buf_malloced != NULL)
1703 free (buf_malloced);
1714 /* Use the entire string, and count the number of
1720 int count = u8_strmblen (arg_end);
1725 if (!(result == resultbuf || result == NULL))
1727 if (buf_malloced != NULL)
1728 free (buf_malloced);
1739 /* Use the entire string. */
1740 arg_end = arg + u8_strlen (arg);
1741 /* The number of characters doesn't matter. */
1745 if (has_width && width > characters
1746 && !(dp->flags & FLAG_LEFT))
1748 size_t n = width - characters;
1749 ENSURE_ALLOCATION (xsum (length, n));
1750 DCHAR_SET (result + length, ' ', n);
1754 # if DCHAR_IS_UINT8_T
1756 size_t n = arg_end - arg;
1757 ENSURE_ALLOCATION (xsum (length, n));
1758 DCHAR_CPY (result + length, arg, n);
1763 DCHAR_T *converted = result + length;
1764 size_t converted_len = allocated - length;
1766 /* Convert from UTF-8 to locale encoding. */
1767 if (u8_conv_to_encoding (locale_charset (),
1768 iconveh_question_mark,
1769 arg, arg_end - arg, NULL,
1770 &converted, &converted_len)
1773 /* Convert from UTF-8 to UTF-16/UTF-32. */
1775 U8_TO_DCHAR (arg, arg_end - arg,
1776 converted, &converted_len);
1777 if (converted == NULL)
1780 int saved_errno = errno;
1781 if (!(result == resultbuf || result == NULL))
1783 if (buf_malloced != NULL)
1784 free (buf_malloced);
1786 errno = saved_errno;
1789 if (converted != result + length)
1791 ENSURE_ALLOCATION (xsum (length, converted_len));
1792 DCHAR_CPY (result + length, converted, converted_len);
1795 length += converted_len;
1799 if (has_width && width > characters
1800 && (dp->flags & FLAG_LEFT))
1802 size_t n = width - characters;
1803 ENSURE_ALLOCATION (xsum (length, n));
1804 DCHAR_SET (result + length, ' ', n);
1810 case TYPE_U16_STRING:
1812 const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
1813 const uint16_t *arg_end;
1818 /* Use only PRECISION characters, from the left. */
1821 for (; precision > 0; precision--)
1823 int count = u16_strmblen (arg_end);
1828 if (!(result == resultbuf || result == NULL))
1830 if (buf_malloced != NULL)
1831 free (buf_malloced);
1842 /* Use the entire string, and count the number of
1848 int count = u16_strmblen (arg_end);
1853 if (!(result == resultbuf || result == NULL))
1855 if (buf_malloced != NULL)
1856 free (buf_malloced);
1867 /* Use the entire string. */
1868 arg_end = arg + u16_strlen (arg);
1869 /* The number of characters doesn't matter. */
1873 if (has_width && width > characters
1874 && !(dp->flags & FLAG_LEFT))
1876 size_t n = width - characters;
1877 ENSURE_ALLOCATION (xsum (length, n));
1878 DCHAR_SET (result + length, ' ', n);
1882 # if DCHAR_IS_UINT16_T
1884 size_t n = arg_end - arg;
1885 ENSURE_ALLOCATION (xsum (length, n));
1886 DCHAR_CPY (result + length, arg, n);
1891 DCHAR_T *converted = result + length;
1892 size_t converted_len = allocated - length;
1894 /* Convert from UTF-16 to locale encoding. */
1895 if (u16_conv_to_encoding (locale_charset (),
1896 iconveh_question_mark,
1897 arg, arg_end - arg, NULL,
1898 &converted, &converted_len)
1901 /* Convert from UTF-16 to UTF-8/UTF-32. */
1903 U16_TO_DCHAR (arg, arg_end - arg,
1904 converted, &converted_len);
1905 if (converted == NULL)
1908 int saved_errno = errno;
1909 if (!(result == resultbuf || result == NULL))
1911 if (buf_malloced != NULL)
1912 free (buf_malloced);
1914 errno = saved_errno;
1917 if (converted != result + length)
1919 ENSURE_ALLOCATION (xsum (length, converted_len));
1920 DCHAR_CPY (result + length, converted, converted_len);
1923 length += converted_len;
1927 if (has_width && width > characters
1928 && (dp->flags & FLAG_LEFT))
1930 size_t n = width - characters;
1931 ENSURE_ALLOCATION (xsum (length, n));
1932 DCHAR_SET (result + length, ' ', n);
1938 case TYPE_U32_STRING:
1940 const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
1941 const uint32_t *arg_end;
1946 /* Use only PRECISION characters, from the left. */
1949 for (; precision > 0; precision--)
1951 int count = u32_strmblen (arg_end);
1956 if (!(result == resultbuf || result == NULL))
1958 if (buf_malloced != NULL)
1959 free (buf_malloced);
1970 /* Use the entire string, and count the number of
1976 int count = u32_strmblen (arg_end);
1981 if (!(result == resultbuf || result == NULL))
1983 if (buf_malloced != NULL)
1984 free (buf_malloced);
1995 /* Use the entire string. */
1996 arg_end = arg + u32_strlen (arg);
1997 /* The number of characters doesn't matter. */
2001 if (has_width && width > characters
2002 && !(dp->flags & FLAG_LEFT))
2004 size_t n = width - characters;
2005 ENSURE_ALLOCATION (xsum (length, n));
2006 DCHAR_SET (result + length, ' ', n);
2010 # if DCHAR_IS_UINT32_T
2012 size_t n = arg_end - arg;
2013 ENSURE_ALLOCATION (xsum (length, n));
2014 DCHAR_CPY (result + length, arg, n);
2019 DCHAR_T *converted = result + length;
2020 size_t converted_len = allocated - length;
2022 /* Convert from UTF-32 to locale encoding. */
2023 if (u32_conv_to_encoding (locale_charset (),
2024 iconveh_question_mark,
2025 arg, arg_end - arg, NULL,
2026 &converted, &converted_len)
2029 /* Convert from UTF-32 to UTF-8/UTF-16. */
2031 U32_TO_DCHAR (arg, arg_end - arg,
2032 converted, &converted_len);
2033 if (converted == NULL)
2036 int saved_errno = errno;
2037 if (!(result == resultbuf || result == NULL))
2039 if (buf_malloced != NULL)
2040 free (buf_malloced);
2042 errno = saved_errno;
2045 if (converted != result + length)
2047 ENSURE_ALLOCATION (xsum (length, converted_len));
2048 DCHAR_CPY (result + length, converted, converted_len);
2051 length += converted_len;
2055 if (has_width && width > characters
2056 && (dp->flags & FLAG_LEFT))
2058 size_t n = width - characters;
2059 ENSURE_ALLOCATION (xsum (length, n));
2060 DCHAR_SET (result + length, ' ', n);
2071 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2072 else if ((dp->conversion == 'a' || dp->conversion == 'A')
2073 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2075 # if NEED_PRINTF_DOUBLE
2076 || a.arg[dp->arg_index].type == TYPE_DOUBLE
2078 # if NEED_PRINTF_LONG_DOUBLE
2079 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2085 arg_type type = a.arg[dp->arg_index].type;
2086 int flags = dp->flags;
2092 DCHAR_T tmpbuf[700];
2099 if (dp->width_start != dp->width_end)
2101 if (dp->width_arg_index != ARG_NONE)
2105 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2107 arg = a.arg[dp->width_arg_index].a.a_int;
2110 /* "A negative field width is taken as a '-' flag
2111 followed by a positive field width." */
2113 width = (unsigned int) (-arg);
2120 const FCHAR_T *digitp = dp->width_start;
2123 width = xsum (xtimes (width, 10), *digitp++ - '0');
2124 while (digitp != dp->width_end);
2131 if (dp->precision_start != dp->precision_end)
2133 if (dp->precision_arg_index != ARG_NONE)
2137 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2139 arg = a.arg[dp->precision_arg_index].a.a_int;
2140 /* "A negative precision is taken as if the precision
2150 const FCHAR_T *digitp = dp->precision_start + 1;
2153 while (digitp != dp->precision_end)
2154 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2159 /* Allocate a temporary buffer of sufficient size. */
2160 if (type == TYPE_LONGDOUBLE)
2162 (unsigned int) ((LDBL_DIG + 1)
2163 * 0.831 /* decimal -> hexadecimal */
2165 + 1; /* turn floor into ceil */
2168 (unsigned int) ((DBL_DIG + 1)
2169 * 0.831 /* decimal -> hexadecimal */
2171 + 1; /* turn floor into ceil */
2172 if (tmp_length < precision)
2173 tmp_length = precision;
2174 /* Account for sign, decimal point etc. */
2175 tmp_length = xsum (tmp_length, 12);
2177 if (tmp_length < width)
2180 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2182 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2186 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2188 if (size_overflow_p (tmp_memsize))
2189 /* Overflow, would lead to out of memory. */
2191 tmp = (DCHAR_T *) malloc (tmp_memsize);
2193 /* Out of memory. */
2199 if (type == TYPE_LONGDOUBLE)
2201 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2202 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2206 if (dp->conversion == 'A')
2208 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2212 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2218 DECL_LONG_DOUBLE_ROUNDING
2220 BEGIN_LONG_DOUBLE_ROUNDING ();
2222 if (signbit (arg)) /* arg < 0.0L or negative zero */
2230 else if (flags & FLAG_SHOWSIGN)
2232 else if (flags & FLAG_SPACE)
2235 if (arg > 0.0L && arg + arg == arg)
2237 if (dp->conversion == 'A')
2239 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2243 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2249 long double mantissa;
2252 mantissa = printf_frexpl (arg, &exponent);
2260 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2262 /* Round the mantissa. */
2263 long double tail = mantissa;
2266 for (q = precision; ; q--)
2268 int digit = (int) tail;
2272 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
2281 for (q = precision; q > 0; q--)
2287 *p++ = dp->conversion - 'A' + 'X';
2292 digit = (int) mantissa;
2295 if ((flags & FLAG_ALT)
2296 || mantissa > 0.0L || precision > 0)
2298 *p++ = decimal_point_char ();
2299 /* This loop terminates because we assume
2300 that FLT_RADIX is a power of 2. */
2301 while (mantissa > 0.0L)
2304 digit = (int) mantissa;
2309 : dp->conversion - 10);
2313 while (precision > 0)
2320 *p++ = dp->conversion - 'A' + 'P';
2321 # if WIDE_CHAR_VERSION
2323 static const wchar_t decimal_format[] =
2324 { '%', '+', 'd', '\0' };
2325 SNPRINTF (p, 6 + 1, decimal_format, exponent);
2330 if (sizeof (DCHAR_T) == 1)
2332 sprintf ((char *) p, "%+d", exponent);
2340 sprintf (expbuf, "%+d", exponent);
2341 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2347 END_LONG_DOUBLE_ROUNDING ();
2355 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2356 double arg = a.arg[dp->arg_index].a.a_double;
2360 if (dp->conversion == 'A')
2362 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2366 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2373 if (signbit (arg)) /* arg < 0.0 or negative zero */
2381 else if (flags & FLAG_SHOWSIGN)
2383 else if (flags & FLAG_SPACE)
2386 if (arg > 0.0 && arg + arg == arg)
2388 if (dp->conversion == 'A')
2390 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2394 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2403 mantissa = printf_frexp (arg, &exponent);
2411 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2413 /* Round the mantissa. */
2414 double tail = mantissa;
2417 for (q = precision; ; q--)
2419 int digit = (int) tail;
2423 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
2432 for (q = precision; q > 0; q--)
2438 *p++ = dp->conversion - 'A' + 'X';
2443 digit = (int) mantissa;
2446 if ((flags & FLAG_ALT)
2447 || mantissa > 0.0 || precision > 0)
2449 *p++ = decimal_point_char ();
2450 /* This loop terminates because we assume
2451 that FLT_RADIX is a power of 2. */
2452 while (mantissa > 0.0)
2455 digit = (int) mantissa;
2460 : dp->conversion - 10);
2464 while (precision > 0)
2471 *p++ = dp->conversion - 'A' + 'P';
2472 # if WIDE_CHAR_VERSION
2474 static const wchar_t decimal_format[] =
2475 { '%', '+', 'd', '\0' };
2476 SNPRINTF (p, 6 + 1, decimal_format, exponent);
2481 if (sizeof (DCHAR_T) == 1)
2483 sprintf ((char *) p, "%+d", exponent);
2491 sprintf (expbuf, "%+d", exponent);
2492 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2502 /* The generated string now extends from tmp to p, with the
2503 zero padding insertion point being at pad_ptr. */
2504 if (has_width && p - tmp < width)
2506 size_t pad = width - (p - tmp);
2507 DCHAR_T *end = p + pad;
2509 if (flags & FLAG_LEFT)
2511 /* Pad with spaces on the right. */
2512 for (; pad > 0; pad--)
2515 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
2517 /* Pad with zeroes. */
2522 for (; pad > 0; pad--)
2527 /* Pad with spaces on the left. */
2532 for (; pad > 0; pad--)
2540 size_t count = p - tmp;
2542 if (count >= tmp_length)
2543 /* tmp_length was incorrectly calculated - fix the
2547 /* Make room for the result. */
2548 if (count >= allocated - length)
2550 size_t n = xsum (length, count);
2552 ENSURE_ALLOCATION (n);
2555 /* Append the result. */
2556 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
2563 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2564 else if ((dp->conversion == 'f' || dp->conversion == 'F'
2565 || dp->conversion == 'e' || dp->conversion == 'E'
2566 || dp->conversion == 'g' || dp->conversion == 'G'
2567 || dp->conversion == 'a' || dp->conversion == 'A')
2569 # if NEED_PRINTF_DOUBLE
2570 || a.arg[dp->arg_index].type == TYPE_DOUBLE
2571 # elif NEED_PRINTF_INFINITE_DOUBLE
2572 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
2573 /* The systems (mingw) which produce wrong output
2574 for Inf, -Inf, and NaN also do so for -0.0.
2575 Therefore we treat this case here as well. */
2576 && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
2578 # if NEED_PRINTF_LONG_DOUBLE
2579 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2580 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2581 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2582 /* Some systems produce wrong output for Inf,
2583 -Inf, and NaN. Some systems in this category
2584 (IRIX 5.3) also do so for -0.0. Therefore we
2585 treat this case here as well. */
2586 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
2590 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2591 arg_type type = a.arg[dp->arg_index].type;
2593 int flags = dp->flags;
2599 DCHAR_T tmpbuf[700];
2606 if (dp->width_start != dp->width_end)
2608 if (dp->width_arg_index != ARG_NONE)
2612 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2614 arg = a.arg[dp->width_arg_index].a.a_int;
2617 /* "A negative field width is taken as a '-' flag
2618 followed by a positive field width." */
2620 width = (unsigned int) (-arg);
2627 const FCHAR_T *digitp = dp->width_start;
2630 width = xsum (xtimes (width, 10), *digitp++ - '0');
2631 while (digitp != dp->width_end);
2638 if (dp->precision_start != dp->precision_end)
2640 if (dp->precision_arg_index != ARG_NONE)
2644 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2646 arg = a.arg[dp->precision_arg_index].a.a_int;
2647 /* "A negative precision is taken as if the precision
2657 const FCHAR_T *digitp = dp->precision_start + 1;
2660 while (digitp != dp->precision_end)
2661 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2666 /* POSIX specifies the default precision to be 6 for %f, %F,
2667 %e, %E, but not for %g, %G. Implementations appear to use
2668 the same default precision also for %g, %G. But for %a, %A,
2669 the default precision is 0. */
2671 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
2674 /* Allocate a temporary buffer of sufficient size. */
2675 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2676 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
2677 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2678 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
2679 # elif NEED_PRINTF_LONG_DOUBLE
2680 tmp_length = LDBL_DIG + 1;
2681 # elif NEED_PRINTF_DOUBLE
2682 tmp_length = DBL_DIG + 1;
2686 if (tmp_length < precision)
2687 tmp_length = precision;
2688 # if NEED_PRINTF_LONG_DOUBLE
2689 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2690 if (type == TYPE_LONGDOUBLE)
2692 if (dp->conversion == 'f' || dp->conversion == 'F')
2694 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2695 if (!(isnanl (arg) || arg + arg == arg))
2697 /* arg is finite and nonzero. */
2698 int exponent = floorlog10l (arg < 0 ? -arg : arg);
2699 if (exponent >= 0 && tmp_length < exponent + precision)
2700 tmp_length = exponent + precision;
2704 # if NEED_PRINTF_DOUBLE
2705 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2706 if (type == TYPE_DOUBLE)
2708 if (dp->conversion == 'f' || dp->conversion == 'F')
2710 double arg = a.arg[dp->arg_index].a.a_double;
2711 if (!(isnand (arg) || arg + arg == arg))
2713 /* arg is finite and nonzero. */
2714 int exponent = floorlog10 (arg < 0 ? -arg : arg);
2715 if (exponent >= 0 && tmp_length < exponent + precision)
2716 tmp_length = exponent + precision;
2720 /* Account for sign, decimal point etc. */
2721 tmp_length = xsum (tmp_length, 12);
2723 if (tmp_length < width)
2726 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2728 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2732 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2734 if (size_overflow_p (tmp_memsize))
2735 /* Overflow, would lead to out of memory. */
2737 tmp = (DCHAR_T *) malloc (tmp_memsize);
2739 /* Out of memory. */
2746 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2747 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2748 if (type == TYPE_LONGDOUBLE)
2751 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2755 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2757 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2761 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2767 DECL_LONG_DOUBLE_ROUNDING
2769 BEGIN_LONG_DOUBLE_ROUNDING ();
2771 if (signbit (arg)) /* arg < 0.0L or negative zero */
2779 else if (flags & FLAG_SHOWSIGN)
2781 else if (flags & FLAG_SPACE)
2784 if (arg > 0.0L && arg + arg == arg)
2786 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2788 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2792 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2797 # if NEED_PRINTF_LONG_DOUBLE
2800 if (dp->conversion == 'f' || dp->conversion == 'F')
2806 scale10_round_decimal_long_double (arg, precision);
2809 END_LONG_DOUBLE_ROUNDING ();
2812 ndigits = strlen (digits);
2814 if (ndigits > precision)
2818 *p++ = digits[ndigits];
2820 while (ndigits > precision);
2823 /* Here ndigits <= precision. */
2824 if ((flags & FLAG_ALT) || precision > 0)
2826 *p++ = decimal_point_char ();
2827 for (; precision > ndigits; precision--)
2832 *p++ = digits[ndigits];
2838 else if (dp->conversion == 'e' || dp->conversion == 'E')
2846 if ((flags & FLAG_ALT) || precision > 0)
2848 *p++ = decimal_point_char ();
2849 for (; precision > 0; precision--)
2860 exponent = floorlog10l (arg);
2865 scale10_round_decimal_long_double (arg,
2866 (int)precision - exponent);
2869 END_LONG_DOUBLE_ROUNDING ();
2872 ndigits = strlen (digits);
2874 if (ndigits == precision + 1)
2876 if (ndigits < precision
2877 || ndigits > precision + 2)
2878 /* The exponent was not guessed
2879 precisely enough. */
2882 /* None of two values of exponent is
2883 the right one. Prevent an endless
2887 if (ndigits == precision)
2893 /* Here ndigits = precision+1. */
2894 if (is_borderline (digits, precision))
2896 /* Maybe the exponent guess was too high
2897 and a smaller exponent can be reached
2898 by turning a 10...0 into 9...9x. */
2900 scale10_round_decimal_long_double (arg,
2901 (int)precision - exponent + 1);
2902 if (digits2 == NULL)
2905 END_LONG_DOUBLE_ROUNDING ();
2908 if (strlen (digits2) == precision + 1)
2917 /* Here ndigits = precision+1. */
2919 *p++ = digits[--ndigits];
2920 if ((flags & FLAG_ALT) || precision > 0)
2922 *p++ = decimal_point_char ();
2926 *p++ = digits[ndigits];
2933 *p++ = dp->conversion; /* 'e' or 'E' */
2934 # if WIDE_CHAR_VERSION
2936 static const wchar_t decimal_format[] =
2937 { '%', '+', '.', '2', 'd', '\0' };
2938 SNPRINTF (p, 6 + 1, decimal_format, exponent);
2943 if (sizeof (DCHAR_T) == 1)
2945 sprintf ((char *) p, "%+.2d", exponent);
2953 sprintf (expbuf, "%+.2d", exponent);
2954 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2959 else if (dp->conversion == 'g' || dp->conversion == 'G')
2963 /* precision >= 1. */
2966 /* The exponent is 0, >= -4, < precision.
2967 Use fixed-point notation. */
2969 size_t ndigits = precision;
2970 /* Number of trailing zeroes that have to be
2973 (flags & FLAG_ALT ? 0 : precision - 1);
2977 if ((flags & FLAG_ALT) || ndigits > nzeroes)
2979 *p++ = decimal_point_char ();
2980 while (ndigits > nzeroes)
2996 exponent = floorlog10l (arg);
3001 scale10_round_decimal_long_double (arg,
3002 (int)(precision - 1) - exponent);
3005 END_LONG_DOUBLE_ROUNDING ();
3008 ndigits = strlen (digits);
3010 if (ndigits == precision)
3012 if (ndigits < precision - 1
3013 || ndigits > precision + 1)
3014 /* The exponent was not guessed
3015 precisely enough. */
3018 /* None of two values of exponent is
3019 the right one. Prevent an endless
3023 if (ndigits < precision)
3029 /* Here ndigits = precision. */
3030 if (is_borderline (digits, precision - 1))
3032 /* Maybe the exponent guess was too high
3033 and a smaller exponent can be reached
3034 by turning a 10...0 into 9...9x. */
3036 scale10_round_decimal_long_double (arg,
3037 (int)(precision - 1) - exponent + 1);
3038 if (digits2 == NULL)
3041 END_LONG_DOUBLE_ROUNDING ();
3044 if (strlen (digits2) == precision)
3053 /* Here ndigits = precision. */
3055 /* Determine the number of trailing zeroes
3056 that have to be dropped. */
3058 if ((flags & FLAG_ALT) == 0)
3059 while (nzeroes < ndigits
3060 && digits[nzeroes] == '0')
3063 /* The exponent is now determined. */
3065 && exponent < (long)precision)
3067 /* Fixed-point notation:
3068 max(exponent,0)+1 digits, then the
3069 decimal point, then the remaining
3070 digits without trailing zeroes. */
3073 size_t count = exponent + 1;
3074 /* Note: count <= precision = ndigits. */
3075 for (; count > 0; count--)
3076 *p++ = digits[--ndigits];
3077 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3079 *p++ = decimal_point_char ();
3080 while (ndigits > nzeroes)
3083 *p++ = digits[ndigits];
3089 size_t count = -exponent - 1;
3091 *p++ = decimal_point_char ();
3092 for (; count > 0; count--)
3094 while (ndigits > nzeroes)
3097 *p++ = digits[ndigits];
3103 /* Exponential notation. */
3104 *p++ = digits[--ndigits];
3105 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3107 *p++ = decimal_point_char ();
3108 while (ndigits > nzeroes)
3111 *p++ = digits[ndigits];
3114 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3115 # if WIDE_CHAR_VERSION
3117 static const wchar_t decimal_format[] =
3118 { '%', '+', '.', '2', 'd', '\0' };
3119 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3124 if (sizeof (DCHAR_T) == 1)
3126 sprintf ((char *) p, "%+.2d", exponent);
3134 sprintf (expbuf, "%+.2d", exponent);
3135 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3147 /* arg is finite. */
3153 if (dp->conversion == 'f' || dp->conversion == 'F')
3156 if ((flags & FLAG_ALT) || precision > 0)
3158 *p++ = decimal_point_char ();
3159 for (; precision > 0; precision--)
3163 else if (dp->conversion == 'e' || dp->conversion == 'E')
3166 if ((flags & FLAG_ALT) || precision > 0)
3168 *p++ = decimal_point_char ();
3169 for (; precision > 0; precision--)
3172 *p++ = dp->conversion; /* 'e' or 'E' */
3177 else if (dp->conversion == 'g' || dp->conversion == 'G')
3180 if (flags & FLAG_ALT)
3183 (precision > 0 ? precision - 1 : 0);
3184 *p++ = decimal_point_char ();
3185 for (; ndigits > 0; --ndigits)
3189 else if (dp->conversion == 'a' || dp->conversion == 'A')
3192 *p++ = dp->conversion - 'A' + 'X';
3195 if ((flags & FLAG_ALT) || precision > 0)
3197 *p++ = decimal_point_char ();
3198 for (; precision > 0; precision--)
3201 *p++ = dp->conversion - 'A' + 'P';
3210 END_LONG_DOUBLE_ROUNDING ();
3213 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3217 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3219 double arg = a.arg[dp->arg_index].a.a_double;
3223 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3225 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3229 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3236 if (signbit (arg)) /* arg < 0.0 or negative zero */
3244 else if (flags & FLAG_SHOWSIGN)
3246 else if (flags & FLAG_SPACE)
3249 if (arg > 0.0 && arg + arg == arg)
3251 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3253 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3257 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3262 # if NEED_PRINTF_DOUBLE
3265 if (dp->conversion == 'f' || dp->conversion == 'F')
3271 scale10_round_decimal_double (arg, precision);
3274 ndigits = strlen (digits);
3276 if (ndigits > precision)
3280 *p++ = digits[ndigits];
3282 while (ndigits > precision);
3285 /* Here ndigits <= precision. */
3286 if ((flags & FLAG_ALT) || precision > 0)
3288 *p++ = decimal_point_char ();
3289 for (; precision > ndigits; precision--)
3294 *p++ = digits[ndigits];
3300 else if (dp->conversion == 'e' || dp->conversion == 'E')
3308 if ((flags & FLAG_ALT) || precision > 0)
3310 *p++ = decimal_point_char ();
3311 for (; precision > 0; precision--)
3322 exponent = floorlog10 (arg);
3327 scale10_round_decimal_double (arg,
3328 (int)precision - exponent);
3331 ndigits = strlen (digits);
3333 if (ndigits == precision + 1)
3335 if (ndigits < precision
3336 || ndigits > precision + 2)
3337 /* The exponent was not guessed
3338 precisely enough. */
3341 /* None of two values of exponent is
3342 the right one. Prevent an endless
3346 if (ndigits == precision)
3352 /* Here ndigits = precision+1. */
3353 if (is_borderline (digits, precision))
3355 /* Maybe the exponent guess was too high
3356 and a smaller exponent can be reached
3357 by turning a 10...0 into 9...9x. */
3359 scale10_round_decimal_double (arg,
3360 (int)precision - exponent + 1);
3361 if (digits2 == NULL)
3366 if (strlen (digits2) == precision + 1)
3375 /* Here ndigits = precision+1. */
3377 *p++ = digits[--ndigits];
3378 if ((flags & FLAG_ALT) || precision > 0)
3380 *p++ = decimal_point_char ();
3384 *p++ = digits[ndigits];
3391 *p++ = dp->conversion; /* 'e' or 'E' */
3392 # if WIDE_CHAR_VERSION
3394 static const wchar_t decimal_format[] =
3395 /* Produce the same number of exponent digits
3396 as the native printf implementation. */
3397 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3398 { '%', '+', '.', '3', 'd', '\0' };
3400 { '%', '+', '.', '2', 'd', '\0' };
3402 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3408 static const char decimal_format[] =
3409 /* Produce the same number of exponent digits
3410 as the native printf implementation. */
3411 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3416 if (sizeof (DCHAR_T) == 1)
3418 sprintf ((char *) p, decimal_format, exponent);
3426 sprintf (expbuf, decimal_format, exponent);
3427 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3433 else if (dp->conversion == 'g' || dp->conversion == 'G')
3437 /* precision >= 1. */
3440 /* The exponent is 0, >= -4, < precision.
3441 Use fixed-point notation. */
3443 size_t ndigits = precision;
3444 /* Number of trailing zeroes that have to be
3447 (flags & FLAG_ALT ? 0 : precision - 1);
3451 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3453 *p++ = decimal_point_char ();
3454 while (ndigits > nzeroes)
3470 exponent = floorlog10 (arg);
3475 scale10_round_decimal_double (arg,
3476 (int)(precision - 1) - exponent);
3479 ndigits = strlen (digits);
3481 if (ndigits == precision)
3483 if (ndigits < precision - 1
3484 || ndigits > precision + 1)
3485 /* The exponent was not guessed
3486 precisely enough. */
3489 /* None of two values of exponent is
3490 the right one. Prevent an endless
3494 if (ndigits < precision)
3500 /* Here ndigits = precision. */
3501 if (is_borderline (digits, precision - 1))
3503 /* Maybe the exponent guess was too high
3504 and a smaller exponent can be reached
3505 by turning a 10...0 into 9...9x. */
3507 scale10_round_decimal_double (arg,
3508 (int)(precision - 1) - exponent + 1);
3509 if (digits2 == NULL)
3514 if (strlen (digits2) == precision)
3523 /* Here ndigits = precision. */
3525 /* Determine the number of trailing zeroes
3526 that have to be dropped. */
3528 if ((flags & FLAG_ALT) == 0)
3529 while (nzeroes < ndigits
3530 && digits[nzeroes] == '0')
3533 /* The exponent is now determined. */
3535 && exponent < (long)precision)
3537 /* Fixed-point notation:
3538 max(exponent,0)+1 digits, then the
3539 decimal point, then the remaining
3540 digits without trailing zeroes. */
3543 size_t count = exponent + 1;
3544 /* Note: count <= precision = ndigits. */
3545 for (; count > 0; count--)
3546 *p++ = digits[--ndigits];
3547 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3549 *p++ = decimal_point_char ();
3550 while (ndigits > nzeroes)
3553 *p++ = digits[ndigits];
3559 size_t count = -exponent - 1;
3561 *p++ = decimal_point_char ();
3562 for (; count > 0; count--)
3564 while (ndigits > nzeroes)
3567 *p++ = digits[ndigits];
3573 /* Exponential notation. */
3574 *p++ = digits[--ndigits];
3575 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3577 *p++ = decimal_point_char ();
3578 while (ndigits > nzeroes)
3581 *p++ = digits[ndigits];
3584 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3585 # if WIDE_CHAR_VERSION
3587 static const wchar_t decimal_format[] =
3588 /* Produce the same number of exponent digits
3589 as the native printf implementation. */
3590 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3591 { '%', '+', '.', '3', 'd', '\0' };
3593 { '%', '+', '.', '2', 'd', '\0' };
3595 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3601 static const char decimal_format[] =
3602 /* Produce the same number of exponent digits
3603 as the native printf implementation. */
3604 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3609 if (sizeof (DCHAR_T) == 1)
3611 sprintf ((char *) p, decimal_format, exponent);
3619 sprintf (expbuf, decimal_format, exponent);
3620 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3633 /* arg is finite. */
3639 if (dp->conversion == 'f' || dp->conversion == 'F')
3642 if ((flags & FLAG_ALT) || precision > 0)
3644 *p++ = decimal_point_char ();
3645 for (; precision > 0; precision--)
3649 else if (dp->conversion == 'e' || dp->conversion == 'E')
3652 if ((flags & FLAG_ALT) || precision > 0)
3654 *p++ = decimal_point_char ();
3655 for (; precision > 0; precision--)
3658 *p++ = dp->conversion; /* 'e' or 'E' */
3660 /* Produce the same number of exponent digits as
3661 the native printf implementation. */
3662 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3668 else if (dp->conversion == 'g' || dp->conversion == 'G')
3671 if (flags & FLAG_ALT)
3674 (precision > 0 ? precision - 1 : 0);
3675 *p++ = decimal_point_char ();
3676 for (; ndigits > 0; --ndigits)
3688 /* The generated string now extends from tmp to p, with the
3689 zero padding insertion point being at pad_ptr. */
3690 if (has_width && p - tmp < width)
3692 size_t pad = width - (p - tmp);
3693 DCHAR_T *end = p + pad;
3695 if (flags & FLAG_LEFT)
3697 /* Pad with spaces on the right. */
3698 for (; pad > 0; pad--)
3701 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3703 /* Pad with zeroes. */
3708 for (; pad > 0; pad--)
3713 /* Pad with spaces on the left. */
3718 for (; pad > 0; pad--)
3726 size_t count = p - tmp;
3728 if (count >= tmp_length)
3729 /* tmp_length was incorrectly calculated - fix the
3733 /* Make room for the result. */
3734 if (count >= allocated - length)
3736 size_t n = xsum (length, count);
3738 ENSURE_ALLOCATION (n);
3741 /* Append the result. */
3742 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3751 arg_type type = a.arg[dp->arg_index].type;
3752 int flags = dp->flags;
3753 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3757 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3761 #if NEED_PRINTF_UNBOUNDED_PRECISION
3764 # define prec_ourselves 0
3766 #if NEED_PRINTF_FLAG_LEFTADJUST
3767 # define pad_ourselves 1
3768 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3771 # define pad_ourselves 0
3774 unsigned int prefix_count;
3775 int prefixes[2] IF_LINT (= { 0 });
3778 TCHAR_T tmpbuf[700];
3782 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3785 if (dp->width_start != dp->width_end)
3787 if (dp->width_arg_index != ARG_NONE)
3791 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3793 arg = a.arg[dp->width_arg_index].a.a_int;
3796 /* "A negative field width is taken as a '-' flag
3797 followed by a positive field width." */
3799 width = (unsigned int) (-arg);
3806 const FCHAR_T *digitp = dp->width_start;
3809 width = xsum (xtimes (width, 10), *digitp++ - '0');
3810 while (digitp != dp->width_end);
3816 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3819 if (dp->precision_start != dp->precision_end)
3821 if (dp->precision_arg_index != ARG_NONE)
3825 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3827 arg = a.arg[dp->precision_arg_index].a.a_int;
3828 /* "A negative precision is taken as if the precision
3838 const FCHAR_T *digitp = dp->precision_start + 1;
3841 while (digitp != dp->precision_end)
3842 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3848 /* Decide whether to handle the precision ourselves. */
3849 #if NEED_PRINTF_UNBOUNDED_PRECISION
3850 switch (dp->conversion)
3852 case 'd': case 'i': case 'u':
3854 case 'x': case 'X': case 'p':
3855 prec_ourselves = has_precision && (precision > 0);
3863 /* Decide whether to perform the padding ourselves. */
3864 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
3865 switch (dp->conversion)
3867 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3868 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
3869 to perform the padding after this conversion. Functions
3870 with unistdio extensions perform the padding based on
3871 character count rather than element count. */
3874 # if NEED_PRINTF_FLAG_ZERO
3875 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
3881 pad_ourselves = prec_ourselves;
3887 /* Allocate a temporary buffer of sufficient size for calling
3890 switch (dp->conversion)
3893 case 'd': case 'i': case 'u':
3894 # if HAVE_LONG_LONG_INT
3895 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3897 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3898 * 0.30103 /* binary -> decimal */
3900 + 1; /* turn floor into ceil */
3903 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3905 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3906 * 0.30103 /* binary -> decimal */
3908 + 1; /* turn floor into ceil */
3911 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3912 * 0.30103 /* binary -> decimal */
3914 + 1; /* turn floor into ceil */
3915 if (tmp_length < precision)
3916 tmp_length = precision;
3917 /* Multiply by 2, as an estimate for FLAG_GROUP. */
3918 tmp_length = xsum (tmp_length, tmp_length);
3919 /* Add 1, to account for a leading sign. */
3920 tmp_length = xsum (tmp_length, 1);
3924 # if HAVE_LONG_LONG_INT
3925 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3927 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3928 * 0.333334 /* binary -> octal */
3930 + 1; /* turn floor into ceil */
3933 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3935 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3936 * 0.333334 /* binary -> octal */
3938 + 1; /* turn floor into ceil */
3941 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3942 * 0.333334 /* binary -> octal */
3944 + 1; /* turn floor into ceil */
3945 if (tmp_length < precision)
3946 tmp_length = precision;
3947 /* Add 1, to account for a leading sign. */
3948 tmp_length = xsum (tmp_length, 1);
3952 # if HAVE_LONG_LONG_INT
3953 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3955 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3956 * 0.25 /* binary -> hexadecimal */
3958 + 1; /* turn floor into ceil */
3961 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3963 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3964 * 0.25 /* binary -> hexadecimal */
3966 + 1; /* turn floor into ceil */
3969 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3970 * 0.25 /* binary -> hexadecimal */
3972 + 1; /* turn floor into ceil */
3973 if (tmp_length < precision)
3974 tmp_length = precision;
3975 /* Add 2, to account for a leading sign or alternate form. */
3976 tmp_length = xsum (tmp_length, 2);
3980 if (type == TYPE_LONGDOUBLE)
3982 (unsigned int) (LDBL_MAX_EXP
3983 * 0.30103 /* binary -> decimal */
3984 * 2 /* estimate for FLAG_GROUP */
3986 + 1 /* turn floor into ceil */
3987 + 10; /* sign, decimal point etc. */
3990 (unsigned int) (DBL_MAX_EXP
3991 * 0.30103 /* binary -> decimal */
3992 * 2 /* estimate for FLAG_GROUP */
3994 + 1 /* turn floor into ceil */
3995 + 10; /* sign, decimal point etc. */
3996 tmp_length = xsum (tmp_length, precision);
3999 case 'e': case 'E': case 'g': case 'G':
4001 12; /* sign, decimal point, exponent etc. */
4002 tmp_length = xsum (tmp_length, precision);
4006 if (type == TYPE_LONGDOUBLE)
4008 (unsigned int) (LDBL_DIG
4009 * 0.831 /* decimal -> hexadecimal */
4011 + 1; /* turn floor into ceil */
4014 (unsigned int) (DBL_DIG
4015 * 0.831 /* decimal -> hexadecimal */
4017 + 1; /* turn floor into ceil */
4018 if (tmp_length < precision)
4019 tmp_length = precision;
4020 /* Account for sign, decimal point etc. */
4021 tmp_length = xsum (tmp_length, 12);
4025 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
4026 if (type == TYPE_WIDE_CHAR)
4027 tmp_length = MB_CUR_MAX;
4035 if (type == TYPE_WIDE_STRING)
4038 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
4040 # if !WIDE_CHAR_VERSION
4041 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
4046 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
4051 (unsigned int) (sizeof (void *) * CHAR_BIT
4052 * 0.25 /* binary -> hexadecimal */
4054 + 1 /* turn floor into ceil */
4055 + 2; /* account for leading 0x */
4064 # if ENABLE_UNISTDIO
4065 /* Padding considers the number of characters, therefore
4066 the number of elements after padding may be
4067 > max (tmp_length, width)
4069 <= tmp_length + width. */
4070 tmp_length = xsum (tmp_length, width);
4072 /* Padding considers the number of elements,
4074 if (tmp_length < width)
4079 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
4082 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4086 size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4088 if (size_overflow_p (tmp_memsize))
4089 /* Overflow, would lead to out of memory. */
4091 tmp = (TCHAR_T *) malloc (tmp_memsize);
4093 /* Out of memory. */
4098 /* Construct the format string for calling snprintf or
4102 #if NEED_PRINTF_FLAG_GROUPING
4103 /* The underlying implementation doesn't support the ' flag.
4104 Produce no grouping characters in this case; this is
4105 acceptable because the grouping is locale dependent. */
4107 if (flags & FLAG_GROUP)
4110 if (flags & FLAG_LEFT)
4112 if (flags & FLAG_SHOWSIGN)
4114 if (flags & FLAG_SPACE)
4116 if (flags & FLAG_ALT)
4120 if (flags & FLAG_ZERO)
4122 if (dp->width_start != dp->width_end)
4124 size_t n = dp->width_end - dp->width_start;
4125 /* The width specification is known to consist only
4126 of standard ASCII characters. */
4127 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4129 memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4134 const FCHAR_T *mp = dp->width_start;
4136 *fbp++ = (unsigned char) *mp++;
4141 if (!prec_ourselves)
4143 if (dp->precision_start != dp->precision_end)
4145 size_t n = dp->precision_end - dp->precision_start;
4146 /* The precision specification is known to consist only
4147 of standard ASCII characters. */
4148 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4150 memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4155 const FCHAR_T *mp = dp->precision_start;
4157 *fbp++ = (unsigned char) *mp++;
4165 #if HAVE_LONG_LONG_INT
4166 case TYPE_LONGLONGINT:
4167 case TYPE_ULONGLONGINT:
4168 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4181 case TYPE_WIDE_CHAR:
4184 case TYPE_WIDE_STRING:
4188 case TYPE_LONGDOUBLE:
4194 #if NEED_PRINTF_DIRECTIVE_F
4195 if (dp->conversion == 'F')
4199 *fbp = dp->conversion;
4201 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4206 /* On glibc2 systems from glibc >= 2.3 - probably also older
4207 ones - we know that snprintf's returns value conforms to
4208 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4209 Therefore we can avoid using %n in this situation.
4210 On glibc2 systems from 2004-10-18 or newer, the use of %n
4211 in format strings in writable memory may crash the program
4212 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4213 in this situation. */
4214 /* On native Win32 systems (such as mingw), we can avoid using
4216 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4217 snprintf does not write more than the specified number
4218 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4219 '4', '5', '6' into buf, not '4', '5', '\0'.)
4220 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4221 allows us to recognize the case of an insufficient
4222 buffer size: it returns -1 in this case.
4223 On native Win32 systems (such as mingw) where the OS is
4224 Windows Vista, the use of %n in format strings by default
4225 crashes the program. See
4226 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4227 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4228 So we should avoid %n in this situation. */
4235 /* Construct the arguments for calling snprintf or sprintf. */
4237 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4239 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4241 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4243 if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4245 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4247 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4251 /* The SNPRINTF result is appended after result[0..length].
4252 The latter is an array of DCHAR_T; SNPRINTF appends an
4253 array of TCHAR_T to it. This is possible because
4254 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4255 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4256 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4257 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4258 where an snprintf() with maxlen==1 acts like sprintf(). */
4259 ENSURE_ALLOCATION (xsum (length,
4260 (2 + TCHARS_PER_DCHAR - 1)
4261 / TCHARS_PER_DCHAR));
4262 /* Prepare checking whether snprintf returns the count
4264 *(TCHAR_T *) (result + length) = '\0';
4273 size_t maxlen = allocated - length;
4274 /* SNPRINTF can fail if its second argument is
4276 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4277 maxlen = INT_MAX / TCHARS_PER_DCHAR;
4278 maxlen = maxlen * TCHARS_PER_DCHAR;
4279 # define SNPRINTF_BUF(arg) \
4280 switch (prefix_count) \
4283 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4288 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4290 prefixes[0], arg, &count); \
4293 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4295 prefixes[0], prefixes[1], arg, \
4302 # define SNPRINTF_BUF(arg) \
4303 switch (prefix_count) \
4306 count = sprintf (tmp, buf, arg); \
4309 count = sprintf (tmp, buf, prefixes[0], arg); \
4312 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4324 int arg = a.arg[dp->arg_index].a.a_schar;
4330 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4336 int arg = a.arg[dp->arg_index].a.a_short;
4342 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4348 int arg = a.arg[dp->arg_index].a.a_int;
4354 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4360 long int arg = a.arg[dp->arg_index].a.a_longint;
4366 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
4370 #if HAVE_LONG_LONG_INT
4371 case TYPE_LONGLONGINT:
4373 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
4377 case TYPE_ULONGLONGINT:
4379 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
4386 double arg = a.arg[dp->arg_index].a.a_double;
4390 case TYPE_LONGDOUBLE:
4392 long double arg = a.arg[dp->arg_index].a.a_longdouble;
4398 int arg = a.arg[dp->arg_index].a.a_char;
4403 case TYPE_WIDE_CHAR:
4405 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
4412 const char *arg = a.arg[dp->arg_index].a.a_string;
4417 case TYPE_WIDE_STRING:
4419 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
4426 void *arg = a.arg[dp->arg_index].a.a_pointer;
4435 /* Portability: Not all implementations of snprintf()
4436 are ISO C 99 compliant. Determine the number of
4437 bytes that snprintf() has produced or would have
4441 /* Verify that snprintf() has NUL-terminated its
4444 && ((TCHAR_T *) (result + length)) [count] != '\0')
4446 /* Portability hack. */
4447 if (retcount > count)
4452 /* snprintf() doesn't understand the '%n'
4456 /* Don't use the '%n' directive; instead, look
4457 at the snprintf() return value. */
4463 /* Look at the snprintf() return value. */
4466 /* HP-UX 10.20 snprintf() is doubly deficient:
4467 It doesn't understand the '%n' directive,
4468 *and* it returns -1 (rather than the length
4469 that would have been required) when the
4470 buffer is too small. */
4471 size_t bigger_need =
4472 xsum (xtimes (allocated, 2), 12);
4473 ENSURE_ALLOCATION (bigger_need);
4482 /* Attempt to handle failure. */
4485 if (!(result == resultbuf || result == NULL))
4487 if (buf_malloced != NULL)
4488 free (buf_malloced);
4495 /* Handle overflow of the allocated buffer.
4496 If such an overflow occurs, a C99 compliant snprintf()
4497 returns a count >= maxlen. However, a non-compliant
4498 snprintf() function returns only count = maxlen - 1. To
4499 cover both cases, test whether count >= maxlen - 1. */
4500 if ((unsigned int) count + 1 >= maxlen)
4502 /* If maxlen already has attained its allowed maximum,
4503 allocating more memory will not increase maxlen.
4504 Instead of looping, bail out. */
4505 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
4509 /* Need at least (count + 1) * sizeof (TCHAR_T)
4510 bytes. (The +1 is for the trailing NUL.)
4511 But ask for (count + 2) * sizeof (TCHAR_T)
4512 bytes, so that in the next round, we likely get
4513 maxlen > (unsigned int) count + 1
4514 and so we don't get here again.
4515 And allocate proportionally, to avoid looping
4516 eternally if snprintf() reports a too small
4520 ((unsigned int) count + 2
4521 + TCHARS_PER_DCHAR - 1)
4522 / TCHARS_PER_DCHAR),
4523 xtimes (allocated, 2));
4525 ENSURE_ALLOCATION (n);
4531 #if NEED_PRINTF_UNBOUNDED_PRECISION
4534 /* Handle the precision. */
4537 (TCHAR_T *) (result + length);
4541 size_t prefix_count;
4545 /* Put the additional zeroes after the sign. */
4547 && (*prec_ptr == '-' || *prec_ptr == '+'
4548 || *prec_ptr == ' '))
4550 /* Put the additional zeroes after the 0x prefix if
4551 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
4553 && prec_ptr[0] == '0'
4554 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
4557 move = count - prefix_count;
4558 if (precision > move)
4560 /* Insert zeroes. */
4561 size_t insert = precision - move;
4567 (count + insert + TCHARS_PER_DCHAR - 1)
4568 / TCHARS_PER_DCHAR);
4569 length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
4570 ENSURE_ALLOCATION (n);
4571 length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
4572 prec_ptr = (TCHAR_T *) (result + length);
4575 prec_end = prec_ptr + count;
4576 prec_ptr += prefix_count;
4578 while (prec_end > prec_ptr)
4581 prec_end[insert] = prec_end[0];
4587 while (prec_end > prec_ptr);
4595 if (count >= tmp_length)
4596 /* tmp_length was incorrectly calculated - fix the
4602 /* Convert from TCHAR_T[] to DCHAR_T[]. */
4603 if (dp->conversion == 'c' || dp->conversion == 's')
4605 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
4607 The result string is not certainly ASCII. */
4608 const TCHAR_T *tmpsrc;
4611 /* This code assumes that TCHAR_T is 'char'. */
4612 typedef int TCHAR_T_verify
4613 [2 * (sizeof (TCHAR_T) == 1) - 1];
4615 tmpsrc = (TCHAR_T *) (result + length);
4621 if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
4622 iconveh_question_mark,
4625 &tmpdst, &tmpdst_len)
4628 int saved_errno = errno;
4629 if (!(result == resultbuf || result == NULL))
4631 if (buf_malloced != NULL)
4632 free (buf_malloced);
4634 errno = saved_errno;
4637 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
4638 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
4644 /* The result string is ASCII.
4645 Simple 1:1 conversion. */
4647 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
4648 no-op conversion, in-place on the array starting
4649 at (result + length). */
4650 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
4653 const TCHAR_T *tmpsrc;
4658 if (result == resultbuf)
4660 tmpsrc = (TCHAR_T *) (result + length);
4661 /* ENSURE_ALLOCATION will not move tmpsrc
4662 (because it's part of resultbuf). */
4663 ENSURE_ALLOCATION (xsum (length, count));
4667 /* ENSURE_ALLOCATION will move the array
4668 (because it uses realloc(). */
4669 ENSURE_ALLOCATION (xsum (length, count));
4670 tmpsrc = (TCHAR_T *) (result + length);
4674 ENSURE_ALLOCATION (xsum (length, count));
4676 tmpdst = result + length;
4677 /* Copy backwards, because of overlapping. */
4680 for (n = count; n > 0; n--)
4681 *--tmpdst = (unsigned char) *--tmpsrc;
4686 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
4687 /* Make room for the result. */
4688 if (count > allocated - length)
4690 /* Need at least count elements. But allocate
4693 xmax (xsum (length, count), xtimes (allocated, 2));
4695 ENSURE_ALLOCATION (n);
4699 /* Here count <= allocated - length. */
4701 /* Perform padding. */
4702 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4703 if (pad_ourselves && has_width)
4706 # if ENABLE_UNISTDIO
4707 /* Outside POSIX, it's preferrable to compare the width
4708 against the number of _characters_ of the converted
4710 w = DCHAR_MBSNLEN (result + length, count);
4712 /* The width is compared against the number of _bytes_
4713 of the converted value, says POSIX. */
4718 size_t pad = width - w;
4720 /* Make room for the result. */
4721 if (xsum (count, pad) > allocated - length)
4723 /* Need at least count + pad elements. But
4724 allocate proportionally. */
4726 xmax (xsum3 (length, count, pad),
4727 xtimes (allocated, 2));
4731 ENSURE_ALLOCATION (n);
4734 ENSURE_ALLOCATION (n);
4737 /* Here count + pad <= allocated - length. */
4740 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
4741 DCHAR_T * const rp = result + length;
4743 DCHAR_T * const rp = tmp;
4745 DCHAR_T *p = rp + count;
4746 DCHAR_T *end = p + pad;
4748 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4749 if (dp->conversion == 'c'
4750 || dp->conversion == 's')
4751 /* No zero-padding for string directives. */
4756 pad_ptr = (*rp == '-' ? rp + 1 : rp);
4757 /* No zero-padding of "inf" and "nan". */
4758 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
4759 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
4762 /* The generated string now extends from rp to p,
4763 with the zero padding insertion point being at
4766 count = count + pad; /* = end - rp */
4768 if (flags & FLAG_LEFT)
4770 /* Pad with spaces on the right. */
4771 for (; pad > 0; pad--)
4774 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4776 /* Pad with zeroes. */
4781 for (; pad > 0; pad--)
4786 /* Pad with spaces on the left. */
4791 for (; pad > 0; pad--)
4799 /* Here still count <= allocated - length. */
4801 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
4802 /* The snprintf() result did fit. */
4804 /* Append the sprintf() result. */
4805 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4812 #if NEED_PRINTF_DIRECTIVE_F
4813 if (dp->conversion == 'F')
4815 /* Convert the %f result to upper case for %F. */
4816 DCHAR_T *rp = result + length;
4818 for (rc = count; rc > 0; rc--, rp++)
4819 if (*rp >= 'a' && *rp <= 'z')
4820 *rp = *rp - 'a' + 'A';
4831 /* Add the final NUL. */
4832 ENSURE_ALLOCATION (xsum (length, 1));
4833 result[length] = '\0';
4835 if (result != resultbuf && length + 1 < allocated)
4837 /* Shrink the allocated memory if possible. */
4840 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
4845 if (buf_malloced != NULL)
4846 free (buf_malloced);
4849 /* Note that we can produce a big string of a length > INT_MAX. POSIX
4850 says that snprintf() fails with errno = EOVERFLOW in this case, but
4851 that's only because snprintf() returns an 'int'. This function does
4852 not have this limitation. */
4857 if (!(result == resultbuf || result == NULL))
4859 if (buf_malloced != NULL)
4860 free (buf_malloced);
4867 if (!(result == resultbuf || result == NULL))
4869 if (buf_malloced != NULL)
4870 free (buf_malloced);
4878 #undef TCHARS_PER_DCHAR
4885 #undef DCHAR_IS_TCHAR