1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2007 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
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
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 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
123 # define EOVERFLOW E2BIG
128 # define local_wcslen wcslen
130 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
131 a dependency towards this library, here is a local substitute.
132 Define this substitute only once, even if this file is included
133 twice in the same compilation unit. */
134 # ifndef local_wcslen_defined
135 # define local_wcslen_defined 1
137 local_wcslen (const wchar_t *s
)
141 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
149 /* Default parameters. */
151 # if WIDE_CHAR_VERSION
152 # define VASNPRINTF vasnwprintf
153 # define FCHAR_T wchar_t
154 # define DCHAR_T wchar_t
155 # define TCHAR_T wchar_t
156 # define DCHAR_IS_TCHAR 1
157 # define DIRECTIVE wchar_t_directive
158 # define DIRECTIVES wchar_t_directives
159 # define PRINTF_PARSE wprintf_parse
160 # define DCHAR_CPY wmemcpy
162 # define VASNPRINTF vasnprintf
163 # define FCHAR_T char
164 # define DCHAR_T char
165 # define TCHAR_T char
166 # define DCHAR_IS_TCHAR 1
167 # define DIRECTIVE char_directive
168 # define DIRECTIVES char_directives
169 # define PRINTF_PARSE printf_parse
170 # define DCHAR_CPY memcpy
173 #if WIDE_CHAR_VERSION
174 /* TCHAR_T is wchar_t. */
175 # define USE_SNPRINTF 1
176 # if HAVE_DECL__SNWPRINTF
177 /* On Windows, the function swprintf() has a different signature than
178 on Unix; we use the _snwprintf() function instead. */
179 # define SNPRINTF _snwprintf
182 # define SNPRINTF swprintf
185 /* TCHAR_T is char. */
186 # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
187 But don't use it on BeOS, since BeOS snprintf produces no output if the
188 size argument is >= 0x3000000. */
189 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
190 # define USE_SNPRINTF 1
192 # define USE_SNPRINTF 0
194 # if HAVE_DECL__SNPRINTF
196 # define SNPRINTF _snprintf
199 # define SNPRINTF snprintf
200 /* Here we need to call the native snprintf, not rpl_snprintf. */
204 /* Here we need to call the native sprintf, not rpl_sprintf. */
207 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
208 /* Determine the decimal-point character according to the current locale. */
209 # ifndef decimal_point_char_defined
210 # define decimal_point_char_defined 1
212 decimal_point_char ()
215 /* Determine it in a multithread-safe way. We know nl_langinfo is
216 multithread-safe on glibc systems, but is not required to be multithread-
217 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
218 is rarely multithread-safe. */
219 # if HAVE_NL_LANGINFO && __GLIBC__
220 point
= nl_langinfo (RADIXCHAR
);
223 sprintf (pointbuf
, "%#.0f", 1.0);
224 point
= &pointbuf
[1];
226 point
= localeconv () -> decimal_point
;
228 /* The decimal point is always a single byte: either '.' or ','. */
229 return (point
[0] != '\0' ? point
[0] : '.');
234 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
236 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
238 is_infinite_or_zero (double x
)
240 return isnan (x
) || x
+ x
== x
;
245 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
247 /* Equivalent to !isfinite(x), but does not require libm. */
249 is_infinitel (long double x
)
251 return isnanl (x
) || (x
+ x
== x
&& x
!= 0.0L);
256 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
258 /* Converting 'long double' to decimal without rare rounding bugs requires
259 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
260 (and slower) algorithms. */
262 typedef unsigned int mp_limb_t
;
263 # define GMP_LIMB_BITS 32
264 typedef int mp_limb_verify
[2 * (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
) - 1];
266 typedef unsigned long long mp_twolimb_t
;
267 # define GMP_TWOLIMB_BITS 64
268 typedef int mp_twolimb_verify
[2 * (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
) - 1];
270 /* Representation of a bignum >= 0. */
274 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
277 /* Compute the product of two bignums >= 0.
278 Return the allocated memory in case of success, NULL in case of memory
279 allocation failure. */
281 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
288 if (src1
.nlimbs
<= src2
.nlimbs
)
302 /* Now 0 <= len1 <= len2. */
305 /* src1 or src2 is zero. */
307 dest
->limbs
= (mp_limb_t
*) malloc (1);
311 /* Here 1 <= len1 <= len2. */
317 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
320 for (k
= len2
; k
> 0; )
322 for (i
= 0; i
< len1
; i
++)
324 mp_limb_t digit1
= p1
[i
];
325 mp_twolimb_t carry
= 0;
326 for (j
= 0; j
< len2
; j
++)
328 mp_limb_t digit2
= p2
[j
];
329 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
331 dp
[i
+ j
] = (mp_limb_t
) carry
;
332 carry
= carry
>> GMP_LIMB_BITS
;
334 dp
[i
+ len2
] = (mp_limb_t
) carry
;
337 while (dlen
> 0 && dp
[dlen
- 1] == 0)
345 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
346 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
348 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
350 Return the allocated memory in case of success, NULL in case of memory
351 allocation failure. */
353 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
356 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
357 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
358 If m<n, then q:=0 and r:=a.
359 If m>=n=1, perform a single-precision division:
362 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
363 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
364 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
365 Normalise [q[m-1],...,q[0]], yields q.
366 If m>=n>1, perform a multiple-precision division:
367 We have a/b < beta^(m-n+1).
368 s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
369 Shift a and b left by s bits, copying them. r:=a.
370 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
371 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
373 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
374 In case of overflow (q* >= beta) set q* := beta-1.
375 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
376 and c3 := b[n-2] * q*.
377 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
378 occurred. Furthermore 0 <= c3 < beta^2.
379 If there was overflow and
380 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
381 the next test can be skipped.}
382 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
383 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
385 Put r := r - b * q* * beta^j. In detail:
386 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
387 hence: u:=0, for i:=0 to n-1 do
389 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
390 u:=u div beta (+ 1, if carry in subtraction)
392 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
394 the carry u does not overflow.}
395 If a negative carry occurs, put q* := q* - 1
396 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
398 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
399 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
401 The room for q[j] can be allocated at the memory location of r[n+j].
402 Finally, round-to-even:
403 Shift r left by 1 bit.
404 If r > b or if r = b and q[0] is odd, q := q+1.
406 const mp_limb_t
*a_ptr
= a
.limbs
;
407 size_t a_len
= a
.nlimbs
;
408 const mp_limb_t
*b_ptr
= b
.limbs
;
409 size_t b_len
= b
.nlimbs
;
411 mp_limb_t
*tmp_roomptr
= NULL
;
417 /* Allocate room for a_len+2 digits.
418 (Need a_len+1 digits for the real division and 1 more digit for the
419 final rounding of q.) */
420 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
425 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
432 /* Division by zero. */
434 if (b_ptr
[b_len
- 1] == 0)
440 /* Here m = a_len >= 0 and n = b_len > 0. */
444 /* m<n: trivial case. q=0, r := copy of a. */
447 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
448 q_ptr
= roomptr
+ a_len
;
453 /* n=1: single precision division.
454 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
458 mp_limb_t den
= b_ptr
[0];
459 mp_limb_t remainder
= 0;
460 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
461 mp_limb_t
*destptr
= q_ptr
+ a_len
;
463 for (count
= a_len
; count
> 0; count
--)
466 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
467 *--destptr
= num
/ den
;
468 remainder
= num
% den
;
470 /* Normalise and store r. */
473 r_ptr
[0] = remainder
;
480 if (q_ptr
[q_len
- 1] == 0)
486 /* n>1: multiple precision division.
487 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
488 beta^(m-n-1) <= a/b < beta^(m-n+1). */
492 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
520 /* 0 <= s < GMP_LIMB_BITS.
521 Copy b, shifting it left by s bits. */
524 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
525 if (tmp_roomptr
== NULL
)
531 const mp_limb_t
*sourceptr
= b_ptr
;
532 mp_limb_t
*destptr
= tmp_roomptr
;
533 mp_twolimb_t accu
= 0;
535 for (count
= b_len
; count
> 0; count
--)
537 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
538 *destptr
++ = (mp_limb_t
) accu
;
539 accu
= accu
>> GMP_LIMB_BITS
;
541 /* accu must be zero, since that was how s was determined. */
547 /* Copy a, shifting it left by s bits, yields r.
549 At the beginning: r = roomptr[0..a_len],
550 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
554 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
559 const mp_limb_t
*sourceptr
= a_ptr
;
560 mp_limb_t
*destptr
= r_ptr
;
561 mp_twolimb_t accu
= 0;
563 for (count
= a_len
; count
> 0; count
--)
565 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
566 *destptr
++ = (mp_limb_t
) accu
;
567 accu
= accu
>> GMP_LIMB_BITS
;
569 *destptr
++ = (mp_limb_t
) accu
;
571 q_ptr
= roomptr
+ b_len
;
572 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
574 size_t j
= a_len
- b_len
; /* m-n */
575 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
576 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
577 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
578 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
579 /* Division loop, traversed m-n+1 times.
580 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
585 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
587 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
589 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
590 | r_ptr
[j
+ b_len
- 1];
591 q_star
= num
/ b_msd
;
596 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
597 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
598 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
599 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
600 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
602 If yes, jump directly to the subtraction loop.
603 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
604 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
605 if (r_ptr
[j
+ b_len
] > b_msd
606 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
607 /* r[j+n] >= b[n-1]+1 or
608 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
613 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
615 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
616 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
617 mp_twolimb_t c3
= /* b[n-2] * q* */
618 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
619 /* While c2 < c3, increase c2 and decrease c3.
620 Consider c3-c2. While it is > 0, decrease it by
621 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
622 this can happen only twice. */
625 q_star
= q_star
- 1; /* q* := q* - 1 */
626 if (c3
- c2
> b_msdd
)
627 q_star
= q_star
- 1; /* q* := q* - 1 */
633 /* Subtract r := r - b * q* * beta^j. */
636 const mp_limb_t
*sourceptr
= b_ptr
;
637 mp_limb_t
*destptr
= r_ptr
+ j
;
638 mp_twolimb_t carry
= 0;
640 for (count
= b_len
; count
> 0; count
--)
642 /* Here 0 <= carry <= q*. */
645 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
646 + (mp_limb_t
) ~(*destptr
);
647 /* Here 0 <= carry <= beta*q* + beta-1. */
648 *destptr
++ = ~(mp_limb_t
) carry
;
649 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
651 cr
= (mp_limb_t
) carry
;
653 /* Subtract cr from r_ptr[j + b_len], then forget about
655 if (cr
> r_ptr
[j
+ b_len
])
657 /* Subtraction gave a carry. */
658 q_star
= q_star
- 1; /* q* := q* - 1 */
661 const mp_limb_t
*sourceptr
= b_ptr
;
662 mp_limb_t
*destptr
= r_ptr
+ j
;
665 for (count
= b_len
; count
> 0; count
--)
667 mp_limb_t source1
= *sourceptr
++;
668 mp_limb_t source2
= *destptr
;
669 *destptr
++ = source1
+ source2
+ carry
;
672 ? source1
>= (mp_limb_t
) ~source2
673 : source1
> (mp_limb_t
) ~source2
);
676 /* Forget about the carry and about r[j+n]. */
679 /* q* is determined. Store it as q[j]. */
688 if (q_ptr
[q_len
- 1] == 0)
690 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
691 b is shifted left by s bits. */
692 /* Shift r right by s bits. */
695 mp_limb_t ptr
= r_ptr
+ r_len
;
696 mp_twolimb_t accu
= 0;
698 for (count
= r_len
; count
> 0; count
--)
700 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
701 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
702 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
707 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
710 /* Compare r << 1 with b. */
718 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
719 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
720 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
730 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
735 for (i
= 0; i
< q_len
; i
++)
736 if (++(q_ptr
[i
]) != 0)
741 if (tmp_roomptr
!= NULL
)
748 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
750 Destroys the contents of a.
751 Return the allocated memory - containing the decimal digits in low-to-high
752 order, terminated with a NUL character - in case of success, NULL in case
753 of memory allocation failure. */
755 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
757 mp_limb_t
*a_ptr
= a
.limbs
;
758 size_t a_len
= a
.nlimbs
;
759 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
760 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
761 char *c_ptr
= (char *) malloc (xsum (c_len
, extra_zeroes
));
765 for (; extra_zeroes
> 0; extra_zeroes
--)
769 /* Divide a by 10^9, in-place. */
770 mp_limb_t remainder
= 0;
771 mp_limb_t
*ptr
= a_ptr
+ a_len
;
773 for (count
= a_len
; count
> 0; count
--)
776 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
777 *ptr
= num
/ 1000000000;
778 remainder
= num
% 1000000000;
780 /* Store the remainder as 9 decimal digits. */
781 for (count
= 9; count
> 0; count
--)
783 *d_ptr
++ = '0' + (remainder
% 10);
784 remainder
= remainder
/ 10;
787 if (a_ptr
[a_len
- 1] == 0)
790 /* Remove leading zeroes. */
791 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
793 /* But keep at least one zero. */
796 /* Terminate the string. */
802 # if NEED_PRINTF_LONG_DOUBLE
804 /* Assuming x is finite and >= 0:
805 write x as x = 2^e * m, where m is a bignum.
806 Return the allocated memory in case of success, NULL in case of memory
807 allocation failure. */
809 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
816 /* Allocate memory for result. */
817 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
818 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
821 /* Split into exponential part and mantissa. */
822 y
= frexpl (x
, &exp
);
823 if (!(y
>= 0.0L && y
< 1.0L))
825 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
826 latter is an integer. */
827 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
828 I'm not sure whether it's safe to cast a 'long double' value between
829 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
830 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
832 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
833 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
836 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
839 if (!(y
>= 0.0L && y
< 1.0L))
841 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
844 if (!(y
>= 0.0L && y
< 1.0L))
846 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
851 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
854 if (!(y
>= 0.0L && y
< 1.0L))
856 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
860 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
863 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
866 if (!(y
>= 0.0L && y
< 1.0L))
868 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
871 if (!(y
>= 0.0L && y
< 1.0L))
873 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
878 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
881 *ep
= exp
- LDBL_MANT_BIT
;
887 # if NEED_PRINTF_DOUBLE
889 /* Assuming x is finite and >= 0:
890 write x as x = 2^e * m, where m is a bignum.
891 Return the allocated memory in case of success, NULL in case of memory
892 allocation failure. */
894 decode_double (double x
, int *ep
, mpn_t
*mp
)
901 /* Allocate memory for result. */
902 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
903 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
906 /* Split into exponential part and mantissa. */
908 if (!(y
>= 0.0 && y
< 1.0))
910 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
911 latter is an integer. */
912 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
913 I'm not sure whether it's safe to cast a 'double' value between
914 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
915 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
917 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
918 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
921 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
924 if (!(y
>= 0.0 && y
< 1.0))
926 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
929 if (!(y
>= 0.0 && y
< 1.0))
931 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
936 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
939 if (!(y
>= 0.0 && y
< 1.0))
941 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
945 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
948 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
951 if (!(y
>= 0.0 && y
< 1.0))
953 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
956 if (!(y
>= 0.0 && y
< 1.0))
958 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
963 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
966 *ep
= exp
- DBL_MANT_BIT
;
972 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
973 Returns the decimal representation of round (x * 10^n).
974 Return the allocated memory - containing the decimal digits in low-to-high
975 order, terminated with a NUL character - in case of success, NULL in case
976 of memory allocation failure. */
978 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
986 unsigned int s_limbs
;
995 /* x = 2^e * m, hence
996 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
997 = round (2^s * 5^n * m). */
1000 /* Factor out a common power of 10 if possible. */
1003 extra_zeroes
= (s
< n
? s
: n
);
1007 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1008 Before converting to decimal, we need to compute
1009 z = round (2^s * 5^n * m). */
1010 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1011 sign. 2.322 is slightly larger than log(5)/log(2). */
1012 abs_n
= (n
>= 0 ? n
: -n
);
1013 abs_s
= (s
>= 0 ? s
: -s
);
1014 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1015 + abs_s
/ GMP_LIMB_BITS
+ 1)
1016 * sizeof (mp_limb_t
));
1017 if (pow5_ptr
== NULL
)
1022 /* Initialize with 1. */
1025 /* Multiply with 5^|n|. */
1028 static mp_limb_t
const small_pow5
[13 + 1] =
1030 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1031 48828125, 244140625, 1220703125
1034 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1036 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1038 mp_twolimb_t carry
= 0;
1039 for (j
= 0; j
< pow5_len
; j
++)
1041 mp_limb_t digit2
= pow5_ptr
[j
];
1042 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1043 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1044 carry
= carry
>> GMP_LIMB_BITS
;
1047 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1050 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1051 s_bits
= abs_s
% GMP_LIMB_BITS
;
1052 if (n
>= 0 ? s
>= 0 : s
<= 0)
1054 /* Multiply with 2^|s|. */
1057 mp_limb_t
*ptr
= pow5_ptr
;
1058 mp_twolimb_t accu
= 0;
1060 for (count
= pow5_len
; count
> 0; count
--)
1062 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1063 *ptr
++ = (mp_limb_t
) accu
;
1064 accu
= accu
>> GMP_LIMB_BITS
;
1068 *ptr
= (mp_limb_t
) accu
;
1075 for (count
= pow5_len
; count
> 0;)
1078 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1080 for (count
= s_limbs
; count
> 0;)
1083 pow5_ptr
[count
] = 0;
1085 pow5_len
+= s_limbs
;
1087 pow5
.limbs
= pow5_ptr
;
1088 pow5
.nlimbs
= pow5_len
;
1091 /* Multiply m with pow5. No division needed. */
1092 z_memory
= multiply (m
, pow5
, &z
);
1096 /* Divide m by pow5 and round. */
1097 z_memory
= divide (m
, pow5
, &z
);
1102 pow5
.limbs
= pow5_ptr
;
1103 pow5
.nlimbs
= pow5_len
;
1107 Multiply m with pow5, then divide by 2^|s|. */
1111 tmp_memory
= multiply (m
, pow5
, &numerator
);
1112 if (tmp_memory
== NULL
)
1118 /* Construct 2^|s|. */
1120 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1122 for (i
= 0; i
< s_limbs
; i
++)
1124 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1125 denominator
.limbs
= ptr
;
1126 denominator
.nlimbs
= s_limbs
+ 1;
1128 z_memory
= divide (numerator
, denominator
, &z
);
1134 Multiply m with 2^s, then divide by pow5. */
1137 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1138 * sizeof (mp_limb_t
));
1139 if (num_ptr
== NULL
)
1146 mp_limb_t
*destptr
= num_ptr
;
1149 for (i
= 0; i
< s_limbs
; i
++)
1154 const mp_limb_t
*sourceptr
= m
.limbs
;
1155 mp_twolimb_t accu
= 0;
1157 for (count
= m
.nlimbs
; count
> 0; count
--)
1159 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1160 *destptr
++ = (mp_limb_t
) accu
;
1161 accu
= accu
>> GMP_LIMB_BITS
;
1164 *destptr
++ = (mp_limb_t
) accu
;
1168 const mp_limb_t
*sourceptr
= m
.limbs
;
1170 for (count
= m
.nlimbs
; count
> 0; count
--)
1171 *destptr
++ = *sourceptr
++;
1173 numerator
.limbs
= num_ptr
;
1174 numerator
.nlimbs
= destptr
- num_ptr
;
1176 z_memory
= divide (numerator
, pow5
, &z
);
1183 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1185 if (z_memory
== NULL
)
1187 digits
= convert_to_decimal (z
, extra_zeroes
);
1192 # if NEED_PRINTF_LONG_DOUBLE
1194 /* Assuming x is finite and >= 0, and n is an integer:
1195 Returns the decimal representation of round (x * 10^n).
1196 Return the allocated memory - containing the decimal digits in low-to-high
1197 order, terminated with a NUL character - in case of success, NULL in case
1198 of memory allocation failure. */
1200 scale10_round_decimal_long_double (long double x
, int n
)
1204 void *memory
= decode_long_double (x
, &e
, &m
);
1205 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1210 # if NEED_PRINTF_DOUBLE
1212 /* Assuming x is finite and >= 0, and n is an integer:
1213 Returns the decimal representation of round (x * 10^n).
1214 Return the allocated memory - containing the decimal digits in low-to-high
1215 order, terminated with a NUL character - in case of success, NULL in case
1216 of memory allocation failure. */
1218 scale10_round_decimal_double (double x
, int n
)
1222 void *memory
= decode_double (x
, &e
, &m
);
1223 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1228 # if NEED_PRINTF_LONG_DOUBLE
1230 /* Assuming x is finite and > 0:
1231 Return an approximation for n with 10^n <= x < 10^(n+1).
1232 The approximation is usually the right n, but may be off by 1 sometimes. */
1234 floorlog10l (long double x
)
1241 /* Split into exponential part and mantissa. */
1242 y
= frexpl (x
, &exp
);
1243 if (!(y
>= 0.0L && y
< 1.0L))
1249 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1251 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1252 exp
-= GMP_LIMB_BITS
;
1254 if (y
< (1.0L / (1 << 16)))
1256 y
*= 1.0L * (1 << 16);
1259 if (y
< (1.0L / (1 << 8)))
1261 y
*= 1.0L * (1 << 8);
1264 if (y
< (1.0L / (1 << 4)))
1266 y
*= 1.0L * (1 << 4);
1269 if (y
< (1.0L / (1 << 2)))
1271 y
*= 1.0L * (1 << 2);
1274 if (y
< (1.0L / (1 << 1)))
1276 y
*= 1.0L * (1 << 1);
1280 if (!(y
>= 0.5L && y
< 1.0L))
1282 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1285 if (z
< 0.70710678118654752444)
1287 z
*= 1.4142135623730950488;
1290 if (z
< 0.8408964152537145431)
1292 z
*= 1.1892071150027210667;
1295 if (z
< 0.91700404320467123175)
1297 z
*= 1.0905077326652576592;
1300 if (z
< 0.9576032806985736469)
1302 z
*= 1.0442737824274138403;
1305 /* Now 0.95 <= z <= 1.01. */
1307 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1308 Four terms are enough to get an approximation with error < 10^-7. */
1309 l
-= z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1310 /* Finally multiply with log(2)/log(10), yields an approximation for
1312 l
*= 0.30102999566398119523;
1313 /* Round down to the next integer. */
1314 return (int) l
+ (l
< 0 ? -1 : 0);
1319 # if NEED_PRINTF_DOUBLE
1321 /* Assuming x is finite and > 0:
1322 Return an approximation for n with 10^n <= x < 10^(n+1).
1323 The approximation is usually the right n, but may be off by 1 sometimes. */
1325 floorlog10 (double x
)
1332 /* Split into exponential part and mantissa. */
1333 y
= frexp (x
, &exp
);
1334 if (!(y
>= 0.0 && y
< 1.0))
1340 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1342 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1343 exp
-= GMP_LIMB_BITS
;
1345 if (y
< (1.0 / (1 << 16)))
1347 y
*= 1.0 * (1 << 16);
1350 if (y
< (1.0 / (1 << 8)))
1352 y
*= 1.0 * (1 << 8);
1355 if (y
< (1.0 / (1 << 4)))
1357 y
*= 1.0 * (1 << 4);
1360 if (y
< (1.0 / (1 << 2)))
1362 y
*= 1.0 * (1 << 2);
1365 if (y
< (1.0 / (1 << 1)))
1367 y
*= 1.0 * (1 << 1);
1371 if (!(y
>= 0.5 && y
< 1.0))
1373 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1376 if (z
< 0.70710678118654752444)
1378 z
*= 1.4142135623730950488;
1381 if (z
< 0.8408964152537145431)
1383 z
*= 1.1892071150027210667;
1386 if (z
< 0.91700404320467123175)
1388 z
*= 1.0905077326652576592;
1391 if (z
< 0.9576032806985736469)
1393 z
*= 1.0442737824274138403;
1396 /* Now 0.95 <= z <= 1.01. */
1398 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1399 Four terms are enough to get an approximation with error < 10^-7. */
1400 l
-= z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1401 /* Finally multiply with log(2)/log(10), yields an approximation for
1403 l
*= 0.30102999566398119523;
1404 /* Round down to the next integer. */
1405 return (int) l
+ (l
< 0 ? -1 : 0);
1413 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1414 const FCHAR_T
*format
, va_list args
)
1419 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1420 /* errno is already set. */
1428 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1436 size_t buf_neededlength
;
1438 TCHAR_T
*buf_malloced
;
1442 /* Output string accumulator. */
1447 /* Allocate a small buffer that will hold a directive passed to
1448 sprintf or snprintf. */
1450 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1452 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1454 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1455 buf_malloced
= NULL
;
1460 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1461 if (size_overflow_p (buf_memsize
))
1462 goto out_of_memory_1
;
1463 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1465 goto out_of_memory_1
;
1469 if (resultbuf
!= NULL
)
1472 allocated
= *lengthp
;
1481 result is either == resultbuf or == NULL or malloc-allocated.
1482 If length > 0, then result != NULL. */
1484 /* Ensures that allocated >= needed. Aborts through a jump to
1485 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1486 #define ENSURE_ALLOCATION(needed) \
1487 if ((needed) > allocated) \
1489 size_t memory_size; \
1492 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1493 if ((needed) > allocated) \
1494 allocated = (needed); \
1495 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1496 if (size_overflow_p (memory_size)) \
1497 goto out_of_memory; \
1498 if (result == resultbuf || result == NULL) \
1499 memory = (DCHAR_T *) malloc (memory_size); \
1501 memory = (DCHAR_T *) realloc (result, memory_size); \
1502 if (memory == NULL) \
1503 goto out_of_memory; \
1504 if (result == resultbuf && length > 0) \
1505 DCHAR_CPY (memory, result, length); \
1509 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1511 if (cp
!= dp
->dir_start
)
1513 size_t n
= dp
->dir_start
- cp
;
1514 size_t augmented_length
= xsum (length
, n
);
1516 ENSURE_ALLOCATION (augmented_length
);
1517 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1518 need that the format string contains only ASCII characters
1519 if FCHAR_T and DCHAR_T are not the same type. */
1520 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1522 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1523 length
= augmented_length
;
1528 result
[length
++] = (unsigned char) *cp
++;
1535 /* Execute a single directive. */
1536 if (dp
->conversion
== '%')
1538 size_t augmented_length
;
1540 if (!(dp
->arg_index
== ARG_NONE
))
1542 augmented_length
= xsum (length
, 1);
1543 ENSURE_ALLOCATION (augmented_length
);
1544 result
[length
] = '%';
1545 length
= augmented_length
;
1549 if (!(dp
->arg_index
!= ARG_NONE
))
1552 if (dp
->conversion
== 'n')
1554 switch (a
.arg
[dp
->arg_index
].type
)
1556 case TYPE_COUNT_SCHAR_POINTER
:
1557 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1559 case TYPE_COUNT_SHORT_POINTER
:
1560 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1562 case TYPE_COUNT_INT_POINTER
:
1563 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1565 case TYPE_COUNT_LONGINT_POINTER
:
1566 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1568 #if HAVE_LONG_LONG_INT
1569 case TYPE_COUNT_LONGLONGINT_POINTER
:
1570 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1578 /* The unistdio extensions. */
1579 else if (dp
->conversion
== 'U')
1581 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1582 int flags
= dp
->flags
;
1590 if (dp
->width_start
!= dp
->width_end
)
1592 if (dp
->width_arg_index
!= ARG_NONE
)
1596 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1598 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1601 /* "A negative field width is taken as a '-' flag
1602 followed by a positive field width." */
1604 width
= (unsigned int) (-arg
);
1611 const FCHAR_T
*digitp
= dp
->width_start
;
1614 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1615 while (digitp
!= dp
->width_end
);
1622 if (dp
->precision_start
!= dp
->precision_end
)
1624 if (dp
->precision_arg_index
!= ARG_NONE
)
1628 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
1630 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
1631 /* "A negative precision is taken as if the precision
1641 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
1644 while (digitp
!= dp
->precision_end
)
1645 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
1652 case TYPE_U8_STRING
:
1654 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
1655 const uint8_t *arg_end
;
1660 /* Use only PRECISION characters, from the left. */
1663 for (; precision
> 0; precision
--)
1665 int count
= u8_strmblen (arg_end
);
1670 if (!(result
== resultbuf
|| result
== NULL
))
1672 if (buf_malloced
!= NULL
)
1673 free (buf_malloced
);
1684 /* Use the entire string, and count the number of
1690 int count
= u8_strmblen (arg_end
);
1695 if (!(result
== resultbuf
|| result
== NULL
))
1697 if (buf_malloced
!= NULL
)
1698 free (buf_malloced
);
1709 /* Use the entire string. */
1710 arg_end
= arg
+ u8_strlen (arg
);
1711 /* The number of characters doesn't matter. */
1715 if (has_width
&& width
> characters
1716 && !(dp
->flags
& FLAG_LEFT
))
1718 size_t n
= width
- characters
;
1719 ENSURE_ALLOCATION (xsum (length
, n
));
1720 DCHAR_SET (result
+ length
, ' ', n
);
1724 # if DCHAR_IS_UINT8_T
1726 size_t n
= arg_end
- arg
;
1727 ENSURE_ALLOCATION (xsum (length
, n
));
1728 DCHAR_CPY (result
+ length
, arg
, n
);
1733 DCHAR_T
*converted
= result
+ length
;
1734 size_t converted_len
= allocated
- length
;
1736 /* Convert from UTF-8 to locale encoding. */
1737 if (u8_conv_to_encoding (locale_charset (),
1738 iconveh_question_mark
,
1739 arg
, arg_end
- arg
, NULL
,
1740 &converted
, &converted_len
)
1743 /* Convert from UTF-8 to UTF-16/UTF-32. */
1745 U8_TO_DCHAR (arg
, arg_end
- arg
,
1746 converted
, &converted_len
);
1747 if (converted
== NULL
)
1750 int saved_errno
= errno
;
1751 if (!(result
== resultbuf
|| result
== NULL
))
1753 if (buf_malloced
!= NULL
)
1754 free (buf_malloced
);
1756 errno
= saved_errno
;
1759 if (converted
!= result
+ length
)
1761 ENSURE_ALLOCATION (xsum (length
, converted_len
));
1762 DCHAR_CPY (result
+ length
, converted
, converted_len
);
1765 length
+= converted_len
;
1769 if (has_width
&& width
> characters
1770 && (dp
->flags
& FLAG_LEFT
))
1772 size_t n
= width
- characters
;
1773 ENSURE_ALLOCATION (xsum (length
, n
));
1774 DCHAR_SET (result
+ length
, ' ', n
);
1780 case TYPE_U16_STRING
:
1782 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
1783 const uint16_t *arg_end
;
1788 /* Use only PRECISION characters, from the left. */
1791 for (; precision
> 0; precision
--)
1793 int count
= u16_strmblen (arg_end
);
1798 if (!(result
== resultbuf
|| result
== NULL
))
1800 if (buf_malloced
!= NULL
)
1801 free (buf_malloced
);
1812 /* Use the entire string, and count the number of
1818 int count
= u16_strmblen (arg_end
);
1823 if (!(result
== resultbuf
|| result
== NULL
))
1825 if (buf_malloced
!= NULL
)
1826 free (buf_malloced
);
1837 /* Use the entire string. */
1838 arg_end
= arg
+ u16_strlen (arg
);
1839 /* The number of characters doesn't matter. */
1843 if (has_width
&& width
> characters
1844 && !(dp
->flags
& FLAG_LEFT
))
1846 size_t n
= width
- characters
;
1847 ENSURE_ALLOCATION (xsum (length
, n
));
1848 DCHAR_SET (result
+ length
, ' ', n
);
1852 # if DCHAR_IS_UINT16_T
1854 size_t n
= arg_end
- arg
;
1855 ENSURE_ALLOCATION (xsum (length
, n
));
1856 DCHAR_CPY (result
+ length
, arg
, n
);
1861 DCHAR_T
*converted
= result
+ length
;
1862 size_t converted_len
= allocated
- length
;
1864 /* Convert from UTF-16 to locale encoding. */
1865 if (u16_conv_to_encoding (locale_charset (),
1866 iconveh_question_mark
,
1867 arg
, arg_end
- arg
, NULL
,
1868 &converted
, &converted_len
)
1871 /* Convert from UTF-16 to UTF-8/UTF-32. */
1873 U16_TO_DCHAR (arg
, arg_end
- arg
,
1874 converted
, &converted_len
);
1875 if (converted
== NULL
)
1878 int saved_errno
= errno
;
1879 if (!(result
== resultbuf
|| result
== NULL
))
1881 if (buf_malloced
!= NULL
)
1882 free (buf_malloced
);
1884 errno
= saved_errno
;
1887 if (converted
!= result
+ length
)
1889 ENSURE_ALLOCATION (xsum (length
, converted_len
));
1890 DCHAR_CPY (result
+ length
, converted
, converted_len
);
1893 length
+= converted_len
;
1897 if (has_width
&& width
> characters
1898 && (dp
->flags
& FLAG_LEFT
))
1900 size_t n
= width
- characters
;
1901 ENSURE_ALLOCATION (xsum (length
, n
));
1902 DCHAR_SET (result
+ length
, ' ', n
);
1908 case TYPE_U32_STRING
:
1910 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
1911 const uint32_t *arg_end
;
1916 /* Use only PRECISION characters, from the left. */
1919 for (; precision
> 0; precision
--)
1921 int count
= u32_strmblen (arg_end
);
1926 if (!(result
== resultbuf
|| result
== NULL
))
1928 if (buf_malloced
!= NULL
)
1929 free (buf_malloced
);
1940 /* Use the entire string, and count the number of
1946 int count
= u32_strmblen (arg_end
);
1951 if (!(result
== resultbuf
|| result
== NULL
))
1953 if (buf_malloced
!= NULL
)
1954 free (buf_malloced
);
1965 /* Use the entire string. */
1966 arg_end
= arg
+ u32_strlen (arg
);
1967 /* The number of characters doesn't matter. */
1971 if (has_width
&& width
> characters
1972 && !(dp
->flags
& FLAG_LEFT
))
1974 size_t n
= width
- characters
;
1975 ENSURE_ALLOCATION (xsum (length
, n
));
1976 DCHAR_SET (result
+ length
, ' ', n
);
1980 # if DCHAR_IS_UINT32_T
1982 size_t n
= arg_end
- arg
;
1983 ENSURE_ALLOCATION (xsum (length
, n
));
1984 DCHAR_CPY (result
+ length
, arg
, n
);
1989 DCHAR_T
*converted
= result
+ length
;
1990 size_t converted_len
= allocated
- length
;
1992 /* Convert from UTF-32 to locale encoding. */
1993 if (u32_conv_to_encoding (locale_charset (),
1994 iconveh_question_mark
,
1995 arg
, arg_end
- arg
, NULL
,
1996 &converted
, &converted_len
)
1999 /* Convert from UTF-32 to UTF-8/UTF-16. */
2001 U32_TO_DCHAR (arg
, arg_end
- arg
,
2002 converted
, &converted_len
);
2003 if (converted
== NULL
)
2006 int saved_errno
= errno
;
2007 if (!(result
== resultbuf
|| result
== NULL
))
2009 if (buf_malloced
!= NULL
)
2010 free (buf_malloced
);
2012 errno
= saved_errno
;
2015 if (converted
!= result
+ length
)
2017 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2018 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2021 length
+= converted_len
;
2025 if (has_width
&& width
> characters
2026 && (dp
->flags
& FLAG_LEFT
))
2028 size_t n
= width
- characters
;
2029 ENSURE_ALLOCATION (xsum (length
, n
));
2030 DCHAR_SET (result
+ length
, ' ', n
);
2041 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2042 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
2043 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2045 # if NEED_PRINTF_DOUBLE
2046 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2048 # if NEED_PRINTF_LONG_DOUBLE
2049 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2055 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2056 int flags
= dp
->flags
;
2062 DCHAR_T tmpbuf
[700];
2069 if (dp
->width_start
!= dp
->width_end
)
2071 if (dp
->width_arg_index
!= ARG_NONE
)
2075 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2077 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2080 /* "A negative field width is taken as a '-' flag
2081 followed by a positive field width." */
2083 width
= (unsigned int) (-arg
);
2090 const FCHAR_T
*digitp
= dp
->width_start
;
2093 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2094 while (digitp
!= dp
->width_end
);
2101 if (dp
->precision_start
!= dp
->precision_end
)
2103 if (dp
->precision_arg_index
!= ARG_NONE
)
2107 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2109 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2110 /* "A negative precision is taken as if the precision
2120 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2123 while (digitp
!= dp
->precision_end
)
2124 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2129 /* Allocate a temporary buffer of sufficient size. */
2130 if (type
== TYPE_LONGDOUBLE
)
2132 (unsigned int) ((LDBL_DIG
+ 1)
2133 * 0.831 /* decimal -> hexadecimal */
2135 + 1; /* turn floor into ceil */
2138 (unsigned int) ((DBL_DIG
+ 1)
2139 * 0.831 /* decimal -> hexadecimal */
2141 + 1; /* turn floor into ceil */
2142 if (tmp_length
< precision
)
2143 tmp_length
= precision
;
2144 /* Account for sign, decimal point etc. */
2145 tmp_length
= xsum (tmp_length
, 12);
2147 if (tmp_length
< width
)
2150 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
2152 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
2156 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
2158 if (size_overflow_p (tmp_memsize
))
2159 /* Overflow, would lead to out of memory. */
2161 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
2163 /* Out of memory. */
2169 if (type
== TYPE_LONGDOUBLE
)
2171 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2172 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
2176 if (dp
->conversion
== 'A')
2178 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2182 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2188 DECL_LONG_DOUBLE_ROUNDING
2190 BEGIN_LONG_DOUBLE_ROUNDING ();
2192 if (signbit (arg
)) /* arg < 0.0L or negative zero */
2200 else if (flags
& FLAG_SHOWSIGN
)
2202 else if (flags
& FLAG_SPACE
)
2205 if (arg
> 0.0L && arg
+ arg
== arg
)
2207 if (dp
->conversion
== 'A')
2209 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2213 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2219 long double mantissa
;
2222 mantissa
= printf_frexpl (arg
, &exponent
);
2230 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
2232 /* Round the mantissa. */
2233 long double tail
= mantissa
;
2236 for (q
= precision
; ; q
--)
2238 int digit
= (int) tail
;
2242 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
2251 for (q
= precision
; q
> 0; q
--)
2257 *p
++ = dp
->conversion
- 'A' + 'X';
2262 digit
= (int) mantissa
;
2265 if ((flags
& FLAG_ALT
)
2266 || mantissa
> 0.0L || precision
> 0)
2268 *p
++ = decimal_point_char ();
2269 /* This loop terminates because we assume
2270 that FLT_RADIX is a power of 2. */
2271 while (mantissa
> 0.0L)
2274 digit
= (int) mantissa
;
2279 : dp
->conversion
- 10);
2283 while (precision
> 0)
2290 *p
++ = dp
->conversion
- 'A' + 'P';
2291 # if WIDE_CHAR_VERSION
2293 static const wchar_t decimal_format
[] =
2294 { '%', '+', 'd', '\0' };
2295 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2300 if (sizeof (DCHAR_T
) == 1)
2302 sprintf ((char *) p
, "%+d", exponent
);
2310 sprintf (expbuf
, "%+d", exponent
);
2311 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2317 END_LONG_DOUBLE_ROUNDING ();
2325 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2326 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
2330 if (dp
->conversion
== 'A')
2332 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2336 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2343 if (signbit (arg
)) /* arg < 0.0 or negative zero */
2351 else if (flags
& FLAG_SHOWSIGN
)
2353 else if (flags
& FLAG_SPACE
)
2356 if (arg
> 0.0 && arg
+ arg
== arg
)
2358 if (dp
->conversion
== 'A')
2360 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2364 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2373 mantissa
= printf_frexp (arg
, &exponent
);
2381 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
2383 /* Round the mantissa. */
2384 double tail
= mantissa
;
2387 for (q
= precision
; ; q
--)
2389 int digit
= (int) tail
;
2393 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
2402 for (q
= precision
; q
> 0; q
--)
2408 *p
++ = dp
->conversion
- 'A' + 'X';
2413 digit
= (int) mantissa
;
2416 if ((flags
& FLAG_ALT
)
2417 || mantissa
> 0.0 || precision
> 0)
2419 *p
++ = decimal_point_char ();
2420 /* This loop terminates because we assume
2421 that FLT_RADIX is a power of 2. */
2422 while (mantissa
> 0.0)
2425 digit
= (int) mantissa
;
2430 : dp
->conversion
- 10);
2434 while (precision
> 0)
2441 *p
++ = dp
->conversion
- 'A' + 'P';
2442 # if WIDE_CHAR_VERSION
2444 static const wchar_t decimal_format
[] =
2445 { '%', '+', 'd', '\0' };
2446 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2451 if (sizeof (DCHAR_T
) == 1)
2453 sprintf ((char *) p
, "%+d", exponent
);
2461 sprintf (expbuf
, "%+d", exponent
);
2462 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2472 /* The generated string now extends from tmp to p, with the
2473 zero padding insertion point being at pad_ptr. */
2474 if (has_width
&& p
- tmp
< width
)
2476 size_t pad
= width
- (p
- tmp
);
2477 DCHAR_T
*end
= p
+ pad
;
2479 if (flags
& FLAG_LEFT
)
2481 /* Pad with spaces on the right. */
2482 for (; pad
> 0; pad
--)
2485 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
2487 /* Pad with zeroes. */
2492 for (; pad
> 0; pad
--)
2497 /* Pad with spaces on the left. */
2502 for (; pad
> 0; pad
--)
2510 size_t count
= p
- tmp
;
2512 if (count
>= tmp_length
)
2513 /* tmp_length was incorrectly calculated - fix the
2517 /* Make room for the result. */
2518 if (count
>= allocated
- length
)
2520 size_t n
= xsum (length
, count
);
2522 ENSURE_ALLOCATION (n
);
2525 /* Append the result. */
2526 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
2533 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2534 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
2535 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
2536 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
2537 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
2539 # if NEED_PRINTF_DOUBLE
2540 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2541 # elif NEED_PRINTF_INFINITE_DOUBLE
2542 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2543 /* The systems (mingw) which produce wrong output
2544 for Inf, -Inf, and NaN also do so for -0.0.
2545 Therefore we treat this case here as well. */
2546 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
2548 # if NEED_PRINTF_LONG_DOUBLE
2549 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2550 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2551 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2552 /* Some systems produce wrong output for Inf,
2554 && is_infinitel (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
2558 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2559 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2561 int flags
= dp
->flags
;
2567 DCHAR_T tmpbuf
[700];
2574 if (dp
->width_start
!= dp
->width_end
)
2576 if (dp
->width_arg_index
!= ARG_NONE
)
2580 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2582 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2585 /* "A negative field width is taken as a '-' flag
2586 followed by a positive field width." */
2588 width
= (unsigned int) (-arg
);
2595 const FCHAR_T
*digitp
= dp
->width_start
;
2598 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2599 while (digitp
!= dp
->width_end
);
2606 if (dp
->precision_start
!= dp
->precision_end
)
2608 if (dp
->precision_arg_index
!= ARG_NONE
)
2612 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2614 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2615 /* "A negative precision is taken as if the precision
2625 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2628 while (digitp
!= dp
->precision_end
)
2629 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2634 /* POSIX specifies the default precision to be 6 for %f, %F,
2635 %e, %E, but not for %g, %G. Implementations appear to use
2636 the same default precision also for %g, %G. */
2640 /* Allocate a temporary buffer of sufficient size. */
2641 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2642 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
2643 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2644 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
2645 # elif NEED_PRINTF_LONG_DOUBLE
2646 tmp_length
= LDBL_DIG
+ 1;
2647 # elif NEED_PRINTF_DOUBLE
2648 tmp_length
= DBL_DIG
+ 1;
2652 if (tmp_length
< precision
)
2653 tmp_length
= precision
;
2654 # if NEED_PRINTF_LONG_DOUBLE
2655 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2656 if (type
== TYPE_LONGDOUBLE
)
2658 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2660 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
2661 if (!(isnanl (arg
) || arg
+ arg
== arg
))
2663 /* arg is finite and nonzero. */
2664 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
2665 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
2666 tmp_length
= exponent
+ precision
;
2670 # if NEED_PRINTF_DOUBLE
2671 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2672 if (type
== TYPE_DOUBLE
)
2674 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2676 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
2677 if (!(isnan (arg
) || arg
+ arg
== arg
))
2679 /* arg is finite and nonzero. */
2680 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
2681 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
2682 tmp_length
= exponent
+ precision
;
2686 /* Account for sign, decimal point etc. */
2687 tmp_length
= xsum (tmp_length
, 12);
2689 if (tmp_length
< width
)
2692 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
2694 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
2698 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
2700 if (size_overflow_p (tmp_memsize
))
2701 /* Overflow, would lead to out of memory. */
2703 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
2705 /* Out of memory. */
2712 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2713 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2714 if (type
== TYPE_LONGDOUBLE
)
2717 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
2721 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2723 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2727 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2733 DECL_LONG_DOUBLE_ROUNDING
2735 BEGIN_LONG_DOUBLE_ROUNDING ();
2737 if (signbit (arg
)) /* arg < 0.0L or negative zero */
2745 else if (flags
& FLAG_SHOWSIGN
)
2747 else if (flags
& FLAG_SPACE
)
2750 if (arg
> 0.0L && arg
+ arg
== arg
)
2752 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2754 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2758 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2763 # if NEED_PRINTF_LONG_DOUBLE
2766 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2772 scale10_round_decimal_long_double (arg
, precision
);
2775 END_LONG_DOUBLE_ROUNDING ();
2778 ndigits
= strlen (digits
);
2780 if (ndigits
> precision
)
2784 *p
++ = digits
[ndigits
];
2786 while (ndigits
> precision
);
2789 /* Here ndigits <= precision. */
2790 if ((flags
& FLAG_ALT
) || precision
> 0)
2792 *p
++ = decimal_point_char ();
2793 for (; precision
> ndigits
; precision
--)
2798 *p
++ = digits
[ndigits
];
2804 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
2812 if ((flags
& FLAG_ALT
) || precision
> 0)
2814 *p
++ = decimal_point_char ();
2815 for (; precision
> 0; precision
--)
2826 exponent
= floorlog10l (arg
);
2831 scale10_round_decimal_long_double (arg
,
2832 (int)precision
- exponent
);
2835 END_LONG_DOUBLE_ROUNDING ();
2838 ndigits
= strlen (digits
);
2840 if (ndigits
== precision
+ 1)
2842 if (ndigits
< precision
2843 || ndigits
> precision
+ 2)
2844 /* The exponent was not guessed
2845 precisely enough. */
2848 /* None of two values of exponent is
2849 the right one. Prevent an endless
2853 if (ndigits
== precision
)
2860 /* Here ndigits = precision+1. */
2861 *p
++ = digits
[--ndigits
];
2862 if ((flags
& FLAG_ALT
) || precision
> 0)
2864 *p
++ = decimal_point_char ();
2868 *p
++ = digits
[ndigits
];
2875 *p
++ = dp
->conversion
; /* 'e' or 'E' */
2876 # if WIDE_CHAR_VERSION
2878 static const wchar_t decimal_format
[] =
2879 { '%', '+', '.', '2', 'd', '\0' };
2880 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2885 if (sizeof (DCHAR_T
) == 1)
2887 sprintf ((char *) p
, "%+.2d", exponent
);
2895 sprintf (expbuf
, "%+.2d", exponent
);
2896 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2901 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
2905 /* precision >= 1. */
2908 /* The exponent is 0, >= -4, < precision.
2909 Use fixed-point notation. */
2911 size_t ndigits
= precision
;
2912 /* Number of trailing zeroes that have to be
2915 (flags
& FLAG_ALT
? 0 : precision
- 1);
2919 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
2921 *p
++ = decimal_point_char ();
2922 while (ndigits
> nzeroes
)
2938 exponent
= floorlog10l (arg
);
2943 scale10_round_decimal_long_double (arg
,
2944 (int)(precision
- 1) - exponent
);
2947 END_LONG_DOUBLE_ROUNDING ();
2950 ndigits
= strlen (digits
);
2952 if (ndigits
== precision
)
2954 if (ndigits
< precision
- 1
2955 || ndigits
> precision
+ 1)
2956 /* The exponent was not guessed
2957 precisely enough. */
2960 /* None of two values of exponent is
2961 the right one. Prevent an endless
2965 if (ndigits
< precision
)
2971 /* Here ndigits = precision. */
2973 /* Determine the number of trailing zeroes
2974 that have to be dropped. */
2976 if ((flags
& FLAG_ALT
) == 0)
2977 while (nzeroes
< ndigits
2978 && digits
[nzeroes
] == '0')
2981 /* The exponent is now determined. */
2983 && exponent
< (long)precision
)
2985 /* Fixed-point notation:
2986 max(exponent,0)+1 digits, then the
2987 decimal point, then the remaining
2988 digits without trailing zeroes. */
2991 size_t count
= exponent
+ 1;
2992 /* Note: count <= precision = ndigits. */
2993 for (; count
> 0; count
--)
2994 *p
++ = digits
[--ndigits
];
2995 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
2997 *p
++ = decimal_point_char ();
2998 while (ndigits
> nzeroes
)
3001 *p
++ = digits
[ndigits
];
3007 size_t count
= -exponent
- 1;
3009 *p
++ = decimal_point_char ();
3010 for (; count
> 0; count
--)
3012 while (ndigits
> nzeroes
)
3015 *p
++ = digits
[ndigits
];
3021 /* Exponential notation. */
3022 *p
++ = digits
[--ndigits
];
3023 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3025 *p
++ = decimal_point_char ();
3026 while (ndigits
> nzeroes
)
3029 *p
++ = digits
[ndigits
];
3032 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3033 # if WIDE_CHAR_VERSION
3035 static const wchar_t decimal_format
[] =
3036 { '%', '+', '.', '2', 'd', '\0' };
3037 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3042 if (sizeof (DCHAR_T
) == 1)
3044 sprintf ((char *) p
, "%+.2d", exponent
);
3052 sprintf (expbuf
, "%+.2d", exponent
);
3053 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3065 /* arg is finite. */
3070 END_LONG_DOUBLE_ROUNDING ();
3073 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3077 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3079 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3083 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3085 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3089 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3096 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3104 else if (flags
& FLAG_SHOWSIGN
)
3106 else if (flags
& FLAG_SPACE
)
3109 if (arg
> 0.0 && arg
+ arg
== arg
)
3111 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3113 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3117 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3122 # if NEED_PRINTF_DOUBLE
3125 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3131 scale10_round_decimal_double (arg
, precision
);
3134 ndigits
= strlen (digits
);
3136 if (ndigits
> precision
)
3140 *p
++ = digits
[ndigits
];
3142 while (ndigits
> precision
);
3145 /* Here ndigits <= precision. */
3146 if ((flags
& FLAG_ALT
) || precision
> 0)
3148 *p
++ = decimal_point_char ();
3149 for (; precision
> ndigits
; precision
--)
3154 *p
++ = digits
[ndigits
];
3160 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3168 if ((flags
& FLAG_ALT
) || precision
> 0)
3170 *p
++ = decimal_point_char ();
3171 for (; precision
> 0; precision
--)
3182 exponent
= floorlog10 (arg
);
3187 scale10_round_decimal_double (arg
,
3188 (int)precision
- exponent
);
3191 ndigits
= strlen (digits
);
3193 if (ndigits
== precision
+ 1)
3195 if (ndigits
< precision
3196 || ndigits
> precision
+ 2)
3197 /* The exponent was not guessed
3198 precisely enough. */
3201 /* None of two values of exponent is
3202 the right one. Prevent an endless
3206 if (ndigits
== precision
)
3213 /* Here ndigits = precision+1. */
3214 *p
++ = digits
[--ndigits
];
3215 if ((flags
& FLAG_ALT
) || precision
> 0)
3217 *p
++ = decimal_point_char ();
3221 *p
++ = digits
[ndigits
];
3228 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3229 # if WIDE_CHAR_VERSION
3231 static const wchar_t decimal_format
[] =
3232 /* Produce the same number of exponent digits
3233 as the native printf implementation. */
3234 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3235 { '%', '+', '.', '3', 'd', '\0' };
3237 { '%', '+', '.', '2', 'd', '\0' };
3239 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3245 static const char decimal_format
[] =
3246 /* Produce the same number of exponent digits
3247 as the native printf implementation. */
3248 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3253 if (sizeof (DCHAR_T
) == 1)
3255 sprintf ((char *) p
, decimal_format
, exponent
);
3263 sprintf (expbuf
, decimal_format
, exponent
);
3264 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3270 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3274 /* precision >= 1. */
3277 /* The exponent is 0, >= -4, < precision.
3278 Use fixed-point notation. */
3280 size_t ndigits
= precision
;
3281 /* Number of trailing zeroes that have to be
3284 (flags
& FLAG_ALT
? 0 : precision
- 1);
3288 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3290 *p
++ = decimal_point_char ();
3291 while (ndigits
> nzeroes
)
3307 exponent
= floorlog10 (arg
);
3312 scale10_round_decimal_double (arg
,
3313 (int)(precision
- 1) - exponent
);
3316 ndigits
= strlen (digits
);
3318 if (ndigits
== precision
)
3320 if (ndigits
< precision
- 1
3321 || ndigits
> precision
+ 1)
3322 /* The exponent was not guessed
3323 precisely enough. */
3326 /* None of two values of exponent is
3327 the right one. Prevent an endless
3331 if (ndigits
< precision
)
3337 /* Here ndigits = precision. */
3339 /* Determine the number of trailing zeroes
3340 that have to be dropped. */
3342 if ((flags
& FLAG_ALT
) == 0)
3343 while (nzeroes
< ndigits
3344 && digits
[nzeroes
] == '0')
3347 /* The exponent is now determined. */
3349 && exponent
< (long)precision
)
3351 /* Fixed-point notation:
3352 max(exponent,0)+1 digits, then the
3353 decimal point, then the remaining
3354 digits without trailing zeroes. */
3357 size_t count
= exponent
+ 1;
3358 /* Note: count <= precision = ndigits. */
3359 for (; count
> 0; count
--)
3360 *p
++ = digits
[--ndigits
];
3361 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3363 *p
++ = decimal_point_char ();
3364 while (ndigits
> nzeroes
)
3367 *p
++ = digits
[ndigits
];
3373 size_t count
= -exponent
- 1;
3375 *p
++ = decimal_point_char ();
3376 for (; count
> 0; count
--)
3378 while (ndigits
> nzeroes
)
3381 *p
++ = digits
[ndigits
];
3387 /* Exponential notation. */
3388 *p
++ = digits
[--ndigits
];
3389 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3391 *p
++ = decimal_point_char ();
3392 while (ndigits
> nzeroes
)
3395 *p
++ = digits
[ndigits
];
3398 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3399 # if WIDE_CHAR_VERSION
3401 static const wchar_t decimal_format
[] =
3402 /* Produce the same number of exponent digits
3403 as the native printf implementation. */
3404 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3405 { '%', '+', '.', '3', 'd', '\0' };
3407 { '%', '+', '.', '2', 'd', '\0' };
3409 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3415 static const char decimal_format
[] =
3416 /* Produce the same number of exponent digits
3417 as the native printf implementation. */
3418 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3423 if (sizeof (DCHAR_T
) == 1)
3425 sprintf ((char *) p
, decimal_format
, exponent
);
3433 sprintf (expbuf
, decimal_format
, exponent
);
3434 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3447 /* arg is finite. */
3453 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3456 if ((flags
& FLAG_ALT
) || precision
> 0)
3458 *p
++ = decimal_point_char ();
3459 for (; precision
> 0; precision
--)
3463 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3466 if ((flags
& FLAG_ALT
) || precision
> 0)
3468 *p
++ = decimal_point_char ();
3469 for (; precision
> 0; precision
--)
3472 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3474 /* Produce the same number of exponent digits as
3475 the native printf implementation. */
3476 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3482 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3485 if (flags
& FLAG_ALT
)
3488 (precision
> 0 ? precision
- 1 : 0);
3489 *p
++ = decimal_point_char ();
3490 for (; ndigits
> 0; --ndigits
)
3502 /* The generated string now extends from tmp to p, with the
3503 zero padding insertion point being at pad_ptr. */
3504 if (has_width
&& p
- tmp
< width
)
3506 size_t pad
= width
- (p
- tmp
);
3507 DCHAR_T
*end
= p
+ pad
;
3509 if (flags
& FLAG_LEFT
)
3511 /* Pad with spaces on the right. */
3512 for (; pad
> 0; pad
--)
3515 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3517 /* Pad with zeroes. */
3522 for (; pad
> 0; pad
--)
3527 /* Pad with spaces on the left. */
3532 for (; pad
> 0; pad
--)
3540 size_t count
= p
- tmp
;
3542 if (count
>= tmp_length
)
3543 /* tmp_length was incorrectly calculated - fix the
3547 /* Make room for the result. */
3548 if (count
>= allocated
- length
)
3550 size_t n
= xsum (length
, count
);
3552 ENSURE_ALLOCATION (n
);
3555 /* Append the result. */
3556 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3565 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3566 int flags
= dp
->flags
;
3567 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3571 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3575 #if NEED_PRINTF_UNBOUNDED_PRECISION
3578 # define prec_ourselves 0
3580 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3583 # define pad_ourselves 0
3586 unsigned int prefix_count
;
3590 TCHAR_T tmpbuf
[700];
3594 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3597 if (dp
->width_start
!= dp
->width_end
)
3599 if (dp
->width_arg_index
!= ARG_NONE
)
3603 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3605 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3608 /* "A negative field width is taken as a '-' flag
3609 followed by a positive field width." */
3611 width
= (unsigned int) (-arg
);
3618 const FCHAR_T
*digitp
= dp
->width_start
;
3621 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3622 while (digitp
!= dp
->width_end
);
3628 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3631 if (dp
->precision_start
!= dp
->precision_end
)
3633 if (dp
->precision_arg_index
!= ARG_NONE
)
3637 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3639 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3640 /* "A negative precision is taken as if the precision
3650 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3653 while (digitp
!= dp
->precision_end
)
3654 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3661 /* Allocate a temporary buffer of sufficient size for calling
3664 switch (dp
->conversion
)
3667 case 'd': case 'i': case 'u':
3668 # if HAVE_LONG_LONG_INT
3669 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3671 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3672 * 0.30103 /* binary -> decimal */
3674 + 1; /* turn floor into ceil */
3677 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3679 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3680 * 0.30103 /* binary -> decimal */
3682 + 1; /* turn floor into ceil */
3685 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3686 * 0.30103 /* binary -> decimal */
3688 + 1; /* turn floor into ceil */
3689 if (tmp_length
< precision
)
3690 tmp_length
= precision
;
3691 /* Multiply by 2, as an estimate for FLAG_GROUP. */
3692 tmp_length
= xsum (tmp_length
, tmp_length
);
3693 /* Add 1, to account for a leading sign. */
3694 tmp_length
= xsum (tmp_length
, 1);
3698 # if HAVE_LONG_LONG_INT
3699 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3701 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3702 * 0.333334 /* binary -> octal */
3704 + 1; /* turn floor into ceil */
3707 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3709 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3710 * 0.333334 /* binary -> octal */
3712 + 1; /* turn floor into ceil */
3715 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3716 * 0.333334 /* binary -> octal */
3718 + 1; /* turn floor into ceil */
3719 if (tmp_length
< precision
)
3720 tmp_length
= precision
;
3721 /* Add 1, to account for a leading sign. */
3722 tmp_length
= xsum (tmp_length
, 1);
3726 # if HAVE_LONG_LONG_INT
3727 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3729 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3730 * 0.25 /* binary -> hexadecimal */
3732 + 1; /* turn floor into ceil */
3735 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3737 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3738 * 0.25 /* binary -> hexadecimal */
3740 + 1; /* turn floor into ceil */
3743 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3744 * 0.25 /* binary -> hexadecimal */
3746 + 1; /* turn floor into ceil */
3747 if (tmp_length
< precision
)
3748 tmp_length
= precision
;
3749 /* Add 2, to account for a leading sign or alternate form. */
3750 tmp_length
= xsum (tmp_length
, 2);
3754 if (type
== TYPE_LONGDOUBLE
)
3756 (unsigned int) (LDBL_MAX_EXP
3757 * 0.30103 /* binary -> decimal */
3758 * 2 /* estimate for FLAG_GROUP */
3760 + 1 /* turn floor into ceil */
3761 + 10; /* sign, decimal point etc. */
3764 (unsigned int) (DBL_MAX_EXP
3765 * 0.30103 /* binary -> decimal */
3766 * 2 /* estimate for FLAG_GROUP */
3768 + 1 /* turn floor into ceil */
3769 + 10; /* sign, decimal point etc. */
3770 tmp_length
= xsum (tmp_length
, precision
);
3773 case 'e': case 'E': case 'g': case 'G':
3775 12; /* sign, decimal point, exponent etc. */
3776 tmp_length
= xsum (tmp_length
, precision
);
3780 if (type
== TYPE_LONGDOUBLE
)
3782 (unsigned int) (LDBL_DIG
3783 * 0.831 /* decimal -> hexadecimal */
3785 + 1; /* turn floor into ceil */
3788 (unsigned int) (DBL_DIG
3789 * 0.831 /* decimal -> hexadecimal */
3791 + 1; /* turn floor into ceil */
3792 if (tmp_length
< precision
)
3793 tmp_length
= precision
;
3794 /* Account for sign, decimal point etc. */
3795 tmp_length
= xsum (tmp_length
, 12);
3799 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
3800 if (type
== TYPE_WIDE_CHAR
)
3801 tmp_length
= MB_CUR_MAX
;
3809 if (type
== TYPE_WIDE_STRING
)
3812 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
3814 # if !WIDE_CHAR_VERSION
3815 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
3820 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
3825 (unsigned int) (sizeof (void *) * CHAR_BIT
3826 * 0.25 /* binary -> hexadecimal */
3828 + 1 /* turn floor into ceil */
3829 + 2; /* account for leading 0x */
3836 # if ENABLE_UNISTDIO
3837 /* Padding considers the number of characters, therefore the
3838 number of elements after padding may be
3839 > max (tmp_length, width)
3841 <= tmp_length + width. */
3842 tmp_length
= xsum (tmp_length
, width
);
3844 /* Padding considers the number of elements, says POSIX. */
3845 if (tmp_length
< width
)
3849 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3852 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
3856 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
3858 if (size_overflow_p (tmp_memsize
))
3859 /* Overflow, would lead to out of memory. */
3861 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
3863 /* Out of memory. */
3868 /* Decide whether to handle the precision ourselves. */
3869 #if NEED_PRINTF_UNBOUNDED_PRECISION
3870 switch (dp
->conversion
)
3872 case 'd': case 'i': case 'u':
3874 case 'x': case 'X': case 'p':
3875 prec_ourselves
= has_precision
&& (precision
> 0);
3883 /* Decide whether to perform the padding ourselves. */
3884 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3885 switch (dp
->conversion
)
3887 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3888 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
3889 to perform the padding after this conversion. Functions
3890 with unistdio extensions perform the padding based on
3891 character count rather than element count. */
3894 # if NEED_PRINTF_FLAG_ZERO
3895 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
3901 pad_ourselves
= prec_ourselves
;
3906 /* Construct the format string for calling snprintf or
3910 #if NEED_PRINTF_FLAG_GROUPING
3911 /* The underlying implementation doesn't support the ' flag.
3912 Produce no grouping characters in this case; this is
3913 acceptable because the grouping is locale dependent. */
3915 if (flags
& FLAG_GROUP
)
3918 if (flags
& FLAG_LEFT
)
3920 if (flags
& FLAG_SHOWSIGN
)
3922 if (flags
& FLAG_SPACE
)
3924 if (flags
& FLAG_ALT
)
3928 if (flags
& FLAG_ZERO
)
3930 if (dp
->width_start
!= dp
->width_end
)
3932 size_t n
= dp
->width_end
- dp
->width_start
;
3933 /* The width specification is known to consist only
3934 of standard ASCII characters. */
3935 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
3937 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
3942 const FCHAR_T
*mp
= dp
->width_start
;
3944 *fbp
++ = (unsigned char) *mp
++;
3949 if (!prec_ourselves
)
3951 if (dp
->precision_start
!= dp
->precision_end
)
3953 size_t n
= dp
->precision_end
- dp
->precision_start
;
3954 /* The precision specification is known to consist only
3955 of standard ASCII characters. */
3956 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
3958 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
3963 const FCHAR_T
*mp
= dp
->precision_start
;
3965 *fbp
++ = (unsigned char) *mp
++;
3973 #if HAVE_LONG_LONG_INT
3974 case TYPE_LONGLONGINT
:
3975 case TYPE_ULONGLONGINT
:
3976 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3989 case TYPE_WIDE_CHAR
:
3992 case TYPE_WIDE_STRING
:
3996 case TYPE_LONGDOUBLE
:
4002 #if NEED_PRINTF_DIRECTIVE_F
4003 if (dp
->conversion
== 'F')
4007 *fbp
= dp
->conversion
;
4009 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
4014 /* On glibc2 systems from glibc >= 2.3 - probably also older
4015 ones - we know that snprintf's returns value conforms to
4016 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4017 Therefore we can avoid using %n in this situation.
4018 On glibc2 systems from 2004-10-18 or newer, the use of %n
4019 in format strings in writable memory may crash the program
4020 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4021 in this situation. */
4028 /* Construct the arguments for calling snprintf or sprintf. */
4030 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
4032 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4034 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
4036 if (dp
->precision_arg_index
!= ARG_NONE
)
4038 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4040 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4044 /* The SNPRINTF result is appended after result[0..length].
4045 The latter is an array of DCHAR_T; SNPRINTF appends an
4046 array of TCHAR_T to it. This is possible because
4047 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4048 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4049 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4050 /* Prepare checking whether snprintf returns the count
4052 ENSURE_ALLOCATION (xsum (length
, 1));
4053 *(TCHAR_T
*) (result
+ length
) = '\0';
4062 size_t maxlen
= allocated
- length
;
4063 /* SNPRINTF can fail if its second argument is
4065 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
4066 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
4067 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
4068 # define SNPRINTF_BUF(arg) \
4069 switch (prefix_count) \
4072 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4077 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4079 prefixes[0], arg, &count); \
4082 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4084 prefixes[0], prefixes[1], arg, \
4091 # define SNPRINTF_BUF(arg) \
4092 switch (prefix_count) \
4095 count = sprintf (tmp, buf, arg); \
4098 count = sprintf (tmp, buf, prefixes[0], arg); \
4101 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4113 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
4119 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
4125 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
4131 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
4137 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
4143 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
4149 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
4155 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
4159 #if HAVE_LONG_LONG_INT
4160 case TYPE_LONGLONGINT
:
4162 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
4166 case TYPE_ULONGLONGINT
:
4168 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
4175 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4179 case TYPE_LONGDOUBLE
:
4181 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
4187 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
4192 case TYPE_WIDE_CHAR
:
4194 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
4201 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
4206 case TYPE_WIDE_STRING
:
4208 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
4215 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
4224 /* Portability: Not all implementations of snprintf()
4225 are ISO C 99 compliant. Determine the number of
4226 bytes that snprintf() has produced or would have
4230 /* Verify that snprintf() has NUL-terminated its
4233 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
4235 /* Portability hack. */
4236 if (retcount
> count
)
4241 /* snprintf() doesn't understand the '%n'
4245 /* Don't use the '%n' directive; instead, look
4246 at the snprintf() return value. */
4252 /* Look at the snprintf() return value. */
4255 /* HP-UX 10.20 snprintf() is doubly deficient:
4256 It doesn't understand the '%n' directive,
4257 *and* it returns -1 (rather than the length
4258 that would have been required) when the
4259 buffer is too small. */
4260 size_t bigger_need
=
4261 xsum (xtimes (allocated
, 2), 12);
4262 ENSURE_ALLOCATION (bigger_need
);
4271 /* Attempt to handle failure. */
4274 if (!(result
== resultbuf
|| result
== NULL
))
4276 if (buf_malloced
!= NULL
)
4277 free (buf_malloced
);
4284 /* Handle overflow of the allocated buffer.
4285 If such an overflow occurs, a C99 compliant snprintf()
4286 returns a count >= maxlen. However, a non-compliant
4287 snprintf() function returns only count = maxlen - 1. To
4288 cover both cases, test whether count >= maxlen - 1. */
4289 if ((unsigned int) count
+ 1 >= maxlen
)
4291 /* If maxlen already has attained its allowed maximum,
4292 allocating more memory will not increase maxlen.
4293 Instead of looping, bail out. */
4294 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
4298 /* Need at least count * sizeof (TCHAR_T) bytes.
4299 But allocate proportionally, to avoid looping
4300 eternally if snprintf() reports a too small
4304 (count
+ TCHARS_PER_DCHAR
- 1)
4305 / TCHARS_PER_DCHAR
),
4306 xtimes (allocated
, 2));
4308 ENSURE_ALLOCATION (n
);
4314 #if NEED_PRINTF_UNBOUNDED_PRECISION
4317 /* Handle the precision. */
4320 (TCHAR_T
*) (result
+ length
);
4324 size_t prefix_count
;
4328 /* Put the additional zeroes after the sign. */
4330 && (*prec_ptr
== '-' || *prec_ptr
== '+'
4331 || *prec_ptr
== ' '))
4333 /* Put the additional zeroes after the 0x prefix if
4334 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
4336 && prec_ptr
[0] == '0'
4337 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
4340 move
= count
- prefix_count
;
4341 if (precision
> move
)
4343 /* Insert zeroes. */
4344 size_t insert
= precision
- move
;
4350 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
4351 / TCHARS_PER_DCHAR
);
4352 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
4353 ENSURE_ALLOCATION (n
);
4354 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
4355 prec_ptr
= (TCHAR_T
*) (result
+ length
);
4358 prec_end
= prec_ptr
+ count
;
4359 prec_ptr
+= prefix_count
;
4361 while (prec_end
> prec_ptr
)
4364 prec_end
[insert
] = prec_end
[0];
4370 while (prec_end
> prec_ptr
);
4379 if (count
>= tmp_length
)
4380 /* tmp_length was incorrectly calculated - fix the
4385 /* Convert from TCHAR_T[] to DCHAR_T[]. */
4386 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
4388 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
4390 The result string is not certainly ASCII. */
4391 const TCHAR_T
*tmpsrc
;
4394 /* This code assumes that TCHAR_T is 'char'. */
4395 typedef int TCHAR_T_verify
4396 [2 * (sizeof (TCHAR_T
) == 1) - 1];
4398 tmpsrc
= (TCHAR_T
*) (result
+ length
);
4404 if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
4405 iconveh_question_mark
,
4408 &tmpdst
, &tmpdst_len
)
4411 int saved_errno
= errno
;
4412 if (!(result
== resultbuf
|| result
== NULL
))
4414 if (buf_malloced
!= NULL
)
4415 free (buf_malloced
);
4417 errno
= saved_errno
;
4420 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
4421 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
4427 /* The result string is ASCII.
4428 Simple 1:1 conversion. */
4430 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
4431 no-op conversion, in-place on the array starting
4432 at (result + length). */
4433 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
4436 const TCHAR_T
*tmpsrc
;
4441 if (result
== resultbuf
)
4443 tmpsrc
= (TCHAR_T
*) (result
+ length
);
4444 /* ENSURE_ALLOCATION will not move tmpsrc
4445 (because it's part of resultbuf). */
4446 ENSURE_ALLOCATION (xsum (length
, count
));
4450 /* ENSURE_ALLOCATION will move the array
4451 (because it uses realloc(). */
4452 ENSURE_ALLOCATION (xsum (length
, count
));
4453 tmpsrc
= (TCHAR_T
*) (result
+ length
);
4457 ENSURE_ALLOCATION (xsum (length
, count
));
4459 tmpdst
= result
+ length
;
4460 /* Copy backwards, because of overlapping. */
4463 for (n
= count
; n
> 0; n
--)
4464 *--tmpdst
= (unsigned char) *--tmpsrc
;
4469 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
4470 /* Make room for the result. */
4471 if (count
> allocated
- length
)
4473 /* Need at least count elements. But allocate
4476 xmax (xsum (length
, count
), xtimes (allocated
, 2));
4478 ENSURE_ALLOCATION (n
);
4482 /* Here count <= allocated - length. */
4484 /* Perform padding. */
4485 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4486 if (pad_ourselves
&& has_width
)
4489 # if ENABLE_UNISTDIO
4490 /* Outside POSIX, it's preferrable to compare the width
4491 against the number of _characters_ of the converted
4493 w
= DCHAR_MBSNLEN (result
+ length
, count
);
4495 /* The width is compared against the number of _bytes_
4496 of the converted value, says POSIX. */
4501 size_t pad
= width
- w
;
4503 /* Make room for the result. */
4504 if (xsum (count
, pad
) > allocated
- length
)
4506 /* Need at least count + pad elements. But
4507 allocate proportionally. */
4509 xmax (xsum3 (length
, count
, pad
),
4510 xtimes (allocated
, 2));
4513 ENSURE_ALLOCATION (n
);
4516 /* Here count + pad <= allocated - length. */
4519 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
4520 DCHAR_T
* const rp
= result
+ length
;
4522 DCHAR_T
* const rp
= tmp
;
4524 DCHAR_T
*p
= rp
+ count
;
4525 DCHAR_T
*end
= p
+ pad
;
4526 # if NEED_PRINTF_FLAG_ZERO
4528 # if !DCHAR_IS_TCHAR
4529 if (dp
->conversion
== 'c'
4530 || dp
->conversion
== 's')
4531 /* No zero-padding for string directives. */
4536 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
4537 /* No zero-padding of "inf" and "nan". */
4538 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
4539 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
4543 /* The generated string now extends from rp to p,
4544 with the zero padding insertion point being at
4547 count
= count
+ pad
; /* = end - rp */
4549 if (flags
& FLAG_LEFT
)
4551 /* Pad with spaces on the right. */
4552 for (; pad
> 0; pad
--)
4555 # if NEED_PRINTF_FLAG_ZERO
4556 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4558 /* Pad with zeroes. */
4563 for (; pad
> 0; pad
--)
4569 /* Pad with spaces on the left. */
4574 for (; pad
> 0; pad
--)
4582 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
4583 if (count
>= tmp_length
)
4584 /* tmp_length was incorrectly calculated - fix the
4589 /* Here still count <= allocated - length. */
4591 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
4592 /* The snprintf() result did fit. */
4594 /* Append the sprintf() result. */
4595 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4602 #if NEED_PRINTF_DIRECTIVE_F
4603 if (dp
->conversion
== 'F')
4605 /* Convert the %f result to upper case for %F. */
4606 DCHAR_T
*rp
= result
+ length
;
4608 for (rc
= count
; rc
> 0; rc
--, rp
++)
4609 if (*rp
>= 'a' && *rp
<= 'z')
4610 *rp
= *rp
- 'a' + 'A';
4621 /* Add the final NUL. */
4622 ENSURE_ALLOCATION (xsum (length
, 1));
4623 result
[length
] = '\0';
4625 if (result
!= resultbuf
&& length
+ 1 < allocated
)
4627 /* Shrink the allocated memory if possible. */
4630 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
4635 if (buf_malloced
!= NULL
)
4636 free (buf_malloced
);
4639 /* Note that we can produce a big string of a length > INT_MAX. POSIX
4640 says that snprintf() fails with errno = EOVERFLOW in this case, but
4641 that's only because snprintf() returns an 'int'. This function does
4642 not have this limitation. */
4646 if (!(result
== resultbuf
|| result
== NULL
))
4648 if (buf_malloced
!= NULL
)
4649 free (buf_malloced
);
4655 if (!(result
== resultbuf
|| result
== NULL
))
4657 if (buf_malloced
!= NULL
)
4658 free (buf_malloced
);
4666 #undef TCHARS_PER_DCHAR
4673 #undef DCHAR_IS_TCHAR