1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2017 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
17 /* This file can be parametrized with the following macros:
18 VASNPRINTF The name of the function being defined.
19 FCHAR_T The element type of the format string.
20 DCHAR_T The element type of the destination (result) string.
21 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
22 in the format string are ASCII. MUST be set if
23 FCHAR_T and DCHAR_T are not the same type.
24 DIRECTIVE Structure denoting a format directive.
26 DIRECTIVES Structure denoting the set of format directives of a
27 format string. Depends on FCHAR_T.
28 PRINTF_PARSE Function that parses a format string.
30 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
31 DCHAR_SET memset like function for DCHAR_T[] arrays.
32 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
33 SNPRINTF The system's snprintf (or similar) function.
34 This may be either snprintf or swprintf.
35 TCHAR_T The element type of the argument and result string
36 of the said SNPRINTF function. This may be either
37 char or wchar_t. The code exploits that
38 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
39 alignof (TCHAR_T) <= alignof (DCHAR_T).
40 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
41 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
42 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
43 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
44 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
46 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
47 This must come before <config.h> because <config.h> may include
48 <features.h>, and once <features.h> has been included, it's too late. */
50 # define _GNU_SOURCE 1
62 # if WIDE_CHAR_VERSION
63 # include "vasnwprintf.h"
65 # include "vasnprintf.h"
69 #include <locale.h> /* localeconv() */
70 #include <stdio.h> /* snprintf(), sprintf() */
71 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
72 #include <string.h> /* memcpy(), strlen() */
73 #include <errno.h> /* errno */
74 #include <limits.h> /* CHAR_BIT */
75 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
77 # include <langinfo.h>
80 # if WIDE_CHAR_VERSION
81 # include "wprintf-parse.h"
83 # include "printf-parse.h"
87 /* Checked size_t computations. */
92 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
97 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
99 # include "isnand-nolibm.h"
102 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
104 # include "isnanl-nolibm.h"
108 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
110 # include "isnand-nolibm.h"
111 # include "printf-frexp.h"
114 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
116 # include "isnanl-nolibm.h"
117 # include "printf-frexpl.h"
123 # define FALLTHROUGH ((void) 0)
125 # define FALLTHROUGH __attribute__ ((__fallthrough__))
129 /* Default parameters. */
131 # if WIDE_CHAR_VERSION
132 # define VASNPRINTF vasnwprintf
133 # define FCHAR_T wchar_t
134 # define DCHAR_T wchar_t
135 # define TCHAR_T wchar_t
136 # define DCHAR_IS_TCHAR 1
137 # define DIRECTIVE wchar_t_directive
138 # define DIRECTIVES wchar_t_directives
139 # define PRINTF_PARSE wprintf_parse
140 # define DCHAR_CPY wmemcpy
141 # define DCHAR_SET wmemset
143 # define VASNPRINTF vasnprintf
144 # define FCHAR_T char
145 # define DCHAR_T char
146 # define TCHAR_T char
147 # define DCHAR_IS_TCHAR 1
148 # define DIRECTIVE char_directive
149 # define DIRECTIVES char_directives
150 # define PRINTF_PARSE printf_parse
151 # define DCHAR_CPY memcpy
152 # define DCHAR_SET memset
155 #if WIDE_CHAR_VERSION
156 /* TCHAR_T is wchar_t. */
157 # define USE_SNPRINTF 1
158 # if HAVE_DECL__SNWPRINTF
159 /* On Windows, the function swprintf() has a different signature than
160 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
161 instead. The mingw function snwprintf() has fewer bugs than the
162 MSVCRT function _snwprintf(), so prefer that. */
163 # if defined __MINGW32__
164 # define SNPRINTF snwprintf
166 # define SNPRINTF _snwprintf
167 # define USE_MSVC__SNPRINTF 1
171 # define SNPRINTF swprintf
174 /* TCHAR_T is char. */
175 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
176 But don't use it on BeOS, since BeOS snprintf produces no output if the
177 size argument is >= 0x3000000.
178 Also don't use it on Linux libc5, since there snprintf with size = 1
179 writes any output without bounds, like sprintf. */
180 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
181 # define USE_SNPRINTF 1
183 # define USE_SNPRINTF 0
185 # if HAVE_DECL__SNPRINTF
186 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
187 function _snprintf(), so prefer that. */
188 # if defined __MINGW32__
189 # define SNPRINTF snprintf
190 /* Here we need to call the native snprintf, not rpl_snprintf. */
193 /* MSVC versions < 14 did not have snprintf, only _snprintf. */
194 # define SNPRINTF _snprintf
195 # define USE_MSVC__SNPRINTF 1
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 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
208 warnings in this file. Use -Dlint to suppress them. */
209 #if defined GCC_LINT || defined lint
210 # define IF_LINT(Code) Code
212 # define IF_LINT(Code) /* empty */
215 /* Avoid some warnings from "gcc -Wshadow".
216 This file doesn't use the exp() and remainder() functions. */
220 #define remainder rem
222 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && !WIDE_CHAR_VERSION
223 # if (HAVE_STRNLEN && !defined _AIX)
224 # define local_strnlen strnlen
226 # ifndef local_strnlen_defined
227 # define local_strnlen_defined 1
229 local_strnlen (const char *string
, size_t maxlen
)
231 const char *end
= memchr (string
, '\0', maxlen
);
232 return end
? (size_t) (end
- string
) : maxlen
;
238 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
240 # define local_wcslen wcslen
242 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
243 a dependency towards this library, here is a local substitute.
244 Define this substitute only once, even if this file is included
245 twice in the same compilation unit. */
246 # ifndef local_wcslen_defined
247 # define local_wcslen_defined 1
249 local_wcslen (const wchar_t *s
)
253 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
261 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
263 # define local_wcsnlen wcsnlen
265 # ifndef local_wcsnlen_defined
266 # define local_wcsnlen_defined 1
268 local_wcsnlen (const wchar_t *s
, size_t maxlen
)
272 for (ptr
= s
; maxlen
> 0 && *ptr
!= (wchar_t) 0; ptr
++, maxlen
--)
280 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
281 /* Determine the decimal-point character according to the current locale. */
282 # ifndef decimal_point_char_defined
283 # define decimal_point_char_defined 1
285 decimal_point_char (void)
288 /* Determine it in a multithread-safe way. We know nl_langinfo is
289 multithread-safe on glibc systems and Mac OS X systems, but is not required
290 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
291 localeconv() is rarely multithread-safe. */
292 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
293 point
= nl_langinfo (RADIXCHAR
);
296 sprintf (pointbuf
, "%#.0f", 1.0);
297 point
= &pointbuf
[1];
299 point
= localeconv () -> decimal_point
;
301 /* The decimal point is always a single byte: either '.' or ','. */
302 return (point
[0] != '\0' ? point
[0] : '.');
307 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
309 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
311 is_infinite_or_zero (double x
)
313 return isnand (x
) || x
+ x
== x
;
318 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
320 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
322 is_infinite_or_zerol (long double x
)
324 return isnanl (x
) || x
+ x
== x
;
329 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
331 /* Converting 'long double' to decimal without rare rounding bugs requires
332 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
333 (and slower) algorithms. */
335 typedef unsigned int mp_limb_t
;
336 # define GMP_LIMB_BITS 32
337 verify (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
);
339 typedef unsigned long long mp_twolimb_t
;
340 # define GMP_TWOLIMB_BITS 64
341 verify (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
);
343 /* Representation of a bignum >= 0. */
347 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
350 /* Compute the product of two bignums >= 0.
351 Return the allocated memory in case of success, NULL in case of memory
352 allocation failure. */
354 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
361 if (src1
.nlimbs
<= src2
.nlimbs
)
375 /* Now 0 <= len1 <= len2. */
378 /* src1 or src2 is zero. */
380 dest
->limbs
= (mp_limb_t
*) malloc (1);
384 /* Here 1 <= len1 <= len2. */
390 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
393 for (k
= len2
; k
> 0; )
395 for (i
= 0; i
< len1
; i
++)
397 mp_limb_t digit1
= p1
[i
];
398 mp_twolimb_t carry
= 0;
399 for (j
= 0; j
< len2
; j
++)
401 mp_limb_t digit2
= p2
[j
];
402 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
404 dp
[i
+ j
] = (mp_limb_t
) carry
;
405 carry
= carry
>> GMP_LIMB_BITS
;
407 dp
[i
+ len2
] = (mp_limb_t
) carry
;
410 while (dlen
> 0 && dp
[dlen
- 1] == 0)
418 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
419 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
421 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
423 Return the allocated memory in case of success, NULL in case of memory
424 allocation failure. */
426 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
429 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
430 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
431 If m<n, then q:=0 and r:=a.
432 If m>=n=1, perform a single-precision division:
435 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
436 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
437 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
438 Normalise [q[m-1],...,q[0]], yields q.
439 If m>=n>1, perform a multiple-precision division:
440 We have a/b < beta^(m-n+1).
441 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
442 Shift a and b left by s bits, copying them. r:=a.
443 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
444 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
446 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
447 In case of overflow (q* >= beta) set q* := beta-1.
448 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
449 and c3 := b[n-2] * q*.
450 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
451 occurred. Furthermore 0 <= c3 < beta^2.
452 If there was overflow and
453 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
454 the next test can be skipped.}
455 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
456 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
458 Put r := r - b * q* * beta^j. In detail:
459 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
460 hence: u:=0, for i:=0 to n-1 do
462 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
463 u:=u div beta (+ 1, if carry in subtraction)
465 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
467 the carry u does not overflow.}
468 If a negative carry occurs, put q* := q* - 1
469 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
471 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
472 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
474 The room for q[j] can be allocated at the memory location of r[n+j].
475 Finally, round-to-even:
476 Shift r left by 1 bit.
477 If r > b or if r = b and q[0] is odd, q := q+1.
479 const mp_limb_t
*a_ptr
= a
.limbs
;
480 size_t a_len
= a
.nlimbs
;
481 const mp_limb_t
*b_ptr
= b
.limbs
;
482 size_t b_len
= b
.nlimbs
;
484 mp_limb_t
*tmp_roomptr
= NULL
;
490 /* Allocate room for a_len+2 digits.
491 (Need a_len+1 digits for the real division and 1 more digit for the
492 final rounding of q.) */
493 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
498 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
505 /* Division by zero. */
507 if (b_ptr
[b_len
- 1] == 0)
513 /* Here m = a_len >= 0 and n = b_len > 0. */
517 /* m<n: trivial case. q=0, r := copy of a. */
520 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
521 q_ptr
= roomptr
+ a_len
;
526 /* n=1: single precision division.
527 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
531 mp_limb_t den
= b_ptr
[0];
532 mp_limb_t remainder
= 0;
533 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
534 mp_limb_t
*destptr
= q_ptr
+ a_len
;
536 for (count
= a_len
; count
> 0; count
--)
539 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
540 *--destptr
= num
/ den
;
541 remainder
= num
% den
;
543 /* Normalise and store r. */
546 r_ptr
[0] = remainder
;
553 if (q_ptr
[q_len
- 1] == 0)
559 /* n>1: multiple precision division.
560 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
561 beta^(m-n-1) <= a/b < beta^(m-n+1). */
565 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
566 /* Determine s = GMP_LIMB_BITS - integer_length (msd).
567 Code copied from gnulib's integer_length.c. */
568 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
569 s
= __builtin_clz (msd
);
571 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
572 if (GMP_LIMB_BITS
<= DBL_MANT_BIT
)
574 /* Use 'double' operations.
575 Assumes an IEEE 754 'double' implementation. */
576 # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
577 # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
579 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
580 union { double value
; unsigned int word
[NWORDS
]; } m
;
582 /* Use a single integer to floating-point conversion. */
586 - (((m
.word
[DBL_EXPBIT0_WORD
] >> DBL_EXPBIT0_BIT
) & DBL_EXP_MASK
)
622 /* 0 <= s < GMP_LIMB_BITS.
623 Copy b, shifting it left by s bits. */
626 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
627 if (tmp_roomptr
== NULL
)
633 const mp_limb_t
*sourceptr
= b_ptr
;
634 mp_limb_t
*destptr
= tmp_roomptr
;
635 mp_twolimb_t accu
= 0;
637 for (count
= b_len
; count
> 0; count
--)
639 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
640 *destptr
++ = (mp_limb_t
) accu
;
641 accu
= accu
>> GMP_LIMB_BITS
;
643 /* accu must be zero, since that was how s was determined. */
649 /* Copy a, shifting it left by s bits, yields r.
651 At the beginning: r = roomptr[0..a_len],
652 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
656 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
661 const mp_limb_t
*sourceptr
= a_ptr
;
662 mp_limb_t
*destptr
= r_ptr
;
663 mp_twolimb_t accu
= 0;
665 for (count
= a_len
; count
> 0; count
--)
667 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
668 *destptr
++ = (mp_limb_t
) accu
;
669 accu
= accu
>> GMP_LIMB_BITS
;
671 *destptr
++ = (mp_limb_t
) accu
;
673 q_ptr
= roomptr
+ b_len
;
674 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
676 size_t j
= a_len
- b_len
; /* m-n */
677 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
678 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
679 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
680 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
681 /* Division loop, traversed m-n+1 times.
682 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
687 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
689 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
691 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
692 | r_ptr
[j
+ b_len
- 1];
693 q_star
= num
/ b_msd
;
698 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
699 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
700 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
701 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
702 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
704 If yes, jump directly to the subtraction loop.
705 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
706 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
707 if (r_ptr
[j
+ b_len
] > b_msd
708 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
709 /* r[j+n] >= b[n-1]+1 or
710 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
715 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
717 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
718 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
719 mp_twolimb_t c3
= /* b[n-2] * q* */
720 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
721 /* While c2 < c3, increase c2 and decrease c3.
722 Consider c3-c2. While it is > 0, decrease it by
723 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
724 this can happen only twice. */
727 q_star
= q_star
- 1; /* q* := q* - 1 */
728 if (c3
- c2
> b_msdd
)
729 q_star
= q_star
- 1; /* q* := q* - 1 */
735 /* Subtract r := r - b * q* * beta^j. */
738 const mp_limb_t
*sourceptr
= b_ptr
;
739 mp_limb_t
*destptr
= r_ptr
+ j
;
740 mp_twolimb_t carry
= 0;
742 for (count
= b_len
; count
> 0; count
--)
744 /* Here 0 <= carry <= q*. */
747 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
748 + (mp_limb_t
) ~(*destptr
);
749 /* Here 0 <= carry <= beta*q* + beta-1. */
750 *destptr
++ = ~(mp_limb_t
) carry
;
751 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
753 cr
= (mp_limb_t
) carry
;
755 /* Subtract cr from r_ptr[j + b_len], then forget about
757 if (cr
> r_ptr
[j
+ b_len
])
759 /* Subtraction gave a carry. */
760 q_star
= q_star
- 1; /* q* := q* - 1 */
763 const mp_limb_t
*sourceptr
= b_ptr
;
764 mp_limb_t
*destptr
= r_ptr
+ j
;
767 for (count
= b_len
; count
> 0; count
--)
769 mp_limb_t source1
= *sourceptr
++;
770 mp_limb_t source2
= *destptr
;
771 *destptr
++ = source1
+ source2
+ carry
;
774 ? source1
>= (mp_limb_t
) ~source2
775 : source1
> (mp_limb_t
) ~source2
);
778 /* Forget about the carry and about r[j+n]. */
781 /* q* is determined. Store it as q[j]. */
790 if (q_ptr
[q_len
- 1] == 0)
792 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
793 b is shifted left by s bits. */
794 /* Shift r right by s bits. */
797 mp_limb_t ptr
= r_ptr
+ r_len
;
798 mp_twolimb_t accu
= 0;
800 for (count
= r_len
; count
> 0; count
--)
802 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
803 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
804 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
809 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
812 /* Compare r << 1 with b. */
820 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
821 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
822 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
832 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
837 for (i
= 0; i
< q_len
; i
++)
838 if (++(q_ptr
[i
]) != 0)
843 if (tmp_roomptr
!= NULL
)
850 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
852 Destroys the contents of a.
853 Return the allocated memory - containing the decimal digits in low-to-high
854 order, terminated with a NUL character - in case of success, NULL in case
855 of memory allocation failure. */
857 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
859 mp_limb_t
*a_ptr
= a
.limbs
;
860 size_t a_len
= a
.nlimbs
;
861 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
862 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
863 char *c_ptr
= (char *) malloc (xsum (c_len
, extra_zeroes
));
867 for (; extra_zeroes
> 0; extra_zeroes
--)
871 /* Divide a by 10^9, in-place. */
872 mp_limb_t remainder
= 0;
873 mp_limb_t
*ptr
= a_ptr
+ a_len
;
875 for (count
= a_len
; count
> 0; count
--)
878 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
879 *ptr
= num
/ 1000000000;
880 remainder
= num
% 1000000000;
882 /* Store the remainder as 9 decimal digits. */
883 for (count
= 9; count
> 0; count
--)
885 *d_ptr
++ = '0' + (remainder
% 10);
886 remainder
= remainder
/ 10;
889 if (a_ptr
[a_len
- 1] == 0)
892 /* Remove leading zeroes. */
893 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
895 /* But keep at least one zero. */
898 /* Terminate the string. */
904 # if NEED_PRINTF_LONG_DOUBLE
906 /* Assuming x is finite and >= 0:
907 write x as x = 2^e * m, where m is a bignum.
908 Return the allocated memory in case of success, NULL in case of memory
909 allocation failure. */
911 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
918 /* Allocate memory for result. */
919 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
920 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
923 /* Split into exponential part and mantissa. */
924 y
= frexpl (x
, &exp
);
925 if (!(y
>= 0.0L && y
< 1.0L))
927 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
928 latter is an integer. */
929 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
930 I'm not sure whether it's safe to cast a 'long double' value between
931 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
932 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
934 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
935 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
938 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
941 if (!(y
>= 0.0L && y
< 1.0L))
943 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
946 if (!(y
>= 0.0L && y
< 1.0L))
948 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
953 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
956 if (!(y
>= 0.0L && y
< 1.0L))
958 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
962 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
965 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
968 if (!(y
>= 0.0L && y
< 1.0L))
970 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
973 if (!(y
>= 0.0L && y
< 1.0L))
975 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
977 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
983 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
986 *ep
= exp
- LDBL_MANT_BIT
;
992 # if NEED_PRINTF_DOUBLE
994 /* Assuming x is finite and >= 0:
995 write x as x = 2^e * m, where m is a bignum.
996 Return the allocated memory in case of success, NULL in case of memory
997 allocation failure. */
999 decode_double (double x
, int *ep
, mpn_t
*mp
)
1006 /* Allocate memory for result. */
1007 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
1008 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
1009 if (m
.limbs
== NULL
)
1011 /* Split into exponential part and mantissa. */
1012 y
= frexp (x
, &exp
);
1013 if (!(y
>= 0.0 && y
< 1.0))
1015 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
1016 latter is an integer. */
1017 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
1018 I'm not sure whether it's safe to cast a 'double' value between
1019 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1020 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1022 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
1023 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1026 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1029 if (!(y
>= 0.0 && y
< 1.0))
1031 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1034 if (!(y
>= 0.0 && y
< 1.0))
1036 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1041 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
1044 if (!(y
>= 0.0 && y
< 1.0))
1046 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1050 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1053 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1056 if (!(y
>= 0.0 && y
< 1.0))
1058 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1061 if (!(y
>= 0.0 && y
< 1.0))
1063 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1068 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1071 *ep
= exp
- DBL_MANT_BIT
;
1077 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1078 Returns the decimal representation of round (x * 10^n).
1079 Return the allocated memory - containing the decimal digits in low-to-high
1080 order, terminated with a NUL character - in case of success, NULL in case
1081 of memory allocation failure. */
1083 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
1086 size_t extra_zeroes
;
1089 mp_limb_t
*pow5_ptr
;
1091 unsigned int s_limbs
;
1092 unsigned int s_bits
;
1100 /* x = 2^e * m, hence
1101 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1102 = round (2^s * 5^n * m). */
1105 /* Factor out a common power of 10 if possible. */
1108 extra_zeroes
= (s
< n
? s
: n
);
1112 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1113 Before converting to decimal, we need to compute
1114 z = round (2^s * 5^n * m). */
1115 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1116 sign. 2.322 is slightly larger than log(5)/log(2). */
1117 abs_n
= (n
>= 0 ? n
: -n
);
1118 abs_s
= (s
>= 0 ? s
: -s
);
1119 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1120 + abs_s
/ GMP_LIMB_BITS
+ 1)
1121 * sizeof (mp_limb_t
));
1122 if (pow5_ptr
== NULL
)
1127 /* Initialize with 1. */
1130 /* Multiply with 5^|n|. */
1133 static mp_limb_t
const small_pow5
[13 + 1] =
1135 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1136 48828125, 244140625, 1220703125
1139 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1141 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1143 mp_twolimb_t carry
= 0;
1144 for (j
= 0; j
< pow5_len
; j
++)
1146 mp_limb_t digit2
= pow5_ptr
[j
];
1147 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1148 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1149 carry
= carry
>> GMP_LIMB_BITS
;
1152 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1155 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1156 s_bits
= abs_s
% GMP_LIMB_BITS
;
1157 if (n
>= 0 ? s
>= 0 : s
<= 0)
1159 /* Multiply with 2^|s|. */
1162 mp_limb_t
*ptr
= pow5_ptr
;
1163 mp_twolimb_t accu
= 0;
1165 for (count
= pow5_len
; count
> 0; count
--)
1167 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1168 *ptr
++ = (mp_limb_t
) accu
;
1169 accu
= accu
>> GMP_LIMB_BITS
;
1173 *ptr
= (mp_limb_t
) accu
;
1180 for (count
= pow5_len
; count
> 0;)
1183 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1185 for (count
= s_limbs
; count
> 0;)
1188 pow5_ptr
[count
] = 0;
1190 pow5_len
+= s_limbs
;
1192 pow5
.limbs
= pow5_ptr
;
1193 pow5
.nlimbs
= pow5_len
;
1196 /* Multiply m with pow5. No division needed. */
1197 z_memory
= multiply (m
, pow5
, &z
);
1201 /* Divide m by pow5 and round. */
1202 z_memory
= divide (m
, pow5
, &z
);
1207 pow5
.limbs
= pow5_ptr
;
1208 pow5
.nlimbs
= pow5_len
;
1212 Multiply m with pow5, then divide by 2^|s|. */
1216 tmp_memory
= multiply (m
, pow5
, &numerator
);
1217 if (tmp_memory
== NULL
)
1223 /* Construct 2^|s|. */
1225 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1227 for (i
= 0; i
< s_limbs
; i
++)
1229 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1230 denominator
.limbs
= ptr
;
1231 denominator
.nlimbs
= s_limbs
+ 1;
1233 z_memory
= divide (numerator
, denominator
, &z
);
1239 Multiply m with 2^s, then divide by pow5. */
1242 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1243 * sizeof (mp_limb_t
));
1244 if (num_ptr
== NULL
)
1251 mp_limb_t
*destptr
= num_ptr
;
1254 for (i
= 0; i
< s_limbs
; i
++)
1259 const mp_limb_t
*sourceptr
= m
.limbs
;
1260 mp_twolimb_t accu
= 0;
1262 for (count
= m
.nlimbs
; count
> 0; count
--)
1264 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1265 *destptr
++ = (mp_limb_t
) accu
;
1266 accu
= accu
>> GMP_LIMB_BITS
;
1269 *destptr
++ = (mp_limb_t
) accu
;
1273 const mp_limb_t
*sourceptr
= m
.limbs
;
1275 for (count
= m
.nlimbs
; count
> 0; count
--)
1276 *destptr
++ = *sourceptr
++;
1278 numerator
.limbs
= num_ptr
;
1279 numerator
.nlimbs
= destptr
- num_ptr
;
1281 z_memory
= divide (numerator
, pow5
, &z
);
1288 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1290 if (z_memory
== NULL
)
1292 digits
= convert_to_decimal (z
, extra_zeroes
);
1297 # if NEED_PRINTF_LONG_DOUBLE
1299 /* Assuming x is finite and >= 0, and n is an integer:
1300 Returns the decimal representation of round (x * 10^n).
1301 Return the allocated memory - containing the decimal digits in low-to-high
1302 order, terminated with a NUL character - in case of success, NULL in case
1303 of memory allocation failure. */
1305 scale10_round_decimal_long_double (long double x
, int n
)
1309 void *memory
= decode_long_double (x
, &e
, &m
);
1310 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1315 # if NEED_PRINTF_DOUBLE
1317 /* Assuming x is finite and >= 0, and n is an integer:
1318 Returns the decimal representation of round (x * 10^n).
1319 Return the allocated memory - containing the decimal digits in low-to-high
1320 order, terminated with a NUL character - in case of success, NULL in case
1321 of memory allocation failure. */
1323 scale10_round_decimal_double (double x
, int n
)
1327 void *memory
= decode_double (x
, &e
, &m
);
1328 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1333 # if NEED_PRINTF_LONG_DOUBLE
1335 /* Assuming x is finite and > 0:
1336 Return an approximation for n with 10^n <= x < 10^(n+1).
1337 The approximation is usually the right n, but may be off by 1 sometimes. */
1339 floorlog10l (long double x
)
1346 /* Split into exponential part and mantissa. */
1347 y
= frexpl (x
, &exp
);
1348 if (!(y
>= 0.0L && y
< 1.0L))
1354 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1356 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1357 exp
-= GMP_LIMB_BITS
;
1359 if (y
< (1.0L / (1 << 16)))
1361 y
*= 1.0L * (1 << 16);
1364 if (y
< (1.0L / (1 << 8)))
1366 y
*= 1.0L * (1 << 8);
1369 if (y
< (1.0L / (1 << 4)))
1371 y
*= 1.0L * (1 << 4);
1374 if (y
< (1.0L / (1 << 2)))
1376 y
*= 1.0L * (1 << 2);
1379 if (y
< (1.0L / (1 << 1)))
1381 y
*= 1.0L * (1 << 1);
1385 if (!(y
>= 0.5L && y
< 1.0L))
1387 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1390 if (z
< 0.70710678118654752444)
1392 z
*= 1.4142135623730950488;
1395 if (z
< 0.8408964152537145431)
1397 z
*= 1.1892071150027210667;
1400 if (z
< 0.91700404320467123175)
1402 z
*= 1.0905077326652576592;
1405 if (z
< 0.9576032806985736469)
1407 z
*= 1.0442737824274138403;
1410 /* Now 0.95 <= z <= 1.01. */
1412 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1413 Four terms are enough to get an approximation with error < 10^-7. */
1414 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1415 /* Finally multiply with log(2)/log(10), yields an approximation for
1417 l
*= 0.30102999566398119523;
1418 /* Round down to the next integer. */
1419 return (int) l
+ (l
< 0 ? -1 : 0);
1424 # if NEED_PRINTF_DOUBLE
1426 /* Assuming x is finite and > 0:
1427 Return an approximation for n with 10^n <= x < 10^(n+1).
1428 The approximation is usually the right n, but may be off by 1 sometimes. */
1430 floorlog10 (double x
)
1437 /* Split into exponential part and mantissa. */
1438 y
= frexp (x
, &exp
);
1439 if (!(y
>= 0.0 && y
< 1.0))
1445 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1447 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1448 exp
-= GMP_LIMB_BITS
;
1450 if (y
< (1.0 / (1 << 16)))
1452 y
*= 1.0 * (1 << 16);
1455 if (y
< (1.0 / (1 << 8)))
1457 y
*= 1.0 * (1 << 8);
1460 if (y
< (1.0 / (1 << 4)))
1462 y
*= 1.0 * (1 << 4);
1465 if (y
< (1.0 / (1 << 2)))
1467 y
*= 1.0 * (1 << 2);
1470 if (y
< (1.0 / (1 << 1)))
1472 y
*= 1.0 * (1 << 1);
1476 if (!(y
>= 0.5 && y
< 1.0))
1478 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1481 if (z
< 0.70710678118654752444)
1483 z
*= 1.4142135623730950488;
1486 if (z
< 0.8408964152537145431)
1488 z
*= 1.1892071150027210667;
1491 if (z
< 0.91700404320467123175)
1493 z
*= 1.0905077326652576592;
1496 if (z
< 0.9576032806985736469)
1498 z
*= 1.0442737824274138403;
1501 /* Now 0.95 <= z <= 1.01. */
1503 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1504 Four terms are enough to get an approximation with error < 10^-7. */
1505 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1506 /* Finally multiply with log(2)/log(10), yields an approximation for
1508 l
*= 0.30102999566398119523;
1509 /* Round down to the next integer. */
1510 return (int) l
+ (l
< 0 ? -1 : 0);
1515 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1516 a single '1' digit. */
1518 is_borderline (const char *digits
, size_t precision
)
1520 for (; precision
> 0; precision
--, digits
++)
1526 return *digits
== '\0';
1531 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
1533 /* Use a different function name, to make it possible that the 'wchar_t'
1534 parametrization and the 'char' parametrization get compiled in the same
1535 translation unit. */
1536 # if WIDE_CHAR_VERSION
1537 # define MAX_ROOM_NEEDED wmax_room_needed
1539 # define MAX_ROOM_NEEDED max_room_needed
1542 /* Returns the number of TCHAR_T units needed as temporary space for the result
1543 of sprintf or SNPRINTF of a single conversion directive. */
1545 MAX_ROOM_NEEDED (const arguments
*ap
, size_t arg_index
, FCHAR_T conversion
,
1546 arg_type type
, int flags
, size_t width
, int has_precision
,
1547 size_t precision
, int pad_ourselves
)
1553 case 'd': case 'i': case 'u':
1554 # if HAVE_LONG_LONG_INT
1555 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1557 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1558 * 0.30103 /* binary -> decimal */
1560 + 1; /* turn floor into ceil */
1563 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1565 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1566 * 0.30103 /* binary -> decimal */
1568 + 1; /* turn floor into ceil */
1571 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1572 * 0.30103 /* binary -> decimal */
1574 + 1; /* turn floor into ceil */
1575 if (tmp_length
< precision
)
1576 tmp_length
= precision
;
1577 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1578 tmp_length
= xsum (tmp_length
, tmp_length
);
1579 /* Add 1, to account for a leading sign. */
1580 tmp_length
= xsum (tmp_length
, 1);
1584 # if HAVE_LONG_LONG_INT
1585 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1587 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1588 * 0.333334 /* binary -> octal */
1590 + 1; /* turn floor into ceil */
1593 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1595 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1596 * 0.333334 /* binary -> octal */
1598 + 1; /* turn floor into ceil */
1601 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1602 * 0.333334 /* binary -> octal */
1604 + 1; /* turn floor into ceil */
1605 if (tmp_length
< precision
)
1606 tmp_length
= precision
;
1607 /* Add 1, to account for a leading sign. */
1608 tmp_length
= xsum (tmp_length
, 1);
1612 # if HAVE_LONG_LONG_INT
1613 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1615 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1616 * 0.25 /* binary -> hexadecimal */
1618 + 1; /* turn floor into ceil */
1621 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1623 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1624 * 0.25 /* binary -> hexadecimal */
1626 + 1; /* turn floor into ceil */
1629 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1630 * 0.25 /* binary -> hexadecimal */
1632 + 1; /* turn floor into ceil */
1633 if (tmp_length
< precision
)
1634 tmp_length
= precision
;
1635 /* Add 2, to account for a leading sign or alternate form. */
1636 tmp_length
= xsum (tmp_length
, 2);
1640 if (type
== TYPE_LONGDOUBLE
)
1642 (unsigned int) (LDBL_MAX_EXP
1643 * 0.30103 /* binary -> decimal */
1644 * 2 /* estimate for FLAG_GROUP */
1646 + 1 /* turn floor into ceil */
1647 + 10; /* sign, decimal point etc. */
1650 (unsigned int) (DBL_MAX_EXP
1651 * 0.30103 /* binary -> decimal */
1652 * 2 /* estimate for FLAG_GROUP */
1654 + 1 /* turn floor into ceil */
1655 + 10; /* sign, decimal point etc. */
1656 tmp_length
= xsum (tmp_length
, precision
);
1659 case 'e': case 'E': case 'g': case 'G':
1661 12; /* sign, decimal point, exponent etc. */
1662 tmp_length
= xsum (tmp_length
, precision
);
1666 if (type
== TYPE_LONGDOUBLE
)
1668 (unsigned int) (LDBL_DIG
1669 * 0.831 /* decimal -> hexadecimal */
1671 + 1; /* turn floor into ceil */
1674 (unsigned int) (DBL_DIG
1675 * 0.831 /* decimal -> hexadecimal */
1677 + 1; /* turn floor into ceil */
1678 if (tmp_length
< precision
)
1679 tmp_length
= precision
;
1680 /* Account for sign, decimal point etc. */
1681 tmp_length
= xsum (tmp_length
, 12);
1685 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1686 if (type
== TYPE_WIDE_CHAR
)
1687 tmp_length
= MB_CUR_MAX
;
1695 if (type
== TYPE_WIDE_STRING
)
1697 # if WIDE_CHAR_VERSION
1698 /* ISO C says about %ls in fwprintf:
1699 "If the precision is not specified or is greater than the size
1700 of the array, the array shall contain a null wide character."
1701 So if there is a precision, we must not use wcslen. */
1702 const wchar_t *arg
= ap
->arg
[arg_index
].a
.a_wide_string
;
1705 tmp_length
= local_wcsnlen (arg
, precision
);
1707 tmp_length
= local_wcslen (arg
);
1709 /* ISO C says about %ls in fprintf:
1710 "If a precision is specified, no more than that many bytes are
1711 written (including shift sequences, if any), and the array
1712 shall contain a null wide character if, to equal the multibyte
1713 character sequence length given by the precision, the function
1714 would need to access a wide character one past the end of the
1716 So if there is a precision, we must not use wcslen. */
1717 /* This case has already been handled separately in VASNPRINTF. */
1724 # if WIDE_CHAR_VERSION
1725 /* ISO C says about %s in fwprintf:
1726 "If the precision is not specified or is greater than the size
1727 of the converted array, the converted array shall contain a
1728 null wide character."
1729 So if there is a precision, we must not use strlen. */
1730 /* This case has already been handled separately in VASNPRINTF. */
1733 /* ISO C says about %s in fprintf:
1734 "If the precision is not specified or greater than the size of
1735 the array, the array shall contain a null character."
1736 So if there is a precision, we must not use strlen. */
1737 const char *arg
= ap
->arg
[arg_index
].a
.a_string
;
1740 tmp_length
= local_strnlen (arg
, precision
);
1742 tmp_length
= strlen (arg
);
1749 (unsigned int) (sizeof (void *) * CHAR_BIT
1750 * 0.25 /* binary -> hexadecimal */
1752 + 1 /* turn floor into ceil */
1753 + 2; /* account for leading 0x */
1762 # if ENABLE_UNISTDIO
1763 /* Padding considers the number of characters, therefore the number of
1764 elements after padding may be
1765 > max (tmp_length, width)
1767 <= tmp_length + width. */
1768 tmp_length
= xsum (tmp_length
, width
);
1770 /* Padding considers the number of elements, says POSIX. */
1771 if (tmp_length
< width
)
1776 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1784 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1785 const FCHAR_T
*format
, va_list args
)
1790 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1791 /* errno is already set. */
1795 if (d.dir != d.direct_alloc_dir) \
1797 if (a.arg != a.direct_alloc_arg) \
1800 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1808 size_t buf_neededlength
;
1810 TCHAR_T
*buf_malloced
;
1814 /* Output string accumulator. */
1819 /* Allocate a small buffer that will hold a directive passed to
1820 sprintf or snprintf. */
1822 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1824 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1826 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1827 buf_malloced
= NULL
;
1832 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1833 if (size_overflow_p (buf_memsize
))
1834 goto out_of_memory_1
;
1835 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1837 goto out_of_memory_1
;
1841 if (resultbuf
!= NULL
)
1844 allocated
= *lengthp
;
1853 result is either == resultbuf or == NULL or malloc-allocated.
1854 If length > 0, then result != NULL. */
1856 /* Ensures that allocated >= needed. Aborts through a jump to
1857 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1858 #define ENSURE_ALLOCATION(needed) \
1859 if ((needed) > allocated) \
1861 size_t memory_size; \
1864 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1865 if ((needed) > allocated) \
1866 allocated = (needed); \
1867 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1868 if (size_overflow_p (memory_size)) \
1869 goto out_of_memory; \
1870 if (result == resultbuf || result == NULL) \
1871 memory = (DCHAR_T *) malloc (memory_size); \
1873 memory = (DCHAR_T *) realloc (result, memory_size); \
1874 if (memory == NULL) \
1875 goto out_of_memory; \
1876 if (result == resultbuf && length > 0) \
1877 DCHAR_CPY (memory, result, length); \
1881 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1883 if (cp
!= dp
->dir_start
)
1885 size_t n
= dp
->dir_start
- cp
;
1886 size_t augmented_length
= xsum (length
, n
);
1888 ENSURE_ALLOCATION (augmented_length
);
1889 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1890 need that the format string contains only ASCII characters
1891 if FCHAR_T and DCHAR_T are not the same type. */
1892 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1894 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1895 length
= augmented_length
;
1900 result
[length
++] = *cp
++;
1907 /* Execute a single directive. */
1908 if (dp
->conversion
== '%')
1910 size_t augmented_length
;
1912 if (!(dp
->arg_index
== ARG_NONE
))
1914 augmented_length
= xsum (length
, 1);
1915 ENSURE_ALLOCATION (augmented_length
);
1916 result
[length
] = '%';
1917 length
= augmented_length
;
1921 if (!(dp
->arg_index
!= ARG_NONE
))
1924 if (dp
->conversion
== 'n')
1926 switch (a
.arg
[dp
->arg_index
].type
)
1928 case TYPE_COUNT_SCHAR_POINTER
:
1929 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1931 case TYPE_COUNT_SHORT_POINTER
:
1932 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1934 case TYPE_COUNT_INT_POINTER
:
1935 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1937 case TYPE_COUNT_LONGINT_POINTER
:
1938 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1940 #if HAVE_LONG_LONG_INT
1941 case TYPE_COUNT_LONGLONGINT_POINTER
:
1942 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1950 /* The unistdio extensions. */
1951 else if (dp
->conversion
== 'U')
1953 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1954 int flags
= dp
->flags
;
1962 if (dp
->width_start
!= dp
->width_end
)
1964 if (dp
->width_arg_index
!= ARG_NONE
)
1968 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1970 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1974 /* "A negative field width is taken as a '-' flag
1975 followed by a positive field width." */
1982 const FCHAR_T
*digitp
= dp
->width_start
;
1985 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1986 while (digitp
!= dp
->width_end
);
1993 if (dp
->precision_start
!= dp
->precision_end
)
1995 if (dp
->precision_arg_index
!= ARG_NONE
)
1999 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2001 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2002 /* "A negative precision is taken as if the precision
2012 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2015 while (digitp
!= dp
->precision_end
)
2016 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2023 case TYPE_U8_STRING
:
2025 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
2026 const uint8_t *arg_end
;
2031 /* Use only PRECISION characters, from the left. */
2034 for (; precision
> 0; precision
--)
2036 int count
= u8_strmblen (arg_end
);
2041 if (!(result
== resultbuf
|| result
== NULL
))
2043 if (buf_malloced
!= NULL
)
2044 free (buf_malloced
);
2055 /* Use the entire string, and count the number of
2061 int count
= u8_strmblen (arg_end
);
2066 if (!(result
== resultbuf
|| result
== NULL
))
2068 if (buf_malloced
!= NULL
)
2069 free (buf_malloced
);
2080 /* Use the entire string. */
2081 arg_end
= arg
+ u8_strlen (arg
);
2082 /* The number of characters doesn't matter. */
2086 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2088 size_t n
= width
- characters
;
2089 ENSURE_ALLOCATION (xsum (length
, n
));
2090 DCHAR_SET (result
+ length
, ' ', n
);
2094 # if DCHAR_IS_UINT8_T
2096 size_t n
= arg_end
- arg
;
2097 ENSURE_ALLOCATION (xsum (length
, n
));
2098 DCHAR_CPY (result
+ length
, arg
, n
);
2103 DCHAR_T
*converted
= result
+ length
;
2104 size_t converted_len
= allocated
- length
;
2106 /* Convert from UTF-8 to locale encoding. */
2108 u8_conv_to_encoding (locale_charset (),
2109 iconveh_question_mark
,
2110 arg
, arg_end
- arg
, NULL
,
2111 converted
, &converted_len
);
2113 /* Convert from UTF-8 to UTF-16/UTF-32. */
2115 U8_TO_DCHAR (arg
, arg_end
- arg
,
2116 converted
, &converted_len
);
2118 if (converted
== NULL
)
2120 int saved_errno
= errno
;
2121 if (!(result
== resultbuf
|| result
== NULL
))
2123 if (buf_malloced
!= NULL
)
2124 free (buf_malloced
);
2126 errno
= saved_errno
;
2129 if (converted
!= result
+ length
)
2131 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2132 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2135 length
+= converted_len
;
2139 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2141 size_t n
= width
- characters
;
2142 ENSURE_ALLOCATION (xsum (length
, n
));
2143 DCHAR_SET (result
+ length
, ' ', n
);
2149 case TYPE_U16_STRING
:
2151 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
2152 const uint16_t *arg_end
;
2157 /* Use only PRECISION characters, from the left. */
2160 for (; precision
> 0; precision
--)
2162 int count
= u16_strmblen (arg_end
);
2167 if (!(result
== resultbuf
|| result
== NULL
))
2169 if (buf_malloced
!= NULL
)
2170 free (buf_malloced
);
2181 /* Use the entire string, and count the number of
2187 int count
= u16_strmblen (arg_end
);
2192 if (!(result
== resultbuf
|| result
== NULL
))
2194 if (buf_malloced
!= NULL
)
2195 free (buf_malloced
);
2206 /* Use the entire string. */
2207 arg_end
= arg
+ u16_strlen (arg
);
2208 /* The number of characters doesn't matter. */
2212 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2214 size_t n
= width
- characters
;
2215 ENSURE_ALLOCATION (xsum (length
, n
));
2216 DCHAR_SET (result
+ length
, ' ', n
);
2220 # if DCHAR_IS_UINT16_T
2222 size_t n
= arg_end
- arg
;
2223 ENSURE_ALLOCATION (xsum (length
, n
));
2224 DCHAR_CPY (result
+ length
, arg
, n
);
2229 DCHAR_T
*converted
= result
+ length
;
2230 size_t converted_len
= allocated
- length
;
2232 /* Convert from UTF-16 to locale encoding. */
2234 u16_conv_to_encoding (locale_charset (),
2235 iconveh_question_mark
,
2236 arg
, arg_end
- arg
, NULL
,
2237 converted
, &converted_len
);
2239 /* Convert from UTF-16 to UTF-8/UTF-32. */
2241 U16_TO_DCHAR (arg
, arg_end
- arg
,
2242 converted
, &converted_len
);
2244 if (converted
== NULL
)
2246 int saved_errno
= errno
;
2247 if (!(result
== resultbuf
|| result
== NULL
))
2249 if (buf_malloced
!= NULL
)
2250 free (buf_malloced
);
2252 errno
= saved_errno
;
2255 if (converted
!= result
+ length
)
2257 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2258 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2261 length
+= converted_len
;
2265 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2267 size_t n
= width
- characters
;
2268 ENSURE_ALLOCATION (xsum (length
, n
));
2269 DCHAR_SET (result
+ length
, ' ', n
);
2275 case TYPE_U32_STRING
:
2277 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
2278 const uint32_t *arg_end
;
2283 /* Use only PRECISION characters, from the left. */
2286 for (; precision
> 0; precision
--)
2288 int count
= u32_strmblen (arg_end
);
2293 if (!(result
== resultbuf
|| result
== NULL
))
2295 if (buf_malloced
!= NULL
)
2296 free (buf_malloced
);
2307 /* Use the entire string, and count the number of
2313 int count
= u32_strmblen (arg_end
);
2318 if (!(result
== resultbuf
|| result
== NULL
))
2320 if (buf_malloced
!= NULL
)
2321 free (buf_malloced
);
2332 /* Use the entire string. */
2333 arg_end
= arg
+ u32_strlen (arg
);
2334 /* The number of characters doesn't matter. */
2338 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2340 size_t n
= width
- characters
;
2341 ENSURE_ALLOCATION (xsum (length
, n
));
2342 DCHAR_SET (result
+ length
, ' ', n
);
2346 # if DCHAR_IS_UINT32_T
2348 size_t n
= arg_end
- arg
;
2349 ENSURE_ALLOCATION (xsum (length
, n
));
2350 DCHAR_CPY (result
+ length
, arg
, n
);
2355 DCHAR_T
*converted
= result
+ length
;
2356 size_t converted_len
= allocated
- length
;
2358 /* Convert from UTF-32 to locale encoding. */
2360 u32_conv_to_encoding (locale_charset (),
2361 iconveh_question_mark
,
2362 arg
, arg_end
- arg
, NULL
,
2363 converted
, &converted_len
);
2365 /* Convert from UTF-32 to UTF-8/UTF-16. */
2367 U32_TO_DCHAR (arg
, arg_end
- arg
,
2368 converted
, &converted_len
);
2370 if (converted
== NULL
)
2372 int saved_errno
= errno
;
2373 if (!(result
== resultbuf
|| result
== NULL
))
2375 if (buf_malloced
!= NULL
)
2376 free (buf_malloced
);
2378 errno
= saved_errno
;
2381 if (converted
!= result
+ length
)
2383 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2384 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2387 length
+= converted_len
;
2391 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2393 size_t n
= width
- characters
;
2394 ENSURE_ALLOCATION (xsum (length
, n
));
2395 DCHAR_SET (result
+ length
, ' ', n
);
2406 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2407 else if (dp
->conversion
== 's'
2408 # if WIDE_CHAR_VERSION
2409 && a
.arg
[dp
->arg_index
].type
!= TYPE_WIDE_STRING
2411 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_STRING
2415 /* The normal handling of the 's' directive below requires
2416 allocating a temporary buffer. The determination of its
2417 length (tmp_length), in the case when a precision is
2418 specified, below requires a conversion between a char[]
2419 string and a wchar_t[] wide string. It could be done, but
2420 we have no guarantee that the implementation of sprintf will
2421 use the exactly same algorithm. Without this guarantee, it
2422 is possible to have buffer overrun bugs. In order to avoid
2423 such bugs, we implement the entire processing of the 's'
2424 directive ourselves. */
2425 int flags
= dp
->flags
;
2433 if (dp
->width_start
!= dp
->width_end
)
2435 if (dp
->width_arg_index
!= ARG_NONE
)
2439 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2441 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2445 /* "A negative field width is taken as a '-' flag
2446 followed by a positive field width." */
2453 const FCHAR_T
*digitp
= dp
->width_start
;
2456 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2457 while (digitp
!= dp
->width_end
);
2464 if (dp
->precision_start
!= dp
->precision_end
)
2466 if (dp
->precision_arg_index
!= ARG_NONE
)
2470 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2472 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2473 /* "A negative precision is taken as if the precision
2483 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2486 while (digitp
!= dp
->precision_end
)
2487 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2492 # if WIDE_CHAR_VERSION
2493 /* %s in vasnwprintf. See the specification of fwprintf. */
2495 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
2496 const char *arg_end
;
2501 /* Use only as many bytes as needed to produce PRECISION
2502 wide characters, from the left. */
2505 memset (&state
, '\0', sizeof (mbstate_t));
2509 for (; precision
> 0; precision
--)
2513 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2515 count
= mblen (arg_end
, MB_CUR_MAX
);
2518 /* Found the terminating NUL. */
2522 /* Invalid or incomplete multibyte character. */
2523 if (!(result
== resultbuf
|| result
== NULL
))
2525 if (buf_malloced
!= NULL
)
2526 free (buf_malloced
);
2537 /* Use the entire string, and count the number of wide
2541 memset (&state
, '\0', sizeof (mbstate_t));
2549 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2551 count
= mblen (arg_end
, MB_CUR_MAX
);
2554 /* Found the terminating NUL. */
2558 /* Invalid or incomplete multibyte character. */
2559 if (!(result
== resultbuf
|| result
== NULL
))
2561 if (buf_malloced
!= NULL
)
2562 free (buf_malloced
);
2573 /* Use the entire string. */
2574 arg_end
= arg
+ strlen (arg
);
2575 /* The number of characters doesn't matter. */
2579 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2581 size_t n
= width
- characters
;
2582 ENSURE_ALLOCATION (xsum (length
, n
));
2583 DCHAR_SET (result
+ length
, ' ', n
);
2587 if (has_precision
|| has_width
)
2589 /* We know the number of wide characters in advance. */
2593 memset (&state
, '\0', sizeof (mbstate_t));
2595 ENSURE_ALLOCATION (xsum (length
, characters
));
2596 for (remaining
= characters
; remaining
> 0; remaining
--)
2601 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2603 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2606 /* mbrtowc not consistent with mbrlen, or mbtowc
2607 not consistent with mblen. */
2609 result
[length
++] = wc
;
2612 if (!(arg
== arg_end
))
2619 memset (&state
, '\0', sizeof (mbstate_t));
2621 while (arg
< arg_end
)
2626 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2628 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2631 /* mbrtowc not consistent with mbrlen, or mbtowc
2632 not consistent with mblen. */
2634 ENSURE_ALLOCATION (xsum (length
, 1));
2635 result
[length
++] = wc
;
2640 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2642 size_t n
= width
- characters
;
2643 ENSURE_ALLOCATION (xsum (length
, n
));
2644 DCHAR_SET (result
+ length
, ' ', n
);
2649 /* %ls in vasnprintf. See the specification of fprintf. */
2651 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
2652 const wchar_t *arg_end
;
2654 # if !DCHAR_IS_TCHAR
2655 /* This code assumes that TCHAR_T is 'char'. */
2656 verify (sizeof (TCHAR_T
) == 1);
2665 /* Use only as many wide characters as needed to produce
2666 at most PRECISION bytes, from the left. */
2667 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2669 memset (&state
, '\0', sizeof (mbstate_t));
2673 while (precision
> 0)
2675 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2679 /* Found the terminating null wide character. */
2681 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2682 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2684 count
= wctomb (cbuf
, *arg_end
);
2688 /* Cannot convert. */
2689 if (!(result
== resultbuf
|| result
== NULL
))
2691 if (buf_malloced
!= NULL
)
2692 free (buf_malloced
);
2697 if (precision
< count
)
2700 characters
+= count
;
2710 /* Use the entire string, and count the number of
2712 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2714 memset (&state
, '\0', sizeof (mbstate_t));
2720 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2724 /* Found the terminating null wide character. */
2726 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2727 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2729 count
= wctomb (cbuf
, *arg_end
);
2733 /* Cannot convert. */
2734 if (!(result
== resultbuf
|| result
== NULL
))
2736 if (buf_malloced
!= NULL
)
2737 free (buf_malloced
);
2743 characters
+= count
;
2749 /* Use the entire string. */
2750 arg_end
= arg
+ local_wcslen (arg
);
2751 /* The number of bytes doesn't matter. */
2756 # if !DCHAR_IS_TCHAR
2757 /* Convert the string into a piece of temporary memory. */
2758 tmpsrc
= (TCHAR_T
*) malloc (characters
* sizeof (TCHAR_T
));
2762 TCHAR_T
*tmpptr
= tmpsrc
;
2764 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2766 memset (&state
, '\0', sizeof (mbstate_t));
2768 for (remaining
= characters
; remaining
> 0; )
2770 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2775 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2776 count
= wcrtomb (cbuf
, *arg
, &state
);
2778 count
= wctomb (cbuf
, *arg
);
2781 /* Inconsistency. */
2783 memcpy (tmpptr
, cbuf
, count
);
2788 if (!(arg
== arg_end
))
2792 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2794 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2795 iconveh_question_mark
,
2801 int saved_errno
= errno
;
2803 if (!(result
== resultbuf
|| result
== NULL
))
2805 if (buf_malloced
!= NULL
)
2806 free (buf_malloced
);
2808 errno
= saved_errno
;
2816 # if ENABLE_UNISTDIO
2817 /* Outside POSIX, it's preferable to compare the width
2818 against the number of _characters_ of the converted
2820 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2822 /* The width is compared against the number of _bytes_
2823 of the converted value, says POSIX. */
2828 /* w doesn't matter. */
2831 if (w
< width
&& !(dp
->flags
& FLAG_LEFT
))
2833 size_t n
= width
- w
;
2834 ENSURE_ALLOCATION (xsum (length
, n
));
2835 DCHAR_SET (result
+ length
, ' ', n
);
2840 if (has_precision
|| has_width
)
2842 /* We know the number of bytes in advance. */
2844 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2846 memset (&state
, '\0', sizeof (mbstate_t));
2848 ENSURE_ALLOCATION (xsum (length
, characters
));
2849 for (remaining
= characters
; remaining
> 0; )
2851 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2856 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2857 count
= wcrtomb (cbuf
, *arg
, &state
);
2859 count
= wctomb (cbuf
, *arg
);
2862 /* Inconsistency. */
2864 memcpy (result
+ length
, cbuf
, count
);
2869 if (!(arg
== arg_end
))
2874 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2876 memset (&state
, '\0', sizeof (mbstate_t));
2878 while (arg
< arg_end
)
2880 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2885 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2886 count
= wcrtomb (cbuf
, *arg
, &state
);
2888 count
= wctomb (cbuf
, *arg
);
2892 /* Cannot convert. */
2893 if (!(result
== resultbuf
|| result
== NULL
))
2895 if (buf_malloced
!= NULL
)
2896 free (buf_malloced
);
2901 ENSURE_ALLOCATION (xsum (length
, count
));
2902 memcpy (result
+ length
, cbuf
, count
);
2908 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
2909 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
2911 length
+= tmpdst_len
;
2914 if (w
< width
&& (dp
->flags
& FLAG_LEFT
))
2916 size_t n
= width
- w
;
2917 ENSURE_ALLOCATION (xsum (length
, n
));
2918 DCHAR_SET (result
+ length
, ' ', n
);
2925 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2926 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
2927 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2929 # if NEED_PRINTF_DOUBLE
2930 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2932 # if NEED_PRINTF_LONG_DOUBLE
2933 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2939 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2940 int flags
= dp
->flags
;
2946 DCHAR_T tmpbuf
[700];
2952 if (dp
->width_start
!= dp
->width_end
)
2954 if (dp
->width_arg_index
!= ARG_NONE
)
2958 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2960 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2964 /* "A negative field width is taken as a '-' flag
2965 followed by a positive field width." */
2972 const FCHAR_T
*digitp
= dp
->width_start
;
2975 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2976 while (digitp
!= dp
->width_end
);
2982 if (dp
->precision_start
!= dp
->precision_end
)
2984 if (dp
->precision_arg_index
!= ARG_NONE
)
2988 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2990 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2991 /* "A negative precision is taken as if the precision
3001 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3004 while (digitp
!= dp
->precision_end
)
3005 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3010 /* Allocate a temporary buffer of sufficient size. */
3011 if (type
== TYPE_LONGDOUBLE
)
3013 (unsigned int) ((LDBL_DIG
+ 1)
3014 * 0.831 /* decimal -> hexadecimal */
3016 + 1; /* turn floor into ceil */
3019 (unsigned int) ((DBL_DIG
+ 1)
3020 * 0.831 /* decimal -> hexadecimal */
3022 + 1; /* turn floor into ceil */
3023 if (tmp_length
< precision
)
3024 tmp_length
= precision
;
3025 /* Account for sign, decimal point etc. */
3026 tmp_length
= xsum (tmp_length
, 12);
3028 if (tmp_length
< width
)
3031 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3033 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3037 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3039 if (size_overflow_p (tmp_memsize
))
3040 /* Overflow, would lead to out of memory. */
3042 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3044 /* Out of memory. */
3050 if (type
== TYPE_LONGDOUBLE
)
3052 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3053 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3057 if (dp
->conversion
== 'A')
3059 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3063 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3069 DECL_LONG_DOUBLE_ROUNDING
3071 BEGIN_LONG_DOUBLE_ROUNDING ();
3073 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3081 else if (flags
& FLAG_SHOWSIGN
)
3083 else if (flags
& FLAG_SPACE
)
3086 if (arg
> 0.0L && arg
+ arg
== arg
)
3088 if (dp
->conversion
== 'A')
3090 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3094 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3100 long double mantissa
;
3103 mantissa
= printf_frexpl (arg
, &exponent
);
3111 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
3113 /* Round the mantissa. */
3114 long double tail
= mantissa
;
3117 for (q
= precision
; ; q
--)
3119 int digit
= (int) tail
;
3123 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
3132 for (q
= precision
; q
> 0; q
--)
3138 *p
++ = dp
->conversion
- 'A' + 'X';
3143 digit
= (int) mantissa
;
3146 if ((flags
& FLAG_ALT
)
3147 || mantissa
> 0.0L || precision
> 0)
3149 *p
++ = decimal_point_char ();
3150 /* This loop terminates because we assume
3151 that FLT_RADIX is a power of 2. */
3152 while (mantissa
> 0.0L)
3155 digit
= (int) mantissa
;
3160 : dp
->conversion
- 10);
3164 while (precision
> 0)
3171 *p
++ = dp
->conversion
- 'A' + 'P';
3172 # if WIDE_CHAR_VERSION
3174 static const wchar_t decimal_format
[] =
3175 { '%', '+', 'd', '\0' };
3176 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3181 if (sizeof (DCHAR_T
) == 1)
3183 sprintf ((char *) p
, "%+d", exponent
);
3191 sprintf (expbuf
, "%+d", exponent
);
3192 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3198 END_LONG_DOUBLE_ROUNDING ();
3206 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3207 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3211 if (dp
->conversion
== 'A')
3213 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3217 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3224 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3232 else if (flags
& FLAG_SHOWSIGN
)
3234 else if (flags
& FLAG_SPACE
)
3237 if (arg
> 0.0 && arg
+ arg
== arg
)
3239 if (dp
->conversion
== 'A')
3241 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3245 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3254 mantissa
= printf_frexp (arg
, &exponent
);
3262 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
3264 /* Round the mantissa. */
3265 double tail
= mantissa
;
3268 for (q
= precision
; ; q
--)
3270 int digit
= (int) tail
;
3274 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
3283 for (q
= precision
; q
> 0; q
--)
3289 *p
++ = dp
->conversion
- 'A' + 'X';
3294 digit
= (int) mantissa
;
3297 if ((flags
& FLAG_ALT
)
3298 || mantissa
> 0.0 || precision
> 0)
3300 *p
++ = decimal_point_char ();
3301 /* This loop terminates because we assume
3302 that FLT_RADIX is a power of 2. */
3303 while (mantissa
> 0.0)
3306 digit
= (int) mantissa
;
3311 : dp
->conversion
- 10);
3315 while (precision
> 0)
3322 *p
++ = dp
->conversion
- 'A' + 'P';
3323 # if WIDE_CHAR_VERSION
3325 static const wchar_t decimal_format
[] =
3326 { '%', '+', 'd', '\0' };
3327 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3332 if (sizeof (DCHAR_T
) == 1)
3334 sprintf ((char *) p
, "%+d", exponent
);
3342 sprintf (expbuf
, "%+d", exponent
);
3343 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3354 /* The generated string now extends from tmp to p, with the
3355 zero padding insertion point being at pad_ptr. */
3360 size_t pad
= width
- count
;
3361 DCHAR_T
*end
= p
+ pad
;
3363 if (flags
& FLAG_LEFT
)
3365 /* Pad with spaces on the right. */
3366 for (; pad
> 0; pad
--)
3369 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3371 /* Pad with zeroes. */
3376 for (; pad
> 0; pad
--)
3381 /* Pad with spaces on the left. */
3386 for (; pad
> 0; pad
--)
3395 if (count
>= tmp_length
)
3396 /* tmp_length was incorrectly calculated - fix the
3400 /* Make room for the result. */
3401 if (count
>= allocated
- length
)
3403 size_t n
= xsum (length
, count
);
3405 ENSURE_ALLOCATION (n
);
3408 /* Append the result. */
3409 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3415 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3416 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
3417 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
3418 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
3419 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
3421 # if NEED_PRINTF_DOUBLE
3422 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3423 # elif NEED_PRINTF_INFINITE_DOUBLE
3424 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3425 /* The systems (mingw) which produce wrong output
3426 for Inf, -Inf, and NaN also do so for -0.0.
3427 Therefore we treat this case here as well. */
3428 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
3430 # if NEED_PRINTF_LONG_DOUBLE
3431 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3432 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3433 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3434 /* Some systems produce wrong output for Inf,
3435 -Inf, and NaN. Some systems in this category
3436 (IRIX 5.3) also do so for -0.0. Therefore we
3437 treat this case here as well. */
3438 && is_infinite_or_zerol (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
3442 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3443 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3445 int flags
= dp
->flags
;
3451 DCHAR_T tmpbuf
[700];
3457 if (dp
->width_start
!= dp
->width_end
)
3459 if (dp
->width_arg_index
!= ARG_NONE
)
3463 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3465 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3469 /* "A negative field width is taken as a '-' flag
3470 followed by a positive field width." */
3477 const FCHAR_T
*digitp
= dp
->width_start
;
3480 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3481 while (digitp
!= dp
->width_end
);
3487 if (dp
->precision_start
!= dp
->precision_end
)
3489 if (dp
->precision_arg_index
!= ARG_NONE
)
3493 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3495 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3496 /* "A negative precision is taken as if the precision
3506 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3509 while (digitp
!= dp
->precision_end
)
3510 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3515 /* POSIX specifies the default precision to be 6 for %f, %F,
3516 %e, %E, but not for %g, %G. Implementations appear to use
3517 the same default precision also for %g, %G. But for %a, %A,
3518 the default precision is 0. */
3520 if (!(dp
->conversion
== 'a' || dp
->conversion
== 'A'))
3523 /* Allocate a temporary buffer of sufficient size. */
3524 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3525 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
3526 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3527 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
3528 # elif NEED_PRINTF_LONG_DOUBLE
3529 tmp_length
= LDBL_DIG
+ 1;
3530 # elif NEED_PRINTF_DOUBLE
3531 tmp_length
= DBL_DIG
+ 1;
3535 if (tmp_length
< precision
)
3536 tmp_length
= precision
;
3537 # if NEED_PRINTF_LONG_DOUBLE
3538 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3539 if (type
== TYPE_LONGDOUBLE
)
3541 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3543 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3544 if (!(isnanl (arg
) || arg
+ arg
== arg
))
3546 /* arg is finite and nonzero. */
3547 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
3548 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3549 tmp_length
= exponent
+ precision
;
3553 # if NEED_PRINTF_DOUBLE
3554 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3555 if (type
== TYPE_DOUBLE
)
3557 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3559 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3560 if (!(isnand (arg
) || arg
+ arg
== arg
))
3562 /* arg is finite and nonzero. */
3563 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
3564 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3565 tmp_length
= exponent
+ precision
;
3569 /* Account for sign, decimal point etc. */
3570 tmp_length
= xsum (tmp_length
, 12);
3572 if (tmp_length
< width
)
3575 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3577 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3581 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3583 if (size_overflow_p (tmp_memsize
))
3584 /* Overflow, would lead to out of memory. */
3586 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3588 /* Out of memory. */
3595 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3596 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3597 if (type
== TYPE_LONGDOUBLE
)
3600 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3604 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3606 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3610 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3616 DECL_LONG_DOUBLE_ROUNDING
3618 BEGIN_LONG_DOUBLE_ROUNDING ();
3620 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3628 else if (flags
& FLAG_SHOWSIGN
)
3630 else if (flags
& FLAG_SPACE
)
3633 if (arg
> 0.0L && arg
+ arg
== arg
)
3635 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3637 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3641 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3646 # if NEED_PRINTF_LONG_DOUBLE
3649 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3655 scale10_round_decimal_long_double (arg
, precision
);
3658 END_LONG_DOUBLE_ROUNDING ();
3661 ndigits
= strlen (digits
);
3663 if (ndigits
> precision
)
3667 *p
++ = digits
[ndigits
];
3669 while (ndigits
> precision
);
3672 /* Here ndigits <= precision. */
3673 if ((flags
& FLAG_ALT
) || precision
> 0)
3675 *p
++ = decimal_point_char ();
3676 for (; precision
> ndigits
; precision
--)
3681 *p
++ = digits
[ndigits
];
3687 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3695 if ((flags
& FLAG_ALT
) || precision
> 0)
3697 *p
++ = decimal_point_char ();
3698 for (; precision
> 0; precision
--)
3709 exponent
= floorlog10l (arg
);
3714 scale10_round_decimal_long_double (arg
,
3715 (int)precision
- exponent
);
3718 END_LONG_DOUBLE_ROUNDING ();
3721 ndigits
= strlen (digits
);
3723 if (ndigits
== precision
+ 1)
3725 if (ndigits
< precision
3726 || ndigits
> precision
+ 2)
3727 /* The exponent was not guessed
3728 precisely enough. */
3731 /* None of two values of exponent is
3732 the right one. Prevent an endless
3736 if (ndigits
== precision
)
3742 /* Here ndigits = precision+1. */
3743 if (is_borderline (digits
, precision
))
3745 /* Maybe the exponent guess was too high
3746 and a smaller exponent can be reached
3747 by turning a 10...0 into 9...9x. */
3749 scale10_round_decimal_long_double (arg
,
3750 (int)precision
- exponent
+ 1);
3751 if (digits2
== NULL
)
3754 END_LONG_DOUBLE_ROUNDING ();
3757 if (strlen (digits2
) == precision
+ 1)
3766 /* Here ndigits = precision+1. */
3768 *p
++ = digits
[--ndigits
];
3769 if ((flags
& FLAG_ALT
) || precision
> 0)
3771 *p
++ = decimal_point_char ();
3775 *p
++ = digits
[ndigits
];
3782 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3783 # if WIDE_CHAR_VERSION
3785 static const wchar_t decimal_format
[] =
3786 { '%', '+', '.', '2', 'd', '\0' };
3787 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3792 if (sizeof (DCHAR_T
) == 1)
3794 sprintf ((char *) p
, "%+.2d", exponent
);
3802 sprintf (expbuf
, "%+.2d", exponent
);
3803 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3808 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3812 /* precision >= 1. */
3815 /* The exponent is 0, >= -4, < precision.
3816 Use fixed-point notation. */
3818 size_t ndigits
= precision
;
3819 /* Number of trailing zeroes that have to be
3822 (flags
& FLAG_ALT
? 0 : precision
- 1);
3826 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3828 *p
++ = decimal_point_char ();
3829 while (ndigits
> nzeroes
)
3845 exponent
= floorlog10l (arg
);
3850 scale10_round_decimal_long_double (arg
,
3851 (int)(precision
- 1) - exponent
);
3854 END_LONG_DOUBLE_ROUNDING ();
3857 ndigits
= strlen (digits
);
3859 if (ndigits
== precision
)
3861 if (ndigits
< precision
- 1
3862 || ndigits
> precision
+ 1)
3863 /* The exponent was not guessed
3864 precisely enough. */
3867 /* None of two values of exponent is
3868 the right one. Prevent an endless
3872 if (ndigits
< precision
)
3878 /* Here ndigits = precision. */
3879 if (is_borderline (digits
, precision
- 1))
3881 /* Maybe the exponent guess was too high
3882 and a smaller exponent can be reached
3883 by turning a 10...0 into 9...9x. */
3885 scale10_round_decimal_long_double (arg
,
3886 (int)(precision
- 1) - exponent
+ 1);
3887 if (digits2
== NULL
)
3890 END_LONG_DOUBLE_ROUNDING ();
3893 if (strlen (digits2
) == precision
)
3902 /* Here ndigits = precision. */
3904 /* Determine the number of trailing zeroes
3905 that have to be dropped. */
3907 if ((flags
& FLAG_ALT
) == 0)
3908 while (nzeroes
< ndigits
3909 && digits
[nzeroes
] == '0')
3912 /* The exponent is now determined. */
3914 && exponent
< (long)precision
)
3916 /* Fixed-point notation:
3917 max(exponent,0)+1 digits, then the
3918 decimal point, then the remaining
3919 digits without trailing zeroes. */
3922 size_t ecount
= exponent
+ 1;
3923 /* Note: count <= precision = ndigits. */
3924 for (; ecount
> 0; ecount
--)
3925 *p
++ = digits
[--ndigits
];
3926 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3928 *p
++ = decimal_point_char ();
3929 while (ndigits
> nzeroes
)
3932 *p
++ = digits
[ndigits
];
3938 size_t ecount
= -exponent
- 1;
3940 *p
++ = decimal_point_char ();
3941 for (; ecount
> 0; ecount
--)
3943 while (ndigits
> nzeroes
)
3946 *p
++ = digits
[ndigits
];
3952 /* Exponential notation. */
3953 *p
++ = digits
[--ndigits
];
3954 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3956 *p
++ = decimal_point_char ();
3957 while (ndigits
> nzeroes
)
3960 *p
++ = digits
[ndigits
];
3963 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3964 # if WIDE_CHAR_VERSION
3966 static const wchar_t decimal_format
[] =
3967 { '%', '+', '.', '2', 'd', '\0' };
3968 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3973 if (sizeof (DCHAR_T
) == 1)
3975 sprintf ((char *) p
, "%+.2d", exponent
);
3983 sprintf (expbuf
, "%+.2d", exponent
);
3984 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3996 /* arg is finite. */
4002 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4005 if ((flags
& FLAG_ALT
) || precision
> 0)
4007 *p
++ = decimal_point_char ();
4008 for (; precision
> 0; precision
--)
4012 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4015 if ((flags
& FLAG_ALT
) || precision
> 0)
4017 *p
++ = decimal_point_char ();
4018 for (; precision
> 0; precision
--)
4021 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4026 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4029 if (flags
& FLAG_ALT
)
4032 (precision
> 0 ? precision
- 1 : 0);
4033 *p
++ = decimal_point_char ();
4034 for (; ndigits
> 0; --ndigits
)
4038 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
4041 *p
++ = dp
->conversion
- 'A' + 'X';
4044 if ((flags
& FLAG_ALT
) || precision
> 0)
4046 *p
++ = decimal_point_char ();
4047 for (; precision
> 0; precision
--)
4050 *p
++ = dp
->conversion
- 'A' + 'P';
4059 END_LONG_DOUBLE_ROUNDING ();
4062 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4066 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4068 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4072 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4074 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
4078 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
4085 if (signbit (arg
)) /* arg < 0.0 or negative zero */
4093 else if (flags
& FLAG_SHOWSIGN
)
4095 else if (flags
& FLAG_SPACE
)
4098 if (arg
> 0.0 && arg
+ arg
== arg
)
4100 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4102 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
4106 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
4111 # if NEED_PRINTF_DOUBLE
4114 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4120 scale10_round_decimal_double (arg
, precision
);
4123 ndigits
= strlen (digits
);
4125 if (ndigits
> precision
)
4129 *p
++ = digits
[ndigits
];
4131 while (ndigits
> precision
);
4134 /* Here ndigits <= precision. */
4135 if ((flags
& FLAG_ALT
) || precision
> 0)
4137 *p
++ = decimal_point_char ();
4138 for (; precision
> ndigits
; precision
--)
4143 *p
++ = digits
[ndigits
];
4149 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4157 if ((flags
& FLAG_ALT
) || precision
> 0)
4159 *p
++ = decimal_point_char ();
4160 for (; precision
> 0; precision
--)
4171 exponent
= floorlog10 (arg
);
4176 scale10_round_decimal_double (arg
,
4177 (int)precision
- exponent
);
4180 ndigits
= strlen (digits
);
4182 if (ndigits
== precision
+ 1)
4184 if (ndigits
< precision
4185 || ndigits
> precision
+ 2)
4186 /* The exponent was not guessed
4187 precisely enough. */
4190 /* None of two values of exponent is
4191 the right one. Prevent an endless
4195 if (ndigits
== precision
)
4201 /* Here ndigits = precision+1. */
4202 if (is_borderline (digits
, precision
))
4204 /* Maybe the exponent guess was too high
4205 and a smaller exponent can be reached
4206 by turning a 10...0 into 9...9x. */
4208 scale10_round_decimal_double (arg
,
4209 (int)precision
- exponent
+ 1);
4210 if (digits2
== NULL
)
4215 if (strlen (digits2
) == precision
+ 1)
4224 /* Here ndigits = precision+1. */
4226 *p
++ = digits
[--ndigits
];
4227 if ((flags
& FLAG_ALT
) || precision
> 0)
4229 *p
++ = decimal_point_char ();
4233 *p
++ = digits
[ndigits
];
4240 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4241 # if WIDE_CHAR_VERSION
4243 static const wchar_t decimal_format
[] =
4244 /* Produce the same number of exponent digits
4245 as the native printf implementation. */
4246 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4247 { '%', '+', '.', '3', 'd', '\0' };
4249 { '%', '+', '.', '2', 'd', '\0' };
4251 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4257 static const char decimal_format
[] =
4258 /* Produce the same number of exponent digits
4259 as the native printf implementation. */
4260 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4265 if (sizeof (DCHAR_T
) == 1)
4267 sprintf ((char *) p
, decimal_format
, exponent
);
4275 sprintf (expbuf
, decimal_format
, exponent
);
4276 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4282 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4286 /* precision >= 1. */
4289 /* The exponent is 0, >= -4, < precision.
4290 Use fixed-point notation. */
4292 size_t ndigits
= precision
;
4293 /* Number of trailing zeroes that have to be
4296 (flags
& FLAG_ALT
? 0 : precision
- 1);
4300 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4302 *p
++ = decimal_point_char ();
4303 while (ndigits
> nzeroes
)
4319 exponent
= floorlog10 (arg
);
4324 scale10_round_decimal_double (arg
,
4325 (int)(precision
- 1) - exponent
);
4328 ndigits
= strlen (digits
);
4330 if (ndigits
== precision
)
4332 if (ndigits
< precision
- 1
4333 || ndigits
> precision
+ 1)
4334 /* The exponent was not guessed
4335 precisely enough. */
4338 /* None of two values of exponent is
4339 the right one. Prevent an endless
4343 if (ndigits
< precision
)
4349 /* Here ndigits = precision. */
4350 if (is_borderline (digits
, precision
- 1))
4352 /* Maybe the exponent guess was too high
4353 and a smaller exponent can be reached
4354 by turning a 10...0 into 9...9x. */
4356 scale10_round_decimal_double (arg
,
4357 (int)(precision
- 1) - exponent
+ 1);
4358 if (digits2
== NULL
)
4363 if (strlen (digits2
) == precision
)
4372 /* Here ndigits = precision. */
4374 /* Determine the number of trailing zeroes
4375 that have to be dropped. */
4377 if ((flags
& FLAG_ALT
) == 0)
4378 while (nzeroes
< ndigits
4379 && digits
[nzeroes
] == '0')
4382 /* The exponent is now determined. */
4384 && exponent
< (long)precision
)
4386 /* Fixed-point notation:
4387 max(exponent,0)+1 digits, then the
4388 decimal point, then the remaining
4389 digits without trailing zeroes. */
4392 size_t ecount
= exponent
+ 1;
4393 /* Note: ecount <= precision = ndigits. */
4394 for (; ecount
> 0; ecount
--)
4395 *p
++ = digits
[--ndigits
];
4396 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4398 *p
++ = decimal_point_char ();
4399 while (ndigits
> nzeroes
)
4402 *p
++ = digits
[ndigits
];
4408 size_t ecount
= -exponent
- 1;
4410 *p
++ = decimal_point_char ();
4411 for (; ecount
> 0; ecount
--)
4413 while (ndigits
> nzeroes
)
4416 *p
++ = digits
[ndigits
];
4422 /* Exponential notation. */
4423 *p
++ = digits
[--ndigits
];
4424 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4426 *p
++ = decimal_point_char ();
4427 while (ndigits
> nzeroes
)
4430 *p
++ = digits
[ndigits
];
4433 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4434 # if WIDE_CHAR_VERSION
4436 static const wchar_t decimal_format
[] =
4437 /* Produce the same number of exponent digits
4438 as the native printf implementation. */
4439 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4440 { '%', '+', '.', '3', 'd', '\0' };
4442 { '%', '+', '.', '2', 'd', '\0' };
4444 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4450 static const char decimal_format
[] =
4451 /* Produce the same number of exponent digits
4452 as the native printf implementation. */
4453 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4458 if (sizeof (DCHAR_T
) == 1)
4460 sprintf ((char *) p
, decimal_format
, exponent
);
4468 sprintf (expbuf
, decimal_format
, exponent
);
4469 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4482 /* arg is finite. */
4488 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4491 if ((flags
& FLAG_ALT
) || precision
> 0)
4493 *p
++ = decimal_point_char ();
4494 for (; precision
> 0; precision
--)
4498 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4501 if ((flags
& FLAG_ALT
) || precision
> 0)
4503 *p
++ = decimal_point_char ();
4504 for (; precision
> 0; precision
--)
4507 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4509 /* Produce the same number of exponent digits as
4510 the native printf implementation. */
4511 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4517 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4520 if (flags
& FLAG_ALT
)
4523 (precision
> 0 ? precision
- 1 : 0);
4524 *p
++ = decimal_point_char ();
4525 for (; ndigits
> 0; --ndigits
)
4537 /* The generated string now extends from tmp to p, with the
4538 zero padding insertion point being at pad_ptr. */
4543 size_t pad
= width
- count
;
4544 DCHAR_T
*end
= p
+ pad
;
4546 if (flags
& FLAG_LEFT
)
4548 /* Pad with spaces on the right. */
4549 for (; pad
> 0; pad
--)
4552 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4554 /* Pad with zeroes. */
4559 for (; pad
> 0; pad
--)
4564 /* Pad with spaces on the left. */
4569 for (; pad
> 0; pad
--)
4578 if (count
>= tmp_length
)
4579 /* tmp_length was incorrectly calculated - fix the
4583 /* Make room for the result. */
4584 if (count
>= allocated
- length
)
4586 size_t n
= xsum (length
, count
);
4588 ENSURE_ALLOCATION (n
);
4591 /* Append the result. */
4592 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4600 arg_type type
= a
.arg
[dp
->arg_index
].type
;
4601 int flags
= dp
->flags
;
4602 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4605 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4608 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4612 #if NEED_PRINTF_UNBOUNDED_PRECISION
4615 # define prec_ourselves 0
4617 #if NEED_PRINTF_FLAG_LEFTADJUST
4618 # define pad_ourselves 1
4619 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4622 # define pad_ourselves 0
4625 unsigned int prefix_count
;
4626 int prefixes
[2] IF_LINT (= { 0 });
4630 TCHAR_T tmpbuf
[700];
4634 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4637 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4639 if (dp
->width_start
!= dp
->width_end
)
4641 if (dp
->width_arg_index
!= ARG_NONE
)
4645 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4647 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
4651 /* "A negative field width is taken as a '-' flag
4652 followed by a positive field width." */
4659 const FCHAR_T
*digitp
= dp
->width_start
;
4662 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
4663 while (digitp
!= dp
->width_end
);
4665 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4671 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4674 if (dp
->precision_start
!= dp
->precision_end
)
4676 if (dp
->precision_arg_index
!= ARG_NONE
)
4680 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4682 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4683 /* "A negative precision is taken as if the precision
4693 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
4696 while (digitp
!= dp
->precision_end
)
4697 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
4703 /* Decide whether to handle the precision ourselves. */
4704 #if NEED_PRINTF_UNBOUNDED_PRECISION
4705 switch (dp
->conversion
)
4707 case 'd': case 'i': case 'u':
4709 case 'x': case 'X': case 'p':
4710 prec_ourselves
= has_precision
&& (precision
> 0);
4718 /* Decide whether to perform the padding ourselves. */
4719 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4720 switch (dp
->conversion
)
4722 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4723 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4724 to perform the padding after this conversion. Functions
4725 with unistdio extensions perform the padding based on
4726 character count rather than element count. */
4729 # if NEED_PRINTF_FLAG_ZERO
4730 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4736 pad_ourselves
= prec_ourselves
;
4742 /* Allocate a temporary buffer of sufficient size for calling
4745 MAX_ROOM_NEEDED (&a
, dp
->arg_index
, dp
->conversion
, type
,
4746 flags
, width
, has_precision
, precision
,
4749 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
4753 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
4755 if (size_overflow_p (tmp_memsize
))
4756 /* Overflow, would lead to out of memory. */
4758 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
4760 /* Out of memory. */
4765 /* Construct the format string for calling snprintf or
4769 #if NEED_PRINTF_FLAG_GROUPING
4770 /* The underlying implementation doesn't support the ' flag.
4771 Produce no grouping characters in this case; this is
4772 acceptable because the grouping is locale dependent. */
4774 if (flags
& FLAG_GROUP
)
4777 if (flags
& FLAG_LEFT
)
4779 if (flags
& FLAG_SHOWSIGN
)
4781 if (flags
& FLAG_SPACE
)
4783 if (flags
& FLAG_ALT
)
4785 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4786 if (flags
& FLAG_LOCALIZED
)
4791 if (flags
& FLAG_ZERO
)
4793 if (dp
->width_start
!= dp
->width_end
)
4795 size_t n
= dp
->width_end
- dp
->width_start
;
4796 /* The width specification is known to consist only
4797 of standard ASCII characters. */
4798 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4800 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
4805 const FCHAR_T
*mp
= dp
->width_start
;
4812 if (!prec_ourselves
)
4814 if (dp
->precision_start
!= dp
->precision_end
)
4816 size_t n
= dp
->precision_end
- dp
->precision_start
;
4817 /* The precision specification is known to consist only
4818 of standard ASCII characters. */
4819 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4821 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
4826 const FCHAR_T
*mp
= dp
->precision_start
;
4836 #if HAVE_LONG_LONG_INT
4837 case TYPE_LONGLONGINT
:
4838 case TYPE_ULONGLONGINT
:
4839 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4852 case TYPE_WIDE_CHAR
:
4855 case TYPE_WIDE_STRING
:
4859 case TYPE_LONGDOUBLE
:
4865 #if NEED_PRINTF_DIRECTIVE_F
4866 if (dp
->conversion
== 'F')
4870 *fbp
= dp
->conversion
;
4872 # if ! (((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) \
4873 && !defined __UCLIBC__) \
4874 || (defined __APPLE__ && defined __MACH__) \
4875 || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4880 /* On glibc2 systems from glibc >= 2.3 - probably also older
4881 ones - we know that snprintf's return value conforms to
4882 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
4883 gl_SNPRINTF_TRUNCATION_C99 pass.
4884 Therefore we can avoid using %n in this situation.
4885 On glibc2 systems from 2004-10-18 or newer, the use of %n
4886 in format strings in writable memory may crash the program
4887 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4888 in this situation. */
4889 /* On Mac OS X 10.3 or newer, we know that snprintf's return
4890 value conforms to ISO C 99: the tests gl_SNPRINTF_RETVAL_C99
4891 and gl_SNPRINTF_TRUNCATION_C99 pass.
4892 Therefore we can avoid using %n in this situation.
4893 On Mac OS X 10.13 or newer, the use of %n in format strings
4894 in writable memory by default crashes the program, so we
4895 should avoid it in this situation. */
4896 /* On native Windows systems (such as mingw), we can avoid using
4898 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4899 snprintf does not write more than the specified number
4900 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4901 '4', '5', '6' into buf, not '4', '5', '\0'.)
4902 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4903 allows us to recognize the case of an insufficient
4904 buffer size: it returns -1 in this case.
4905 On native Windows systems (such as mingw) where the OS is
4906 Windows Vista, the use of %n in format strings by default
4907 crashes the program. See
4908 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4909 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4910 So we should avoid %n in this situation. */
4917 /* Construct the arguments for calling snprintf or sprintf. */
4919 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
4921 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4923 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
4925 if (!prec_ourselves
&& dp
->precision_arg_index
!= ARG_NONE
)
4927 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4929 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4933 /* The SNPRINTF result is appended after result[0..length].
4934 The latter is an array of DCHAR_T; SNPRINTF appends an
4935 array of TCHAR_T to it. This is possible because
4936 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4937 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4938 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4939 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4940 where an snprintf() with maxlen==1 acts like sprintf(). */
4941 ENSURE_ALLOCATION (xsum (length
,
4942 (2 + TCHARS_PER_DCHAR
- 1)
4943 / TCHARS_PER_DCHAR
));
4944 /* Prepare checking whether snprintf returns the count
4946 *(TCHAR_T
*) (result
+ length
) = '\0';
4957 size_t maxlen
= allocated
- length
;
4958 /* SNPRINTF can fail if its second argument is
4960 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
4961 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
4962 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
4963 # define SNPRINTF_BUF(arg) \
4964 switch (prefix_count) \
4967 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4972 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4974 prefixes[0], arg, &count); \
4977 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4979 prefixes[0], prefixes[1], arg, \
4986 # define SNPRINTF_BUF(arg) \
4987 switch (prefix_count) \
4990 count = sprintf (tmp, buf, arg); \
4993 count = sprintf (tmp, buf, prefixes[0], arg); \
4996 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
5009 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
5015 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
5021 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
5027 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
5033 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
5039 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
5045 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
5051 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
5055 #if HAVE_LONG_LONG_INT
5056 case TYPE_LONGLONGINT
:
5058 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
5062 case TYPE_ULONGLONGINT
:
5064 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
5071 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
5075 case TYPE_LONGDOUBLE
:
5077 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
5083 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
5088 case TYPE_WIDE_CHAR
:
5090 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
5097 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
5102 case TYPE_WIDE_STRING
:
5104 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
5111 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
5120 /* Portability: Not all implementations of snprintf()
5121 are ISO C 99 compliant. Determine the number of
5122 bytes that snprintf() has produced or would have
5126 /* Verify that snprintf() has NUL-terminated its
5129 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
5131 /* Portability hack. */
5132 if (retcount
> count
)
5137 /* snprintf() doesn't understand the '%n'
5141 /* Don't use the '%n' directive; instead, look
5142 at the snprintf() return value. */
5148 /* Look at the snprintf() return value. */
5151 # if !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
5152 /* HP-UX 10.20 snprintf() is doubly deficient:
5153 It doesn't understand the '%n' directive,
5154 *and* it returns -1 (rather than the length
5155 that would have been required) when the
5156 buffer is too small.
5157 But a failure at this point can also come
5158 from other reasons than a too small buffer,
5159 such as an invalid wide string argument to
5160 the %ls directive, or possibly an invalid
5161 floating-point argument. */
5163 MAX_ROOM_NEEDED (&a
, dp
->arg_index
,
5164 dp
->conversion
, type
, flags
,
5167 precision
, pad_ourselves
);
5169 if (maxlen
< tmp_length
)
5171 /* Make more room. But try to do through
5172 this reallocation only once. */
5173 size_t bigger_need
=
5176 TCHARS_PER_DCHAR
- 1)
5177 / TCHARS_PER_DCHAR
);
5178 /* And always grow proportionally.
5179 (There may be several arguments, each
5180 needing a little more room than the
5182 size_t bigger_need2
=
5183 xsum (xtimes (allocated
, 2), 12);
5184 if (bigger_need
< bigger_need2
)
5185 bigger_need
= bigger_need2
;
5186 ENSURE_ALLOCATION (bigger_need
);
5197 /* Attempt to handle failure. */
5200 /* SNPRINTF or sprintf failed. Save and use the errno
5201 that it has set, if any. */
5202 int saved_errno
= errno
;
5203 if (saved_errno
== 0)
5205 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5206 saved_errno
= EILSEQ
;
5208 saved_errno
= EINVAL
;
5211 if (!(result
== resultbuf
|| result
== NULL
))
5213 if (buf_malloced
!= NULL
)
5214 free (buf_malloced
);
5217 errno
= saved_errno
;
5222 /* Handle overflow of the allocated buffer.
5223 If such an overflow occurs, a C99 compliant snprintf()
5224 returns a count >= maxlen. However, a non-compliant
5225 snprintf() function returns only count = maxlen - 1. To
5226 cover both cases, test whether count >= maxlen - 1. */
5227 if ((unsigned int) count
+ 1 >= maxlen
)
5229 /* If maxlen already has attained its allowed maximum,
5230 allocating more memory will not increase maxlen.
5231 Instead of looping, bail out. */
5232 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
5236 /* Need at least (count + 1) * sizeof (TCHAR_T)
5237 bytes. (The +1 is for the trailing NUL.)
5238 But ask for (count + 2) * sizeof (TCHAR_T)
5239 bytes, so that in the next round, we likely get
5240 maxlen > (unsigned int) count + 1
5241 and so we don't get here again.
5242 And allocate proportionally, to avoid looping
5243 eternally if snprintf() reports a too small
5247 ((unsigned int) count
+ 2
5248 + TCHARS_PER_DCHAR
- 1)
5249 / TCHARS_PER_DCHAR
),
5250 xtimes (allocated
, 2));
5252 ENSURE_ALLOCATION (n
);
5258 #if NEED_PRINTF_UNBOUNDED_PRECISION
5261 /* Handle the precision. */
5264 (TCHAR_T
*) (result
+ length
);
5268 size_t prefix_count
;
5272 /* Put the additional zeroes after the sign. */
5274 && (*prec_ptr
== '-' || *prec_ptr
== '+'
5275 || *prec_ptr
== ' '))
5277 /* Put the additional zeroes after the 0x prefix if
5278 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5280 && prec_ptr
[0] == '0'
5281 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
5284 move
= count
- prefix_count
;
5285 if (precision
> move
)
5287 /* Insert zeroes. */
5288 size_t insert
= precision
- move
;
5294 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
5295 / TCHARS_PER_DCHAR
);
5296 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5297 ENSURE_ALLOCATION (n
);
5298 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5299 prec_ptr
= (TCHAR_T
*) (result
+ length
);
5302 prec_end
= prec_ptr
+ count
;
5303 prec_ptr
+= prefix_count
;
5305 while (prec_end
> prec_ptr
)
5308 prec_end
[insert
] = prec_end
[0];
5314 while (prec_end
> prec_ptr
);
5322 if (count
>= tmp_length
)
5323 /* tmp_length was incorrectly calculated - fix the
5329 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5330 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5332 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5334 The result string is not certainly ASCII. */
5335 const TCHAR_T
*tmpsrc
;
5338 /* This code assumes that TCHAR_T is 'char'. */
5339 verify (sizeof (TCHAR_T
) == 1);
5341 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5346 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5347 iconveh_question_mark
,
5353 int saved_errno
= errno
;
5354 if (!(result
== resultbuf
|| result
== NULL
))
5356 if (buf_malloced
!= NULL
)
5357 free (buf_malloced
);
5359 errno
= saved_errno
;
5362 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
5363 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
5369 /* The result string is ASCII.
5370 Simple 1:1 conversion. */
5372 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5373 no-op conversion, in-place on the array starting
5374 at (result + length). */
5375 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
5378 const TCHAR_T
*tmpsrc
;
5383 if (result
== resultbuf
)
5385 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5386 /* ENSURE_ALLOCATION will not move tmpsrc
5387 (because it's part of resultbuf). */
5388 ENSURE_ALLOCATION (xsum (length
, count
));
5392 /* ENSURE_ALLOCATION will move the array
5393 (because it uses realloc(). */
5394 ENSURE_ALLOCATION (xsum (length
, count
));
5395 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5399 ENSURE_ALLOCATION (xsum (length
, count
));
5401 tmpdst
= result
+ length
;
5402 /* Copy backwards, because of overlapping. */
5405 for (n
= count
; n
> 0; n
--)
5406 *--tmpdst
= *--tmpsrc
;
5411 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5412 /* Make room for the result. */
5413 if (count
> allocated
- length
)
5415 /* Need at least count elements. But allocate
5418 xmax (xsum (length
, count
), xtimes (allocated
, 2));
5420 ENSURE_ALLOCATION (n
);
5424 /* Here count <= allocated - length. */
5426 /* Perform padding. */
5427 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5428 if (pad_ourselves
&& has_width
)
5431 # if ENABLE_UNISTDIO
5432 /* Outside POSIX, it's preferable to compare the width
5433 against the number of _characters_ of the converted
5435 w
= DCHAR_MBSNLEN (result
+ length
, count
);
5437 /* The width is compared against the number of _bytes_
5438 of the converted value, says POSIX. */
5443 size_t pad
= width
- w
;
5445 /* Make room for the result. */
5446 if (xsum (count
, pad
) > allocated
- length
)
5448 /* Need at least count + pad elements. But
5449 allocate proportionally. */
5451 xmax (xsum3 (length
, count
, pad
),
5452 xtimes (allocated
, 2));
5456 ENSURE_ALLOCATION (n
);
5459 ENSURE_ALLOCATION (n
);
5462 /* Here count + pad <= allocated - length. */
5465 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5466 DCHAR_T
* const rp
= result
+ length
;
5468 DCHAR_T
* const rp
= tmp
;
5470 DCHAR_T
*p
= rp
+ count
;
5471 DCHAR_T
*end
= p
+ pad
;
5473 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5474 if (dp
->conversion
== 'c'
5475 || dp
->conversion
== 's')
5476 /* No zero-padding for string directives. */
5481 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
5482 /* No zero-padding of "inf" and "nan". */
5483 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
5484 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
5487 /* The generated string now extends from rp to p,
5488 with the zero padding insertion point being at
5491 count
= count
+ pad
; /* = end - rp */
5493 if (flags
& FLAG_LEFT
)
5495 /* Pad with spaces on the right. */
5496 for (; pad
> 0; pad
--)
5499 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
5501 /* Pad with zeroes. */
5506 for (; pad
> 0; pad
--)
5511 /* Pad with spaces on the left. */
5516 for (; pad
> 0; pad
--)
5524 /* Here still count <= allocated - length. */
5526 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5527 /* The snprintf() result did fit. */
5529 /* Append the sprintf() result. */
5530 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
5537 #if NEED_PRINTF_DIRECTIVE_F
5538 if (dp
->conversion
== 'F')
5540 /* Convert the %f result to upper case for %F. */
5541 DCHAR_T
*rp
= result
+ length
;
5543 for (rc
= count
; rc
> 0; rc
--, rp
++)
5544 if (*rp
>= 'a' && *rp
<= 'z')
5545 *rp
= *rp
- 'a' + 'A';
5553 #undef pad_ourselves
5554 #undef prec_ourselves
5559 /* Add the final NUL. */
5560 ENSURE_ALLOCATION (xsum (length
, 1));
5561 result
[length
] = '\0';
5563 if (result
!= resultbuf
&& length
+ 1 < allocated
)
5565 /* Shrink the allocated memory if possible. */
5568 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
5573 if (buf_malloced
!= NULL
)
5574 free (buf_malloced
);
5577 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5578 says that snprintf() fails with errno = EOVERFLOW in this case, but
5579 that's only because snprintf() returns an 'int'. This function does
5580 not have this limitation. */
5585 if (!(result
== resultbuf
|| result
== NULL
))
5587 if (buf_malloced
!= NULL
)
5588 free (buf_malloced
);
5595 if (!(result
== resultbuf
|| result
== NULL
))
5597 if (buf_malloced
!= NULL
)
5598 free (buf_malloced
);
5606 #undef MAX_ROOM_NEEDED
5607 #undef TCHARS_PER_DCHAR
5615 #undef DCHAR_IS_TCHAR