1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2018 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 <https://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 /* We need extra_zeroes bytes for zeroes, followed by c_len bytes for the
864 digits of a, followed by 1 byte for the terminating NUL. */
865 char *c_ptr
= (char *) malloc (xsum (xsum (extra_zeroes
, c_len
), 1));
869 for (; extra_zeroes
> 0; extra_zeroes
--)
873 /* Divide a by 10^9, in-place. */
874 mp_limb_t remainder
= 0;
875 mp_limb_t
*ptr
= a_ptr
+ a_len
;
877 for (count
= a_len
; count
> 0; count
--)
880 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
881 *ptr
= num
/ 1000000000;
882 remainder
= num
% 1000000000;
884 /* Store the remainder as 9 decimal digits. */
885 for (count
= 9; count
> 0; count
--)
887 *d_ptr
++ = '0' + (remainder
% 10);
888 remainder
= remainder
/ 10;
891 if (a_ptr
[a_len
- 1] == 0)
894 /* Remove leading zeroes. */
895 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
897 /* But keep at least one zero. */
900 /* Terminate the string. */
906 # if NEED_PRINTF_LONG_DOUBLE
908 /* Assuming x is finite and >= 0:
909 write x as x = 2^e * m, where m is a bignum.
910 Return the allocated memory in case of success, NULL in case of memory
911 allocation failure. */
913 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
920 /* Allocate memory for result. */
921 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
922 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
925 /* Split into exponential part and mantissa. */
926 y
= frexpl (x
, &exp
);
927 if (!(y
>= 0.0L && y
< 1.0L))
929 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
930 latter is an integer. */
931 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
932 I'm not sure whether it's safe to cast a 'long double' value between
933 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
934 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
936 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
937 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
940 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
943 if (!(y
>= 0.0L && y
< 1.0L))
945 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
948 if (!(y
>= 0.0L && y
< 1.0L))
950 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
955 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
958 if (!(y
>= 0.0L && y
< 1.0L))
960 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
964 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
967 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
970 if (!(y
>= 0.0L && y
< 1.0L))
972 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
975 if (!(y
>= 0.0L && y
< 1.0L))
977 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
979 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
985 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
988 *ep
= exp
- LDBL_MANT_BIT
;
994 # if NEED_PRINTF_DOUBLE
996 /* Assuming x is finite and >= 0:
997 write x as x = 2^e * m, where m is a bignum.
998 Return the allocated memory in case of success, NULL in case of memory
999 allocation failure. */
1001 decode_double (double x
, int *ep
, mpn_t
*mp
)
1008 /* Allocate memory for result. */
1009 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
1010 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
1011 if (m
.limbs
== NULL
)
1013 /* Split into exponential part and mantissa. */
1014 y
= frexp (x
, &exp
);
1015 if (!(y
>= 0.0 && y
< 1.0))
1017 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
1018 latter is an integer. */
1019 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
1020 I'm not sure whether it's safe to cast a 'double' value between
1021 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1022 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1024 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
1025 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1028 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1031 if (!(y
>= 0.0 && y
< 1.0))
1033 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1036 if (!(y
>= 0.0 && y
< 1.0))
1038 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1043 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
1046 if (!(y
>= 0.0 && y
< 1.0))
1048 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1052 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1055 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1058 if (!(y
>= 0.0 && y
< 1.0))
1060 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1063 if (!(y
>= 0.0 && y
< 1.0))
1065 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1070 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1073 *ep
= exp
- DBL_MANT_BIT
;
1079 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1080 Returns the decimal representation of round (x * 10^n).
1081 Return the allocated memory - containing the decimal digits in low-to-high
1082 order, terminated with a NUL character - in case of success, NULL in case
1083 of memory allocation failure. */
1085 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
1088 size_t extra_zeroes
;
1091 mp_limb_t
*pow5_ptr
;
1093 unsigned int s_limbs
;
1094 unsigned int s_bits
;
1102 /* x = 2^e * m, hence
1103 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1104 = round (2^s * 5^n * m). */
1107 /* Factor out a common power of 10 if possible. */
1110 extra_zeroes
= (s
< n
? s
: n
);
1114 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1115 Before converting to decimal, we need to compute
1116 z = round (2^s * 5^n * m). */
1117 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1118 sign. 2.322 is slightly larger than log(5)/log(2). */
1119 abs_n
= (n
>= 0 ? n
: -n
);
1120 abs_s
= (s
>= 0 ? s
: -s
);
1121 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1122 + abs_s
/ GMP_LIMB_BITS
+ 1)
1123 * sizeof (mp_limb_t
));
1124 if (pow5_ptr
== NULL
)
1129 /* Initialize with 1. */
1132 /* Multiply with 5^|n|. */
1135 static mp_limb_t
const small_pow5
[13 + 1] =
1137 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1138 48828125, 244140625, 1220703125
1141 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1143 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1145 mp_twolimb_t carry
= 0;
1146 for (j
= 0; j
< pow5_len
; j
++)
1148 mp_limb_t digit2
= pow5_ptr
[j
];
1149 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1150 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1151 carry
= carry
>> GMP_LIMB_BITS
;
1154 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1157 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1158 s_bits
= abs_s
% GMP_LIMB_BITS
;
1159 if (n
>= 0 ? s
>= 0 : s
<= 0)
1161 /* Multiply with 2^|s|. */
1164 mp_limb_t
*ptr
= pow5_ptr
;
1165 mp_twolimb_t accu
= 0;
1167 for (count
= pow5_len
; count
> 0; count
--)
1169 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1170 *ptr
++ = (mp_limb_t
) accu
;
1171 accu
= accu
>> GMP_LIMB_BITS
;
1175 *ptr
= (mp_limb_t
) accu
;
1182 for (count
= pow5_len
; count
> 0;)
1185 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1187 for (count
= s_limbs
; count
> 0;)
1190 pow5_ptr
[count
] = 0;
1192 pow5_len
+= s_limbs
;
1194 pow5
.limbs
= pow5_ptr
;
1195 pow5
.nlimbs
= pow5_len
;
1198 /* Multiply m with pow5. No division needed. */
1199 z_memory
= multiply (m
, pow5
, &z
);
1203 /* Divide m by pow5 and round. */
1204 z_memory
= divide (m
, pow5
, &z
);
1209 pow5
.limbs
= pow5_ptr
;
1210 pow5
.nlimbs
= pow5_len
;
1214 Multiply m with pow5, then divide by 2^|s|. */
1218 tmp_memory
= multiply (m
, pow5
, &numerator
);
1219 if (tmp_memory
== NULL
)
1225 /* Construct 2^|s|. */
1227 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1229 for (i
= 0; i
< s_limbs
; i
++)
1231 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1232 denominator
.limbs
= ptr
;
1233 denominator
.nlimbs
= s_limbs
+ 1;
1235 z_memory
= divide (numerator
, denominator
, &z
);
1241 Multiply m with 2^s, then divide by pow5. */
1244 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1245 * sizeof (mp_limb_t
));
1246 if (num_ptr
== NULL
)
1253 mp_limb_t
*destptr
= num_ptr
;
1256 for (i
= 0; i
< s_limbs
; i
++)
1261 const mp_limb_t
*sourceptr
= m
.limbs
;
1262 mp_twolimb_t accu
= 0;
1264 for (count
= m
.nlimbs
; count
> 0; count
--)
1266 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1267 *destptr
++ = (mp_limb_t
) accu
;
1268 accu
= accu
>> GMP_LIMB_BITS
;
1271 *destptr
++ = (mp_limb_t
) accu
;
1275 const mp_limb_t
*sourceptr
= m
.limbs
;
1277 for (count
= m
.nlimbs
; count
> 0; count
--)
1278 *destptr
++ = *sourceptr
++;
1280 numerator
.limbs
= num_ptr
;
1281 numerator
.nlimbs
= destptr
- num_ptr
;
1283 z_memory
= divide (numerator
, pow5
, &z
);
1290 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1292 if (z_memory
== NULL
)
1294 digits
= convert_to_decimal (z
, extra_zeroes
);
1299 # if NEED_PRINTF_LONG_DOUBLE
1301 /* Assuming x is finite and >= 0, and n is an integer:
1302 Returns the decimal representation of round (x * 10^n).
1303 Return the allocated memory - containing the decimal digits in low-to-high
1304 order, terminated with a NUL character - in case of success, NULL in case
1305 of memory allocation failure. */
1307 scale10_round_decimal_long_double (long double x
, int n
)
1311 void *memory
= decode_long_double (x
, &e
, &m
);
1312 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1317 # if NEED_PRINTF_DOUBLE
1319 /* Assuming x is finite and >= 0, and n is an integer:
1320 Returns the decimal representation of round (x * 10^n).
1321 Return the allocated memory - containing the decimal digits in low-to-high
1322 order, terminated with a NUL character - in case of success, NULL in case
1323 of memory allocation failure. */
1325 scale10_round_decimal_double (double x
, int n
)
1329 void *memory
= decode_double (x
, &e
, &m
);
1330 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1335 # if NEED_PRINTF_LONG_DOUBLE
1337 /* Assuming x is finite and > 0:
1338 Return an approximation for n with 10^n <= x < 10^(n+1).
1339 The approximation is usually the right n, but may be off by 1 sometimes. */
1341 floorlog10l (long double x
)
1348 /* Split into exponential part and mantissa. */
1349 y
= frexpl (x
, &exp
);
1350 if (!(y
>= 0.0L && y
< 1.0L))
1356 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1358 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1359 exp
-= GMP_LIMB_BITS
;
1361 if (y
< (1.0L / (1 << 16)))
1363 y
*= 1.0L * (1 << 16);
1366 if (y
< (1.0L / (1 << 8)))
1368 y
*= 1.0L * (1 << 8);
1371 if (y
< (1.0L / (1 << 4)))
1373 y
*= 1.0L * (1 << 4);
1376 if (y
< (1.0L / (1 << 2)))
1378 y
*= 1.0L * (1 << 2);
1381 if (y
< (1.0L / (1 << 1)))
1383 y
*= 1.0L * (1 << 1);
1387 if (!(y
>= 0.5L && y
< 1.0L))
1389 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1392 if (z
< 0.70710678118654752444)
1394 z
*= 1.4142135623730950488;
1397 if (z
< 0.8408964152537145431)
1399 z
*= 1.1892071150027210667;
1402 if (z
< 0.91700404320467123175)
1404 z
*= 1.0905077326652576592;
1407 if (z
< 0.9576032806985736469)
1409 z
*= 1.0442737824274138403;
1412 /* Now 0.95 <= z <= 1.01. */
1414 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1415 Four terms are enough to get an approximation with error < 10^-7. */
1416 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1417 /* Finally multiply with log(2)/log(10), yields an approximation for
1419 l
*= 0.30102999566398119523;
1420 /* Round down to the next integer. */
1421 return (int) l
+ (l
< 0 ? -1 : 0);
1426 # if NEED_PRINTF_DOUBLE
1428 /* Assuming x is finite and > 0:
1429 Return an approximation for n with 10^n <= x < 10^(n+1).
1430 The approximation is usually the right n, but may be off by 1 sometimes. */
1432 floorlog10 (double x
)
1439 /* Split into exponential part and mantissa. */
1440 y
= frexp (x
, &exp
);
1441 if (!(y
>= 0.0 && y
< 1.0))
1447 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1449 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1450 exp
-= GMP_LIMB_BITS
;
1452 if (y
< (1.0 / (1 << 16)))
1454 y
*= 1.0 * (1 << 16);
1457 if (y
< (1.0 / (1 << 8)))
1459 y
*= 1.0 * (1 << 8);
1462 if (y
< (1.0 / (1 << 4)))
1464 y
*= 1.0 * (1 << 4);
1467 if (y
< (1.0 / (1 << 2)))
1469 y
*= 1.0 * (1 << 2);
1472 if (y
< (1.0 / (1 << 1)))
1474 y
*= 1.0 * (1 << 1);
1478 if (!(y
>= 0.5 && y
< 1.0))
1480 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1483 if (z
< 0.70710678118654752444)
1485 z
*= 1.4142135623730950488;
1488 if (z
< 0.8408964152537145431)
1490 z
*= 1.1892071150027210667;
1493 if (z
< 0.91700404320467123175)
1495 z
*= 1.0905077326652576592;
1498 if (z
< 0.9576032806985736469)
1500 z
*= 1.0442737824274138403;
1503 /* Now 0.95 <= z <= 1.01. */
1505 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1506 Four terms are enough to get an approximation with error < 10^-7. */
1507 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1508 /* Finally multiply with log(2)/log(10), yields an approximation for
1510 l
*= 0.30102999566398119523;
1511 /* Round down to the next integer. */
1512 return (int) l
+ (l
< 0 ? -1 : 0);
1517 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1518 a single '1' digit. */
1520 is_borderline (const char *digits
, size_t precision
)
1522 for (; precision
> 0; precision
--, digits
++)
1528 return *digits
== '\0';
1533 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
1535 /* Use a different function name, to make it possible that the 'wchar_t'
1536 parametrization and the 'char' parametrization get compiled in the same
1537 translation unit. */
1538 # if WIDE_CHAR_VERSION
1539 # define MAX_ROOM_NEEDED wmax_room_needed
1541 # define MAX_ROOM_NEEDED max_room_needed
1544 /* Returns the number of TCHAR_T units needed as temporary space for the result
1545 of sprintf or SNPRINTF of a single conversion directive. */
1547 MAX_ROOM_NEEDED (const arguments
*ap
, size_t arg_index
, FCHAR_T conversion
,
1548 arg_type type
, int flags
, size_t width
, int has_precision
,
1549 size_t precision
, int pad_ourselves
)
1555 case 'd': case 'i': case 'u':
1556 # if HAVE_LONG_LONG_INT
1557 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1559 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1560 * 0.30103 /* binary -> decimal */
1562 + 1; /* turn floor into ceil */
1565 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1567 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1568 * 0.30103 /* binary -> decimal */
1570 + 1; /* turn floor into ceil */
1573 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1574 * 0.30103 /* binary -> decimal */
1576 + 1; /* turn floor into ceil */
1577 if (tmp_length
< precision
)
1578 tmp_length
= precision
;
1579 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1580 tmp_length
= xsum (tmp_length
, tmp_length
);
1581 /* Add 1, to account for a leading sign. */
1582 tmp_length
= xsum (tmp_length
, 1);
1586 # if HAVE_LONG_LONG_INT
1587 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1589 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1590 * 0.333334 /* binary -> octal */
1592 + 1; /* turn floor into ceil */
1595 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1597 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1598 * 0.333334 /* binary -> octal */
1600 + 1; /* turn floor into ceil */
1603 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1604 * 0.333334 /* binary -> octal */
1606 + 1; /* turn floor into ceil */
1607 if (tmp_length
< precision
)
1608 tmp_length
= precision
;
1609 /* Add 1, to account for a leading sign. */
1610 tmp_length
= xsum (tmp_length
, 1);
1614 # if HAVE_LONG_LONG_INT
1615 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1617 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1618 * 0.25 /* binary -> hexadecimal */
1620 + 1; /* turn floor into ceil */
1623 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1625 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1626 * 0.25 /* binary -> hexadecimal */
1628 + 1; /* turn floor into ceil */
1631 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1632 * 0.25 /* binary -> hexadecimal */
1634 + 1; /* turn floor into ceil */
1635 if (tmp_length
< precision
)
1636 tmp_length
= precision
;
1637 /* Add 2, to account for a leading sign or alternate form. */
1638 tmp_length
= xsum (tmp_length
, 2);
1642 if (type
== TYPE_LONGDOUBLE
)
1644 (unsigned int) (LDBL_MAX_EXP
1645 * 0.30103 /* binary -> decimal */
1646 * 2 /* estimate for FLAG_GROUP */
1648 + 1 /* turn floor into ceil */
1649 + 10; /* sign, decimal point etc. */
1652 (unsigned int) (DBL_MAX_EXP
1653 * 0.30103 /* binary -> decimal */
1654 * 2 /* estimate for FLAG_GROUP */
1656 + 1 /* turn floor into ceil */
1657 + 10; /* sign, decimal point etc. */
1658 tmp_length
= xsum (tmp_length
, precision
);
1661 case 'e': case 'E': case 'g': case 'G':
1663 12; /* sign, decimal point, exponent etc. */
1664 tmp_length
= xsum (tmp_length
, precision
);
1668 if (type
== TYPE_LONGDOUBLE
)
1670 (unsigned int) (LDBL_DIG
1671 * 0.831 /* decimal -> hexadecimal */
1673 + 1; /* turn floor into ceil */
1676 (unsigned int) (DBL_DIG
1677 * 0.831 /* decimal -> hexadecimal */
1679 + 1; /* turn floor into ceil */
1680 if (tmp_length
< precision
)
1681 tmp_length
= precision
;
1682 /* Account for sign, decimal point etc. */
1683 tmp_length
= xsum (tmp_length
, 12);
1687 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1688 if (type
== TYPE_WIDE_CHAR
)
1689 tmp_length
= MB_CUR_MAX
;
1697 if (type
== TYPE_WIDE_STRING
)
1699 # if WIDE_CHAR_VERSION
1700 /* ISO C says about %ls in fwprintf:
1701 "If the precision is not specified or is greater than the size
1702 of the array, the array shall contain a null wide character."
1703 So if there is a precision, we must not use wcslen. */
1704 const wchar_t *arg
= ap
->arg
[arg_index
].a
.a_wide_string
;
1707 tmp_length
= local_wcsnlen (arg
, precision
);
1709 tmp_length
= local_wcslen (arg
);
1711 /* ISO C says about %ls in fprintf:
1712 "If a precision is specified, no more than that many bytes are
1713 written (including shift sequences, if any), and the array
1714 shall contain a null wide character if, to equal the multibyte
1715 character sequence length given by the precision, the function
1716 would need to access a wide character one past the end of the
1718 So if there is a precision, we must not use wcslen. */
1719 /* This case has already been handled separately in VASNPRINTF. */
1726 # if WIDE_CHAR_VERSION
1727 /* ISO C says about %s in fwprintf:
1728 "If the precision is not specified or is greater than the size
1729 of the converted array, the converted array shall contain a
1730 null wide character."
1731 So if there is a precision, we must not use strlen. */
1732 /* This case has already been handled separately in VASNPRINTF. */
1735 /* ISO C says about %s in fprintf:
1736 "If the precision is not specified or greater than the size of
1737 the array, the array shall contain a null character."
1738 So if there is a precision, we must not use strlen. */
1739 const char *arg
= ap
->arg
[arg_index
].a
.a_string
;
1742 tmp_length
= local_strnlen (arg
, precision
);
1744 tmp_length
= strlen (arg
);
1751 (unsigned int) (sizeof (void *) * CHAR_BIT
1752 * 0.25 /* binary -> hexadecimal */
1754 + 1 /* turn floor into ceil */
1755 + 2; /* account for leading 0x */
1764 # if ENABLE_UNISTDIO
1765 /* Padding considers the number of characters, therefore the number of
1766 elements after padding may be
1767 > max (tmp_length, width)
1769 <= tmp_length + width. */
1770 tmp_length
= xsum (tmp_length
, width
);
1772 /* Padding considers the number of elements, says POSIX. */
1773 if (tmp_length
< width
)
1778 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1786 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1787 const FCHAR_T
*format
, va_list args
)
1792 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1793 /* errno is already set. */
1797 if (d.dir != d.direct_alloc_dir) \
1799 if (a.arg != a.direct_alloc_arg) \
1802 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1810 size_t buf_neededlength
;
1812 TCHAR_T
*buf_malloced
;
1816 /* Output string accumulator. */
1821 /* Allocate a small buffer that will hold a directive passed to
1822 sprintf or snprintf. */
1824 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1826 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1828 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1829 buf_malloced
= NULL
;
1834 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1835 if (size_overflow_p (buf_memsize
))
1836 goto out_of_memory_1
;
1837 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1839 goto out_of_memory_1
;
1843 if (resultbuf
!= NULL
)
1846 allocated
= *lengthp
;
1855 result is either == resultbuf or == NULL or malloc-allocated.
1856 If length > 0, then result != NULL. */
1858 /* Ensures that allocated >= needed. Aborts through a jump to
1859 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1860 #define ENSURE_ALLOCATION(needed) \
1861 if ((needed) > allocated) \
1863 size_t memory_size; \
1866 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1867 if ((needed) > allocated) \
1868 allocated = (needed); \
1869 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1870 if (size_overflow_p (memory_size)) \
1871 goto out_of_memory; \
1872 if (result == resultbuf || result == NULL) \
1873 memory = (DCHAR_T *) malloc (memory_size); \
1875 memory = (DCHAR_T *) realloc (result, memory_size); \
1876 if (memory == NULL) \
1877 goto out_of_memory; \
1878 if (result == resultbuf && length > 0) \
1879 DCHAR_CPY (memory, result, length); \
1883 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1885 if (cp
!= dp
->dir_start
)
1887 size_t n
= dp
->dir_start
- cp
;
1888 size_t augmented_length
= xsum (length
, n
);
1890 ENSURE_ALLOCATION (augmented_length
);
1891 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1892 need that the format string contains only ASCII characters
1893 if FCHAR_T and DCHAR_T are not the same type. */
1894 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1896 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1897 length
= augmented_length
;
1902 result
[length
++] = *cp
++;
1909 /* Execute a single directive. */
1910 if (dp
->conversion
== '%')
1912 size_t augmented_length
;
1914 if (!(dp
->arg_index
== ARG_NONE
))
1916 augmented_length
= xsum (length
, 1);
1917 ENSURE_ALLOCATION (augmented_length
);
1918 result
[length
] = '%';
1919 length
= augmented_length
;
1923 if (!(dp
->arg_index
!= ARG_NONE
))
1926 if (dp
->conversion
== 'n')
1928 switch (a
.arg
[dp
->arg_index
].type
)
1930 case TYPE_COUNT_SCHAR_POINTER
:
1931 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1933 case TYPE_COUNT_SHORT_POINTER
:
1934 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1936 case TYPE_COUNT_INT_POINTER
:
1937 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1939 case TYPE_COUNT_LONGINT_POINTER
:
1940 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1942 #if HAVE_LONG_LONG_INT
1943 case TYPE_COUNT_LONGLONGINT_POINTER
:
1944 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1952 /* The unistdio extensions. */
1953 else if (dp
->conversion
== 'U')
1955 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1956 int flags
= dp
->flags
;
1964 if (dp
->width_start
!= dp
->width_end
)
1966 if (dp
->width_arg_index
!= ARG_NONE
)
1970 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1972 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1976 /* "A negative field width is taken as a '-' flag
1977 followed by a positive field width." */
1984 const FCHAR_T
*digitp
= dp
->width_start
;
1987 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1988 while (digitp
!= dp
->width_end
);
1995 if (dp
->precision_start
!= dp
->precision_end
)
1997 if (dp
->precision_arg_index
!= ARG_NONE
)
2001 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2003 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2004 /* "A negative precision is taken as if the precision
2014 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2017 while (digitp
!= dp
->precision_end
)
2018 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2025 case TYPE_U8_STRING
:
2027 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
2028 const uint8_t *arg_end
;
2033 /* Use only PRECISION characters, from the left. */
2036 for (; precision
> 0; precision
--)
2038 int count
= u8_strmblen (arg_end
);
2043 if (!(result
== resultbuf
|| result
== NULL
))
2045 if (buf_malloced
!= NULL
)
2046 free (buf_malloced
);
2057 /* Use the entire string, and count the number of
2063 int count
= u8_strmblen (arg_end
);
2068 if (!(result
== resultbuf
|| result
== NULL
))
2070 if (buf_malloced
!= NULL
)
2071 free (buf_malloced
);
2082 /* Use the entire string. */
2083 arg_end
= arg
+ u8_strlen (arg
);
2084 /* The number of characters doesn't matter. */
2088 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2090 size_t n
= width
- characters
;
2091 ENSURE_ALLOCATION (xsum (length
, n
));
2092 DCHAR_SET (result
+ length
, ' ', n
);
2096 # if DCHAR_IS_UINT8_T
2098 size_t n
= arg_end
- arg
;
2099 ENSURE_ALLOCATION (xsum (length
, n
));
2100 DCHAR_CPY (result
+ length
, arg
, n
);
2105 DCHAR_T
*converted
= result
+ length
;
2106 size_t converted_len
= allocated
- length
;
2108 /* Convert from UTF-8 to locale encoding. */
2110 u8_conv_to_encoding (locale_charset (),
2111 iconveh_question_mark
,
2112 arg
, arg_end
- arg
, NULL
,
2113 converted
, &converted_len
);
2115 /* Convert from UTF-8 to UTF-16/UTF-32. */
2117 U8_TO_DCHAR (arg
, arg_end
- arg
,
2118 converted
, &converted_len
);
2120 if (converted
== NULL
)
2122 int saved_errno
= errno
;
2123 if (!(result
== resultbuf
|| result
== NULL
))
2125 if (buf_malloced
!= NULL
)
2126 free (buf_malloced
);
2128 errno
= saved_errno
;
2131 if (converted
!= result
+ length
)
2133 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2134 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2137 length
+= converted_len
;
2141 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2143 size_t n
= width
- characters
;
2144 ENSURE_ALLOCATION (xsum (length
, n
));
2145 DCHAR_SET (result
+ length
, ' ', n
);
2151 case TYPE_U16_STRING
:
2153 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
2154 const uint16_t *arg_end
;
2159 /* Use only PRECISION characters, from the left. */
2162 for (; precision
> 0; precision
--)
2164 int count
= u16_strmblen (arg_end
);
2169 if (!(result
== resultbuf
|| result
== NULL
))
2171 if (buf_malloced
!= NULL
)
2172 free (buf_malloced
);
2183 /* Use the entire string, and count the number of
2189 int count
= u16_strmblen (arg_end
);
2194 if (!(result
== resultbuf
|| result
== NULL
))
2196 if (buf_malloced
!= NULL
)
2197 free (buf_malloced
);
2208 /* Use the entire string. */
2209 arg_end
= arg
+ u16_strlen (arg
);
2210 /* The number of characters doesn't matter. */
2214 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2216 size_t n
= width
- characters
;
2217 ENSURE_ALLOCATION (xsum (length
, n
));
2218 DCHAR_SET (result
+ length
, ' ', n
);
2222 # if DCHAR_IS_UINT16_T
2224 size_t n
= arg_end
- arg
;
2225 ENSURE_ALLOCATION (xsum (length
, n
));
2226 DCHAR_CPY (result
+ length
, arg
, n
);
2231 DCHAR_T
*converted
= result
+ length
;
2232 size_t converted_len
= allocated
- length
;
2234 /* Convert from UTF-16 to locale encoding. */
2236 u16_conv_to_encoding (locale_charset (),
2237 iconveh_question_mark
,
2238 arg
, arg_end
- arg
, NULL
,
2239 converted
, &converted_len
);
2241 /* Convert from UTF-16 to UTF-8/UTF-32. */
2243 U16_TO_DCHAR (arg
, arg_end
- arg
,
2244 converted
, &converted_len
);
2246 if (converted
== NULL
)
2248 int saved_errno
= errno
;
2249 if (!(result
== resultbuf
|| result
== NULL
))
2251 if (buf_malloced
!= NULL
)
2252 free (buf_malloced
);
2254 errno
= saved_errno
;
2257 if (converted
!= result
+ length
)
2259 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2260 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2263 length
+= converted_len
;
2267 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2269 size_t n
= width
- characters
;
2270 ENSURE_ALLOCATION (xsum (length
, n
));
2271 DCHAR_SET (result
+ length
, ' ', n
);
2277 case TYPE_U32_STRING
:
2279 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
2280 const uint32_t *arg_end
;
2285 /* Use only PRECISION characters, from the left. */
2288 for (; precision
> 0; precision
--)
2290 int count
= u32_strmblen (arg_end
);
2295 if (!(result
== resultbuf
|| result
== NULL
))
2297 if (buf_malloced
!= NULL
)
2298 free (buf_malloced
);
2309 /* Use the entire string, and count the number of
2315 int count
= u32_strmblen (arg_end
);
2320 if (!(result
== resultbuf
|| result
== NULL
))
2322 if (buf_malloced
!= NULL
)
2323 free (buf_malloced
);
2334 /* Use the entire string. */
2335 arg_end
= arg
+ u32_strlen (arg
);
2336 /* The number of characters doesn't matter. */
2340 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2342 size_t n
= width
- characters
;
2343 ENSURE_ALLOCATION (xsum (length
, n
));
2344 DCHAR_SET (result
+ length
, ' ', n
);
2348 # if DCHAR_IS_UINT32_T
2350 size_t n
= arg_end
- arg
;
2351 ENSURE_ALLOCATION (xsum (length
, n
));
2352 DCHAR_CPY (result
+ length
, arg
, n
);
2357 DCHAR_T
*converted
= result
+ length
;
2358 size_t converted_len
= allocated
- length
;
2360 /* Convert from UTF-32 to locale encoding. */
2362 u32_conv_to_encoding (locale_charset (),
2363 iconveh_question_mark
,
2364 arg
, arg_end
- arg
, NULL
,
2365 converted
, &converted_len
);
2367 /* Convert from UTF-32 to UTF-8/UTF-16. */
2369 U32_TO_DCHAR (arg
, arg_end
- arg
,
2370 converted
, &converted_len
);
2372 if (converted
== NULL
)
2374 int saved_errno
= errno
;
2375 if (!(result
== resultbuf
|| result
== NULL
))
2377 if (buf_malloced
!= NULL
)
2378 free (buf_malloced
);
2380 errno
= saved_errno
;
2383 if (converted
!= result
+ length
)
2385 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2386 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2389 length
+= converted_len
;
2393 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2395 size_t n
= width
- characters
;
2396 ENSURE_ALLOCATION (xsum (length
, n
));
2397 DCHAR_SET (result
+ length
, ' ', n
);
2408 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2409 else if (dp
->conversion
== 's'
2410 # if WIDE_CHAR_VERSION
2411 && a
.arg
[dp
->arg_index
].type
!= TYPE_WIDE_STRING
2413 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_STRING
2417 /* The normal handling of the 's' directive below requires
2418 allocating a temporary buffer. The determination of its
2419 length (tmp_length), in the case when a precision is
2420 specified, below requires a conversion between a char[]
2421 string and a wchar_t[] wide string. It could be done, but
2422 we have no guarantee that the implementation of sprintf will
2423 use the exactly same algorithm. Without this guarantee, it
2424 is possible to have buffer overrun bugs. In order to avoid
2425 such bugs, we implement the entire processing of the 's'
2426 directive ourselves. */
2427 int flags
= dp
->flags
;
2435 if (dp
->width_start
!= dp
->width_end
)
2437 if (dp
->width_arg_index
!= ARG_NONE
)
2441 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2443 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2447 /* "A negative field width is taken as a '-' flag
2448 followed by a positive field width." */
2455 const FCHAR_T
*digitp
= dp
->width_start
;
2458 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2459 while (digitp
!= dp
->width_end
);
2466 if (dp
->precision_start
!= dp
->precision_end
)
2468 if (dp
->precision_arg_index
!= ARG_NONE
)
2472 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2474 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2475 /* "A negative precision is taken as if the precision
2485 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2488 while (digitp
!= dp
->precision_end
)
2489 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2494 # if WIDE_CHAR_VERSION
2495 /* %s in vasnwprintf. See the specification of fwprintf. */
2497 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
2498 const char *arg_end
;
2503 /* Use only as many bytes as needed to produce PRECISION
2504 wide characters, from the left. */
2507 memset (&state
, '\0', sizeof (mbstate_t));
2511 for (; precision
> 0; precision
--)
2515 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2517 count
= mblen (arg_end
, MB_CUR_MAX
);
2520 /* Found the terminating NUL. */
2524 /* Invalid or incomplete multibyte character. */
2525 if (!(result
== resultbuf
|| result
== NULL
))
2527 if (buf_malloced
!= NULL
)
2528 free (buf_malloced
);
2539 /* Use the entire string, and count the number of wide
2543 memset (&state
, '\0', sizeof (mbstate_t));
2551 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2553 count
= mblen (arg_end
, MB_CUR_MAX
);
2556 /* Found the terminating NUL. */
2560 /* Invalid or incomplete multibyte character. */
2561 if (!(result
== resultbuf
|| result
== NULL
))
2563 if (buf_malloced
!= NULL
)
2564 free (buf_malloced
);
2575 /* Use the entire string. */
2576 arg_end
= arg
+ strlen (arg
);
2577 /* The number of characters doesn't matter. */
2581 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2583 size_t n
= width
- characters
;
2584 ENSURE_ALLOCATION (xsum (length
, n
));
2585 DCHAR_SET (result
+ length
, ' ', n
);
2589 if (has_precision
|| has_width
)
2591 /* We know the number of wide characters in advance. */
2595 memset (&state
, '\0', sizeof (mbstate_t));
2597 ENSURE_ALLOCATION (xsum (length
, characters
));
2598 for (remaining
= characters
; remaining
> 0; remaining
--)
2603 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2605 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2608 /* mbrtowc not consistent with mbrlen, or mbtowc
2609 not consistent with mblen. */
2611 result
[length
++] = wc
;
2614 if (!(arg
== arg_end
))
2621 memset (&state
, '\0', sizeof (mbstate_t));
2623 while (arg
< arg_end
)
2628 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2630 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2633 /* mbrtowc not consistent with mbrlen, or mbtowc
2634 not consistent with mblen. */
2636 ENSURE_ALLOCATION (xsum (length
, 1));
2637 result
[length
++] = wc
;
2642 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2644 size_t n
= width
- characters
;
2645 ENSURE_ALLOCATION (xsum (length
, n
));
2646 DCHAR_SET (result
+ length
, ' ', n
);
2651 /* %ls in vasnprintf. See the specification of fprintf. */
2653 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
2654 const wchar_t *arg_end
;
2656 # if !DCHAR_IS_TCHAR
2657 /* This code assumes that TCHAR_T is 'char'. */
2658 verify (sizeof (TCHAR_T
) == 1);
2667 /* Use only as many wide characters as needed to produce
2668 at most PRECISION bytes, from the left. */
2669 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2671 memset (&state
, '\0', sizeof (mbstate_t));
2675 while (precision
> 0)
2677 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2681 /* Found the terminating null wide character. */
2683 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2684 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2686 count
= wctomb (cbuf
, *arg_end
);
2690 /* Cannot convert. */
2691 if (!(result
== resultbuf
|| result
== NULL
))
2693 if (buf_malloced
!= NULL
)
2694 free (buf_malloced
);
2699 if (precision
< count
)
2702 characters
+= count
;
2712 /* Use the entire string, and count the number of
2714 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2716 memset (&state
, '\0', sizeof (mbstate_t));
2722 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2726 /* Found the terminating null wide character. */
2728 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2729 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2731 count
= wctomb (cbuf
, *arg_end
);
2735 /* Cannot convert. */
2736 if (!(result
== resultbuf
|| result
== NULL
))
2738 if (buf_malloced
!= NULL
)
2739 free (buf_malloced
);
2745 characters
+= count
;
2751 /* Use the entire string. */
2752 arg_end
= arg
+ local_wcslen (arg
);
2753 /* The number of bytes doesn't matter. */
2758 # if !DCHAR_IS_TCHAR
2759 /* Convert the string into a piece of temporary memory. */
2760 tmpsrc
= (TCHAR_T
*) malloc (characters
* sizeof (TCHAR_T
));
2764 TCHAR_T
*tmpptr
= tmpsrc
;
2766 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2768 memset (&state
, '\0', sizeof (mbstate_t));
2770 for (remaining
= characters
; remaining
> 0; )
2772 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2777 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2778 count
= wcrtomb (cbuf
, *arg
, &state
);
2780 count
= wctomb (cbuf
, *arg
);
2783 /* Inconsistency. */
2785 memcpy (tmpptr
, cbuf
, count
);
2790 if (!(arg
== arg_end
))
2794 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2796 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2797 iconveh_question_mark
,
2803 int saved_errno
= errno
;
2805 if (!(result
== resultbuf
|| result
== NULL
))
2807 if (buf_malloced
!= NULL
)
2808 free (buf_malloced
);
2810 errno
= saved_errno
;
2818 # if ENABLE_UNISTDIO
2819 /* Outside POSIX, it's preferable to compare the width
2820 against the number of _characters_ of the converted
2822 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2824 /* The width is compared against the number of _bytes_
2825 of the converted value, says POSIX. */
2830 /* w doesn't matter. */
2833 if (w
< width
&& !(dp
->flags
& FLAG_LEFT
))
2835 size_t n
= width
- w
;
2836 ENSURE_ALLOCATION (xsum (length
, n
));
2837 DCHAR_SET (result
+ length
, ' ', n
);
2842 if (has_precision
|| has_width
)
2844 /* We know the number of bytes in advance. */
2846 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2848 memset (&state
, '\0', sizeof (mbstate_t));
2850 ENSURE_ALLOCATION (xsum (length
, characters
));
2851 for (remaining
= characters
; remaining
> 0; )
2853 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2858 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2859 count
= wcrtomb (cbuf
, *arg
, &state
);
2861 count
= wctomb (cbuf
, *arg
);
2864 /* Inconsistency. */
2866 memcpy (result
+ length
, cbuf
, count
);
2871 if (!(arg
== arg_end
))
2876 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2878 memset (&state
, '\0', sizeof (mbstate_t));
2880 while (arg
< arg_end
)
2882 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2887 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2888 count
= wcrtomb (cbuf
, *arg
, &state
);
2890 count
= wctomb (cbuf
, *arg
);
2894 /* Cannot convert. */
2895 if (!(result
== resultbuf
|| result
== NULL
))
2897 if (buf_malloced
!= NULL
)
2898 free (buf_malloced
);
2903 ENSURE_ALLOCATION (xsum (length
, count
));
2904 memcpy (result
+ length
, cbuf
, count
);
2910 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
2911 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
2913 length
+= tmpdst_len
;
2916 if (w
< width
&& (dp
->flags
& FLAG_LEFT
))
2918 size_t n
= width
- w
;
2919 ENSURE_ALLOCATION (xsum (length
, n
));
2920 DCHAR_SET (result
+ length
, ' ', n
);
2927 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2928 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
2929 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2931 # if NEED_PRINTF_DOUBLE
2932 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2934 # if NEED_PRINTF_LONG_DOUBLE
2935 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2941 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2942 int flags
= dp
->flags
;
2948 DCHAR_T tmpbuf
[700];
2954 if (dp
->width_start
!= dp
->width_end
)
2956 if (dp
->width_arg_index
!= ARG_NONE
)
2960 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2962 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2966 /* "A negative field width is taken as a '-' flag
2967 followed by a positive field width." */
2974 const FCHAR_T
*digitp
= dp
->width_start
;
2977 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2978 while (digitp
!= dp
->width_end
);
2984 if (dp
->precision_start
!= dp
->precision_end
)
2986 if (dp
->precision_arg_index
!= ARG_NONE
)
2990 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2992 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2993 /* "A negative precision is taken as if the precision
3003 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3006 while (digitp
!= dp
->precision_end
)
3007 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3012 /* Allocate a temporary buffer of sufficient size. */
3013 if (type
== TYPE_LONGDOUBLE
)
3015 (unsigned int) ((LDBL_DIG
+ 1)
3016 * 0.831 /* decimal -> hexadecimal */
3018 + 1; /* turn floor into ceil */
3021 (unsigned int) ((DBL_DIG
+ 1)
3022 * 0.831 /* decimal -> hexadecimal */
3024 + 1; /* turn floor into ceil */
3025 if (tmp_length
< precision
)
3026 tmp_length
= precision
;
3027 /* Account for sign, decimal point etc. */
3028 tmp_length
= xsum (tmp_length
, 12);
3030 if (tmp_length
< width
)
3033 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3035 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3039 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3041 if (size_overflow_p (tmp_memsize
))
3042 /* Overflow, would lead to out of memory. */
3044 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3046 /* Out of memory. */
3052 if (type
== TYPE_LONGDOUBLE
)
3054 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3055 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3059 if (dp
->conversion
== 'A')
3061 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3065 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3071 DECL_LONG_DOUBLE_ROUNDING
3073 BEGIN_LONG_DOUBLE_ROUNDING ();
3075 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3083 else if (flags
& FLAG_SHOWSIGN
)
3085 else if (flags
& FLAG_SPACE
)
3088 if (arg
> 0.0L && arg
+ arg
== arg
)
3090 if (dp
->conversion
== 'A')
3092 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3096 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3102 long double mantissa
;
3105 mantissa
= printf_frexpl (arg
, &exponent
);
3113 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
3115 /* Round the mantissa. */
3116 long double tail
= mantissa
;
3119 for (q
= precision
; ; q
--)
3121 int digit
= (int) tail
;
3125 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
3134 for (q
= precision
; q
> 0; q
--)
3140 *p
++ = dp
->conversion
- 'A' + 'X';
3145 digit
= (int) mantissa
;
3148 if ((flags
& FLAG_ALT
)
3149 || mantissa
> 0.0L || precision
> 0)
3151 *p
++ = decimal_point_char ();
3152 /* This loop terminates because we assume
3153 that FLT_RADIX is a power of 2. */
3154 while (mantissa
> 0.0L)
3157 digit
= (int) mantissa
;
3162 : dp
->conversion
- 10);
3166 while (precision
> 0)
3173 *p
++ = dp
->conversion
- 'A' + 'P';
3174 # if WIDE_CHAR_VERSION
3176 static const wchar_t decimal_format
[] =
3177 { '%', '+', 'd', '\0' };
3178 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3183 if (sizeof (DCHAR_T
) == 1)
3185 sprintf ((char *) p
, "%+d", exponent
);
3193 sprintf (expbuf
, "%+d", exponent
);
3194 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3200 END_LONG_DOUBLE_ROUNDING ();
3208 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3209 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3213 if (dp
->conversion
== 'A')
3215 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3219 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3226 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3234 else if (flags
& FLAG_SHOWSIGN
)
3236 else if (flags
& FLAG_SPACE
)
3239 if (arg
> 0.0 && arg
+ arg
== arg
)
3241 if (dp
->conversion
== 'A')
3243 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3247 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3256 mantissa
= printf_frexp (arg
, &exponent
);
3264 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
3266 /* Round the mantissa. */
3267 double tail
= mantissa
;
3270 for (q
= precision
; ; q
--)
3272 int digit
= (int) tail
;
3276 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
3285 for (q
= precision
; q
> 0; q
--)
3291 *p
++ = dp
->conversion
- 'A' + 'X';
3296 digit
= (int) mantissa
;
3299 if ((flags
& FLAG_ALT
)
3300 || mantissa
> 0.0 || precision
> 0)
3302 *p
++ = decimal_point_char ();
3303 /* This loop terminates because we assume
3304 that FLT_RADIX is a power of 2. */
3305 while (mantissa
> 0.0)
3308 digit
= (int) mantissa
;
3313 : dp
->conversion
- 10);
3317 while (precision
> 0)
3324 *p
++ = dp
->conversion
- 'A' + 'P';
3325 # if WIDE_CHAR_VERSION
3327 static const wchar_t decimal_format
[] =
3328 { '%', '+', 'd', '\0' };
3329 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3334 if (sizeof (DCHAR_T
) == 1)
3336 sprintf ((char *) p
, "%+d", exponent
);
3344 sprintf (expbuf
, "%+d", exponent
);
3345 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3356 /* The generated string now extends from tmp to p, with the
3357 zero padding insertion point being at pad_ptr. */
3362 size_t pad
= width
- count
;
3363 DCHAR_T
*end
= p
+ pad
;
3365 if (flags
& FLAG_LEFT
)
3367 /* Pad with spaces on the right. */
3368 for (; pad
> 0; pad
--)
3371 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3373 /* Pad with zeroes. */
3378 for (; pad
> 0; pad
--)
3383 /* Pad with spaces on the left. */
3388 for (; pad
> 0; pad
--)
3397 if (count
>= tmp_length
)
3398 /* tmp_length was incorrectly calculated - fix the
3402 /* Make room for the result. */
3403 if (count
>= allocated
- length
)
3405 size_t n
= xsum (length
, count
);
3407 ENSURE_ALLOCATION (n
);
3410 /* Append the result. */
3411 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3417 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3418 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
3419 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
3420 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
3421 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
3423 # if NEED_PRINTF_DOUBLE
3424 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3425 # elif NEED_PRINTF_INFINITE_DOUBLE
3426 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3427 /* The systems (mingw) which produce wrong output
3428 for Inf, -Inf, and NaN also do so for -0.0.
3429 Therefore we treat this case here as well. */
3430 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
3432 # if NEED_PRINTF_LONG_DOUBLE
3433 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3434 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3435 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3436 /* Some systems produce wrong output for Inf,
3437 -Inf, and NaN. Some systems in this category
3438 (IRIX 5.3) also do so for -0.0. Therefore we
3439 treat this case here as well. */
3440 && is_infinite_or_zerol (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
3444 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3445 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3447 int flags
= dp
->flags
;
3453 DCHAR_T tmpbuf
[700];
3459 if (dp
->width_start
!= dp
->width_end
)
3461 if (dp
->width_arg_index
!= ARG_NONE
)
3465 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3467 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3471 /* "A negative field width is taken as a '-' flag
3472 followed by a positive field width." */
3479 const FCHAR_T
*digitp
= dp
->width_start
;
3482 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3483 while (digitp
!= dp
->width_end
);
3489 if (dp
->precision_start
!= dp
->precision_end
)
3491 if (dp
->precision_arg_index
!= ARG_NONE
)
3495 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3497 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3498 /* "A negative precision is taken as if the precision
3508 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3511 while (digitp
!= dp
->precision_end
)
3512 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3517 /* POSIX specifies the default precision to be 6 for %f, %F,
3518 %e, %E, but not for %g, %G. Implementations appear to use
3519 the same default precision also for %g, %G. But for %a, %A,
3520 the default precision is 0. */
3522 if (!(dp
->conversion
== 'a' || dp
->conversion
== 'A'))
3525 /* Allocate a temporary buffer of sufficient size. */
3526 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3527 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
3528 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3529 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
3530 # elif NEED_PRINTF_LONG_DOUBLE
3531 tmp_length
= LDBL_DIG
+ 1;
3532 # elif NEED_PRINTF_DOUBLE
3533 tmp_length
= DBL_DIG
+ 1;
3537 if (tmp_length
< precision
)
3538 tmp_length
= precision
;
3539 # if NEED_PRINTF_LONG_DOUBLE
3540 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3541 if (type
== TYPE_LONGDOUBLE
)
3543 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3545 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3546 if (!(isnanl (arg
) || arg
+ arg
== arg
))
3548 /* arg is finite and nonzero. */
3549 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
3550 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3551 tmp_length
= exponent
+ precision
;
3555 # if NEED_PRINTF_DOUBLE
3556 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3557 if (type
== TYPE_DOUBLE
)
3559 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3561 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3562 if (!(isnand (arg
) || arg
+ arg
== arg
))
3564 /* arg is finite and nonzero. */
3565 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
3566 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3567 tmp_length
= exponent
+ precision
;
3571 /* Account for sign, decimal point etc. */
3572 tmp_length
= xsum (tmp_length
, 12);
3574 if (tmp_length
< width
)
3577 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3579 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3583 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3585 if (size_overflow_p (tmp_memsize
))
3586 /* Overflow, would lead to out of memory. */
3588 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3590 /* Out of memory. */
3597 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3598 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3599 if (type
== TYPE_LONGDOUBLE
)
3602 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3606 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3608 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3612 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3618 DECL_LONG_DOUBLE_ROUNDING
3620 BEGIN_LONG_DOUBLE_ROUNDING ();
3622 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3630 else if (flags
& FLAG_SHOWSIGN
)
3632 else if (flags
& FLAG_SPACE
)
3635 if (arg
> 0.0L && arg
+ arg
== arg
)
3637 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3639 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3643 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3648 # if NEED_PRINTF_LONG_DOUBLE
3651 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3657 scale10_round_decimal_long_double (arg
, precision
);
3660 END_LONG_DOUBLE_ROUNDING ();
3663 ndigits
= strlen (digits
);
3665 if (ndigits
> precision
)
3669 *p
++ = digits
[ndigits
];
3671 while (ndigits
> precision
);
3674 /* Here ndigits <= precision. */
3675 if ((flags
& FLAG_ALT
) || precision
> 0)
3677 *p
++ = decimal_point_char ();
3678 for (; precision
> ndigits
; precision
--)
3683 *p
++ = digits
[ndigits
];
3689 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3697 if ((flags
& FLAG_ALT
) || precision
> 0)
3699 *p
++ = decimal_point_char ();
3700 for (; precision
> 0; precision
--)
3711 exponent
= floorlog10l (arg
);
3716 scale10_round_decimal_long_double (arg
,
3717 (int)precision
- exponent
);
3720 END_LONG_DOUBLE_ROUNDING ();
3723 ndigits
= strlen (digits
);
3725 if (ndigits
== precision
+ 1)
3727 if (ndigits
< precision
3728 || ndigits
> precision
+ 2)
3729 /* The exponent was not guessed
3730 precisely enough. */
3733 /* None of two values of exponent is
3734 the right one. Prevent an endless
3738 if (ndigits
== precision
)
3744 /* Here ndigits = precision+1. */
3745 if (is_borderline (digits
, precision
))
3747 /* Maybe the exponent guess was too high
3748 and a smaller exponent can be reached
3749 by turning a 10...0 into 9...9x. */
3751 scale10_round_decimal_long_double (arg
,
3752 (int)precision
- exponent
+ 1);
3753 if (digits2
== NULL
)
3756 END_LONG_DOUBLE_ROUNDING ();
3759 if (strlen (digits2
) == precision
+ 1)
3768 /* Here ndigits = precision+1. */
3770 *p
++ = digits
[--ndigits
];
3771 if ((flags
& FLAG_ALT
) || precision
> 0)
3773 *p
++ = decimal_point_char ();
3777 *p
++ = digits
[ndigits
];
3784 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3785 # if WIDE_CHAR_VERSION
3787 static const wchar_t decimal_format
[] =
3788 { '%', '+', '.', '2', 'd', '\0' };
3789 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3794 if (sizeof (DCHAR_T
) == 1)
3796 sprintf ((char *) p
, "%+.2d", exponent
);
3804 sprintf (expbuf
, "%+.2d", exponent
);
3805 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3810 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3814 /* precision >= 1. */
3817 /* The exponent is 0, >= -4, < precision.
3818 Use fixed-point notation. */
3820 size_t ndigits
= precision
;
3821 /* Number of trailing zeroes that have to be
3824 (flags
& FLAG_ALT
? 0 : precision
- 1);
3828 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3830 *p
++ = decimal_point_char ();
3831 while (ndigits
> nzeroes
)
3847 exponent
= floorlog10l (arg
);
3852 scale10_round_decimal_long_double (arg
,
3853 (int)(precision
- 1) - exponent
);
3856 END_LONG_DOUBLE_ROUNDING ();
3859 ndigits
= strlen (digits
);
3861 if (ndigits
== precision
)
3863 if (ndigits
< precision
- 1
3864 || ndigits
> precision
+ 1)
3865 /* The exponent was not guessed
3866 precisely enough. */
3869 /* None of two values of exponent is
3870 the right one. Prevent an endless
3874 if (ndigits
< precision
)
3880 /* Here ndigits = precision. */
3881 if (is_borderline (digits
, precision
- 1))
3883 /* Maybe the exponent guess was too high
3884 and a smaller exponent can be reached
3885 by turning a 10...0 into 9...9x. */
3887 scale10_round_decimal_long_double (arg
,
3888 (int)(precision
- 1) - exponent
+ 1);
3889 if (digits2
== NULL
)
3892 END_LONG_DOUBLE_ROUNDING ();
3895 if (strlen (digits2
) == precision
)
3904 /* Here ndigits = precision. */
3906 /* Determine the number of trailing zeroes
3907 that have to be dropped. */
3909 if ((flags
& FLAG_ALT
) == 0)
3910 while (nzeroes
< ndigits
3911 && digits
[nzeroes
] == '0')
3914 /* The exponent is now determined. */
3916 && exponent
< (long)precision
)
3918 /* Fixed-point notation:
3919 max(exponent,0)+1 digits, then the
3920 decimal point, then the remaining
3921 digits without trailing zeroes. */
3924 size_t ecount
= exponent
+ 1;
3925 /* Note: count <= precision = ndigits. */
3926 for (; ecount
> 0; ecount
--)
3927 *p
++ = digits
[--ndigits
];
3928 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3930 *p
++ = decimal_point_char ();
3931 while (ndigits
> nzeroes
)
3934 *p
++ = digits
[ndigits
];
3940 size_t ecount
= -exponent
- 1;
3942 *p
++ = decimal_point_char ();
3943 for (; ecount
> 0; ecount
--)
3945 while (ndigits
> nzeroes
)
3948 *p
++ = digits
[ndigits
];
3954 /* Exponential notation. */
3955 *p
++ = digits
[--ndigits
];
3956 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3958 *p
++ = decimal_point_char ();
3959 while (ndigits
> nzeroes
)
3962 *p
++ = digits
[ndigits
];
3965 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3966 # if WIDE_CHAR_VERSION
3968 static const wchar_t decimal_format
[] =
3969 { '%', '+', '.', '2', 'd', '\0' };
3970 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3975 if (sizeof (DCHAR_T
) == 1)
3977 sprintf ((char *) p
, "%+.2d", exponent
);
3985 sprintf (expbuf
, "%+.2d", exponent
);
3986 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3998 /* arg is finite. */
4004 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4007 if ((flags
& FLAG_ALT
) || precision
> 0)
4009 *p
++ = decimal_point_char ();
4010 for (; precision
> 0; precision
--)
4014 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4017 if ((flags
& FLAG_ALT
) || precision
> 0)
4019 *p
++ = decimal_point_char ();
4020 for (; precision
> 0; precision
--)
4023 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4028 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4031 if (flags
& FLAG_ALT
)
4034 (precision
> 0 ? precision
- 1 : 0);
4035 *p
++ = decimal_point_char ();
4036 for (; ndigits
> 0; --ndigits
)
4040 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
4043 *p
++ = dp
->conversion
- 'A' + 'X';
4046 if ((flags
& FLAG_ALT
) || precision
> 0)
4048 *p
++ = decimal_point_char ();
4049 for (; precision
> 0; precision
--)
4052 *p
++ = dp
->conversion
- 'A' + 'P';
4061 END_LONG_DOUBLE_ROUNDING ();
4064 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4068 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4070 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4074 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4076 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
4080 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
4087 if (signbit (arg
)) /* arg < 0.0 or negative zero */
4095 else if (flags
& FLAG_SHOWSIGN
)
4097 else if (flags
& FLAG_SPACE
)
4100 if (arg
> 0.0 && arg
+ arg
== arg
)
4102 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4104 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
4108 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
4113 # if NEED_PRINTF_DOUBLE
4116 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4122 scale10_round_decimal_double (arg
, precision
);
4125 ndigits
= strlen (digits
);
4127 if (ndigits
> precision
)
4131 *p
++ = digits
[ndigits
];
4133 while (ndigits
> precision
);
4136 /* Here ndigits <= precision. */
4137 if ((flags
& FLAG_ALT
) || precision
> 0)
4139 *p
++ = decimal_point_char ();
4140 for (; precision
> ndigits
; precision
--)
4145 *p
++ = digits
[ndigits
];
4151 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4159 if ((flags
& FLAG_ALT
) || precision
> 0)
4161 *p
++ = decimal_point_char ();
4162 for (; precision
> 0; precision
--)
4173 exponent
= floorlog10 (arg
);
4178 scale10_round_decimal_double (arg
,
4179 (int)precision
- exponent
);
4182 ndigits
= strlen (digits
);
4184 if (ndigits
== precision
+ 1)
4186 if (ndigits
< precision
4187 || ndigits
> precision
+ 2)
4188 /* The exponent was not guessed
4189 precisely enough. */
4192 /* None of two values of exponent is
4193 the right one. Prevent an endless
4197 if (ndigits
== precision
)
4203 /* Here ndigits = precision+1. */
4204 if (is_borderline (digits
, precision
))
4206 /* Maybe the exponent guess was too high
4207 and a smaller exponent can be reached
4208 by turning a 10...0 into 9...9x. */
4210 scale10_round_decimal_double (arg
,
4211 (int)precision
- exponent
+ 1);
4212 if (digits2
== NULL
)
4217 if (strlen (digits2
) == precision
+ 1)
4226 /* Here ndigits = precision+1. */
4228 *p
++ = digits
[--ndigits
];
4229 if ((flags
& FLAG_ALT
) || precision
> 0)
4231 *p
++ = decimal_point_char ();
4235 *p
++ = digits
[ndigits
];
4242 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4243 # if WIDE_CHAR_VERSION
4245 static const wchar_t decimal_format
[] =
4246 /* Produce the same number of exponent digits
4247 as the native printf implementation. */
4248 # if defined _WIN32 && ! defined __CYGWIN__
4249 { '%', '+', '.', '3', 'd', '\0' };
4251 { '%', '+', '.', '2', 'd', '\0' };
4253 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4259 static const char decimal_format
[] =
4260 /* Produce the same number of exponent digits
4261 as the native printf implementation. */
4262 # if defined _WIN32 && ! defined __CYGWIN__
4267 if (sizeof (DCHAR_T
) == 1)
4269 sprintf ((char *) p
, decimal_format
, exponent
);
4277 sprintf (expbuf
, decimal_format
, exponent
);
4278 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4284 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4288 /* precision >= 1. */
4291 /* The exponent is 0, >= -4, < precision.
4292 Use fixed-point notation. */
4294 size_t ndigits
= precision
;
4295 /* Number of trailing zeroes that have to be
4298 (flags
& FLAG_ALT
? 0 : precision
- 1);
4302 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4304 *p
++ = decimal_point_char ();
4305 while (ndigits
> nzeroes
)
4321 exponent
= floorlog10 (arg
);
4326 scale10_round_decimal_double (arg
,
4327 (int)(precision
- 1) - exponent
);
4330 ndigits
= strlen (digits
);
4332 if (ndigits
== precision
)
4334 if (ndigits
< precision
- 1
4335 || ndigits
> precision
+ 1)
4336 /* The exponent was not guessed
4337 precisely enough. */
4340 /* None of two values of exponent is
4341 the right one. Prevent an endless
4345 if (ndigits
< precision
)
4351 /* Here ndigits = precision. */
4352 if (is_borderline (digits
, precision
- 1))
4354 /* Maybe the exponent guess was too high
4355 and a smaller exponent can be reached
4356 by turning a 10...0 into 9...9x. */
4358 scale10_round_decimal_double (arg
,
4359 (int)(precision
- 1) - exponent
+ 1);
4360 if (digits2
== NULL
)
4365 if (strlen (digits2
) == precision
)
4374 /* Here ndigits = precision. */
4376 /* Determine the number of trailing zeroes
4377 that have to be dropped. */
4379 if ((flags
& FLAG_ALT
) == 0)
4380 while (nzeroes
< ndigits
4381 && digits
[nzeroes
] == '0')
4384 /* The exponent is now determined. */
4386 && exponent
< (long)precision
)
4388 /* Fixed-point notation:
4389 max(exponent,0)+1 digits, then the
4390 decimal point, then the remaining
4391 digits without trailing zeroes. */
4394 size_t ecount
= exponent
+ 1;
4395 /* Note: ecount <= precision = ndigits. */
4396 for (; ecount
> 0; ecount
--)
4397 *p
++ = digits
[--ndigits
];
4398 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4400 *p
++ = decimal_point_char ();
4401 while (ndigits
> nzeroes
)
4404 *p
++ = digits
[ndigits
];
4410 size_t ecount
= -exponent
- 1;
4412 *p
++ = decimal_point_char ();
4413 for (; ecount
> 0; ecount
--)
4415 while (ndigits
> nzeroes
)
4418 *p
++ = digits
[ndigits
];
4424 /* Exponential notation. */
4425 *p
++ = digits
[--ndigits
];
4426 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4428 *p
++ = decimal_point_char ();
4429 while (ndigits
> nzeroes
)
4432 *p
++ = digits
[ndigits
];
4435 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4436 # if WIDE_CHAR_VERSION
4438 static const wchar_t decimal_format
[] =
4439 /* Produce the same number of exponent digits
4440 as the native printf implementation. */
4441 # if defined _WIN32 && ! defined __CYGWIN__
4442 { '%', '+', '.', '3', 'd', '\0' };
4444 { '%', '+', '.', '2', 'd', '\0' };
4446 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4452 static const char decimal_format
[] =
4453 /* Produce the same number of exponent digits
4454 as the native printf implementation. */
4455 # if defined _WIN32 && ! defined __CYGWIN__
4460 if (sizeof (DCHAR_T
) == 1)
4462 sprintf ((char *) p
, decimal_format
, exponent
);
4470 sprintf (expbuf
, decimal_format
, exponent
);
4471 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4484 /* arg is finite. */
4490 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4493 if ((flags
& FLAG_ALT
) || precision
> 0)
4495 *p
++ = decimal_point_char ();
4496 for (; precision
> 0; precision
--)
4500 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4503 if ((flags
& FLAG_ALT
) || precision
> 0)
4505 *p
++ = decimal_point_char ();
4506 for (; precision
> 0; precision
--)
4509 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4511 /* Produce the same number of exponent digits as
4512 the native printf implementation. */
4513 # if defined _WIN32 && ! defined __CYGWIN__
4519 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4522 if (flags
& FLAG_ALT
)
4525 (precision
> 0 ? precision
- 1 : 0);
4526 *p
++ = decimal_point_char ();
4527 for (; ndigits
> 0; --ndigits
)
4539 /* The generated string now extends from tmp to p, with the
4540 zero padding insertion point being at pad_ptr. */
4545 size_t pad
= width
- count
;
4546 DCHAR_T
*end
= p
+ pad
;
4548 if (flags
& FLAG_LEFT
)
4550 /* Pad with spaces on the right. */
4551 for (; pad
> 0; pad
--)
4554 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4556 /* Pad with zeroes. */
4561 for (; pad
> 0; pad
--)
4566 /* Pad with spaces on the left. */
4571 for (; pad
> 0; pad
--)
4580 if (count
>= tmp_length
)
4581 /* tmp_length was incorrectly calculated - fix the
4585 /* Make room for the result. */
4586 if (count
>= allocated
- length
)
4588 size_t n
= xsum (length
, count
);
4590 ENSURE_ALLOCATION (n
);
4593 /* Append the result. */
4594 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4602 arg_type type
= a
.arg
[dp
->arg_index
].type
;
4603 int flags
= dp
->flags
;
4604 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4607 #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
4610 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4614 #if NEED_PRINTF_UNBOUNDED_PRECISION
4617 # define prec_ourselves 0
4619 #if NEED_PRINTF_FLAG_LEFTADJUST
4620 # define pad_ourselves 1
4621 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4624 # define pad_ourselves 0
4627 unsigned int prefix_count
;
4628 int prefixes
[2] IF_LINT (= { 0 });
4632 TCHAR_T tmpbuf
[700];
4636 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4639 #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
4641 if (dp
->width_start
!= dp
->width_end
)
4643 if (dp
->width_arg_index
!= ARG_NONE
)
4647 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4649 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
4653 /* "A negative field width is taken as a '-' flag
4654 followed by a positive field width." */
4661 const FCHAR_T
*digitp
= dp
->width_start
;
4664 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
4665 while (digitp
!= dp
->width_end
);
4667 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4673 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4676 if (dp
->precision_start
!= dp
->precision_end
)
4678 if (dp
->precision_arg_index
!= ARG_NONE
)
4682 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4684 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4685 /* "A negative precision is taken as if the precision
4695 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
4698 while (digitp
!= dp
->precision_end
)
4699 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
4705 /* Decide whether to handle the precision ourselves. */
4706 #if NEED_PRINTF_UNBOUNDED_PRECISION
4707 switch (dp
->conversion
)
4709 case 'd': case 'i': case 'u':
4711 case 'x': case 'X': case 'p':
4712 prec_ourselves
= has_precision
&& (precision
> 0);
4720 /* Decide whether to perform the padding ourselves. */
4721 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4722 switch (dp
->conversion
)
4724 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4725 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4726 to perform the padding after this conversion. Functions
4727 with unistdio extensions perform the padding based on
4728 character count rather than element count. */
4731 # if NEED_PRINTF_FLAG_ZERO
4732 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4738 pad_ourselves
= prec_ourselves
;
4744 /* Allocate a temporary buffer of sufficient size for calling
4747 MAX_ROOM_NEEDED (&a
, dp
->arg_index
, dp
->conversion
, type
,
4748 flags
, width
, has_precision
, precision
,
4751 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
4755 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
4757 if (size_overflow_p (tmp_memsize
))
4758 /* Overflow, would lead to out of memory. */
4760 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
4762 /* Out of memory. */
4767 /* Construct the format string for calling snprintf or
4771 #if NEED_PRINTF_FLAG_GROUPING
4772 /* The underlying implementation doesn't support the ' flag.
4773 Produce no grouping characters in this case; this is
4774 acceptable because the grouping is locale dependent. */
4776 if (flags
& FLAG_GROUP
)
4779 if (flags
& FLAG_LEFT
)
4781 if (flags
& FLAG_SHOWSIGN
)
4783 if (flags
& FLAG_SPACE
)
4785 if (flags
& FLAG_ALT
)
4787 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4788 if (flags
& FLAG_LOCALIZED
)
4793 if (flags
& FLAG_ZERO
)
4795 if (dp
->width_start
!= dp
->width_end
)
4797 size_t n
= dp
->width_end
- dp
->width_start
;
4798 /* The width specification is known to consist only
4799 of standard ASCII characters. */
4800 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4802 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
4807 const FCHAR_T
*mp
= dp
->width_start
;
4814 if (!prec_ourselves
)
4816 if (dp
->precision_start
!= dp
->precision_end
)
4818 size_t n
= dp
->precision_end
- dp
->precision_start
;
4819 /* The precision specification is known to consist only
4820 of standard ASCII characters. */
4821 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4823 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
4828 const FCHAR_T
*mp
= dp
->precision_start
;
4838 #if HAVE_LONG_LONG_INT
4839 case TYPE_LONGLONGINT
:
4840 case TYPE_ULONGLONGINT
:
4841 # if defined _WIN32 && ! defined __CYGWIN__
4854 case TYPE_WIDE_CHAR
:
4857 case TYPE_WIDE_STRING
:
4861 case TYPE_LONGDOUBLE
:
4867 #if NEED_PRINTF_DIRECTIVE_F
4868 if (dp
->conversion
== 'F')
4872 *fbp
= dp
->conversion
;
4874 # if ! (((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) \
4875 && !defined __UCLIBC__) \
4876 || (defined __APPLE__ && defined __MACH__) \
4877 || (defined _WIN32 && ! defined __CYGWIN__))
4882 /* On glibc2 systems from glibc >= 2.3 - probably also older
4883 ones - we know that snprintf's return value conforms to
4884 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
4885 gl_SNPRINTF_TRUNCATION_C99 pass.
4886 Therefore we can avoid using %n in this situation.
4887 On glibc2 systems from 2004-10-18 or newer, the use of %n
4888 in format strings in writable memory may crash the program
4889 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4890 in this situation. */
4891 /* On Mac OS X 10.3 or newer, we know that snprintf's return
4892 value conforms to ISO C 99: the tests gl_SNPRINTF_RETVAL_C99
4893 and gl_SNPRINTF_TRUNCATION_C99 pass.
4894 Therefore we can avoid using %n in this situation.
4895 On Mac OS X 10.13 or newer, the use of %n in format strings
4896 in writable memory by default crashes the program, so we
4897 should avoid it in this situation. */
4898 /* On native Windows systems (such as mingw), we can avoid using
4900 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4901 snprintf does not write more than the specified number
4902 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4903 '4', '5', '6' into buf, not '4', '5', '\0'.)
4904 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4905 allows us to recognize the case of an insufficient
4906 buffer size: it returns -1 in this case.
4907 On native Windows systems (such as mingw) where the OS is
4908 Windows Vista, the use of %n in format strings by default
4909 crashes the program. See
4910 <https://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4911 <https://msdn.microsoft.com/en-us/library/ms175782.aspx>
4912 So we should avoid %n in this situation. */
4919 /* Construct the arguments for calling snprintf or sprintf. */
4921 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
4923 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4925 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
4927 if (!prec_ourselves
&& dp
->precision_arg_index
!= ARG_NONE
)
4929 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4931 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4935 /* The SNPRINTF result is appended after result[0..length].
4936 The latter is an array of DCHAR_T; SNPRINTF appends an
4937 array of TCHAR_T to it. This is possible because
4938 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4939 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4940 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4941 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4942 where an snprintf() with maxlen==1 acts like sprintf(). */
4943 ENSURE_ALLOCATION (xsum (length
,
4944 (2 + TCHARS_PER_DCHAR
- 1)
4945 / TCHARS_PER_DCHAR
));
4946 /* Prepare checking whether snprintf returns the count
4948 *(TCHAR_T
*) (result
+ length
) = '\0';
4959 size_t maxlen
= allocated
- length
;
4960 /* SNPRINTF can fail if its second argument is
4962 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
4963 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
4964 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
4965 # define SNPRINTF_BUF(arg) \
4966 switch (prefix_count) \
4969 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4974 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4976 prefixes[0], arg, &count); \
4979 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4981 prefixes[0], prefixes[1], arg, \
4988 # define SNPRINTF_BUF(arg) \
4989 switch (prefix_count) \
4992 count = sprintf (tmp, buf, arg); \
4995 count = sprintf (tmp, buf, prefixes[0], arg); \
4998 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
5011 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
5017 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
5023 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
5029 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
5035 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
5041 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
5047 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
5053 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
5057 #if HAVE_LONG_LONG_INT
5058 case TYPE_LONGLONGINT
:
5060 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
5064 case TYPE_ULONGLONGINT
:
5066 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
5073 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
5077 case TYPE_LONGDOUBLE
:
5079 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
5085 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
5090 case TYPE_WIDE_CHAR
:
5092 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
5099 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
5104 case TYPE_WIDE_STRING
:
5106 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
5113 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
5122 /* Portability: Not all implementations of snprintf()
5123 are ISO C 99 compliant. Determine the number of
5124 bytes that snprintf() has produced or would have
5128 /* Verify that snprintf() has NUL-terminated its
5131 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
5133 /* Portability hack. */
5134 if (retcount
> count
)
5139 /* snprintf() doesn't understand the '%n'
5143 /* Don't use the '%n' directive; instead, look
5144 at the snprintf() return value. */
5150 /* Look at the snprintf() return value. */
5153 # if !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
5154 /* HP-UX 10.20 snprintf() is doubly deficient:
5155 It doesn't understand the '%n' directive,
5156 *and* it returns -1 (rather than the length
5157 that would have been required) when the
5158 buffer is too small.
5159 But a failure at this point can also come
5160 from other reasons than a too small buffer,
5161 such as an invalid wide string argument to
5162 the %ls directive, or possibly an invalid
5163 floating-point argument. */
5165 MAX_ROOM_NEEDED (&a
, dp
->arg_index
,
5166 dp
->conversion
, type
, flags
,
5169 precision
, pad_ourselves
);
5171 if (maxlen
< tmp_length
)
5173 /* Make more room. But try to do through
5174 this reallocation only once. */
5175 size_t bigger_need
=
5178 TCHARS_PER_DCHAR
- 1)
5179 / TCHARS_PER_DCHAR
);
5180 /* And always grow proportionally.
5181 (There may be several arguments, each
5182 needing a little more room than the
5184 size_t bigger_need2
=
5185 xsum (xtimes (allocated
, 2), 12);
5186 if (bigger_need
< bigger_need2
)
5187 bigger_need
= bigger_need2
;
5188 ENSURE_ALLOCATION (bigger_need
);
5199 /* Attempt to handle failure. */
5202 /* SNPRINTF or sprintf failed. Save and use the errno
5203 that it has set, if any. */
5204 int saved_errno
= errno
;
5205 if (saved_errno
== 0)
5207 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5208 saved_errno
= EILSEQ
;
5210 saved_errno
= EINVAL
;
5213 if (!(result
== resultbuf
|| result
== NULL
))
5215 if (buf_malloced
!= NULL
)
5216 free (buf_malloced
);
5219 errno
= saved_errno
;
5224 /* Handle overflow of the allocated buffer.
5225 If such an overflow occurs, a C99 compliant snprintf()
5226 returns a count >= maxlen. However, a non-compliant
5227 snprintf() function returns only count = maxlen - 1. To
5228 cover both cases, test whether count >= maxlen - 1. */
5229 if ((unsigned int) count
+ 1 >= maxlen
)
5231 /* If maxlen already has attained its allowed maximum,
5232 allocating more memory will not increase maxlen.
5233 Instead of looping, bail out. */
5234 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
5238 /* Need at least (count + 1) * sizeof (TCHAR_T)
5239 bytes. (The +1 is for the trailing NUL.)
5240 But ask for (count + 2) * sizeof (TCHAR_T)
5241 bytes, so that in the next round, we likely get
5242 maxlen > (unsigned int) count + 1
5243 and so we don't get here again.
5244 And allocate proportionally, to avoid looping
5245 eternally if snprintf() reports a too small
5249 ((unsigned int) count
+ 2
5250 + TCHARS_PER_DCHAR
- 1)
5251 / TCHARS_PER_DCHAR
),
5252 xtimes (allocated
, 2));
5254 ENSURE_ALLOCATION (n
);
5260 #if NEED_PRINTF_UNBOUNDED_PRECISION
5263 /* Handle the precision. */
5266 (TCHAR_T
*) (result
+ length
);
5270 size_t prefix_count
;
5274 /* Put the additional zeroes after the sign. */
5276 && (*prec_ptr
== '-' || *prec_ptr
== '+'
5277 || *prec_ptr
== ' '))
5279 /* Put the additional zeroes after the 0x prefix if
5280 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5282 && prec_ptr
[0] == '0'
5283 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
5286 move
= count
- prefix_count
;
5287 if (precision
> move
)
5289 /* Insert zeroes. */
5290 size_t insert
= precision
- move
;
5296 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
5297 / TCHARS_PER_DCHAR
);
5298 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5299 ENSURE_ALLOCATION (n
);
5300 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5301 prec_ptr
= (TCHAR_T
*) (result
+ length
);
5304 prec_end
= prec_ptr
+ count
;
5305 prec_ptr
+= prefix_count
;
5307 while (prec_end
> prec_ptr
)
5310 prec_end
[insert
] = prec_end
[0];
5316 while (prec_end
> prec_ptr
);
5324 if (count
>= tmp_length
)
5325 /* tmp_length was incorrectly calculated - fix the
5331 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5332 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5334 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5336 The result string is not certainly ASCII. */
5337 const TCHAR_T
*tmpsrc
;
5340 /* This code assumes that TCHAR_T is 'char'. */
5341 verify (sizeof (TCHAR_T
) == 1);
5343 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5348 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5349 iconveh_question_mark
,
5355 int saved_errno
= errno
;
5356 if (!(result
== resultbuf
|| result
== NULL
))
5358 if (buf_malloced
!= NULL
)
5359 free (buf_malloced
);
5361 errno
= saved_errno
;
5364 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
5365 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
5371 /* The result string is ASCII.
5372 Simple 1:1 conversion. */
5374 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5375 no-op conversion, in-place on the array starting
5376 at (result + length). */
5377 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
5380 const TCHAR_T
*tmpsrc
;
5385 if (result
== resultbuf
)
5387 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5388 /* ENSURE_ALLOCATION will not move tmpsrc
5389 (because it's part of resultbuf). */
5390 ENSURE_ALLOCATION (xsum (length
, count
));
5394 /* ENSURE_ALLOCATION will move the array
5395 (because it uses realloc(). */
5396 ENSURE_ALLOCATION (xsum (length
, count
));
5397 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5401 ENSURE_ALLOCATION (xsum (length
, count
));
5403 tmpdst
= result
+ length
;
5404 /* Copy backwards, because of overlapping. */
5407 for (n
= count
; n
> 0; n
--)
5408 *--tmpdst
= *--tmpsrc
;
5413 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5414 /* Make room for the result. */
5415 if (count
> allocated
- length
)
5417 /* Need at least count elements. But allocate
5420 xmax (xsum (length
, count
), xtimes (allocated
, 2));
5422 ENSURE_ALLOCATION (n
);
5426 /* Here count <= allocated - length. */
5428 /* Perform padding. */
5429 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5430 if (pad_ourselves
&& has_width
)
5433 # if ENABLE_UNISTDIO
5434 /* Outside POSIX, it's preferable to compare the width
5435 against the number of _characters_ of the converted
5437 w
= DCHAR_MBSNLEN (result
+ length
, count
);
5439 /* The width is compared against the number of _bytes_
5440 of the converted value, says POSIX. */
5445 size_t pad
= width
- w
;
5447 /* Make room for the result. */
5448 if (xsum (count
, pad
) > allocated
- length
)
5450 /* Need at least count + pad elements. But
5451 allocate proportionally. */
5453 xmax (xsum3 (length
, count
, pad
),
5454 xtimes (allocated
, 2));
5458 ENSURE_ALLOCATION (n
);
5461 ENSURE_ALLOCATION (n
);
5464 /* Here count + pad <= allocated - length. */
5467 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5468 DCHAR_T
* const rp
= result
+ length
;
5470 DCHAR_T
* const rp
= tmp
;
5472 DCHAR_T
*p
= rp
+ count
;
5473 DCHAR_T
*end
= p
+ pad
;
5475 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5476 if (dp
->conversion
== 'c'
5477 || dp
->conversion
== 's')
5478 /* No zero-padding for string directives. */
5483 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
5484 /* No zero-padding of "inf" and "nan". */
5485 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
5486 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
5489 /* The generated string now extends from rp to p,
5490 with the zero padding insertion point being at
5493 count
= count
+ pad
; /* = end - rp */
5495 if (flags
& FLAG_LEFT
)
5497 /* Pad with spaces on the right. */
5498 for (; pad
> 0; pad
--)
5501 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
5503 /* Pad with zeroes. */
5508 for (; pad
> 0; pad
--)
5513 /* Pad with spaces on the left. */
5518 for (; pad
> 0; pad
--)
5526 /* Here still count <= allocated - length. */
5528 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5529 /* The snprintf() result did fit. */
5531 /* Append the sprintf() result. */
5532 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
5539 #if NEED_PRINTF_DIRECTIVE_F
5540 if (dp
->conversion
== 'F')
5542 /* Convert the %f result to upper case for %F. */
5543 DCHAR_T
*rp
= result
+ length
;
5545 for (rc
= count
; rc
> 0; rc
--, rp
++)
5546 if (*rp
>= 'a' && *rp
<= 'z')
5547 *rp
= *rp
- 'a' + 'A';
5555 #undef pad_ourselves
5556 #undef prec_ourselves
5561 /* Add the final NUL. */
5562 ENSURE_ALLOCATION (xsum (length
, 1));
5563 result
[length
] = '\0';
5565 if (result
!= resultbuf
&& length
+ 1 < allocated
)
5567 /* Shrink the allocated memory if possible. */
5570 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
5575 if (buf_malloced
!= NULL
)
5576 free (buf_malloced
);
5579 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5580 says that snprintf() fails with errno = EOVERFLOW in this case, but
5581 that's only because snprintf() returns an 'int'. This function does
5582 not have this limitation. */
5587 if (!(result
== resultbuf
|| result
== NULL
))
5589 if (buf_malloced
!= NULL
)
5590 free (buf_malloced
);
5597 if (!(result
== resultbuf
|| result
== NULL
))
5599 if (buf_malloced
!= NULL
)
5600 free (buf_malloced
);
5608 #undef MAX_ROOM_NEEDED
5609 #undef TCHARS_PER_DCHAR
5617 #undef DCHAR_IS_TCHAR