1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2012 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 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
17 /* This file can be parametrized with the following macros:
18 VASNPRINTF The name of the function being defined.
19 FCHAR_T The element type of the format string.
20 DCHAR_T The element type of the destination (result) string.
21 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
22 in the format string are ASCII. MUST be set if
23 FCHAR_T and DCHAR_T are not the same type.
24 DIRECTIVE Structure denoting a format directive.
26 DIRECTIVES Structure denoting the set of format directives of a
27 format string. Depends on FCHAR_T.
28 PRINTF_PARSE Function that parses a format string.
30 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
31 DCHAR_SET memset like function for DCHAR_T[] arrays.
32 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
33 SNPRINTF The system's snprintf (or similar) function.
34 This may be either snprintf or swprintf.
35 TCHAR_T The element type of the argument and result string
36 of the said SNPRINTF function. This may be either
37 char or wchar_t. The code exploits that
38 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
39 alignof (TCHAR_T) <= alignof (DCHAR_T).
40 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
41 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
42 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
43 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
44 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
46 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
47 This must come before <config.h> because <config.h> may include
48 <features.h>, and once <features.h> has been included, it's too late. */
50 # define _GNU_SOURCE 1
62 # if WIDE_CHAR_VERSION
63 # include "vasnwprintf.h"
65 # include "vasnprintf.h"
69 #include <locale.h> /* localeconv() */
70 #include <stdio.h> /* snprintf(), sprintf() */
71 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
72 #include <string.h> /* memcpy(), strlen() */
73 #include <errno.h> /* errno */
74 #include <limits.h> /* CHAR_BIT */
75 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
77 # include <langinfo.h>
80 # if WIDE_CHAR_VERSION
81 # include "wprintf-parse.h"
83 # include "printf-parse.h"
87 /* Checked size_t computations. */
92 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
97 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
99 # include "isnand-nolibm.h"
102 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
104 # include "isnanl-nolibm.h"
108 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
110 # include "isnand-nolibm.h"
111 # include "printf-frexp.h"
114 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
116 # include "isnanl-nolibm.h"
117 # include "printf-frexpl.h"
121 /* Default parameters. */
123 # if WIDE_CHAR_VERSION
124 # define VASNPRINTF vasnwprintf
125 # define FCHAR_T wchar_t
126 # define DCHAR_T wchar_t
127 # define TCHAR_T wchar_t
128 # define DCHAR_IS_TCHAR 1
129 # define DIRECTIVE wchar_t_directive
130 # define DIRECTIVES wchar_t_directives
131 # define PRINTF_PARSE wprintf_parse
132 # define DCHAR_CPY wmemcpy
133 # define DCHAR_SET wmemset
135 # define VASNPRINTF vasnprintf
136 # define FCHAR_T char
137 # define DCHAR_T char
138 # define TCHAR_T char
139 # define DCHAR_IS_TCHAR 1
140 # define DIRECTIVE char_directive
141 # define DIRECTIVES char_directives
142 # define PRINTF_PARSE printf_parse
143 # define DCHAR_CPY memcpy
144 # define DCHAR_SET memset
147 #if WIDE_CHAR_VERSION
148 /* TCHAR_T is wchar_t. */
149 # define USE_SNPRINTF 1
150 # if HAVE_DECL__SNWPRINTF
151 /* On Windows, the function swprintf() has a different signature than
152 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
153 instead. The mingw function snwprintf() has fewer bugs than the
154 MSVCRT function _snwprintf(), so prefer that. */
155 # if defined __MINGW32__
156 # define SNPRINTF snwprintf
158 # define SNPRINTF _snwprintf
162 # define SNPRINTF swprintf
165 /* TCHAR_T is char. */
166 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
167 But don't use it on BeOS, since BeOS snprintf produces no output if the
168 size argument is >= 0x3000000.
169 Also don't use it on Linux libc5, since there snprintf with size = 1
170 writes any output without bounds, like sprintf. */
171 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
172 # define USE_SNPRINTF 1
174 # define USE_SNPRINTF 0
176 # if HAVE_DECL__SNPRINTF
177 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
178 function _snprintf(), so prefer that. */
179 # if defined __MINGW32__
180 # define SNPRINTF snprintf
181 /* Here we need to call the native snprintf, not rpl_snprintf. */
184 # define SNPRINTF _snprintf
188 # define SNPRINTF snprintf
189 /* Here we need to call the native snprintf, not rpl_snprintf. */
193 /* Here we need to call the native sprintf, not rpl_sprintf. */
196 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
197 warnings in this file. Use -Dlint to suppress them. */
199 # define IF_LINT(Code) Code
201 # define IF_LINT(Code) /* empty */
204 /* Avoid some warnings from "gcc -Wshadow".
205 This file doesn't use the exp() and remainder() functions. */
209 #define remainder rem
211 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
212 # if (HAVE_STRNLEN && !defined _AIX)
213 # define local_strnlen strnlen
215 # ifndef local_strnlen_defined
216 # define local_strnlen_defined 1
218 local_strnlen (const char *string
, size_t maxlen
)
220 const char *end
= memchr (string
, '\0', maxlen
);
221 return end
? (size_t) (end
- string
) : maxlen
;
227 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
229 # define local_wcslen wcslen
231 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
232 a dependency towards this library, here is a local substitute.
233 Define this substitute only once, even if this file is included
234 twice in the same compilation unit. */
235 # ifndef local_wcslen_defined
236 # define local_wcslen_defined 1
238 local_wcslen (const wchar_t *s
)
242 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
250 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
252 # define local_wcsnlen wcsnlen
254 # ifndef local_wcsnlen_defined
255 # define local_wcsnlen_defined 1
257 local_wcsnlen (const wchar_t *s
, size_t maxlen
)
261 for (ptr
= s
; maxlen
> 0 && *ptr
!= (wchar_t) 0; ptr
++, maxlen
--)
269 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
270 /* Determine the decimal-point character according to the current locale. */
271 # ifndef decimal_point_char_defined
272 # define decimal_point_char_defined 1
274 decimal_point_char (void)
277 /* Determine it in a multithread-safe way. We know nl_langinfo is
278 multithread-safe on glibc systems and Mac OS X systems, but is not required
279 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
280 localeconv() is rarely multithread-safe. */
281 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
282 point
= nl_langinfo (RADIXCHAR
);
285 sprintf (pointbuf
, "%#.0f", 1.0);
286 point
= &pointbuf
[1];
288 point
= localeconv () -> decimal_point
;
290 /* The decimal point is always a single byte: either '.' or ','. */
291 return (point
[0] != '\0' ? point
[0] : '.');
296 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
298 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
300 is_infinite_or_zero (double x
)
302 return isnand (x
) || x
+ x
== x
;
307 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
309 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
311 is_infinite_or_zerol (long double x
)
313 return isnanl (x
) || x
+ x
== x
;
318 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
320 /* Converting 'long double' to decimal without rare rounding bugs requires
321 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
322 (and slower) algorithms. */
324 typedef unsigned int mp_limb_t
;
325 # define GMP_LIMB_BITS 32
326 verify (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
);
328 typedef unsigned long long mp_twolimb_t
;
329 # define GMP_TWOLIMB_BITS 64
330 verify (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
);
332 /* Representation of a bignum >= 0. */
336 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
339 /* Compute the product of two bignums >= 0.
340 Return the allocated memory in case of success, NULL in case of memory
341 allocation failure. */
343 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
350 if (src1
.nlimbs
<= src2
.nlimbs
)
364 /* Now 0 <= len1 <= len2. */
367 /* src1 or src2 is zero. */
369 dest
->limbs
= (mp_limb_t
*) malloc (1);
373 /* Here 1 <= len1 <= len2. */
379 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
382 for (k
= len2
; k
> 0; )
384 for (i
= 0; i
< len1
; i
++)
386 mp_limb_t digit1
= p1
[i
];
387 mp_twolimb_t carry
= 0;
388 for (j
= 0; j
< len2
; j
++)
390 mp_limb_t digit2
= p2
[j
];
391 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
393 dp
[i
+ j
] = (mp_limb_t
) carry
;
394 carry
= carry
>> GMP_LIMB_BITS
;
396 dp
[i
+ len2
] = (mp_limb_t
) carry
;
399 while (dlen
> 0 && dp
[dlen
- 1] == 0)
407 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
408 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
410 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
412 Return the allocated memory in case of success, NULL in case of memory
413 allocation failure. */
415 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
418 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
419 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
420 If m<n, then q:=0 and r:=a.
421 If m>=n=1, perform a single-precision division:
424 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
425 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
426 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
427 Normalise [q[m-1],...,q[0]], yields q.
428 If m>=n>1, perform a multiple-precision division:
429 We have a/b < beta^(m-n+1).
430 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
431 Shift a and b left by s bits, copying them. r:=a.
432 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
433 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
435 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
436 In case of overflow (q* >= beta) set q* := beta-1.
437 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
438 and c3 := b[n-2] * q*.
439 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
440 occurred. Furthermore 0 <= c3 < beta^2.
441 If there was overflow and
442 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
443 the next test can be skipped.}
444 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
445 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
447 Put r := r - b * q* * beta^j. In detail:
448 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
449 hence: u:=0, for i:=0 to n-1 do
451 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
452 u:=u div beta (+ 1, if carry in subtraction)
454 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
456 the carry u does not overflow.}
457 If a negative carry occurs, put q* := q* - 1
458 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
460 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
461 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
463 The room for q[j] can be allocated at the memory location of r[n+j].
464 Finally, round-to-even:
465 Shift r left by 1 bit.
466 If r > b or if r = b and q[0] is odd, q := q+1.
468 const mp_limb_t
*a_ptr
= a
.limbs
;
469 size_t a_len
= a
.nlimbs
;
470 const mp_limb_t
*b_ptr
= b
.limbs
;
471 size_t b_len
= b
.nlimbs
;
473 mp_limb_t
*tmp_roomptr
= NULL
;
479 /* Allocate room for a_len+2 digits.
480 (Need a_len+1 digits for the real division and 1 more digit for the
481 final rounding of q.) */
482 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
487 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
494 /* Division by zero. */
496 if (b_ptr
[b_len
- 1] == 0)
502 /* Here m = a_len >= 0 and n = b_len > 0. */
506 /* m<n: trivial case. q=0, r := copy of a. */
509 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
510 q_ptr
= roomptr
+ a_len
;
515 /* n=1: single precision division.
516 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
520 mp_limb_t den
= b_ptr
[0];
521 mp_limb_t remainder
= 0;
522 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
523 mp_limb_t
*destptr
= q_ptr
+ a_len
;
525 for (count
= a_len
; count
> 0; count
--)
528 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
529 *--destptr
= num
/ den
;
530 remainder
= num
% den
;
532 /* Normalise and store r. */
535 r_ptr
[0] = remainder
;
542 if (q_ptr
[q_len
- 1] == 0)
548 /* n>1: multiple precision division.
549 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
550 beta^(m-n-1) <= a/b < beta^(m-n+1). */
554 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
555 /* Determine s = GMP_LIMB_BITS - integer_length (msd).
556 Code copied from gnulib's integer_length.c. */
557 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
558 s
= __builtin_clz (msd
);
560 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
561 if (GMP_LIMB_BITS
<= DBL_MANT_BIT
)
563 /* Use 'double' operations.
564 Assumes an IEEE 754 'double' implementation. */
565 # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
566 # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
568 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
569 union { double value
; unsigned int word
[NWORDS
]; } m
;
571 /* Use a single integer to floating-point conversion. */
575 - (((m
.word
[DBL_EXPBIT0_WORD
] >> DBL_EXPBIT0_BIT
) & DBL_EXP_MASK
)
611 /* 0 <= s < GMP_LIMB_BITS.
612 Copy b, shifting it left by s bits. */
615 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
616 if (tmp_roomptr
== NULL
)
622 const mp_limb_t
*sourceptr
= b_ptr
;
623 mp_limb_t
*destptr
= tmp_roomptr
;
624 mp_twolimb_t accu
= 0;
626 for (count
= b_len
; count
> 0; count
--)
628 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
629 *destptr
++ = (mp_limb_t
) accu
;
630 accu
= accu
>> GMP_LIMB_BITS
;
632 /* accu must be zero, since that was how s was determined. */
638 /* Copy a, shifting it left by s bits, yields r.
640 At the beginning: r = roomptr[0..a_len],
641 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
645 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
650 const mp_limb_t
*sourceptr
= a_ptr
;
651 mp_limb_t
*destptr
= r_ptr
;
652 mp_twolimb_t accu
= 0;
654 for (count
= a_len
; count
> 0; count
--)
656 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
657 *destptr
++ = (mp_limb_t
) accu
;
658 accu
= accu
>> GMP_LIMB_BITS
;
660 *destptr
++ = (mp_limb_t
) accu
;
662 q_ptr
= roomptr
+ b_len
;
663 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
665 size_t j
= a_len
- b_len
; /* m-n */
666 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
667 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
668 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
669 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
670 /* Division loop, traversed m-n+1 times.
671 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
676 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
678 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
680 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
681 | r_ptr
[j
+ b_len
- 1];
682 q_star
= num
/ b_msd
;
687 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
688 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
689 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
690 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
691 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
693 If yes, jump directly to the subtraction loop.
694 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
695 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
696 if (r_ptr
[j
+ b_len
] > b_msd
697 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
698 /* r[j+n] >= b[n-1]+1 or
699 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
704 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
706 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
707 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
708 mp_twolimb_t c3
= /* b[n-2] * q* */
709 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
710 /* While c2 < c3, increase c2 and decrease c3.
711 Consider c3-c2. While it is > 0, decrease it by
712 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
713 this can happen only twice. */
716 q_star
= q_star
- 1; /* q* := q* - 1 */
717 if (c3
- c2
> b_msdd
)
718 q_star
= q_star
- 1; /* q* := q* - 1 */
724 /* Subtract r := r - b * q* * beta^j. */
727 const mp_limb_t
*sourceptr
= b_ptr
;
728 mp_limb_t
*destptr
= r_ptr
+ j
;
729 mp_twolimb_t carry
= 0;
731 for (count
= b_len
; count
> 0; count
--)
733 /* Here 0 <= carry <= q*. */
736 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
737 + (mp_limb_t
) ~(*destptr
);
738 /* Here 0 <= carry <= beta*q* + beta-1. */
739 *destptr
++ = ~(mp_limb_t
) carry
;
740 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
742 cr
= (mp_limb_t
) carry
;
744 /* Subtract cr from r_ptr[j + b_len], then forget about
746 if (cr
> r_ptr
[j
+ b_len
])
748 /* Subtraction gave a carry. */
749 q_star
= q_star
- 1; /* q* := q* - 1 */
752 const mp_limb_t
*sourceptr
= b_ptr
;
753 mp_limb_t
*destptr
= r_ptr
+ j
;
756 for (count
= b_len
; count
> 0; count
--)
758 mp_limb_t source1
= *sourceptr
++;
759 mp_limb_t source2
= *destptr
;
760 *destptr
++ = source1
+ source2
+ carry
;
763 ? source1
>= (mp_limb_t
) ~source2
764 : source1
> (mp_limb_t
) ~source2
);
767 /* Forget about the carry and about r[j+n]. */
770 /* q* is determined. Store it as q[j]. */
779 if (q_ptr
[q_len
- 1] == 0)
781 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
782 b is shifted left by s bits. */
783 /* Shift r right by s bits. */
786 mp_limb_t ptr
= r_ptr
+ r_len
;
787 mp_twolimb_t accu
= 0;
789 for (count
= r_len
; count
> 0; count
--)
791 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
792 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
793 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
798 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
801 /* Compare r << 1 with b. */
809 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
810 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
811 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
821 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
826 for (i
= 0; i
< q_len
; i
++)
827 if (++(q_ptr
[i
]) != 0)
832 if (tmp_roomptr
!= NULL
)
839 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
841 Destroys the contents of a.
842 Return the allocated memory - containing the decimal digits in low-to-high
843 order, terminated with a NUL character - in case of success, NULL in case
844 of memory allocation failure. */
846 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
848 mp_limb_t
*a_ptr
= a
.limbs
;
849 size_t a_len
= a
.nlimbs
;
850 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
851 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
852 char *c_ptr
= (char *) malloc (xsum (c_len
, extra_zeroes
));
856 for (; extra_zeroes
> 0; extra_zeroes
--)
860 /* Divide a by 10^9, in-place. */
861 mp_limb_t remainder
= 0;
862 mp_limb_t
*ptr
= a_ptr
+ a_len
;
864 for (count
= a_len
; count
> 0; count
--)
867 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
868 *ptr
= num
/ 1000000000;
869 remainder
= num
% 1000000000;
871 /* Store the remainder as 9 decimal digits. */
872 for (count
= 9; count
> 0; count
--)
874 *d_ptr
++ = '0' + (remainder
% 10);
875 remainder
= remainder
/ 10;
878 if (a_ptr
[a_len
- 1] == 0)
881 /* Remove leading zeroes. */
882 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
884 /* But keep at least one zero. */
887 /* Terminate the string. */
893 # if NEED_PRINTF_LONG_DOUBLE
895 /* Assuming x is finite and >= 0:
896 write x as x = 2^e * m, where m is a bignum.
897 Return the allocated memory in case of success, NULL in case of memory
898 allocation failure. */
900 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
907 /* Allocate memory for result. */
908 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
909 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
912 /* Split into exponential part and mantissa. */
913 y
= frexpl (x
, &exp
);
914 if (!(y
>= 0.0L && y
< 1.0L))
916 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
917 latter is an integer. */
918 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
919 I'm not sure whether it's safe to cast a 'long double' value between
920 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
921 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
923 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
924 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
927 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
930 if (!(y
>= 0.0L && y
< 1.0L))
932 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
935 if (!(y
>= 0.0L && y
< 1.0L))
937 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
942 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
945 if (!(y
>= 0.0L && y
< 1.0L))
947 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
951 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
954 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
957 if (!(y
>= 0.0L && y
< 1.0L))
959 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
962 if (!(y
>= 0.0L && y
< 1.0L))
964 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
966 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
972 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
975 *ep
= exp
- LDBL_MANT_BIT
;
981 # if NEED_PRINTF_DOUBLE
983 /* Assuming x is finite and >= 0:
984 write x as x = 2^e * m, where m is a bignum.
985 Return the allocated memory in case of success, NULL in case of memory
986 allocation failure. */
988 decode_double (double x
, int *ep
, mpn_t
*mp
)
995 /* Allocate memory for result. */
996 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
997 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
1000 /* Split into exponential part and mantissa. */
1001 y
= frexp (x
, &exp
);
1002 if (!(y
>= 0.0 && y
< 1.0))
1004 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
1005 latter is an integer. */
1006 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
1007 I'm not sure whether it's safe to cast a 'double' value between
1008 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1009 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1011 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
1012 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1015 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1018 if (!(y
>= 0.0 && y
< 1.0))
1020 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1023 if (!(y
>= 0.0 && y
< 1.0))
1025 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1030 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
1033 if (!(y
>= 0.0 && y
< 1.0))
1035 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1039 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1042 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1045 if (!(y
>= 0.0 && y
< 1.0))
1047 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1050 if (!(y
>= 0.0 && y
< 1.0))
1052 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1057 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1060 *ep
= exp
- DBL_MANT_BIT
;
1066 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1067 Returns the decimal representation of round (x * 10^n).
1068 Return the allocated memory - containing the decimal digits in low-to-high
1069 order, terminated with a NUL character - in case of success, NULL in case
1070 of memory allocation failure. */
1072 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
1075 size_t extra_zeroes
;
1078 mp_limb_t
*pow5_ptr
;
1080 unsigned int s_limbs
;
1081 unsigned int s_bits
;
1089 /* x = 2^e * m, hence
1090 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1091 = round (2^s * 5^n * m). */
1094 /* Factor out a common power of 10 if possible. */
1097 extra_zeroes
= (s
< n
? s
: n
);
1101 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1102 Before converting to decimal, we need to compute
1103 z = round (2^s * 5^n * m). */
1104 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1105 sign. 2.322 is slightly larger than log(5)/log(2). */
1106 abs_n
= (n
>= 0 ? n
: -n
);
1107 abs_s
= (s
>= 0 ? s
: -s
);
1108 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1109 + abs_s
/ GMP_LIMB_BITS
+ 1)
1110 * sizeof (mp_limb_t
));
1111 if (pow5_ptr
== NULL
)
1116 /* Initialize with 1. */
1119 /* Multiply with 5^|n|. */
1122 static mp_limb_t
const small_pow5
[13 + 1] =
1124 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1125 48828125, 244140625, 1220703125
1128 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1130 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1132 mp_twolimb_t carry
= 0;
1133 for (j
= 0; j
< pow5_len
; j
++)
1135 mp_limb_t digit2
= pow5_ptr
[j
];
1136 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1137 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1138 carry
= carry
>> GMP_LIMB_BITS
;
1141 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1144 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1145 s_bits
= abs_s
% GMP_LIMB_BITS
;
1146 if (n
>= 0 ? s
>= 0 : s
<= 0)
1148 /* Multiply with 2^|s|. */
1151 mp_limb_t
*ptr
= pow5_ptr
;
1152 mp_twolimb_t accu
= 0;
1154 for (count
= pow5_len
; count
> 0; count
--)
1156 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1157 *ptr
++ = (mp_limb_t
) accu
;
1158 accu
= accu
>> GMP_LIMB_BITS
;
1162 *ptr
= (mp_limb_t
) accu
;
1169 for (count
= pow5_len
; count
> 0;)
1172 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1174 for (count
= s_limbs
; count
> 0;)
1177 pow5_ptr
[count
] = 0;
1179 pow5_len
+= s_limbs
;
1181 pow5
.limbs
= pow5_ptr
;
1182 pow5
.nlimbs
= pow5_len
;
1185 /* Multiply m with pow5. No division needed. */
1186 z_memory
= multiply (m
, pow5
, &z
);
1190 /* Divide m by pow5 and round. */
1191 z_memory
= divide (m
, pow5
, &z
);
1196 pow5
.limbs
= pow5_ptr
;
1197 pow5
.nlimbs
= pow5_len
;
1201 Multiply m with pow5, then divide by 2^|s|. */
1205 tmp_memory
= multiply (m
, pow5
, &numerator
);
1206 if (tmp_memory
== NULL
)
1212 /* Construct 2^|s|. */
1214 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1216 for (i
= 0; i
< s_limbs
; i
++)
1218 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1219 denominator
.limbs
= ptr
;
1220 denominator
.nlimbs
= s_limbs
+ 1;
1222 z_memory
= divide (numerator
, denominator
, &z
);
1228 Multiply m with 2^s, then divide by pow5. */
1231 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1232 * sizeof (mp_limb_t
));
1233 if (num_ptr
== NULL
)
1240 mp_limb_t
*destptr
= num_ptr
;
1243 for (i
= 0; i
< s_limbs
; i
++)
1248 const mp_limb_t
*sourceptr
= m
.limbs
;
1249 mp_twolimb_t accu
= 0;
1251 for (count
= m
.nlimbs
; count
> 0; count
--)
1253 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1254 *destptr
++ = (mp_limb_t
) accu
;
1255 accu
= accu
>> GMP_LIMB_BITS
;
1258 *destptr
++ = (mp_limb_t
) accu
;
1262 const mp_limb_t
*sourceptr
= m
.limbs
;
1264 for (count
= m
.nlimbs
; count
> 0; count
--)
1265 *destptr
++ = *sourceptr
++;
1267 numerator
.limbs
= num_ptr
;
1268 numerator
.nlimbs
= destptr
- num_ptr
;
1270 z_memory
= divide (numerator
, pow5
, &z
);
1277 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1279 if (z_memory
== NULL
)
1281 digits
= convert_to_decimal (z
, extra_zeroes
);
1286 # if NEED_PRINTF_LONG_DOUBLE
1288 /* Assuming x is finite and >= 0, and n is an integer:
1289 Returns the decimal representation of round (x * 10^n).
1290 Return the allocated memory - containing the decimal digits in low-to-high
1291 order, terminated with a NUL character - in case of success, NULL in case
1292 of memory allocation failure. */
1294 scale10_round_decimal_long_double (long double x
, int n
)
1298 void *memory
= decode_long_double (x
, &e
, &m
);
1299 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1304 # if NEED_PRINTF_DOUBLE
1306 /* Assuming x is finite and >= 0, and n is an integer:
1307 Returns the decimal representation of round (x * 10^n).
1308 Return the allocated memory - containing the decimal digits in low-to-high
1309 order, terminated with a NUL character - in case of success, NULL in case
1310 of memory allocation failure. */
1312 scale10_round_decimal_double (double x
, int n
)
1316 void *memory
= decode_double (x
, &e
, &m
);
1317 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1322 # if NEED_PRINTF_LONG_DOUBLE
1324 /* Assuming x is finite and > 0:
1325 Return an approximation for n with 10^n <= x < 10^(n+1).
1326 The approximation is usually the right n, but may be off by 1 sometimes. */
1328 floorlog10l (long double x
)
1335 /* Split into exponential part and mantissa. */
1336 y
= frexpl (x
, &exp
);
1337 if (!(y
>= 0.0L && y
< 1.0L))
1343 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1345 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1346 exp
-= GMP_LIMB_BITS
;
1348 if (y
< (1.0L / (1 << 16)))
1350 y
*= 1.0L * (1 << 16);
1353 if (y
< (1.0L / (1 << 8)))
1355 y
*= 1.0L * (1 << 8);
1358 if (y
< (1.0L / (1 << 4)))
1360 y
*= 1.0L * (1 << 4);
1363 if (y
< (1.0L / (1 << 2)))
1365 y
*= 1.0L * (1 << 2);
1368 if (y
< (1.0L / (1 << 1)))
1370 y
*= 1.0L * (1 << 1);
1374 if (!(y
>= 0.5L && y
< 1.0L))
1376 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1379 if (z
< 0.70710678118654752444)
1381 z
*= 1.4142135623730950488;
1384 if (z
< 0.8408964152537145431)
1386 z
*= 1.1892071150027210667;
1389 if (z
< 0.91700404320467123175)
1391 z
*= 1.0905077326652576592;
1394 if (z
< 0.9576032806985736469)
1396 z
*= 1.0442737824274138403;
1399 /* Now 0.95 <= z <= 1.01. */
1401 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1402 Four terms are enough to get an approximation with error < 10^-7. */
1403 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1404 /* Finally multiply with log(2)/log(10), yields an approximation for
1406 l
*= 0.30102999566398119523;
1407 /* Round down to the next integer. */
1408 return (int) l
+ (l
< 0 ? -1 : 0);
1413 # if NEED_PRINTF_DOUBLE
1415 /* Assuming x is finite and > 0:
1416 Return an approximation for n with 10^n <= x < 10^(n+1).
1417 The approximation is usually the right n, but may be off by 1 sometimes. */
1419 floorlog10 (double x
)
1426 /* Split into exponential part and mantissa. */
1427 y
= frexp (x
, &exp
);
1428 if (!(y
>= 0.0 && y
< 1.0))
1434 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1436 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1437 exp
-= GMP_LIMB_BITS
;
1439 if (y
< (1.0 / (1 << 16)))
1441 y
*= 1.0 * (1 << 16);
1444 if (y
< (1.0 / (1 << 8)))
1446 y
*= 1.0 * (1 << 8);
1449 if (y
< (1.0 / (1 << 4)))
1451 y
*= 1.0 * (1 << 4);
1454 if (y
< (1.0 / (1 << 2)))
1456 y
*= 1.0 * (1 << 2);
1459 if (y
< (1.0 / (1 << 1)))
1461 y
*= 1.0 * (1 << 1);
1465 if (!(y
>= 0.5 && y
< 1.0))
1467 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1470 if (z
< 0.70710678118654752444)
1472 z
*= 1.4142135623730950488;
1475 if (z
< 0.8408964152537145431)
1477 z
*= 1.1892071150027210667;
1480 if (z
< 0.91700404320467123175)
1482 z
*= 1.0905077326652576592;
1485 if (z
< 0.9576032806985736469)
1487 z
*= 1.0442737824274138403;
1490 /* Now 0.95 <= z <= 1.01. */
1492 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1493 Four terms are enough to get an approximation with error < 10^-7. */
1494 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1495 /* Finally multiply with log(2)/log(10), yields an approximation for
1497 l
*= 0.30102999566398119523;
1498 /* Round down to the next integer. */
1499 return (int) l
+ (l
< 0 ? -1 : 0);
1504 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1505 a single '1' digit. */
1507 is_borderline (const char *digits
, size_t precision
)
1509 for (; precision
> 0; precision
--, digits
++)
1515 return *digits
== '\0';
1520 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
1522 /* Use a different function name, to make it possible that the 'wchar_t'
1523 parametrization and the 'char' parametrization get compiled in the same
1524 translation unit. */
1525 # if WIDE_CHAR_VERSION
1526 # define MAX_ROOM_NEEDED wmax_room_needed
1528 # define MAX_ROOM_NEEDED max_room_needed
1531 /* Returns the number of TCHAR_T units needed as temporary space for the result
1532 of sprintf or SNPRINTF of a single conversion directive. */
1533 static inline size_t
1534 MAX_ROOM_NEEDED (const arguments
*ap
, size_t arg_index
, FCHAR_T conversion
,
1535 arg_type type
, int flags
, size_t width
, int has_precision
,
1536 size_t precision
, int pad_ourselves
)
1542 case 'd': case 'i': case 'u':
1543 # if HAVE_LONG_LONG_INT
1544 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1546 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1547 * 0.30103 /* binary -> decimal */
1549 + 1; /* turn floor into ceil */
1552 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1554 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1555 * 0.30103 /* binary -> decimal */
1557 + 1; /* turn floor into ceil */
1560 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1561 * 0.30103 /* binary -> decimal */
1563 + 1; /* turn floor into ceil */
1564 if (tmp_length
< precision
)
1565 tmp_length
= precision
;
1566 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1567 tmp_length
= xsum (tmp_length
, tmp_length
);
1568 /* Add 1, to account for a leading sign. */
1569 tmp_length
= xsum (tmp_length
, 1);
1573 # if HAVE_LONG_LONG_INT
1574 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1576 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1577 * 0.333334 /* binary -> octal */
1579 + 1; /* turn floor into ceil */
1582 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1584 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1585 * 0.333334 /* binary -> octal */
1587 + 1; /* turn floor into ceil */
1590 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1591 * 0.333334 /* binary -> octal */
1593 + 1; /* turn floor into ceil */
1594 if (tmp_length
< precision
)
1595 tmp_length
= precision
;
1596 /* Add 1, to account for a leading sign. */
1597 tmp_length
= xsum (tmp_length
, 1);
1601 # if HAVE_LONG_LONG_INT
1602 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1604 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1605 * 0.25 /* binary -> hexadecimal */
1607 + 1; /* turn floor into ceil */
1610 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1612 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1613 * 0.25 /* binary -> hexadecimal */
1615 + 1; /* turn floor into ceil */
1618 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1619 * 0.25 /* binary -> hexadecimal */
1621 + 1; /* turn floor into ceil */
1622 if (tmp_length
< precision
)
1623 tmp_length
= precision
;
1624 /* Add 2, to account for a leading sign or alternate form. */
1625 tmp_length
= xsum (tmp_length
, 2);
1629 if (type
== TYPE_LONGDOUBLE
)
1631 (unsigned int) (LDBL_MAX_EXP
1632 * 0.30103 /* binary -> decimal */
1633 * 2 /* estimate for FLAG_GROUP */
1635 + 1 /* turn floor into ceil */
1636 + 10; /* sign, decimal point etc. */
1639 (unsigned int) (DBL_MAX_EXP
1640 * 0.30103 /* binary -> decimal */
1641 * 2 /* estimate for FLAG_GROUP */
1643 + 1 /* turn floor into ceil */
1644 + 10; /* sign, decimal point etc. */
1645 tmp_length
= xsum (tmp_length
, precision
);
1648 case 'e': case 'E': case 'g': case 'G':
1650 12; /* sign, decimal point, exponent etc. */
1651 tmp_length
= xsum (tmp_length
, precision
);
1655 if (type
== TYPE_LONGDOUBLE
)
1657 (unsigned int) (LDBL_DIG
1658 * 0.831 /* decimal -> hexadecimal */
1660 + 1; /* turn floor into ceil */
1663 (unsigned int) (DBL_DIG
1664 * 0.831 /* decimal -> hexadecimal */
1666 + 1; /* turn floor into ceil */
1667 if (tmp_length
< precision
)
1668 tmp_length
= precision
;
1669 /* Account for sign, decimal point etc. */
1670 tmp_length
= xsum (tmp_length
, 12);
1674 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1675 if (type
== TYPE_WIDE_CHAR
)
1676 tmp_length
= MB_CUR_MAX
;
1684 if (type
== TYPE_WIDE_STRING
)
1686 # if WIDE_CHAR_VERSION
1687 /* ISO C says about %ls in fwprintf:
1688 "If the precision is not specified or is greater than the size
1689 of the array, the array shall contain a null wide character."
1690 So if there is a precision, we must not use wcslen. */
1691 const wchar_t *arg
= ap
->arg
[arg_index
].a
.a_wide_string
;
1694 tmp_length
= local_wcsnlen (arg
, precision
);
1696 tmp_length
= local_wcslen (arg
);
1698 /* ISO C says about %ls in fprintf:
1699 "If a precision is specified, no more than that many bytes are
1700 written (including shift sequences, if any), and the array
1701 shall contain a null wide character if, to equal the multibyte
1702 character sequence length given by the precision, the function
1703 would need to access a wide character one past the end of the
1705 So if there is a precision, we must not use wcslen. */
1706 /* This case has already been handled separately in VASNPRINTF. */
1713 # if WIDE_CHAR_VERSION
1714 /* ISO C says about %s in fwprintf:
1715 "If the precision is not specified or is greater than the size
1716 of the converted array, the converted array shall contain a
1717 null wide character."
1718 So if there is a precision, we must not use strlen. */
1719 /* This case has already been handled separately in VASNPRINTF. */
1722 /* ISO C says about %s in fprintf:
1723 "If the precision is not specified or greater than the size of
1724 the array, the array shall contain a null character."
1725 So if there is a precision, we must not use strlen. */
1726 const char *arg
= ap
->arg
[arg_index
].a
.a_string
;
1729 tmp_length
= local_strnlen (arg
, precision
);
1731 tmp_length
= strlen (arg
);
1738 (unsigned int) (sizeof (void *) * CHAR_BIT
1739 * 0.25 /* binary -> hexadecimal */
1741 + 1 /* turn floor into ceil */
1742 + 2; /* account for leading 0x */
1751 # if ENABLE_UNISTDIO
1752 /* Padding considers the number of characters, therefore the number of
1753 elements after padding may be
1754 > max (tmp_length, width)
1756 <= tmp_length + width. */
1757 tmp_length
= xsum (tmp_length
, width
);
1759 /* Padding considers the number of elements, says POSIX. */
1760 if (tmp_length
< width
)
1765 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1773 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1774 const FCHAR_T
*format
, va_list args
)
1779 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1780 /* errno is already set. */
1784 if (d.dir != d.direct_alloc_dir) \
1786 if (a.arg != a.direct_alloc_arg) \
1789 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1797 size_t buf_neededlength
;
1799 TCHAR_T
*buf_malloced
;
1803 /* Output string accumulator. */
1808 /* Allocate a small buffer that will hold a directive passed to
1809 sprintf or snprintf. */
1811 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1813 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1815 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1816 buf_malloced
= NULL
;
1821 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1822 if (size_overflow_p (buf_memsize
))
1823 goto out_of_memory_1
;
1824 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1826 goto out_of_memory_1
;
1830 if (resultbuf
!= NULL
)
1833 allocated
= *lengthp
;
1842 result is either == resultbuf or == NULL or malloc-allocated.
1843 If length > 0, then result != NULL. */
1845 /* Ensures that allocated >= needed. Aborts through a jump to
1846 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1847 #define ENSURE_ALLOCATION(needed) \
1848 if ((needed) > allocated) \
1850 size_t memory_size; \
1853 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1854 if ((needed) > allocated) \
1855 allocated = (needed); \
1856 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1857 if (size_overflow_p (memory_size)) \
1858 goto out_of_memory; \
1859 if (result == resultbuf || result == NULL) \
1860 memory = (DCHAR_T *) malloc (memory_size); \
1862 memory = (DCHAR_T *) realloc (result, memory_size); \
1863 if (memory == NULL) \
1864 goto out_of_memory; \
1865 if (result == resultbuf && length > 0) \
1866 DCHAR_CPY (memory, result, length); \
1870 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1872 if (cp
!= dp
->dir_start
)
1874 size_t n
= dp
->dir_start
- cp
;
1875 size_t augmented_length
= xsum (length
, n
);
1877 ENSURE_ALLOCATION (augmented_length
);
1878 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1879 need that the format string contains only ASCII characters
1880 if FCHAR_T and DCHAR_T are not the same type. */
1881 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1883 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1884 length
= augmented_length
;
1889 result
[length
++] = (unsigned char) *cp
++;
1896 /* Execute a single directive. */
1897 if (dp
->conversion
== '%')
1899 size_t augmented_length
;
1901 if (!(dp
->arg_index
== ARG_NONE
))
1903 augmented_length
= xsum (length
, 1);
1904 ENSURE_ALLOCATION (augmented_length
);
1905 result
[length
] = '%';
1906 length
= augmented_length
;
1910 if (!(dp
->arg_index
!= ARG_NONE
))
1913 if (dp
->conversion
== 'n')
1915 switch (a
.arg
[dp
->arg_index
].type
)
1917 case TYPE_COUNT_SCHAR_POINTER
:
1918 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1920 case TYPE_COUNT_SHORT_POINTER
:
1921 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1923 case TYPE_COUNT_INT_POINTER
:
1924 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1926 case TYPE_COUNT_LONGINT_POINTER
:
1927 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1929 #if HAVE_LONG_LONG_INT
1930 case TYPE_COUNT_LONGLONGINT_POINTER
:
1931 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1939 /* The unistdio extensions. */
1940 else if (dp
->conversion
== 'U')
1942 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1943 int flags
= dp
->flags
;
1951 if (dp
->width_start
!= dp
->width_end
)
1953 if (dp
->width_arg_index
!= ARG_NONE
)
1957 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1959 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1962 /* "A negative field width is taken as a '-' flag
1963 followed by a positive field width." */
1965 width
= (unsigned int) (-arg
);
1972 const FCHAR_T
*digitp
= dp
->width_start
;
1975 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1976 while (digitp
!= dp
->width_end
);
1983 if (dp
->precision_start
!= dp
->precision_end
)
1985 if (dp
->precision_arg_index
!= ARG_NONE
)
1989 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
1991 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
1992 /* "A negative precision is taken as if the precision
2002 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2005 while (digitp
!= dp
->precision_end
)
2006 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2013 case TYPE_U8_STRING
:
2015 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
2016 const uint8_t *arg_end
;
2021 /* Use only PRECISION characters, from the left. */
2024 for (; precision
> 0; precision
--)
2026 int count
= u8_strmblen (arg_end
);
2031 if (!(result
== resultbuf
|| result
== NULL
))
2033 if (buf_malloced
!= NULL
)
2034 free (buf_malloced
);
2045 /* Use the entire string, and count the number of
2051 int count
= u8_strmblen (arg_end
);
2056 if (!(result
== resultbuf
|| result
== NULL
))
2058 if (buf_malloced
!= NULL
)
2059 free (buf_malloced
);
2070 /* Use the entire string. */
2071 arg_end
= arg
+ u8_strlen (arg
);
2072 /* The number of characters doesn't matter. */
2076 if (has_width
&& width
> characters
2077 && !(dp
->flags
& FLAG_LEFT
))
2079 size_t n
= width
- characters
;
2080 ENSURE_ALLOCATION (xsum (length
, n
));
2081 DCHAR_SET (result
+ length
, ' ', n
);
2085 # if DCHAR_IS_UINT8_T
2087 size_t n
= arg_end
- arg
;
2088 ENSURE_ALLOCATION (xsum (length
, n
));
2089 DCHAR_CPY (result
+ length
, arg
, n
);
2094 DCHAR_T
*converted
= result
+ length
;
2095 size_t converted_len
= allocated
- length
;
2097 /* Convert from UTF-8 to locale encoding. */
2099 u8_conv_to_encoding (locale_charset (),
2100 iconveh_question_mark
,
2101 arg
, arg_end
- arg
, NULL
,
2102 converted
, &converted_len
);
2104 /* Convert from UTF-8 to UTF-16/UTF-32. */
2106 U8_TO_DCHAR (arg
, arg_end
- arg
,
2107 converted
, &converted_len
);
2109 if (converted
== NULL
)
2111 int saved_errno
= errno
;
2112 if (!(result
== resultbuf
|| result
== NULL
))
2114 if (buf_malloced
!= NULL
)
2115 free (buf_malloced
);
2117 errno
= saved_errno
;
2120 if (converted
!= result
+ length
)
2122 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2123 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2126 length
+= converted_len
;
2130 if (has_width
&& width
> characters
2131 && (dp
->flags
& FLAG_LEFT
))
2133 size_t n
= width
- characters
;
2134 ENSURE_ALLOCATION (xsum (length
, n
));
2135 DCHAR_SET (result
+ length
, ' ', n
);
2141 case TYPE_U16_STRING
:
2143 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
2144 const uint16_t *arg_end
;
2149 /* Use only PRECISION characters, from the left. */
2152 for (; precision
> 0; precision
--)
2154 int count
= u16_strmblen (arg_end
);
2159 if (!(result
== resultbuf
|| result
== NULL
))
2161 if (buf_malloced
!= NULL
)
2162 free (buf_malloced
);
2173 /* Use the entire string, and count the number of
2179 int count
= u16_strmblen (arg_end
);
2184 if (!(result
== resultbuf
|| result
== NULL
))
2186 if (buf_malloced
!= NULL
)
2187 free (buf_malloced
);
2198 /* Use the entire string. */
2199 arg_end
= arg
+ u16_strlen (arg
);
2200 /* The number of characters doesn't matter. */
2204 if (has_width
&& width
> characters
2205 && !(dp
->flags
& FLAG_LEFT
))
2207 size_t n
= width
- characters
;
2208 ENSURE_ALLOCATION (xsum (length
, n
));
2209 DCHAR_SET (result
+ length
, ' ', n
);
2213 # if DCHAR_IS_UINT16_T
2215 size_t n
= arg_end
- arg
;
2216 ENSURE_ALLOCATION (xsum (length
, n
));
2217 DCHAR_CPY (result
+ length
, arg
, n
);
2222 DCHAR_T
*converted
= result
+ length
;
2223 size_t converted_len
= allocated
- length
;
2225 /* Convert from UTF-16 to locale encoding. */
2227 u16_conv_to_encoding (locale_charset (),
2228 iconveh_question_mark
,
2229 arg
, arg_end
- arg
, NULL
,
2230 converted
, &converted_len
);
2232 /* Convert from UTF-16 to UTF-8/UTF-32. */
2234 U16_TO_DCHAR (arg
, arg_end
- arg
,
2235 converted
, &converted_len
);
2237 if (converted
== NULL
)
2239 int saved_errno
= errno
;
2240 if (!(result
== resultbuf
|| result
== NULL
))
2242 if (buf_malloced
!= NULL
)
2243 free (buf_malloced
);
2245 errno
= saved_errno
;
2248 if (converted
!= result
+ length
)
2250 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2251 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2254 length
+= converted_len
;
2258 if (has_width
&& width
> characters
2259 && (dp
->flags
& FLAG_LEFT
))
2261 size_t n
= width
- characters
;
2262 ENSURE_ALLOCATION (xsum (length
, n
));
2263 DCHAR_SET (result
+ length
, ' ', n
);
2269 case TYPE_U32_STRING
:
2271 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
2272 const uint32_t *arg_end
;
2277 /* Use only PRECISION characters, from the left. */
2280 for (; precision
> 0; precision
--)
2282 int count
= u32_strmblen (arg_end
);
2287 if (!(result
== resultbuf
|| result
== NULL
))
2289 if (buf_malloced
!= NULL
)
2290 free (buf_malloced
);
2301 /* Use the entire string, and count the number of
2307 int count
= u32_strmblen (arg_end
);
2312 if (!(result
== resultbuf
|| result
== NULL
))
2314 if (buf_malloced
!= NULL
)
2315 free (buf_malloced
);
2326 /* Use the entire string. */
2327 arg_end
= arg
+ u32_strlen (arg
);
2328 /* The number of characters doesn't matter. */
2332 if (has_width
&& width
> characters
2333 && !(dp
->flags
& FLAG_LEFT
))
2335 size_t n
= width
- characters
;
2336 ENSURE_ALLOCATION (xsum (length
, n
));
2337 DCHAR_SET (result
+ length
, ' ', n
);
2341 # if DCHAR_IS_UINT32_T
2343 size_t n
= arg_end
- arg
;
2344 ENSURE_ALLOCATION (xsum (length
, n
));
2345 DCHAR_CPY (result
+ length
, arg
, n
);
2350 DCHAR_T
*converted
= result
+ length
;
2351 size_t converted_len
= allocated
- length
;
2353 /* Convert from UTF-32 to locale encoding. */
2355 u32_conv_to_encoding (locale_charset (),
2356 iconveh_question_mark
,
2357 arg
, arg_end
- arg
, NULL
,
2358 converted
, &converted_len
);
2360 /* Convert from UTF-32 to UTF-8/UTF-16. */
2362 U32_TO_DCHAR (arg
, arg_end
- arg
,
2363 converted
, &converted_len
);
2365 if (converted
== NULL
)
2367 int saved_errno
= errno
;
2368 if (!(result
== resultbuf
|| result
== NULL
))
2370 if (buf_malloced
!= NULL
)
2371 free (buf_malloced
);
2373 errno
= saved_errno
;
2376 if (converted
!= result
+ length
)
2378 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2379 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2382 length
+= converted_len
;
2386 if (has_width
&& width
> characters
2387 && (dp
->flags
& FLAG_LEFT
))
2389 size_t n
= width
- characters
;
2390 ENSURE_ALLOCATION (xsum (length
, n
));
2391 DCHAR_SET (result
+ length
, ' ', n
);
2402 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2403 else if (dp
->conversion
== 's'
2404 # if WIDE_CHAR_VERSION
2405 && a
.arg
[dp
->arg_index
].type
!= TYPE_WIDE_STRING
2407 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_STRING
2411 /* The normal handling of the 's' directive below requires
2412 allocating a temporary buffer. The determination of its
2413 length (tmp_length), in the case when a precision is
2414 specified, below requires a conversion between a char[]
2415 string and a wchar_t[] wide string. It could be done, but
2416 we have no guarantee that the implementation of sprintf will
2417 use the exactly same algorithm. Without this guarantee, it
2418 is possible to have buffer overrun bugs. In order to avoid
2419 such bugs, we implement the entire processing of the 's'
2420 directive ourselves. */
2421 int flags
= dp
->flags
;
2429 if (dp
->width_start
!= dp
->width_end
)
2431 if (dp
->width_arg_index
!= ARG_NONE
)
2435 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2437 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2440 /* "A negative field width is taken as a '-' flag
2441 followed by a positive field width." */
2443 width
= (unsigned int) (-arg
);
2450 const FCHAR_T
*digitp
= dp
->width_start
;
2453 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2454 while (digitp
!= dp
->width_end
);
2461 if (dp
->precision_start
!= dp
->precision_end
)
2463 if (dp
->precision_arg_index
!= ARG_NONE
)
2467 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2469 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2470 /* "A negative precision is taken as if the precision
2480 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2483 while (digitp
!= dp
->precision_end
)
2484 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2489 # if WIDE_CHAR_VERSION
2490 /* %s in vasnwprintf. See the specification of fwprintf. */
2492 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
2493 const char *arg_end
;
2498 /* Use only as many bytes as needed to produce PRECISION
2499 wide characters, from the left. */
2502 memset (&state
, '\0', sizeof (mbstate_t));
2506 for (; precision
> 0; precision
--)
2510 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2512 count
= mblen (arg_end
, MB_CUR_MAX
);
2515 /* Found the terminating NUL. */
2519 /* Invalid or incomplete multibyte character. */
2520 if (!(result
== resultbuf
|| result
== NULL
))
2522 if (buf_malloced
!= NULL
)
2523 free (buf_malloced
);
2534 /* Use the entire string, and count the number of wide
2538 memset (&state
, '\0', sizeof (mbstate_t));
2546 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2548 count
= mblen (arg_end
, MB_CUR_MAX
);
2551 /* Found the terminating NUL. */
2555 /* Invalid or incomplete multibyte character. */
2556 if (!(result
== resultbuf
|| result
== NULL
))
2558 if (buf_malloced
!= NULL
)
2559 free (buf_malloced
);
2570 /* Use the entire string. */
2571 arg_end
= arg
+ strlen (arg
);
2572 /* The number of characters doesn't matter. */
2576 if (has_width
&& width
> characters
2577 && !(dp
->flags
& FLAG_LEFT
))
2579 size_t n
= width
- characters
;
2580 ENSURE_ALLOCATION (xsum (length
, n
));
2581 DCHAR_SET (result
+ length
, ' ', n
);
2585 if (has_precision
|| has_width
)
2587 /* We know the number of wide characters in advance. */
2591 memset (&state
, '\0', sizeof (mbstate_t));
2593 ENSURE_ALLOCATION (xsum (length
, characters
));
2594 for (remaining
= characters
; remaining
> 0; remaining
--)
2599 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2601 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2604 /* mbrtowc not consistent with mbrlen, or mbtowc
2605 not consistent with mblen. */
2607 result
[length
++] = wc
;
2610 if (!(arg
== arg_end
))
2617 memset (&state
, '\0', sizeof (mbstate_t));
2619 while (arg
< arg_end
)
2624 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2626 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2629 /* mbrtowc not consistent with mbrlen, or mbtowc
2630 not consistent with mblen. */
2632 ENSURE_ALLOCATION (xsum (length
, 1));
2633 result
[length
++] = wc
;
2638 if (has_width
&& width
> characters
2639 && (dp
->flags
& FLAG_LEFT
))
2641 size_t n
= width
- characters
;
2642 ENSURE_ALLOCATION (xsum (length
, n
));
2643 DCHAR_SET (result
+ length
, ' ', n
);
2648 /* %ls in vasnprintf. See the specification of fprintf. */
2650 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
2651 const wchar_t *arg_end
;
2653 # if !DCHAR_IS_TCHAR
2654 /* This code assumes that TCHAR_T is 'char'. */
2655 verify (sizeof (TCHAR_T
) == 1);
2664 /* Use only as many wide characters as needed to produce
2665 at most PRECISION bytes, from the left. */
2666 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2668 memset (&state
, '\0', sizeof (mbstate_t));
2672 while (precision
> 0)
2674 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2678 /* Found the terminating null wide character. */
2680 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2681 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2683 count
= wctomb (cbuf
, *arg_end
);
2687 /* Cannot convert. */
2688 if (!(result
== resultbuf
|| result
== NULL
))
2690 if (buf_malloced
!= NULL
)
2691 free (buf_malloced
);
2696 if (precision
< count
)
2699 characters
+= count
;
2709 /* Use the entire string, and count the number of
2711 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2713 memset (&state
, '\0', sizeof (mbstate_t));
2719 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2723 /* Found the terminating null wide character. */
2725 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2726 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2728 count
= wctomb (cbuf
, *arg_end
);
2732 /* Cannot convert. */
2733 if (!(result
== resultbuf
|| result
== NULL
))
2735 if (buf_malloced
!= NULL
)
2736 free (buf_malloced
);
2742 characters
+= count
;
2748 /* Use the entire string. */
2749 arg_end
= arg
+ local_wcslen (arg
);
2750 /* The number of bytes doesn't matter. */
2755 # if !DCHAR_IS_TCHAR
2756 /* Convert the string into a piece of temporary memory. */
2757 tmpsrc
= (TCHAR_T
*) malloc (characters
* sizeof (TCHAR_T
));
2761 TCHAR_T
*tmpptr
= tmpsrc
;
2763 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2765 memset (&state
, '\0', sizeof (mbstate_t));
2767 for (remaining
= characters
; remaining
> 0; )
2769 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2774 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2775 count
= wcrtomb (cbuf
, *arg
, &state
);
2777 count
= wctomb (cbuf
, *arg
);
2780 /* Inconsistency. */
2782 memcpy (tmpptr
, cbuf
, count
);
2787 if (!(arg
== arg_end
))
2791 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2793 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2794 iconveh_question_mark
,
2800 int saved_errno
= errno
;
2802 if (!(result
== resultbuf
|| result
== NULL
))
2804 if (buf_malloced
!= NULL
)
2805 free (buf_malloced
);
2807 errno
= saved_errno
;
2815 # if ENABLE_UNISTDIO
2816 /* Outside POSIX, it's preferable to compare the width
2817 against the number of _characters_ of the converted
2819 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2821 /* The width is compared against the number of _bytes_
2822 of the converted value, says POSIX. */
2827 /* w doesn't matter. */
2830 if (has_width
&& width
> w
2831 && !(dp
->flags
& FLAG_LEFT
))
2833 size_t n
= width
- w
;
2834 ENSURE_ALLOCATION (xsum (length
, n
));
2835 DCHAR_SET (result
+ length
, ' ', n
);
2840 if (has_precision
|| has_width
)
2842 /* We know the number of bytes in advance. */
2844 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2846 memset (&state
, '\0', sizeof (mbstate_t));
2848 ENSURE_ALLOCATION (xsum (length
, characters
));
2849 for (remaining
= characters
; remaining
> 0; )
2851 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2856 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2857 count
= wcrtomb (cbuf
, *arg
, &state
);
2859 count
= wctomb (cbuf
, *arg
);
2862 /* Inconsistency. */
2864 memcpy (result
+ length
, cbuf
, count
);
2869 if (!(arg
== arg_end
))
2874 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2876 memset (&state
, '\0', sizeof (mbstate_t));
2878 while (arg
< arg_end
)
2880 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2885 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2886 count
= wcrtomb (cbuf
, *arg
, &state
);
2888 count
= wctomb (cbuf
, *arg
);
2892 /* Cannot convert. */
2893 if (!(result
== resultbuf
|| result
== NULL
))
2895 if (buf_malloced
!= NULL
)
2896 free (buf_malloced
);
2901 ENSURE_ALLOCATION (xsum (length
, count
));
2902 memcpy (result
+ length
, cbuf
, count
);
2908 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
2909 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
2911 length
+= tmpdst_len
;
2914 if (has_width
&& width
> w
2915 && (dp
->flags
& FLAG_LEFT
))
2917 size_t n
= width
- w
;
2918 ENSURE_ALLOCATION (xsum (length
, n
));
2919 DCHAR_SET (result
+ length
, ' ', n
);
2926 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2927 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
2928 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2930 # if NEED_PRINTF_DOUBLE
2931 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2933 # if NEED_PRINTF_LONG_DOUBLE
2934 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2940 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2941 int flags
= dp
->flags
;
2947 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
;
2965 /* "A negative field width is taken as a '-' flag
2966 followed by a positive field width." */
2968 width
= (unsigned int) (-arg
);
2975 const FCHAR_T
*digitp
= dp
->width_start
;
2978 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2979 while (digitp
!= dp
->width_end
);
2986 if (dp
->precision_start
!= dp
->precision_end
)
2988 if (dp
->precision_arg_index
!= ARG_NONE
)
2992 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2994 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2995 /* "A negative precision is taken as if the precision
3005 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3008 while (digitp
!= dp
->precision_end
)
3009 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3014 /* Allocate a temporary buffer of sufficient size. */
3015 if (type
== TYPE_LONGDOUBLE
)
3017 (unsigned int) ((LDBL_DIG
+ 1)
3018 * 0.831 /* decimal -> hexadecimal */
3020 + 1; /* turn floor into ceil */
3023 (unsigned int) ((DBL_DIG
+ 1)
3024 * 0.831 /* decimal -> hexadecimal */
3026 + 1; /* turn floor into ceil */
3027 if (tmp_length
< precision
)
3028 tmp_length
= precision
;
3029 /* Account for sign, decimal point etc. */
3030 tmp_length
= xsum (tmp_length
, 12);
3032 if (tmp_length
< width
)
3035 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3037 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3041 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3043 if (size_overflow_p (tmp_memsize
))
3044 /* Overflow, would lead to out of memory. */
3046 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3048 /* Out of memory. */
3054 if (type
== TYPE_LONGDOUBLE
)
3056 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3057 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3061 if (dp
->conversion
== 'A')
3063 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3067 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3073 DECL_LONG_DOUBLE_ROUNDING
3075 BEGIN_LONG_DOUBLE_ROUNDING ();
3077 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3085 else if (flags
& FLAG_SHOWSIGN
)
3087 else if (flags
& FLAG_SPACE
)
3090 if (arg
> 0.0L && arg
+ arg
== arg
)
3092 if (dp
->conversion
== 'A')
3094 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3098 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3104 long double mantissa
;
3107 mantissa
= printf_frexpl (arg
, &exponent
);
3115 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
3117 /* Round the mantissa. */
3118 long double tail
= mantissa
;
3121 for (q
= precision
; ; q
--)
3123 int digit
= (int) tail
;
3127 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
3136 for (q
= precision
; q
> 0; q
--)
3142 *p
++ = dp
->conversion
- 'A' + 'X';
3147 digit
= (int) mantissa
;
3150 if ((flags
& FLAG_ALT
)
3151 || mantissa
> 0.0L || precision
> 0)
3153 *p
++ = decimal_point_char ();
3154 /* This loop terminates because we assume
3155 that FLT_RADIX is a power of 2. */
3156 while (mantissa
> 0.0L)
3159 digit
= (int) mantissa
;
3164 : dp
->conversion
- 10);
3168 while (precision
> 0)
3175 *p
++ = dp
->conversion
- 'A' + 'P';
3176 # if WIDE_CHAR_VERSION
3178 static const wchar_t decimal_format
[] =
3179 { '%', '+', 'd', '\0' };
3180 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3185 if (sizeof (DCHAR_T
) == 1)
3187 sprintf ((char *) p
, "%+d", exponent
);
3195 sprintf (expbuf
, "%+d", exponent
);
3196 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3202 END_LONG_DOUBLE_ROUNDING ();
3210 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3211 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3215 if (dp
->conversion
== 'A')
3217 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3221 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3228 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3236 else if (flags
& FLAG_SHOWSIGN
)
3238 else if (flags
& FLAG_SPACE
)
3241 if (arg
> 0.0 && arg
+ arg
== arg
)
3243 if (dp
->conversion
== 'A')
3245 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3249 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3258 mantissa
= printf_frexp (arg
, &exponent
);
3266 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
3268 /* Round the mantissa. */
3269 double tail
= mantissa
;
3272 for (q
= precision
; ; q
--)
3274 int digit
= (int) tail
;
3278 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
3287 for (q
= precision
; q
> 0; q
--)
3293 *p
++ = dp
->conversion
- 'A' + 'X';
3298 digit
= (int) mantissa
;
3301 if ((flags
& FLAG_ALT
)
3302 || mantissa
> 0.0 || precision
> 0)
3304 *p
++ = decimal_point_char ();
3305 /* This loop terminates because we assume
3306 that FLT_RADIX is a power of 2. */
3307 while (mantissa
> 0.0)
3310 digit
= (int) mantissa
;
3315 : dp
->conversion
- 10);
3319 while (precision
> 0)
3326 *p
++ = dp
->conversion
- 'A' + 'P';
3327 # if WIDE_CHAR_VERSION
3329 static const wchar_t decimal_format
[] =
3330 { '%', '+', 'd', '\0' };
3331 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3336 if (sizeof (DCHAR_T
) == 1)
3338 sprintf ((char *) p
, "%+d", exponent
);
3346 sprintf (expbuf
, "%+d", exponent
);
3347 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3357 /* The generated string now extends from tmp to p, with the
3358 zero padding insertion point being at pad_ptr. */
3359 if (has_width
&& p
- tmp
< width
)
3361 size_t pad
= width
- (p
- tmp
);
3362 DCHAR_T
*end
= p
+ pad
;
3364 if (flags
& FLAG_LEFT
)
3366 /* Pad with spaces on the right. */
3367 for (; pad
> 0; pad
--)
3370 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3372 /* Pad with zeroes. */
3377 for (; pad
> 0; pad
--)
3382 /* Pad with spaces on the left. */
3387 for (; pad
> 0; pad
--)
3395 size_t count
= p
- tmp
;
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
));
3418 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3419 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
3420 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
3421 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
3422 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
3424 # if NEED_PRINTF_DOUBLE
3425 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3426 # elif NEED_PRINTF_INFINITE_DOUBLE
3427 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3428 /* The systems (mingw) which produce wrong output
3429 for Inf, -Inf, and NaN also do so for -0.0.
3430 Therefore we treat this case here as well. */
3431 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
3433 # if NEED_PRINTF_LONG_DOUBLE
3434 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3435 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3436 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3437 /* Some systems produce wrong output for Inf,
3438 -Inf, and NaN. Some systems in this category
3439 (IRIX 5.3) also do so for -0.0. Therefore we
3440 treat this case here as well. */
3441 && is_infinite_or_zerol (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
3445 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3446 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3448 int flags
= dp
->flags
;
3454 DCHAR_T tmpbuf
[700];
3461 if (dp
->width_start
!= dp
->width_end
)
3463 if (dp
->width_arg_index
!= ARG_NONE
)
3467 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3469 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3472 /* "A negative field width is taken as a '-' flag
3473 followed by a positive field width." */
3475 width
= (unsigned int) (-arg
);
3482 const FCHAR_T
*digitp
= dp
->width_start
;
3485 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3486 while (digitp
!= dp
->width_end
);
3493 if (dp
->precision_start
!= dp
->precision_end
)
3495 if (dp
->precision_arg_index
!= ARG_NONE
)
3499 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3501 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3502 /* "A negative precision is taken as if the precision
3512 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3515 while (digitp
!= dp
->precision_end
)
3516 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3521 /* POSIX specifies the default precision to be 6 for %f, %F,
3522 %e, %E, but not for %g, %G. Implementations appear to use
3523 the same default precision also for %g, %G. But for %a, %A,
3524 the default precision is 0. */
3526 if (!(dp
->conversion
== 'a' || dp
->conversion
== 'A'))
3529 /* Allocate a temporary buffer of sufficient size. */
3530 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3531 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
3532 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3533 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
3534 # elif NEED_PRINTF_LONG_DOUBLE
3535 tmp_length
= LDBL_DIG
+ 1;
3536 # elif NEED_PRINTF_DOUBLE
3537 tmp_length
= DBL_DIG
+ 1;
3541 if (tmp_length
< precision
)
3542 tmp_length
= precision
;
3543 # if NEED_PRINTF_LONG_DOUBLE
3544 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3545 if (type
== TYPE_LONGDOUBLE
)
3547 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3549 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3550 if (!(isnanl (arg
) || arg
+ arg
== arg
))
3552 /* arg is finite and nonzero. */
3553 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
3554 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3555 tmp_length
= exponent
+ precision
;
3559 # if NEED_PRINTF_DOUBLE
3560 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3561 if (type
== TYPE_DOUBLE
)
3563 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3565 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3566 if (!(isnand (arg
) || arg
+ arg
== arg
))
3568 /* arg is finite and nonzero. */
3569 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
3570 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3571 tmp_length
= exponent
+ precision
;
3575 /* Account for sign, decimal point etc. */
3576 tmp_length
= xsum (tmp_length
, 12);
3578 if (tmp_length
< width
)
3581 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3583 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3587 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3589 if (size_overflow_p (tmp_memsize
))
3590 /* Overflow, would lead to out of memory. */
3592 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3594 /* Out of memory. */
3601 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3602 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3603 if (type
== TYPE_LONGDOUBLE
)
3606 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3610 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3612 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3616 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3622 DECL_LONG_DOUBLE_ROUNDING
3624 BEGIN_LONG_DOUBLE_ROUNDING ();
3626 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3634 else if (flags
& FLAG_SHOWSIGN
)
3636 else if (flags
& FLAG_SPACE
)
3639 if (arg
> 0.0L && arg
+ arg
== arg
)
3641 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3643 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3647 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3652 # if NEED_PRINTF_LONG_DOUBLE
3655 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3661 scale10_round_decimal_long_double (arg
, precision
);
3664 END_LONG_DOUBLE_ROUNDING ();
3667 ndigits
= strlen (digits
);
3669 if (ndigits
> precision
)
3673 *p
++ = digits
[ndigits
];
3675 while (ndigits
> precision
);
3678 /* Here ndigits <= precision. */
3679 if ((flags
& FLAG_ALT
) || precision
> 0)
3681 *p
++ = decimal_point_char ();
3682 for (; precision
> ndigits
; precision
--)
3687 *p
++ = digits
[ndigits
];
3693 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3701 if ((flags
& FLAG_ALT
) || precision
> 0)
3703 *p
++ = decimal_point_char ();
3704 for (; precision
> 0; precision
--)
3715 exponent
= floorlog10l (arg
);
3720 scale10_round_decimal_long_double (arg
,
3721 (int)precision
- exponent
);
3724 END_LONG_DOUBLE_ROUNDING ();
3727 ndigits
= strlen (digits
);
3729 if (ndigits
== precision
+ 1)
3731 if (ndigits
< precision
3732 || ndigits
> precision
+ 2)
3733 /* The exponent was not guessed
3734 precisely enough. */
3737 /* None of two values of exponent is
3738 the right one. Prevent an endless
3742 if (ndigits
== precision
)
3748 /* Here ndigits = precision+1. */
3749 if (is_borderline (digits
, precision
))
3751 /* Maybe the exponent guess was too high
3752 and a smaller exponent can be reached
3753 by turning a 10...0 into 9...9x. */
3755 scale10_round_decimal_long_double (arg
,
3756 (int)precision
- exponent
+ 1);
3757 if (digits2
== NULL
)
3760 END_LONG_DOUBLE_ROUNDING ();
3763 if (strlen (digits2
) == precision
+ 1)
3772 /* Here ndigits = precision+1. */
3774 *p
++ = digits
[--ndigits
];
3775 if ((flags
& FLAG_ALT
) || precision
> 0)
3777 *p
++ = decimal_point_char ();
3781 *p
++ = digits
[ndigits
];
3788 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3789 # if WIDE_CHAR_VERSION
3791 static const wchar_t decimal_format
[] =
3792 { '%', '+', '.', '2', 'd', '\0' };
3793 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3798 if (sizeof (DCHAR_T
) == 1)
3800 sprintf ((char *) p
, "%+.2d", exponent
);
3808 sprintf (expbuf
, "%+.2d", exponent
);
3809 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3814 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3818 /* precision >= 1. */
3821 /* The exponent is 0, >= -4, < precision.
3822 Use fixed-point notation. */
3824 size_t ndigits
= precision
;
3825 /* Number of trailing zeroes that have to be
3828 (flags
& FLAG_ALT
? 0 : precision
- 1);
3832 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3834 *p
++ = decimal_point_char ();
3835 while (ndigits
> nzeroes
)
3851 exponent
= floorlog10l (arg
);
3856 scale10_round_decimal_long_double (arg
,
3857 (int)(precision
- 1) - exponent
);
3860 END_LONG_DOUBLE_ROUNDING ();
3863 ndigits
= strlen (digits
);
3865 if (ndigits
== precision
)
3867 if (ndigits
< precision
- 1
3868 || ndigits
> precision
+ 1)
3869 /* The exponent was not guessed
3870 precisely enough. */
3873 /* None of two values of exponent is
3874 the right one. Prevent an endless
3878 if (ndigits
< precision
)
3884 /* Here ndigits = precision. */
3885 if (is_borderline (digits
, precision
- 1))
3887 /* Maybe the exponent guess was too high
3888 and a smaller exponent can be reached
3889 by turning a 10...0 into 9...9x. */
3891 scale10_round_decimal_long_double (arg
,
3892 (int)(precision
- 1) - exponent
+ 1);
3893 if (digits2
== NULL
)
3896 END_LONG_DOUBLE_ROUNDING ();
3899 if (strlen (digits2
) == precision
)
3908 /* Here ndigits = precision. */
3910 /* Determine the number of trailing zeroes
3911 that have to be dropped. */
3913 if ((flags
& FLAG_ALT
) == 0)
3914 while (nzeroes
< ndigits
3915 && digits
[nzeroes
] == '0')
3918 /* The exponent is now determined. */
3920 && exponent
< (long)precision
)
3922 /* Fixed-point notation:
3923 max(exponent,0)+1 digits, then the
3924 decimal point, then the remaining
3925 digits without trailing zeroes. */
3928 size_t count
= exponent
+ 1;
3929 /* Note: count <= precision = ndigits. */
3930 for (; count
> 0; count
--)
3931 *p
++ = digits
[--ndigits
];
3932 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3934 *p
++ = decimal_point_char ();
3935 while (ndigits
> nzeroes
)
3938 *p
++ = digits
[ndigits
];
3944 size_t count
= -exponent
- 1;
3946 *p
++ = decimal_point_char ();
3947 for (; count
> 0; count
--)
3949 while (ndigits
> nzeroes
)
3952 *p
++ = digits
[ndigits
];
3958 /* Exponential notation. */
3959 *p
++ = digits
[--ndigits
];
3960 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3962 *p
++ = decimal_point_char ();
3963 while (ndigits
> nzeroes
)
3966 *p
++ = digits
[ndigits
];
3969 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3970 # if WIDE_CHAR_VERSION
3972 static const wchar_t decimal_format
[] =
3973 { '%', '+', '.', '2', 'd', '\0' };
3974 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3979 if (sizeof (DCHAR_T
) == 1)
3981 sprintf ((char *) p
, "%+.2d", exponent
);
3989 sprintf (expbuf
, "%+.2d", exponent
);
3990 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4002 /* arg is finite. */
4008 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4011 if ((flags
& FLAG_ALT
) || precision
> 0)
4013 *p
++ = decimal_point_char ();
4014 for (; precision
> 0; precision
--)
4018 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4021 if ((flags
& FLAG_ALT
) || precision
> 0)
4023 *p
++ = decimal_point_char ();
4024 for (; precision
> 0; precision
--)
4027 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4032 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4035 if (flags
& FLAG_ALT
)
4038 (precision
> 0 ? precision
- 1 : 0);
4039 *p
++ = decimal_point_char ();
4040 for (; ndigits
> 0; --ndigits
)
4044 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
4047 *p
++ = dp
->conversion
- 'A' + 'X';
4050 if ((flags
& FLAG_ALT
) || precision
> 0)
4052 *p
++ = decimal_point_char ();
4053 for (; precision
> 0; precision
--)
4056 *p
++ = dp
->conversion
- 'A' + 'P';
4065 END_LONG_DOUBLE_ROUNDING ();
4068 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4072 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4074 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4078 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4080 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
4084 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
4091 if (signbit (arg
)) /* arg < 0.0 or negative zero */
4099 else if (flags
& FLAG_SHOWSIGN
)
4101 else if (flags
& FLAG_SPACE
)
4104 if (arg
> 0.0 && arg
+ arg
== arg
)
4106 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4108 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
4112 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
4117 # if NEED_PRINTF_DOUBLE
4120 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4126 scale10_round_decimal_double (arg
, precision
);
4129 ndigits
= strlen (digits
);
4131 if (ndigits
> precision
)
4135 *p
++ = digits
[ndigits
];
4137 while (ndigits
> precision
);
4140 /* Here ndigits <= precision. */
4141 if ((flags
& FLAG_ALT
) || precision
> 0)
4143 *p
++ = decimal_point_char ();
4144 for (; precision
> ndigits
; precision
--)
4149 *p
++ = digits
[ndigits
];
4155 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4163 if ((flags
& FLAG_ALT
) || precision
> 0)
4165 *p
++ = decimal_point_char ();
4166 for (; precision
> 0; precision
--)
4177 exponent
= floorlog10 (arg
);
4182 scale10_round_decimal_double (arg
,
4183 (int)precision
- exponent
);
4186 ndigits
= strlen (digits
);
4188 if (ndigits
== precision
+ 1)
4190 if (ndigits
< precision
4191 || ndigits
> precision
+ 2)
4192 /* The exponent was not guessed
4193 precisely enough. */
4196 /* None of two values of exponent is
4197 the right one. Prevent an endless
4201 if (ndigits
== precision
)
4207 /* Here ndigits = precision+1. */
4208 if (is_borderline (digits
, precision
))
4210 /* Maybe the exponent guess was too high
4211 and a smaller exponent can be reached
4212 by turning a 10...0 into 9...9x. */
4214 scale10_round_decimal_double (arg
,
4215 (int)precision
- exponent
+ 1);
4216 if (digits2
== NULL
)
4221 if (strlen (digits2
) == precision
+ 1)
4230 /* Here ndigits = precision+1. */
4232 *p
++ = digits
[--ndigits
];
4233 if ((flags
& FLAG_ALT
) || precision
> 0)
4235 *p
++ = decimal_point_char ();
4239 *p
++ = digits
[ndigits
];
4246 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4247 # if WIDE_CHAR_VERSION
4249 static const wchar_t decimal_format
[] =
4250 /* Produce the same number of exponent digits
4251 as the native printf implementation. */
4252 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4253 { '%', '+', '.', '3', 'd', '\0' };
4255 { '%', '+', '.', '2', 'd', '\0' };
4257 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4263 static const char decimal_format
[] =
4264 /* Produce the same number of exponent digits
4265 as the native printf implementation. */
4266 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4271 if (sizeof (DCHAR_T
) == 1)
4273 sprintf ((char *) p
, decimal_format
, exponent
);
4281 sprintf (expbuf
, decimal_format
, exponent
);
4282 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4288 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4292 /* precision >= 1. */
4295 /* The exponent is 0, >= -4, < precision.
4296 Use fixed-point notation. */
4298 size_t ndigits
= precision
;
4299 /* Number of trailing zeroes that have to be
4302 (flags
& FLAG_ALT
? 0 : precision
- 1);
4306 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4308 *p
++ = decimal_point_char ();
4309 while (ndigits
> nzeroes
)
4325 exponent
= floorlog10 (arg
);
4330 scale10_round_decimal_double (arg
,
4331 (int)(precision
- 1) - exponent
);
4334 ndigits
= strlen (digits
);
4336 if (ndigits
== precision
)
4338 if (ndigits
< precision
- 1
4339 || ndigits
> precision
+ 1)
4340 /* The exponent was not guessed
4341 precisely enough. */
4344 /* None of two values of exponent is
4345 the right one. Prevent an endless
4349 if (ndigits
< precision
)
4355 /* Here ndigits = precision. */
4356 if (is_borderline (digits
, precision
- 1))
4358 /* Maybe the exponent guess was too high
4359 and a smaller exponent can be reached
4360 by turning a 10...0 into 9...9x. */
4362 scale10_round_decimal_double (arg
,
4363 (int)(precision
- 1) - exponent
+ 1);
4364 if (digits2
== NULL
)
4369 if (strlen (digits2
) == precision
)
4378 /* Here ndigits = precision. */
4380 /* Determine the number of trailing zeroes
4381 that have to be dropped. */
4383 if ((flags
& FLAG_ALT
) == 0)
4384 while (nzeroes
< ndigits
4385 && digits
[nzeroes
] == '0')
4388 /* The exponent is now determined. */
4390 && exponent
< (long)precision
)
4392 /* Fixed-point notation:
4393 max(exponent,0)+1 digits, then the
4394 decimal point, then the remaining
4395 digits without trailing zeroes. */
4398 size_t count
= exponent
+ 1;
4399 /* Note: count <= precision = ndigits. */
4400 for (; count
> 0; count
--)
4401 *p
++ = digits
[--ndigits
];
4402 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4404 *p
++ = decimal_point_char ();
4405 while (ndigits
> nzeroes
)
4408 *p
++ = digits
[ndigits
];
4414 size_t count
= -exponent
- 1;
4416 *p
++ = decimal_point_char ();
4417 for (; count
> 0; count
--)
4419 while (ndigits
> nzeroes
)
4422 *p
++ = digits
[ndigits
];
4428 /* Exponential notation. */
4429 *p
++ = digits
[--ndigits
];
4430 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4432 *p
++ = decimal_point_char ();
4433 while (ndigits
> nzeroes
)
4436 *p
++ = digits
[ndigits
];
4439 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4440 # if WIDE_CHAR_VERSION
4442 static const wchar_t decimal_format
[] =
4443 /* Produce the same number of exponent digits
4444 as the native printf implementation. */
4445 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4446 { '%', '+', '.', '3', 'd', '\0' };
4448 { '%', '+', '.', '2', 'd', '\0' };
4450 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4456 static const char decimal_format
[] =
4457 /* Produce the same number of exponent digits
4458 as the native printf implementation. */
4459 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4464 if (sizeof (DCHAR_T
) == 1)
4466 sprintf ((char *) p
, decimal_format
, exponent
);
4474 sprintf (expbuf
, decimal_format
, exponent
);
4475 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4488 /* arg is finite. */
4494 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4497 if ((flags
& FLAG_ALT
) || precision
> 0)
4499 *p
++ = decimal_point_char ();
4500 for (; precision
> 0; precision
--)
4504 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4507 if ((flags
& FLAG_ALT
) || precision
> 0)
4509 *p
++ = decimal_point_char ();
4510 for (; precision
> 0; precision
--)
4513 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4515 /* Produce the same number of exponent digits as
4516 the native printf implementation. */
4517 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4523 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4526 if (flags
& FLAG_ALT
)
4529 (precision
> 0 ? precision
- 1 : 0);
4530 *p
++ = decimal_point_char ();
4531 for (; ndigits
> 0; --ndigits
)
4543 /* The generated string now extends from tmp to p, with the
4544 zero padding insertion point being at pad_ptr. */
4545 if (has_width
&& p
- tmp
< width
)
4547 size_t pad
= width
- (p
- tmp
);
4548 DCHAR_T
*end
= p
+ pad
;
4550 if (flags
& FLAG_LEFT
)
4552 /* Pad with spaces on the right. */
4553 for (; pad
> 0; pad
--)
4556 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4558 /* Pad with zeroes. */
4563 for (; pad
> 0; pad
--)
4568 /* Pad with spaces on the left. */
4573 for (; pad
> 0; pad
--)
4581 size_t count
= p
- tmp
;
4583 if (count
>= tmp_length
)
4584 /* tmp_length was incorrectly calculated - fix the
4588 /* Make room for the result. */
4589 if (count
>= allocated
- length
)
4591 size_t n
= xsum (length
, count
);
4593 ENSURE_ALLOCATION (n
);
4596 /* Append the result. */
4597 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4606 arg_type type
= a
.arg
[dp
->arg_index
].type
;
4607 int flags
= dp
->flags
;
4608 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4612 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4616 #if NEED_PRINTF_UNBOUNDED_PRECISION
4619 # define prec_ourselves 0
4621 #if NEED_PRINTF_FLAG_LEFTADJUST
4622 # define pad_ourselves 1
4623 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4626 # define pad_ourselves 0
4629 unsigned int prefix_count
;
4630 int prefixes
[2] IF_LINT (= { 0 });
4634 TCHAR_T tmpbuf
[700];
4638 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !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
;
4652 /* "A negative field width is taken as a '-' flag
4653 followed by a positive field width." */
4655 width
= (unsigned int) (-arg
);
4662 const FCHAR_T
*digitp
= dp
->width_start
;
4665 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
4666 while (digitp
!= dp
->width_end
);
4672 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4675 if (dp
->precision_start
!= dp
->precision_end
)
4677 if (dp
->precision_arg_index
!= ARG_NONE
)
4681 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4683 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4684 /* "A negative precision is taken as if the precision
4694 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
4697 while (digitp
!= dp
->precision_end
)
4698 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
4704 /* Decide whether to handle the precision ourselves. */
4705 #if NEED_PRINTF_UNBOUNDED_PRECISION
4706 switch (dp
->conversion
)
4708 case 'd': case 'i': case 'u':
4710 case 'x': case 'X': case 'p':
4711 prec_ourselves
= has_precision
&& (precision
> 0);
4719 /* Decide whether to perform the padding ourselves. */
4720 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4721 switch (dp
->conversion
)
4723 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4724 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4725 to perform the padding after this conversion. Functions
4726 with unistdio extensions perform the padding based on
4727 character count rather than element count. */
4730 # if NEED_PRINTF_FLAG_ZERO
4731 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4737 pad_ourselves
= prec_ourselves
;
4743 /* Allocate a temporary buffer of sufficient size for calling
4746 MAX_ROOM_NEEDED (&a
, dp
->arg_index
, dp
->conversion
, type
,
4747 flags
, width
, has_precision
, precision
,
4750 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
4754 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
4756 if (size_overflow_p (tmp_memsize
))
4757 /* Overflow, would lead to out of memory. */
4759 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
4761 /* Out of memory. */
4766 /* Construct the format string for calling snprintf or
4770 #if NEED_PRINTF_FLAG_GROUPING
4771 /* The underlying implementation doesn't support the ' flag.
4772 Produce no grouping characters in this case; this is
4773 acceptable because the grouping is locale dependent. */
4775 if (flags
& FLAG_GROUP
)
4778 if (flags
& FLAG_LEFT
)
4780 if (flags
& FLAG_SHOWSIGN
)
4782 if (flags
& FLAG_SPACE
)
4784 if (flags
& FLAG_ALT
)
4786 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4787 if (flags
& FLAG_LOCALIZED
)
4792 if (flags
& FLAG_ZERO
)
4794 if (dp
->width_start
!= dp
->width_end
)
4796 size_t n
= dp
->width_end
- dp
->width_start
;
4797 /* The width specification is known to consist only
4798 of standard ASCII characters. */
4799 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4801 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
4806 const FCHAR_T
*mp
= dp
->width_start
;
4808 *fbp
++ = (unsigned char) *mp
++;
4813 if (!prec_ourselves
)
4815 if (dp
->precision_start
!= dp
->precision_end
)
4817 size_t n
= dp
->precision_end
- dp
->precision_start
;
4818 /* The precision specification is known to consist only
4819 of standard ASCII characters. */
4820 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4822 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
4827 const FCHAR_T
*mp
= dp
->precision_start
;
4829 *fbp
++ = (unsigned char) *mp
++;
4837 #if HAVE_LONG_LONG_INT
4838 case TYPE_LONGLONGINT
:
4839 case TYPE_ULONGLONGINT
:
4840 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4853 case TYPE_WIDE_CHAR
:
4856 case TYPE_WIDE_STRING
:
4860 case TYPE_LONGDOUBLE
:
4866 #if NEED_PRINTF_DIRECTIVE_F
4867 if (dp
->conversion
== 'F')
4871 *fbp
= dp
->conversion
;
4873 # if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4878 /* On glibc2 systems from glibc >= 2.3 - probably also older
4879 ones - we know that snprintf's return value conforms to
4880 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
4881 gl_SNPRINTF_TRUNCATION_C99 pass.
4882 Therefore we can avoid using %n in this situation.
4883 On glibc2 systems from 2004-10-18 or newer, the use of %n
4884 in format strings in writable memory may crash the program
4885 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4886 in this situation. */
4887 /* On native Windows systems (such as mingw), we can avoid using
4889 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4890 snprintf does not write more than the specified number
4891 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4892 '4', '5', '6' into buf, not '4', '5', '\0'.)
4893 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4894 allows us to recognize the case of an insufficient
4895 buffer size: it returns -1 in this case.
4896 On native Windows systems (such as mingw) where the OS is
4897 Windows Vista, the use of %n in format strings by default
4898 crashes the program. See
4899 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4900 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4901 So we should avoid %n in this situation. */
4908 /* Construct the arguments for calling snprintf or sprintf. */
4910 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
4912 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4914 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
4916 if (!prec_ourselves
&& dp
->precision_arg_index
!= ARG_NONE
)
4918 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4920 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4924 /* The SNPRINTF result is appended after result[0..length].
4925 The latter is an array of DCHAR_T; SNPRINTF appends an
4926 array of TCHAR_T to it. This is possible because
4927 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4928 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4929 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4930 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4931 where an snprintf() with maxlen==1 acts like sprintf(). */
4932 ENSURE_ALLOCATION (xsum (length
,
4933 (2 + TCHARS_PER_DCHAR
- 1)
4934 / TCHARS_PER_DCHAR
));
4935 /* Prepare checking whether snprintf returns the count
4937 *(TCHAR_T
*) (result
+ length
) = '\0';
4948 size_t maxlen
= allocated
- length
;
4949 /* SNPRINTF can fail if its second argument is
4951 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
4952 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
4953 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
4954 # define SNPRINTF_BUF(arg) \
4955 switch (prefix_count) \
4958 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4963 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4965 prefixes[0], arg, &count); \
4968 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4970 prefixes[0], prefixes[1], arg, \
4977 # define SNPRINTF_BUF(arg) \
4978 switch (prefix_count) \
4981 count = sprintf (tmp, buf, arg); \
4984 count = sprintf (tmp, buf, prefixes[0], arg); \
4987 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
5000 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
5006 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
5012 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
5018 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
5024 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
5030 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
5036 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
5042 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
5046 #if HAVE_LONG_LONG_INT
5047 case TYPE_LONGLONGINT
:
5049 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
5053 case TYPE_ULONGLONGINT
:
5055 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
5062 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
5066 case TYPE_LONGDOUBLE
:
5068 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
5074 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
5079 case TYPE_WIDE_CHAR
:
5081 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
5088 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
5093 case TYPE_WIDE_STRING
:
5095 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
5102 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
5111 /* Portability: Not all implementations of snprintf()
5112 are ISO C 99 compliant. Determine the number of
5113 bytes that snprintf() has produced or would have
5117 /* Verify that snprintf() has NUL-terminated its
5120 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
5122 /* Portability hack. */
5123 if (retcount
> count
)
5128 /* snprintf() doesn't understand the '%n'
5132 /* Don't use the '%n' directive; instead, look
5133 at the snprintf() return value. */
5139 /* Look at the snprintf() return value. */
5142 # if !HAVE_SNPRINTF_RETVAL_C99
5143 /* HP-UX 10.20 snprintf() is doubly deficient:
5144 It doesn't understand the '%n' directive,
5145 *and* it returns -1 (rather than the length
5146 that would have been required) when the
5147 buffer is too small.
5148 But a failure at this point can also come
5149 from other reasons than a too small buffer,
5150 such as an invalid wide string argument to
5151 the %ls directive, or possibly an invalid
5152 floating-point argument. */
5154 MAX_ROOM_NEEDED (&a
, dp
->arg_index
,
5155 dp
->conversion
, type
, flags
,
5156 width
, has_precision
,
5157 precision
, pad_ourselves
);
5159 if (maxlen
< tmp_length
)
5161 /* Make more room. But try to do through
5162 this reallocation only once. */
5163 size_t bigger_need
=
5166 TCHARS_PER_DCHAR
- 1)
5167 / TCHARS_PER_DCHAR
);
5168 /* And always grow proportionally.
5169 (There may be several arguments, each
5170 needing a little more room than the
5172 size_t bigger_need2
=
5173 xsum (xtimes (allocated
, 2), 12);
5174 if (bigger_need
< bigger_need2
)
5175 bigger_need
= bigger_need2
;
5176 ENSURE_ALLOCATION (bigger_need
);
5187 /* Attempt to handle failure. */
5190 /* SNPRINTF or sprintf failed. Save and use the errno
5191 that it has set, if any. */
5192 int saved_errno
= errno
;
5194 if (!(result
== resultbuf
|| result
== NULL
))
5196 if (buf_malloced
!= NULL
)
5197 free (buf_malloced
);
5202 : (dp
->conversion
== 'c' || dp
->conversion
== 's'
5209 /* Handle overflow of the allocated buffer.
5210 If such an overflow occurs, a C99 compliant snprintf()
5211 returns a count >= maxlen. However, a non-compliant
5212 snprintf() function returns only count = maxlen - 1. To
5213 cover both cases, test whether count >= maxlen - 1. */
5214 if ((unsigned int) count
+ 1 >= maxlen
)
5216 /* If maxlen already has attained its allowed maximum,
5217 allocating more memory will not increase maxlen.
5218 Instead of looping, bail out. */
5219 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
5223 /* Need at least (count + 1) * sizeof (TCHAR_T)
5224 bytes. (The +1 is for the trailing NUL.)
5225 But ask for (count + 2) * sizeof (TCHAR_T)
5226 bytes, so that in the next round, we likely get
5227 maxlen > (unsigned int) count + 1
5228 and so we don't get here again.
5229 And allocate proportionally, to avoid looping
5230 eternally if snprintf() reports a too small
5234 ((unsigned int) count
+ 2
5235 + TCHARS_PER_DCHAR
- 1)
5236 / TCHARS_PER_DCHAR
),
5237 xtimes (allocated
, 2));
5239 ENSURE_ALLOCATION (n
);
5245 #if NEED_PRINTF_UNBOUNDED_PRECISION
5248 /* Handle the precision. */
5251 (TCHAR_T
*) (result
+ length
);
5255 size_t prefix_count
;
5259 /* Put the additional zeroes after the sign. */
5261 && (*prec_ptr
== '-' || *prec_ptr
== '+'
5262 || *prec_ptr
== ' '))
5264 /* Put the additional zeroes after the 0x prefix if
5265 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5267 && prec_ptr
[0] == '0'
5268 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
5271 move
= count
- prefix_count
;
5272 if (precision
> move
)
5274 /* Insert zeroes. */
5275 size_t insert
= precision
- move
;
5281 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
5282 / TCHARS_PER_DCHAR
);
5283 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5284 ENSURE_ALLOCATION (n
);
5285 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5286 prec_ptr
= (TCHAR_T
*) (result
+ length
);
5289 prec_end
= prec_ptr
+ count
;
5290 prec_ptr
+= prefix_count
;
5292 while (prec_end
> prec_ptr
)
5295 prec_end
[insert
] = prec_end
[0];
5301 while (prec_end
> prec_ptr
);
5309 if (count
>= tmp_length
)
5310 /* tmp_length was incorrectly calculated - fix the
5316 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5317 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5319 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5321 The result string is not certainly ASCII. */
5322 const TCHAR_T
*tmpsrc
;
5325 /* This code assumes that TCHAR_T is 'char'. */
5326 verify (sizeof (TCHAR_T
) == 1);
5328 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5333 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5334 iconveh_question_mark
,
5340 int saved_errno
= errno
;
5341 if (!(result
== resultbuf
|| result
== NULL
))
5343 if (buf_malloced
!= NULL
)
5344 free (buf_malloced
);
5346 errno
= saved_errno
;
5349 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
5350 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
5356 /* The result string is ASCII.
5357 Simple 1:1 conversion. */
5359 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5360 no-op conversion, in-place on the array starting
5361 at (result + length). */
5362 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
5365 const TCHAR_T
*tmpsrc
;
5370 if (result
== resultbuf
)
5372 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5373 /* ENSURE_ALLOCATION will not move tmpsrc
5374 (because it's part of resultbuf). */
5375 ENSURE_ALLOCATION (xsum (length
, count
));
5379 /* ENSURE_ALLOCATION will move the array
5380 (because it uses realloc(). */
5381 ENSURE_ALLOCATION (xsum (length
, count
));
5382 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5386 ENSURE_ALLOCATION (xsum (length
, count
));
5388 tmpdst
= result
+ length
;
5389 /* Copy backwards, because of overlapping. */
5392 for (n
= count
; n
> 0; n
--)
5393 *--tmpdst
= (unsigned char) *--tmpsrc
;
5398 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5399 /* Make room for the result. */
5400 if (count
> allocated
- length
)
5402 /* Need at least count elements. But allocate
5405 xmax (xsum (length
, count
), xtimes (allocated
, 2));
5407 ENSURE_ALLOCATION (n
);
5411 /* Here count <= allocated - length. */
5413 /* Perform padding. */
5414 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5415 if (pad_ourselves
&& has_width
)
5418 # if ENABLE_UNISTDIO
5419 /* Outside POSIX, it's preferable to compare the width
5420 against the number of _characters_ of the converted
5422 w
= DCHAR_MBSNLEN (result
+ length
, count
);
5424 /* The width is compared against the number of _bytes_
5425 of the converted value, says POSIX. */
5430 size_t pad
= width
- w
;
5432 /* Make room for the result. */
5433 if (xsum (count
, pad
) > allocated
- length
)
5435 /* Need at least count + pad elements. But
5436 allocate proportionally. */
5438 xmax (xsum3 (length
, count
, pad
),
5439 xtimes (allocated
, 2));
5443 ENSURE_ALLOCATION (n
);
5446 ENSURE_ALLOCATION (n
);
5449 /* Here count + pad <= allocated - length. */
5452 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5453 DCHAR_T
* const rp
= result
+ length
;
5455 DCHAR_T
* const rp
= tmp
;
5457 DCHAR_T
*p
= rp
+ count
;
5458 DCHAR_T
*end
= p
+ pad
;
5460 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5461 if (dp
->conversion
== 'c'
5462 || dp
->conversion
== 's')
5463 /* No zero-padding for string directives. */
5468 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
5469 /* No zero-padding of "inf" and "nan". */
5470 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
5471 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
5474 /* The generated string now extends from rp to p,
5475 with the zero padding insertion point being at
5478 count
= count
+ pad
; /* = end - rp */
5480 if (flags
& FLAG_LEFT
)
5482 /* Pad with spaces on the right. */
5483 for (; pad
> 0; pad
--)
5486 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
5488 /* Pad with zeroes. */
5493 for (; pad
> 0; pad
--)
5498 /* Pad with spaces on the left. */
5503 for (; pad
> 0; pad
--)
5511 /* Here still count <= allocated - length. */
5513 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5514 /* The snprintf() result did fit. */
5516 /* Append the sprintf() result. */
5517 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
5524 #if NEED_PRINTF_DIRECTIVE_F
5525 if (dp
->conversion
== 'F')
5527 /* Convert the %f result to upper case for %F. */
5528 DCHAR_T
*rp
= result
+ length
;
5530 for (rc
= count
; rc
> 0; rc
--, rp
++)
5531 if (*rp
>= 'a' && *rp
<= 'z')
5532 *rp
= *rp
- 'a' + 'A';
5540 #undef pad_ourselves
5541 #undef prec_ourselves
5546 /* Add the final NUL. */
5547 ENSURE_ALLOCATION (xsum (length
, 1));
5548 result
[length
] = '\0';
5550 if (result
!= resultbuf
&& length
+ 1 < allocated
)
5552 /* Shrink the allocated memory if possible. */
5555 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
5560 if (buf_malloced
!= NULL
)
5561 free (buf_malloced
);
5564 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5565 says that snprintf() fails with errno = EOVERFLOW in this case, but
5566 that's only because snprintf() returns an 'int'. This function does
5567 not have this limitation. */
5572 if (!(result
== resultbuf
|| result
== NULL
))
5574 if (buf_malloced
!= NULL
)
5575 free (buf_malloced
);
5582 if (!(result
== resultbuf
|| result
== NULL
))
5584 if (buf_malloced
!= NULL
)
5585 free (buf_malloced
);
5593 #undef MAX_ROOM_NEEDED
5594 #undef TCHARS_PER_DCHAR
5602 #undef DCHAR_IS_TCHAR