1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2016 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 Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser 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. */
47 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
48 This must come before <config.h> because <config.h> may include
49 <features.h>, and once <features.h> has been included, it's too late. */
51 # define _GNU_SOURCE 1
58 #include "glib/galloca.h"
63 #include "vasnprintf.h"
65 #include <locale.h> /* localeconv() */
66 #include <stdio.h> /* snprintf(), sprintf() */
67 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
68 #include <string.h> /* memcpy(), strlen() */
69 #include <errno.h> /* errno */
70 #include <limits.h> /* CHAR_BIT */
71 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
72 # include "printf-parse.h"
78 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
83 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
85 # include "isnand-nolibm.h"
88 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
90 # include "isnanl-nolibm.h"
94 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
96 # include "isnand-nolibm.h"
97 # include "printf-frexp.h"
100 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
102 # include "isnanl-nolibm.h"
103 # include "printf-frexpl.h"
107 /* Default parameters. */
109 # if WIDE_CHAR_VERSION
110 # define VASNPRINTF vasnwprintf
111 # define FCHAR_T wchar_t
112 # define DCHAR_T wchar_t
113 # define TCHAR_T wchar_t
114 # define DCHAR_IS_TCHAR 1
115 # define DIRECTIVE wchar_t_directive
116 # define DIRECTIVES wchar_t_directives
117 # define PRINTF_PARSE wprintf_parse
118 # define DCHAR_CPY wmemcpy
119 # define DCHAR_SET wmemset
121 # define VASNPRINTF vasnprintf
122 # define FCHAR_T char
123 # define DCHAR_T char
124 # define TCHAR_T char
125 # define DCHAR_IS_TCHAR 1
126 # define DIRECTIVE char_directive
127 # define DIRECTIVES char_directives
128 # define PRINTF_PARSE printf_parse
129 # define DCHAR_CPY memcpy
130 # define DCHAR_SET memset
133 #if WIDE_CHAR_VERSION
134 /* TCHAR_T is wchar_t. */
135 # define USE_SNPRINTF 1
136 # if HAVE_DECL__SNWPRINTF
137 /* On Windows, the function swprintf() has a different signature than
138 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
139 instead. The mingw function snwprintf() has fewer bugs than the
140 MSVCRT function _snwprintf(), so prefer that. */
141 # if defined __MINGW32__
142 # define SNPRINTF snwprintf
144 # define SNPRINTF _snwprintf
148 # define SNPRINTF swprintf
151 /* TCHAR_T is char. */
152 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
153 But don't use it on BeOS, since BeOS snprintf produces no output if the
154 size argument is >= 0x3000000.
155 Also don't use it on Linux libc5, since there snprintf with size = 1
156 writes any output without bounds, like sprintf. */
157 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
158 # define USE_SNPRINTF 1
160 # define USE_SNPRINTF 0
162 # if HAVE_DECL__SNPRINTF
163 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
164 function _snprintf(), so prefer that. */
165 # if defined __MINGW32__
166 # define SNPRINTF snprintf
167 /* Here we need to call the native snprintf, not rpl_snprintf. */
170 # define SNPRINTF _snprintf
174 # define SNPRINTF snprintf
175 /* Here we need to call the native snprintf, not rpl_snprintf. */
179 /* Here we need to call the native sprintf, not rpl_sprintf. */
182 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
183 warnings in this file. Use -Dlint to suppress them. */
185 # define IF_LINT(Code) Code
187 # define IF_LINT(Code) /* empty */
190 /* Avoid some warnings from "gcc -Wshadow".
191 This file doesn't use the exp() and remainder() functions. */
195 #define remainder rem
197 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
198 # if (HAVE_STRNLEN && !defined _AIX)
199 # define local_strnlen strnlen
201 # ifndef local_strnlen_defined
202 # define local_strnlen_defined 1
204 local_strnlen (const char *string
, size_t maxlen
)
206 const char *end
= memchr (string
, '\0', maxlen
);
207 return end
? (size_t) (end
- string
) : maxlen
;
213 #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
215 # define local_wcslen wcslen
217 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
218 a dependency towards this library, here is a local substitute.
219 Define this substitute only once, even if this file is included
220 twice in the same compilation unit. */
221 # ifndef local_wcslen_defined
222 # define local_wcslen_defined 1
224 local_wcslen (const wchar_t *s
)
228 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
236 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
238 # define local_wcsnlen wcsnlen
240 # ifndef local_wcsnlen_defined
241 # define local_wcsnlen_defined 1
243 local_wcsnlen (const wchar_t *s
, size_t maxlen
)
247 for (ptr
= s
; maxlen
> 0 && *ptr
!= (wchar_t) 0; ptr
++, maxlen
--)
255 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
256 /* Determine the decimal-point character according to the current locale. */
257 # ifndef decimal_point_char_defined
258 # define decimal_point_char_defined 1
260 decimal_point_char (void)
263 /* Determine it in a multithread-safe way. We know nl_langinfo is
264 multithread-safe on glibc systems and Mac OS X systems, but is not required
265 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
266 localeconv() is rarely multithread-safe. */
267 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
268 point
= nl_langinfo (RADIXCHAR
);
271 sprintf (pointbuf
, "%#.0f", 1.0);
272 point
= &pointbuf
[1];
274 point
= localeconv () -> decimal_point
;
276 /* The decimal point is always a single byte: either '.' or ','. */
277 return (point
[0] != '\0' ? point
[0] : '.');
282 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
284 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
286 is_infinite_or_zero (double x
)
288 return isnand (x
) || x
+ x
== x
;
293 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
295 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
297 is_infinite_or_zerol (long double x
)
299 return isnanl (x
) || x
+ x
== x
;
304 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
306 /* Converting 'long double' to decimal without rare rounding bugs requires
307 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
308 (and slower) algorithms. */
310 typedef unsigned int mp_limb_t
;
311 # define GMP_LIMB_BITS 32
312 verify (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
);
314 typedef unsigned long long mp_twolimb_t
;
315 # define GMP_TWOLIMB_BITS 64
316 verify (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
);
318 /* Representation of a bignum >= 0. */
322 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
325 /* Compute the product of two bignums >= 0.
326 Return the allocated memory in case of success, NULL in case of memory
327 allocation failure. */
329 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
336 if (src1
.nlimbs
<= src2
.nlimbs
)
350 /* Now 0 <= len1 <= len2. */
353 /* src1 or src2 is zero. */
355 dest
->limbs
= (mp_limb_t
*) malloc (1);
359 /* Here 1 <= len1 <= len2. */
365 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
368 for (k
= len2
; k
> 0; )
370 for (i
= 0; i
< len1
; i
++)
372 mp_limb_t digit1
= p1
[i
];
373 mp_twolimb_t carry
= 0;
374 for (j
= 0; j
< len2
; j
++)
376 mp_limb_t digit2
= p2
[j
];
377 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
379 dp
[i
+ j
] = (mp_limb_t
) carry
;
380 carry
= carry
>> GMP_LIMB_BITS
;
382 dp
[i
+ len2
] = (mp_limb_t
) carry
;
385 while (dlen
> 0 && dp
[dlen
- 1] == 0)
393 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
394 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
396 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
398 Return the allocated memory in case of success, NULL in case of memory
399 allocation failure. */
401 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
404 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
405 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
406 If m<n, then q:=0 and r:=a.
407 If m>=n=1, perform a single-precision division:
410 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
411 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
412 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
413 Normalise [q[m-1],...,q[0]], yields q.
414 If m>=n>1, perform a multiple-precision division:
415 We have a/b < beta^(m-n+1).
416 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
417 Shift a and b left by s bits, copying them. r:=a.
418 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
419 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
421 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
422 In case of overflow (q* >= beta) set q* := beta-1.
423 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
424 and c3 := b[n-2] * q*.
425 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
426 occurred. Furthermore 0 <= c3 < beta^2.
427 If there was overflow and
428 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
429 the next test can be skipped.}
430 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
431 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
433 Put r := r - b * q* * beta^j. In detail:
434 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
435 hence: u:=0, for i:=0 to n-1 do
437 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
438 u:=u div beta (+ 1, if carry in subtraction)
440 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
442 the carry u does not overflow.}
443 If a negative carry occurs, put q* := q* - 1
444 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
446 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
447 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
449 The room for q[j] can be allocated at the memory location of r[n+j].
450 Finally, round-to-even:
451 Shift r left by 1 bit.
452 If r > b or if r = b and q[0] is odd, q := q+1.
454 const mp_limb_t
*a_ptr
= a
.limbs
;
455 size_t a_len
= a
.nlimbs
;
456 const mp_limb_t
*b_ptr
= b
.limbs
;
457 size_t b_len
= b
.nlimbs
;
459 mp_limb_t
*tmp_roomptr
= NULL
;
465 /* Allocate room for a_len+2 digits.
466 (Need a_len+1 digits for the real division and 1 more digit for the
467 final rounding of q.) */
468 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
473 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
480 /* Division by zero. */
482 if (b_ptr
[b_len
- 1] == 0)
488 /* Here m = a_len >= 0 and n = b_len > 0. */
492 /* m<n: trivial case. q=0, r := copy of a. */
495 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
496 q_ptr
= roomptr
+ a_len
;
501 /* n=1: single precision division.
502 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
506 mp_limb_t den
= b_ptr
[0];
507 mp_limb_t remainder
= 0;
508 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
509 mp_limb_t
*destptr
= q_ptr
+ a_len
;
511 for (count
= a_len
; count
> 0; count
--)
514 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
515 *--destptr
= num
/ den
;
516 remainder
= num
% den
;
518 /* Normalise and store r. */
521 r_ptr
[0] = remainder
;
528 if (q_ptr
[q_len
- 1] == 0)
534 /* n>1: multiple precision division.
535 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
536 beta^(m-n-1) <= a/b < beta^(m-n+1). */
540 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
541 /* Determine s = GMP_LIMB_BITS - integer_length (msd).
542 Code copied from gnulib's integer_length.c. */
543 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
544 s
= __builtin_clz (msd
);
546 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
547 if (GMP_LIMB_BITS
<= DBL_MANT_BIT
)
549 /* Use 'double' operations.
550 Assumes an IEEE 754 'double' implementation. */
551 # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
552 # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
554 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
555 union { double value
; unsigned int word
[NWORDS
]; } m
;
557 /* Use a single integer to floating-point conversion. */
561 - (((m
.word
[DBL_EXPBIT0_WORD
] >> DBL_EXPBIT0_BIT
) & DBL_EXP_MASK
)
597 /* 0 <= s < GMP_LIMB_BITS.
598 Copy b, shifting it left by s bits. */
601 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
602 if (tmp_roomptr
== NULL
)
608 const mp_limb_t
*sourceptr
= b_ptr
;
609 mp_limb_t
*destptr
= tmp_roomptr
;
610 mp_twolimb_t accu
= 0;
612 for (count
= b_len
; count
> 0; count
--)
614 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
615 *destptr
++ = (mp_limb_t
) accu
;
616 accu
= accu
>> GMP_LIMB_BITS
;
618 /* accu must be zero, since that was how s was determined. */
624 /* Copy a, shifting it left by s bits, yields r.
626 At the beginning: r = roomptr[0..a_len],
627 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
631 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
636 const mp_limb_t
*sourceptr
= a_ptr
;
637 mp_limb_t
*destptr
= r_ptr
;
638 mp_twolimb_t accu
= 0;
640 for (count
= a_len
; count
> 0; count
--)
642 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
643 *destptr
++ = (mp_limb_t
) accu
;
644 accu
= accu
>> GMP_LIMB_BITS
;
646 *destptr
++ = (mp_limb_t
) accu
;
648 q_ptr
= roomptr
+ b_len
;
649 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
651 size_t j
= a_len
- b_len
; /* m-n */
652 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
653 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
654 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
655 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
656 /* Division loop, traversed m-n+1 times.
657 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
662 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
664 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
666 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
667 | r_ptr
[j
+ b_len
- 1];
668 q_star
= num
/ b_msd
;
673 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
674 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
675 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
676 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
677 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
679 If yes, jump directly to the subtraction loop.
680 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
681 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
682 if (r_ptr
[j
+ b_len
] > b_msd
683 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
684 /* r[j+n] >= b[n-1]+1 or
685 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
690 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
692 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
693 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
694 mp_twolimb_t c3
= /* b[n-2] * q* */
695 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
696 /* While c2 < c3, increase c2 and decrease c3.
697 Consider c3-c2. While it is > 0, decrease it by
698 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
699 this can happen only twice. */
702 q_star
= q_star
- 1; /* q* := q* - 1 */
703 if (c3
- c2
> b_msdd
)
704 q_star
= q_star
- 1; /* q* := q* - 1 */
710 /* Subtract r := r - b * q* * beta^j. */
713 const mp_limb_t
*sourceptr
= b_ptr
;
714 mp_limb_t
*destptr
= r_ptr
+ j
;
715 mp_twolimb_t carry
= 0;
717 for (count
= b_len
; count
> 0; count
--)
719 /* Here 0 <= carry <= q*. */
722 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
723 + (mp_limb_t
) ~(*destptr
);
724 /* Here 0 <= carry <= beta*q* + beta-1. */
725 *destptr
++ = ~(mp_limb_t
) carry
;
726 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
728 cr
= (mp_limb_t
) carry
;
730 /* Subtract cr from r_ptr[j + b_len], then forget about
732 if (cr
> r_ptr
[j
+ b_len
])
734 /* Subtraction gave a carry. */
735 q_star
= q_star
- 1; /* q* := q* - 1 */
738 const mp_limb_t
*sourceptr
= b_ptr
;
739 mp_limb_t
*destptr
= r_ptr
+ j
;
742 for (count
= b_len
; count
> 0; count
--)
744 mp_limb_t source1
= *sourceptr
++;
745 mp_limb_t source2
= *destptr
;
746 *destptr
++ = source1
+ source2
+ carry
;
749 ? source1
>= (mp_limb_t
) ~source2
750 : source1
> (mp_limb_t
) ~source2
);
753 /* Forget about the carry and about r[j+n]. */
756 /* q* is determined. Store it as q[j]. */
765 if (q_ptr
[q_len
- 1] == 0)
767 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
768 b is shifted left by s bits. */
769 /* Shift r right by s bits. */
772 mp_limb_t ptr
= r_ptr
+ r_len
;
773 mp_twolimb_t accu
= 0;
775 for (count
= r_len
; count
> 0; count
--)
777 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
778 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
779 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
784 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
787 /* Compare r << 1 with b. */
795 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
796 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
797 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
807 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
812 for (i
= 0; i
< q_len
; i
++)
813 if (++(q_ptr
[i
]) != 0)
818 if (tmp_roomptr
!= NULL
)
825 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
827 Destroys the contents of a.
828 Return the allocated memory - containing the decimal digits in low-to-high
829 order, terminated with a NUL character - in case of success, NULL in case
830 of memory allocation failure. */
832 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
834 mp_limb_t
*a_ptr
= a
.limbs
;
835 size_t a_len
= a
.nlimbs
;
836 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
837 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
838 char *c_ptr
= (char *) malloc (xsum (c_len
, extra_zeroes
));
842 for (; extra_zeroes
> 0; extra_zeroes
--)
846 /* Divide a by 10^9, in-place. */
847 mp_limb_t remainder
= 0;
848 mp_limb_t
*ptr
= a_ptr
+ a_len
;
850 for (count
= a_len
; count
> 0; count
--)
853 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
854 *ptr
= num
/ 1000000000;
855 remainder
= num
% 1000000000;
857 /* Store the remainder as 9 decimal digits. */
858 for (count
= 9; count
> 0; count
--)
860 *d_ptr
++ = '0' + (remainder
% 10);
861 remainder
= remainder
/ 10;
864 if (a_ptr
[a_len
- 1] == 0)
867 /* Remove leading zeroes. */
868 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
870 /* But keep at least one zero. */
873 /* Terminate the string. */
879 # if NEED_PRINTF_LONG_DOUBLE
881 /* Assuming x is finite and >= 0:
882 write x as x = 2^e * m, where m is a bignum.
883 Return the allocated memory in case of success, NULL in case of memory
884 allocation failure. */
886 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
893 /* Allocate memory for result. */
894 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
895 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
898 /* Split into exponential part and mantissa. */
899 y
= frexpl (x
, &exp
);
900 if (!(y
>= 0.0L && y
< 1.0L))
902 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
903 latter is an integer. */
904 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
905 I'm not sure whether it's safe to cast a 'long double' value between
906 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
907 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
909 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
910 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
913 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
916 if (!(y
>= 0.0L && y
< 1.0L))
918 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
921 if (!(y
>= 0.0L && y
< 1.0L))
923 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
928 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
931 if (!(y
>= 0.0L && y
< 1.0L))
933 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
937 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
940 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
943 if (!(y
>= 0.0L && y
< 1.0L))
945 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
948 if (!(y
>= 0.0L && y
< 1.0L))
950 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
952 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
958 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
961 *ep
= exp
- LDBL_MANT_BIT
;
967 # if NEED_PRINTF_DOUBLE
969 /* Assuming x is finite and >= 0:
970 write x as x = 2^e * m, where m is a bignum.
971 Return the allocated memory in case of success, NULL in case of memory
972 allocation failure. */
974 decode_double (double x
, int *ep
, mpn_t
*mp
)
981 /* Allocate memory for result. */
982 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
983 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
986 /* Split into exponential part and mantissa. */
988 if (!(y
>= 0.0 && y
< 1.0))
990 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
991 latter is an integer. */
992 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
993 I'm not sure whether it's safe to cast a 'double' value between
994 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
995 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
997 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
998 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1001 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1004 if (!(y
>= 0.0 && y
< 1.0))
1006 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1009 if (!(y
>= 0.0 && y
< 1.0))
1011 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1016 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
1019 if (!(y
>= 0.0 && y
< 1.0))
1021 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1025 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1028 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1031 if (!(y
>= 0.0 && y
< 1.0))
1033 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1036 if (!(y
>= 0.0 && y
< 1.0))
1038 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1043 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1046 *ep
= exp
- DBL_MANT_BIT
;
1052 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1053 Returns the decimal representation of round (x * 10^n).
1054 Return the allocated memory - containing the decimal digits in low-to-high
1055 order, terminated with a NUL character - in case of success, NULL in case
1056 of memory allocation failure. */
1058 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
1061 size_t extra_zeroes
;
1064 mp_limb_t
*pow5_ptr
;
1066 unsigned int s_limbs
;
1067 unsigned int s_bits
;
1075 /* x = 2^e * m, hence
1076 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1077 = round (2^s * 5^n * m). */
1080 /* Factor out a common power of 10 if possible. */
1083 extra_zeroes
= (s
< n
? s
: n
);
1087 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1088 Before converting to decimal, we need to compute
1089 z = round (2^s * 5^n * m). */
1090 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1091 sign. 2.322 is slightly larger than log(5)/log(2). */
1092 abs_n
= (n
>= 0 ? n
: -n
);
1093 abs_s
= (s
>= 0 ? s
: -s
);
1094 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1095 + abs_s
/ GMP_LIMB_BITS
+ 1)
1096 * sizeof (mp_limb_t
));
1097 if (pow5_ptr
== NULL
)
1102 /* Initialize with 1. */
1105 /* Multiply with 5^|n|. */
1108 static mp_limb_t
const small_pow5
[13 + 1] =
1110 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1111 48828125, 244140625, 1220703125
1114 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1116 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1118 mp_twolimb_t carry
= 0;
1119 for (j
= 0; j
< pow5_len
; j
++)
1121 mp_limb_t digit2
= pow5_ptr
[j
];
1122 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1123 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1124 carry
= carry
>> GMP_LIMB_BITS
;
1127 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1130 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1131 s_bits
= abs_s
% GMP_LIMB_BITS
;
1132 if (n
>= 0 ? s
>= 0 : s
<= 0)
1134 /* Multiply with 2^|s|. */
1137 mp_limb_t
*ptr
= pow5_ptr
;
1138 mp_twolimb_t accu
= 0;
1140 for (count
= pow5_len
; count
> 0; count
--)
1142 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1143 *ptr
++ = (mp_limb_t
) accu
;
1144 accu
= accu
>> GMP_LIMB_BITS
;
1148 *ptr
= (mp_limb_t
) accu
;
1155 for (count
= pow5_len
; count
> 0;)
1158 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1160 for (count
= s_limbs
; count
> 0;)
1163 pow5_ptr
[count
] = 0;
1165 pow5_len
+= s_limbs
;
1167 pow5
.limbs
= pow5_ptr
;
1168 pow5
.nlimbs
= pow5_len
;
1171 /* Multiply m with pow5. No division needed. */
1172 z_memory
= multiply (m
, pow5
, &z
);
1176 /* Divide m by pow5 and round. */
1177 z_memory
= divide (m
, pow5
, &z
);
1182 pow5
.limbs
= pow5_ptr
;
1183 pow5
.nlimbs
= pow5_len
;
1187 Multiply m with pow5, then divide by 2^|s|. */
1191 tmp_memory
= multiply (m
, pow5
, &numerator
);
1192 if (tmp_memory
== NULL
)
1198 /* Construct 2^|s|. */
1200 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1202 for (i
= 0; i
< s_limbs
; i
++)
1204 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1205 denominator
.limbs
= ptr
;
1206 denominator
.nlimbs
= s_limbs
+ 1;
1208 z_memory
= divide (numerator
, denominator
, &z
);
1214 Multiply m with 2^s, then divide by pow5. */
1217 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1218 * sizeof (mp_limb_t
));
1219 if (num_ptr
== NULL
)
1226 mp_limb_t
*destptr
= num_ptr
;
1229 for (i
= 0; i
< s_limbs
; i
++)
1234 const mp_limb_t
*sourceptr
= m
.limbs
;
1235 mp_twolimb_t accu
= 0;
1237 for (count
= m
.nlimbs
; count
> 0; count
--)
1239 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1240 *destptr
++ = (mp_limb_t
) accu
;
1241 accu
= accu
>> GMP_LIMB_BITS
;
1244 *destptr
++ = (mp_limb_t
) accu
;
1248 const mp_limb_t
*sourceptr
= m
.limbs
;
1250 for (count
= m
.nlimbs
; count
> 0; count
--)
1251 *destptr
++ = *sourceptr
++;
1253 numerator
.limbs
= num_ptr
;
1254 numerator
.nlimbs
= destptr
- num_ptr
;
1256 z_memory
= divide (numerator
, pow5
, &z
);
1263 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1265 if (z_memory
== NULL
)
1267 digits
= convert_to_decimal (z
, extra_zeroes
);
1272 # if NEED_PRINTF_LONG_DOUBLE
1274 /* Assuming x is finite and >= 0, and n is an integer:
1275 Returns the decimal representation of round (x * 10^n).
1276 Return the allocated memory - containing the decimal digits in low-to-high
1277 order, terminated with a NUL character - in case of success, NULL in case
1278 of memory allocation failure. */
1280 scale10_round_decimal_long_double (long double x
, int n
)
1284 void *memory
= decode_long_double (x
, &e
, &m
);
1285 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1290 # if NEED_PRINTF_DOUBLE
1292 /* Assuming x is finite and >= 0, and n is an integer:
1293 Returns the decimal representation of round (x * 10^n).
1294 Return the allocated memory - containing the decimal digits in low-to-high
1295 order, terminated with a NUL character - in case of success, NULL in case
1296 of memory allocation failure. */
1298 scale10_round_decimal_double (double x
, int n
)
1302 void *memory
= decode_double (x
, &e
, &m
);
1303 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1308 # if NEED_PRINTF_LONG_DOUBLE
1310 /* Assuming x is finite and > 0:
1311 Return an approximation for n with 10^n <= x < 10^(n+1).
1312 The approximation is usually the right n, but may be off by 1 sometimes. */
1314 floorlog10l (long double x
)
1321 /* Split into exponential part and mantissa. */
1322 y
= frexpl (x
, &exp
);
1323 if (!(y
>= 0.0L && y
< 1.0L))
1329 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1331 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1332 exp
-= GMP_LIMB_BITS
;
1334 if (y
< (1.0L / (1 << 16)))
1336 y
*= 1.0L * (1 << 16);
1339 if (y
< (1.0L / (1 << 8)))
1341 y
*= 1.0L * (1 << 8);
1344 if (y
< (1.0L / (1 << 4)))
1346 y
*= 1.0L * (1 << 4);
1349 if (y
< (1.0L / (1 << 2)))
1351 y
*= 1.0L * (1 << 2);
1354 if (y
< (1.0L / (1 << 1)))
1356 y
*= 1.0L * (1 << 1);
1360 if (!(y
>= 0.5L && y
< 1.0L))
1362 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1365 if (z
< 0.70710678118654752444)
1367 z
*= 1.4142135623730950488;
1370 if (z
< 0.8408964152537145431)
1372 z
*= 1.1892071150027210667;
1375 if (z
< 0.91700404320467123175)
1377 z
*= 1.0905077326652576592;
1380 if (z
< 0.9576032806985736469)
1382 z
*= 1.0442737824274138403;
1385 /* Now 0.95 <= z <= 1.01. */
1387 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1388 Four terms are enough to get an approximation with error < 10^-7. */
1389 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1390 /* Finally multiply with log(2)/log(10), yields an approximation for
1392 l
*= 0.30102999566398119523;
1393 /* Round down to the next integer. */
1394 return (int) l
+ (l
< 0 ? -1 : 0);
1399 # if NEED_PRINTF_DOUBLE
1401 /* Assuming x is finite and > 0:
1402 Return an approximation for n with 10^n <= x < 10^(n+1).
1403 The approximation is usually the right n, but may be off by 1 sometimes. */
1405 floorlog10 (double x
)
1412 /* Split into exponential part and mantissa. */
1413 y
= frexp (x
, &exp
);
1414 if (!(y
>= 0.0 && y
< 1.0))
1420 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1422 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1423 exp
-= GMP_LIMB_BITS
;
1425 if (y
< (1.0 / (1 << 16)))
1427 y
*= 1.0 * (1 << 16);
1430 if (y
< (1.0 / (1 << 8)))
1432 y
*= 1.0 * (1 << 8);
1435 if (y
< (1.0 / (1 << 4)))
1437 y
*= 1.0 * (1 << 4);
1440 if (y
< (1.0 / (1 << 2)))
1442 y
*= 1.0 * (1 << 2);
1445 if (y
< (1.0 / (1 << 1)))
1447 y
*= 1.0 * (1 << 1);
1451 if (!(y
>= 0.5 && y
< 1.0))
1453 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1456 if (z
< 0.70710678118654752444)
1458 z
*= 1.4142135623730950488;
1461 if (z
< 0.8408964152537145431)
1463 z
*= 1.1892071150027210667;
1466 if (z
< 0.91700404320467123175)
1468 z
*= 1.0905077326652576592;
1471 if (z
< 0.9576032806985736469)
1473 z
*= 1.0442737824274138403;
1476 /* Now 0.95 <= z <= 1.01. */
1478 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1479 Four terms are enough to get an approximation with error < 10^-7. */
1480 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1481 /* Finally multiply with log(2)/log(10), yields an approximation for
1483 l
*= 0.30102999566398119523;
1484 /* Round down to the next integer. */
1485 return (int) l
+ (l
< 0 ? -1 : 0);
1490 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1491 a single '1' digit. */
1493 is_borderline (const char *digits
, size_t precision
)
1495 for (; precision
> 0; precision
--, digits
++)
1501 return *digits
== '\0';
1506 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
1508 /* Use a different function name, to make it possible that the 'wchar_t'
1509 parametrization and the 'char' parametrization get compiled in the same
1510 translation unit. */
1511 # if WIDE_CHAR_VERSION
1512 # define MAX_ROOM_NEEDED wmax_room_needed
1514 # define MAX_ROOM_NEEDED max_room_needed
1517 /* Returns the number of TCHAR_T units needed as temporary space for the result
1518 of sprintf or SNPRINTF of a single conversion directive. */
1520 MAX_ROOM_NEEDED (const arguments
*ap
, size_t arg_index
, FCHAR_T conversion
,
1521 arg_type type
, int flags
, size_t width
, int has_precision
,
1522 size_t precision
, int pad_ourselves
)
1528 case 'd': case 'i': case 'u':
1530 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1532 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1533 * 0.30103 /* binary -> decimal */
1535 + 1; /* turn floor into ceil */
1538 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1540 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1541 * 0.30103 /* binary -> decimal */
1543 + 1; /* turn floor into ceil */
1546 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1547 * 0.30103 /* binary -> decimal */
1549 + 1; /* turn floor into ceil */
1550 if (tmp_length
< precision
)
1551 tmp_length
= precision
;
1552 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1553 tmp_length
= xsum (tmp_length
, tmp_length
);
1554 /* Add 1, to account for a leading sign. */
1555 tmp_length
= xsum (tmp_length
, 1);
1560 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1562 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1563 * 0.333334 /* binary -> octal */
1565 + 1; /* turn floor into ceil */
1568 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1570 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1571 * 0.333334 /* binary -> octal */
1573 + 1; /* turn floor into ceil */
1576 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1577 * 0.333334 /* binary -> octal */
1579 + 1; /* turn floor into ceil */
1580 if (tmp_length
< precision
)
1581 tmp_length
= precision
;
1582 /* Add 1, to account for a leading sign. */
1583 tmp_length
= xsum (tmp_length
, 1);
1588 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1590 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1591 * 0.25 /* binary -> hexadecimal */
1593 + 1; /* turn floor into ceil */
1596 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1598 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1599 * 0.25 /* binary -> hexadecimal */
1601 + 1; /* turn floor into ceil */
1604 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1605 * 0.25 /* binary -> hexadecimal */
1607 + 1; /* turn floor into ceil */
1608 if (tmp_length
< precision
)
1609 tmp_length
= precision
;
1610 /* Add 2, to account for a leading sign or alternate form. */
1611 tmp_length
= xsum (tmp_length
, 2);
1615 if (type
== TYPE_LONGDOUBLE
)
1617 (unsigned int) (LDBL_MAX_EXP
1618 * 0.30103 /* binary -> decimal */
1619 * 2 /* estimate for FLAG_GROUP */
1621 + 1 /* turn floor into ceil */
1622 + 10; /* sign, decimal point etc. */
1625 (unsigned int) (DBL_MAX_EXP
1626 * 0.30103 /* binary -> decimal */
1627 * 2 /* estimate for FLAG_GROUP */
1629 + 1 /* turn floor into ceil */
1630 + 10; /* sign, decimal point etc. */
1631 tmp_length
= xsum (tmp_length
, precision
);
1634 case 'e': case 'E': case 'g': case 'G':
1636 12; /* sign, decimal point, exponent etc. */
1637 tmp_length
= xsum (tmp_length
, precision
);
1641 if (type
== TYPE_LONGDOUBLE
)
1643 (unsigned int) (LDBL_DIG
1644 * 0.831 /* decimal -> hexadecimal */
1646 + 1; /* turn floor into ceil */
1649 (unsigned int) (DBL_DIG
1650 * 0.831 /* decimal -> hexadecimal */
1652 + 1; /* turn floor into ceil */
1653 if (tmp_length
< precision
)
1654 tmp_length
= precision
;
1655 /* Account for sign, decimal point etc. */
1656 tmp_length
= xsum (tmp_length
, 12);
1660 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1661 if (type
== TYPE_WIDE_CHAR
)
1662 tmp_length
= MB_CUR_MAX
;
1670 if (type
== TYPE_WIDE_STRING
)
1672 # if WIDE_CHAR_VERSION
1673 /* ISO C says about %ls in fwprintf:
1674 "If the precision is not specified or is greater than the size
1675 of the array, the array shall contain a null wide character."
1676 So if there is a precision, we must not use wcslen. */
1677 const wchar_t *arg
= ap
->arg
[arg_index
].a
.a_wide_string
;
1680 tmp_length
= local_wcsnlen (arg
, precision
);
1682 tmp_length
= local_wcslen (arg
);
1684 /* ISO C says about %ls in fprintf:
1685 "If a precision is specified, no more than that many bytes are
1686 written (including shift sequences, if any), and the array
1687 shall contain a null wide character if, to equal the multibyte
1688 character sequence length given by the precision, the function
1689 would need to access a wide character one past the end of the
1691 So if there is a precision, we must not use wcslen. */
1692 /* This case has already been handled separately in VASNPRINTF. */
1699 # if WIDE_CHAR_VERSION
1700 /* ISO C says about %s in fwprintf:
1701 "If the precision is not specified or is greater than the size
1702 of the converted array, the converted array shall contain a
1703 null wide character."
1704 So if there is a precision, we must not use strlen. */
1705 /* This case has already been handled separately in VASNPRINTF. */
1708 /* ISO C says about %s in fprintf:
1709 "If the precision is not specified or greater than the size of
1710 the array, the array shall contain a null character."
1711 So if there is a precision, we must not use strlen. */
1712 const char *arg
= ap
->arg
[arg_index
].a
.a_string
;
1715 tmp_length
= local_strnlen (arg
, precision
);
1717 tmp_length
= strlen (arg
);
1724 (unsigned int) (sizeof (void *) * CHAR_BIT
1725 * 0.25 /* binary -> hexadecimal */
1727 + 1 /* turn floor into ceil */
1728 + 2; /* account for leading 0x */
1737 # if ENABLE_UNISTDIO
1738 /* Padding considers the number of characters, therefore the number of
1739 elements after padding may be
1740 > max (tmp_length, width)
1742 <= tmp_length + width. */
1743 tmp_length
= xsum (tmp_length
, width
);
1745 /* Padding considers the number of elements, says POSIX. */
1746 if (tmp_length
< width
)
1751 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1759 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1760 const FCHAR_T
*format
, va_list args
)
1765 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1766 /* errno is already set. */
1770 if (d.dir != d.direct_alloc_dir) \
1772 if (a.arg != a.direct_alloc_arg) \
1775 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1783 size_t buf_neededlength
;
1785 TCHAR_T
*buf_malloced
;
1789 /* Output string accumulator. */
1794 /* Allocate a small buffer that will hold a directive passed to
1795 sprintf or snprintf. */
1797 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1799 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1801 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1802 buf_malloced
= NULL
;
1807 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1808 if (size_overflow_p (buf_memsize
))
1809 goto out_of_memory_1
;
1810 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1812 goto out_of_memory_1
;
1816 if (resultbuf
!= NULL
)
1819 allocated
= *lengthp
;
1828 result is either == resultbuf or == NULL or malloc-allocated.
1829 If length > 0, then result != NULL. */
1831 /* Ensures that allocated >= needed. Aborts through a jump to
1832 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1833 #define ENSURE_ALLOCATION(needed) \
1834 if ((needed) > allocated) \
1836 size_t memory_size; \
1839 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1840 if ((needed) > allocated) \
1841 allocated = (needed); \
1842 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1843 if (size_overflow_p (memory_size)) \
1844 goto out_of_memory; \
1845 if (result == resultbuf || result == NULL) \
1846 memory = (DCHAR_T *) malloc (memory_size); \
1848 memory = (DCHAR_T *) realloc (result, memory_size); \
1849 if (memory == NULL) \
1850 goto out_of_memory; \
1851 if (result == resultbuf && length > 0) \
1852 DCHAR_CPY (memory, result, length); \
1856 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1858 if (cp
!= dp
->dir_start
)
1860 size_t n
= dp
->dir_start
- cp
;
1861 size_t augmented_length
= xsum (length
, n
);
1863 ENSURE_ALLOCATION (augmented_length
);
1864 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1865 need that the format string contains only ASCII characters
1866 if FCHAR_T and DCHAR_T are not the same type. */
1867 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1869 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1870 length
= augmented_length
;
1875 result
[length
++] = *cp
++;
1882 /* Execute a single directive. */
1883 if (dp
->conversion
== '%')
1885 size_t augmented_length
;
1887 if (!(dp
->arg_index
== ARG_NONE
))
1889 augmented_length
= xsum (length
, 1);
1890 ENSURE_ALLOCATION (augmented_length
);
1891 result
[length
] = '%';
1892 length
= augmented_length
;
1896 if (!(dp
->arg_index
!= ARG_NONE
))
1899 if (dp
->conversion
== 'n')
1901 switch (a
.arg
[dp
->arg_index
].type
)
1903 case TYPE_COUNT_SCHAR_POINTER
:
1904 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1906 case TYPE_COUNT_SHORT_POINTER
:
1907 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1909 case TYPE_COUNT_INT_POINTER
:
1910 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1912 case TYPE_COUNT_LONGINT_POINTER
:
1913 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1916 case TYPE_COUNT_LONGLONGINT_POINTER
:
1917 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1925 /* The unistdio extensions. */
1926 else if (dp
->conversion
== 'U')
1928 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1929 int flags
= dp
->flags
;
1937 if (dp
->width_start
!= dp
->width_end
)
1939 if (dp
->width_arg_index
!= ARG_NONE
)
1943 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1945 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1949 /* "A negative field width is taken as a '-' flag
1950 followed by a positive field width." */
1957 const FCHAR_T
*digitp
= dp
->width_start
;
1960 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1961 while (digitp
!= dp
->width_end
);
1968 if (dp
->precision_start
!= dp
->precision_end
)
1970 if (dp
->precision_arg_index
!= ARG_NONE
)
1974 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
1976 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
1977 /* "A negative precision is taken as if the precision
1987 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
1990 while (digitp
!= dp
->precision_end
)
1991 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
1998 case TYPE_U8_STRING
:
2000 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
2001 const uint8_t *arg_end
;
2006 /* Use only PRECISION characters, from the left. */
2009 for (; precision
> 0; precision
--)
2011 int count
= u8_strmblen (arg_end
);
2016 if (!(result
== resultbuf
|| result
== NULL
))
2018 if (buf_malloced
!= NULL
)
2019 free (buf_malloced
);
2030 /* Use the entire string, and count the number of
2036 int count
= u8_strmblen (arg_end
);
2041 if (!(result
== resultbuf
|| result
== NULL
))
2043 if (buf_malloced
!= NULL
)
2044 free (buf_malloced
);
2055 /* Use the entire string. */
2056 arg_end
= arg
+ u8_strlen (arg
);
2057 /* The number of characters doesn't matter. */
2061 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2063 size_t n
= width
- characters
;
2064 ENSURE_ALLOCATION (xsum (length
, n
));
2065 DCHAR_SET (result
+ length
, ' ', n
);
2069 # if DCHAR_IS_UINT8_T
2071 size_t n
= arg_end
- arg
;
2072 ENSURE_ALLOCATION (xsum (length
, n
));
2073 DCHAR_CPY (result
+ length
, arg
, n
);
2078 DCHAR_T
*converted
= result
+ length
;
2079 size_t converted_len
= allocated
- length
;
2081 /* Convert from UTF-8 to locale encoding. */
2083 u8_conv_to_encoding (locale_charset (),
2084 iconveh_question_mark
,
2085 arg
, arg_end
- arg
, NULL
,
2086 converted
, &converted_len
);
2088 /* Convert from UTF-8 to UTF-16/UTF-32. */
2090 U8_TO_DCHAR (arg
, arg_end
- arg
,
2091 converted
, &converted_len
);
2093 if (converted
== NULL
)
2095 int saved_errno
= errno
;
2096 if (!(result
== resultbuf
|| result
== NULL
))
2098 if (buf_malloced
!= NULL
)
2099 free (buf_malloced
);
2101 errno
= saved_errno
;
2104 if (converted
!= result
+ length
)
2106 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2107 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2110 length
+= converted_len
;
2114 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2116 size_t n
= width
- characters
;
2117 ENSURE_ALLOCATION (xsum (length
, n
));
2118 DCHAR_SET (result
+ length
, ' ', n
);
2124 case TYPE_U16_STRING
:
2126 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
2127 const uint16_t *arg_end
;
2132 /* Use only PRECISION characters, from the left. */
2135 for (; precision
> 0; precision
--)
2137 int count
= u16_strmblen (arg_end
);
2142 if (!(result
== resultbuf
|| result
== NULL
))
2144 if (buf_malloced
!= NULL
)
2145 free (buf_malloced
);
2156 /* Use the entire string, and count the number of
2162 int count
= u16_strmblen (arg_end
);
2167 if (!(result
== resultbuf
|| result
== NULL
))
2169 if (buf_malloced
!= NULL
)
2170 free (buf_malloced
);
2181 /* Use the entire string. */
2182 arg_end
= arg
+ u16_strlen (arg
);
2183 /* The number of characters doesn't matter. */
2187 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2189 size_t n
= width
- characters
;
2190 ENSURE_ALLOCATION (xsum (length
, n
));
2191 DCHAR_SET (result
+ length
, ' ', n
);
2195 # if DCHAR_IS_UINT16_T
2197 size_t n
= arg_end
- arg
;
2198 ENSURE_ALLOCATION (xsum (length
, n
));
2199 DCHAR_CPY (result
+ length
, arg
, n
);
2204 DCHAR_T
*converted
= result
+ length
;
2205 size_t converted_len
= allocated
- length
;
2207 /* Convert from UTF-16 to locale encoding. */
2209 u16_conv_to_encoding (locale_charset (),
2210 iconveh_question_mark
,
2211 arg
, arg_end
- arg
, NULL
,
2212 converted
, &converted_len
);
2214 /* Convert from UTF-16 to UTF-8/UTF-32. */
2216 U16_TO_DCHAR (arg
, arg_end
- arg
,
2217 converted
, &converted_len
);
2219 if (converted
== NULL
)
2221 int saved_errno
= errno
;
2222 if (!(result
== resultbuf
|| result
== NULL
))
2224 if (buf_malloced
!= NULL
)
2225 free (buf_malloced
);
2227 errno
= saved_errno
;
2230 if (converted
!= result
+ length
)
2232 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2233 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2236 length
+= converted_len
;
2240 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2242 size_t n
= width
- characters
;
2243 ENSURE_ALLOCATION (xsum (length
, n
));
2244 DCHAR_SET (result
+ length
, ' ', n
);
2250 case TYPE_U32_STRING
:
2252 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
2253 const uint32_t *arg_end
;
2258 /* Use only PRECISION characters, from the left. */
2261 for (; precision
> 0; precision
--)
2263 int count
= u32_strmblen (arg_end
);
2268 if (!(result
== resultbuf
|| result
== NULL
))
2270 if (buf_malloced
!= NULL
)
2271 free (buf_malloced
);
2282 /* Use the entire string, and count the number of
2288 int count
= u32_strmblen (arg_end
);
2293 if (!(result
== resultbuf
|| result
== NULL
))
2295 if (buf_malloced
!= NULL
)
2296 free (buf_malloced
);
2307 /* Use the entire string. */
2308 arg_end
= arg
+ u32_strlen (arg
);
2309 /* The number of characters doesn't matter. */
2313 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2315 size_t n
= width
- characters
;
2316 ENSURE_ALLOCATION (xsum (length
, n
));
2317 DCHAR_SET (result
+ length
, ' ', n
);
2321 # if DCHAR_IS_UINT32_T
2323 size_t n
= arg_end
- arg
;
2324 ENSURE_ALLOCATION (xsum (length
, n
));
2325 DCHAR_CPY (result
+ length
, arg
, n
);
2330 DCHAR_T
*converted
= result
+ length
;
2331 size_t converted_len
= allocated
- length
;
2333 /* Convert from UTF-32 to locale encoding. */
2335 u32_conv_to_encoding (locale_charset (),
2336 iconveh_question_mark
,
2337 arg
, arg_end
- arg
, NULL
,
2338 converted
, &converted_len
);
2340 /* Convert from UTF-32 to UTF-8/UTF-16. */
2342 U32_TO_DCHAR (arg
, arg_end
- arg
,
2343 converted
, &converted_len
);
2345 if (converted
== NULL
)
2347 int saved_errno
= errno
;
2348 if (!(result
== resultbuf
|| result
== NULL
))
2350 if (buf_malloced
!= NULL
)
2351 free (buf_malloced
);
2353 errno
= saved_errno
;
2356 if (converted
!= result
+ length
)
2358 ENSURE_ALLOCATION (xsum (length
, converted_len
));
2359 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2362 length
+= converted_len
;
2366 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2368 size_t n
= width
- characters
;
2369 ENSURE_ALLOCATION (xsum (length
, n
));
2370 DCHAR_SET (result
+ length
, ' ', n
);
2381 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2382 else if (dp
->conversion
== 's'
2383 # if WIDE_CHAR_VERSION
2384 && a
.arg
[dp
->arg_index
].type
!= TYPE_WIDE_STRING
2386 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_STRING
2390 /* The normal handling of the 's' directive below requires
2391 allocating a temporary buffer. The determination of its
2392 length (tmp_length), in the case when a precision is
2393 specified, below requires a conversion between a char[]
2394 string and a wchar_t[] wide string. It could be done, but
2395 we have no guarantee that the implementation of sprintf will
2396 use the exactly same algorithm. Without this guarantee, it
2397 is possible to have buffer overrun bugs. In order to avoid
2398 such bugs, we implement the entire processing of the 's'
2399 directive ourselves. */
2400 int flags
= dp
->flags
;
2408 if (dp
->width_start
!= dp
->width_end
)
2410 if (dp
->width_arg_index
!= ARG_NONE
)
2414 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2416 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2420 /* "A negative field width is taken as a '-' flag
2421 followed by a positive field width." */
2428 const FCHAR_T
*digitp
= dp
->width_start
;
2431 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2432 while (digitp
!= dp
->width_end
);
2439 if (dp
->precision_start
!= dp
->precision_end
)
2441 if (dp
->precision_arg_index
!= ARG_NONE
)
2445 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2447 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2448 /* "A negative precision is taken as if the precision
2458 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2461 while (digitp
!= dp
->precision_end
)
2462 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2467 # if WIDE_CHAR_VERSION
2468 /* %s in vasnwprintf. See the specification of fwprintf. */
2470 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
2471 const char *arg_end
;
2476 /* Use only as many bytes as needed to produce PRECISION
2477 wide characters, from the left. */
2480 memset (&state
, '\0', sizeof (mbstate_t));
2484 for (; precision
> 0; precision
--)
2488 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2490 count
= mblen (arg_end
, MB_CUR_MAX
);
2493 /* Found the terminating NUL. */
2497 /* Invalid or incomplete multibyte character. */
2498 if (!(result
== resultbuf
|| result
== NULL
))
2500 if (buf_malloced
!= NULL
)
2501 free (buf_malloced
);
2512 /* Use the entire string, and count the number of wide
2516 memset (&state
, '\0', sizeof (mbstate_t));
2524 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2526 count
= mblen (arg_end
, MB_CUR_MAX
);
2529 /* Found the terminating NUL. */
2533 /* Invalid or incomplete multibyte character. */
2534 if (!(result
== resultbuf
|| result
== NULL
))
2536 if (buf_malloced
!= NULL
)
2537 free (buf_malloced
);
2548 /* Use the entire string. */
2549 arg_end
= arg
+ strlen (arg
);
2550 /* The number of characters doesn't matter. */
2554 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2556 size_t n
= width
- characters
;
2557 ENSURE_ALLOCATION (xsum (length
, n
));
2558 DCHAR_SET (result
+ length
, ' ', n
);
2562 if (has_precision
|| has_width
)
2564 /* We know the number of wide characters in advance. */
2568 memset (&state
, '\0', sizeof (mbstate_t));
2570 ENSURE_ALLOCATION (xsum (length
, characters
));
2571 for (remaining
= characters
; remaining
> 0; remaining
--)
2576 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2578 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2581 /* mbrtowc not consistent with mbrlen, or mbtowc
2582 not consistent with mblen. */
2584 result
[length
++] = wc
;
2587 if (!(arg
== arg_end
))
2594 memset (&state
, '\0', sizeof (mbstate_t));
2596 while (arg
< arg_end
)
2601 count
= mbrtowc (&wc
, arg
, arg_end
- arg
, &state
);
2603 count
= mbtowc (&wc
, arg
, arg_end
- arg
);
2606 /* mbrtowc not consistent with mbrlen, or mbtowc
2607 not consistent with mblen. */
2609 ENSURE_ALLOCATION (xsum (length
, 1));
2610 result
[length
++] = wc
;
2615 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2617 size_t n
= width
- characters
;
2618 ENSURE_ALLOCATION (xsum (length
, n
));
2619 DCHAR_SET (result
+ length
, ' ', n
);
2624 /* %ls in vasnprintf. See the specification of fprintf. */
2626 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
2627 const wchar_t *arg_end
;
2629 # if !DCHAR_IS_TCHAR
2630 /* This code assumes that TCHAR_T is 'char'. */
2631 verify (sizeof (TCHAR_T
) == 1);
2640 /* Use only as many wide characters as needed to produce
2641 at most PRECISION bytes, from the left. */
2642 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2644 memset (&state
, '\0', sizeof (mbstate_t));
2648 while (precision
> 0)
2650 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2654 /* Found the terminating null wide character. */
2656 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2657 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2659 count
= wctomb (cbuf
, *arg_end
);
2663 /* Cannot convert. */
2664 if (!(result
== resultbuf
|| result
== NULL
))
2666 if (buf_malloced
!= NULL
)
2667 free (buf_malloced
);
2672 if (precision
< count
)
2675 characters
+= count
;
2685 /* Use the entire string, and count the number of
2687 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2689 memset (&state
, '\0', sizeof (mbstate_t));
2695 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2699 /* Found the terminating null wide character. */
2701 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2702 count
= wcrtomb (cbuf
, *arg_end
, &state
);
2704 count
= wctomb (cbuf
, *arg_end
);
2708 /* Cannot convert. */
2709 if (!(result
== resultbuf
|| result
== NULL
))
2711 if (buf_malloced
!= NULL
)
2712 free (buf_malloced
);
2718 characters
+= count
;
2724 /* Use the entire string. */
2725 arg_end
= arg
+ local_wcslen (arg
);
2726 /* The number of bytes doesn't matter. */
2731 # if !DCHAR_IS_TCHAR
2732 /* Convert the string into a piece of temporary memory. */
2733 tmpsrc
= (TCHAR_T
*) malloc (characters
* sizeof (TCHAR_T
));
2737 TCHAR_T
*tmpptr
= tmpsrc
;
2739 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2741 memset (&state
, '\0', sizeof (mbstate_t));
2743 for (remaining
= characters
; remaining
> 0; )
2745 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2750 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2751 count
= wcrtomb (cbuf
, *arg
, &state
);
2753 count
= wctomb (cbuf
, *arg
);
2756 /* Inconsistency. */
2758 memcpy (tmpptr
, cbuf
, count
);
2763 if (!(arg
== arg_end
))
2767 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2769 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2770 iconveh_question_mark
,
2776 int saved_errno
= errno
;
2778 if (!(result
== resultbuf
|| result
== NULL
))
2780 if (buf_malloced
!= NULL
)
2781 free (buf_malloced
);
2783 errno
= saved_errno
;
2791 # if ENABLE_UNISTDIO
2792 /* Outside POSIX, it's preferable to compare the width
2793 against the number of _characters_ of the converted
2795 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2797 /* The width is compared against the number of _bytes_
2798 of the converted value, says POSIX. */
2803 /* w doesn't matter. */
2806 if (w
< width
&& !(dp
->flags
& FLAG_LEFT
))
2808 size_t n
= width
- w
;
2809 ENSURE_ALLOCATION (xsum (length
, n
));
2810 DCHAR_SET (result
+ length
, ' ', n
);
2815 if (has_precision
|| has_width
)
2817 /* We know the number of bytes in advance. */
2819 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2821 memset (&state
, '\0', sizeof (mbstate_t));
2823 ENSURE_ALLOCATION (xsum (length
, characters
));
2824 for (remaining
= characters
; remaining
> 0; )
2826 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2831 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2832 count
= wcrtomb (cbuf
, *arg
, &state
);
2834 count
= wctomb (cbuf
, *arg
);
2837 /* Inconsistency. */
2839 memcpy (result
+ length
, cbuf
, count
);
2844 if (!(arg
== arg_end
))
2849 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2851 memset (&state
, '\0', sizeof (mbstate_t));
2853 while (arg
< arg_end
)
2855 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2860 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2861 count
= wcrtomb (cbuf
, *arg
, &state
);
2863 count
= wctomb (cbuf
, *arg
);
2867 /* Cannot convert. */
2868 if (!(result
== resultbuf
|| result
== NULL
))
2870 if (buf_malloced
!= NULL
)
2871 free (buf_malloced
);
2876 ENSURE_ALLOCATION (xsum (length
, count
));
2877 memcpy (result
+ length
, cbuf
, count
);
2883 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
2884 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
2886 length
+= tmpdst_len
;
2889 if (w
< width
&& (dp
->flags
& FLAG_LEFT
))
2891 size_t n
= width
- w
;
2892 ENSURE_ALLOCATION (xsum (length
, n
));
2893 DCHAR_SET (result
+ length
, ' ', n
);
2900 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2901 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
2902 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2904 # if NEED_PRINTF_DOUBLE
2905 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2907 # if NEED_PRINTF_LONG_DOUBLE
2908 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2914 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2915 int flags
= dp
->flags
;
2921 DCHAR_T tmpbuf
[700];
2927 if (dp
->width_start
!= dp
->width_end
)
2929 if (dp
->width_arg_index
!= ARG_NONE
)
2933 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2935 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2939 /* "A negative field width is taken as a '-' flag
2940 followed by a positive field width." */
2947 const FCHAR_T
*digitp
= dp
->width_start
;
2950 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2951 while (digitp
!= dp
->width_end
);
2957 if (dp
->precision_start
!= dp
->precision_end
)
2959 if (dp
->precision_arg_index
!= ARG_NONE
)
2963 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2965 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2966 /* "A negative precision is taken as if the precision
2976 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2979 while (digitp
!= dp
->precision_end
)
2980 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2985 /* Allocate a temporary buffer of sufficient size. */
2986 if (type
== TYPE_LONGDOUBLE
)
2988 (unsigned int) ((LDBL_DIG
+ 1)
2989 * 0.831 /* decimal -> hexadecimal */
2991 + 1; /* turn floor into ceil */
2994 (unsigned int) ((DBL_DIG
+ 1)
2995 * 0.831 /* decimal -> hexadecimal */
2997 + 1; /* turn floor into ceil */
2998 if (tmp_length
< precision
)
2999 tmp_length
= precision
;
3000 /* Account for sign, decimal point etc. */
3001 tmp_length
= xsum (tmp_length
, 12);
3003 if (tmp_length
< width
)
3006 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3008 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3012 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3014 if (size_overflow_p (tmp_memsize
))
3015 /* Overflow, would lead to out of memory. */
3017 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3019 /* Out of memory. */
3025 if (type
== TYPE_LONGDOUBLE
)
3027 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3028 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3032 if (dp
->conversion
== 'A')
3034 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3038 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3044 DECL_LONG_DOUBLE_ROUNDING
3046 BEGIN_LONG_DOUBLE_ROUNDING ();
3048 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3056 else if (flags
& FLAG_SHOWSIGN
)
3058 else if (flags
& FLAG_SPACE
)
3061 if (arg
> 0.0L && arg
+ arg
== arg
)
3063 if (dp
->conversion
== 'A')
3065 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3069 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3075 long double mantissa
;
3078 mantissa
= printf_frexpl (arg
, &exponent
);
3086 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
3088 /* Round the mantissa. */
3089 long double tail
= mantissa
;
3092 for (q
= precision
; ; q
--)
3094 int digit
= (int) tail
;
3098 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
3107 for (q
= precision
; q
> 0; q
--)
3113 *p
++ = dp
->conversion
- 'A' + 'X';
3118 digit
= (int) mantissa
;
3121 if ((flags
& FLAG_ALT
)
3122 || mantissa
> 0.0L || precision
> 0)
3124 *p
++ = decimal_point_char ();
3125 /* This loop terminates because we assume
3126 that FLT_RADIX is a power of 2. */
3127 while (mantissa
> 0.0L)
3130 digit
= (int) mantissa
;
3135 : dp
->conversion
- 10);
3139 while (precision
> 0)
3146 *p
++ = dp
->conversion
- 'A' + 'P';
3147 # if WIDE_CHAR_VERSION
3149 static const wchar_t decimal_format
[] =
3150 { '%', '+', 'd', '\0' };
3151 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3156 if (sizeof (DCHAR_T
) == 1)
3158 sprintf ((char *) p
, "%+d", exponent
);
3166 sprintf (expbuf
, "%+d", exponent
);
3167 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3173 END_LONG_DOUBLE_ROUNDING ();
3181 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3182 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3186 if (dp
->conversion
== 'A')
3188 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3192 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3199 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3207 else if (flags
& FLAG_SHOWSIGN
)
3209 else if (flags
& FLAG_SPACE
)
3212 if (arg
> 0.0 && arg
+ arg
== arg
)
3214 if (dp
->conversion
== 'A')
3216 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3220 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3229 mantissa
= printf_frexp (arg
, &exponent
);
3237 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
3239 /* Round the mantissa. */
3240 double tail
= mantissa
;
3243 for (q
= precision
; ; q
--)
3245 int digit
= (int) tail
;
3249 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
3258 for (q
= precision
; q
> 0; q
--)
3264 *p
++ = dp
->conversion
- 'A' + 'X';
3269 digit
= (int) mantissa
;
3272 if ((flags
& FLAG_ALT
)
3273 || mantissa
> 0.0 || precision
> 0)
3275 *p
++ = decimal_point_char ();
3276 /* This loop terminates because we assume
3277 that FLT_RADIX is a power of 2. */
3278 while (mantissa
> 0.0)
3281 digit
= (int) mantissa
;
3286 : dp
->conversion
- 10);
3290 while (precision
> 0)
3297 *p
++ = dp
->conversion
- 'A' + 'P';
3298 # if WIDE_CHAR_VERSION
3300 static const wchar_t decimal_format
[] =
3301 { '%', '+', 'd', '\0' };
3302 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3307 if (sizeof (DCHAR_T
) == 1)
3309 sprintf ((char *) p
, "%+d", exponent
);
3317 sprintf (expbuf
, "%+d", exponent
);
3318 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3329 /* The generated string now extends from tmp to p, with the
3330 zero padding insertion point being at pad_ptr. */
3335 size_t pad
= width
- count
;
3336 DCHAR_T
*end
= p
+ pad
;
3338 if (flags
& FLAG_LEFT
)
3340 /* Pad with spaces on the right. */
3341 for (; pad
> 0; pad
--)
3344 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3346 /* Pad with zeroes. */
3351 for (; pad
> 0; pad
--)
3356 /* Pad with spaces on the left. */
3361 for (; pad
> 0; pad
--)
3370 if (count
>= tmp_length
)
3371 /* tmp_length was incorrectly calculated - fix the
3375 /* Make room for the result. */
3376 if (count
>= allocated
- length
)
3378 size_t n
= xsum (length
, count
);
3380 ENSURE_ALLOCATION (n
);
3383 /* Append the result. */
3384 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3390 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3391 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
3392 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
3393 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
3394 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
3396 # if NEED_PRINTF_DOUBLE
3397 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3398 # elif NEED_PRINTF_INFINITE_DOUBLE
3399 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3400 /* The systems (mingw) which produce wrong output
3401 for Inf, -Inf, and NaN also do so for -0.0.
3402 Therefore we treat this case here as well. */
3403 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
3405 # if NEED_PRINTF_LONG_DOUBLE
3406 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3407 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3408 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3409 /* Some systems produce wrong output for Inf,
3410 -Inf, and NaN. Some systems in this category
3411 (IRIX 5.3) also do so for -0.0. Therefore we
3412 treat this case here as well. */
3413 && is_infinite_or_zerol (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
3417 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3418 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3420 int flags
= dp
->flags
;
3426 DCHAR_T tmpbuf
[700];
3432 if (dp
->width_start
!= dp
->width_end
)
3434 if (dp
->width_arg_index
!= ARG_NONE
)
3438 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3440 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3444 /* "A negative field width is taken as a '-' flag
3445 followed by a positive field width." */
3452 const FCHAR_T
*digitp
= dp
->width_start
;
3455 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3456 while (digitp
!= dp
->width_end
);
3462 if (dp
->precision_start
!= dp
->precision_end
)
3464 if (dp
->precision_arg_index
!= ARG_NONE
)
3468 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3470 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3471 /* "A negative precision is taken as if the precision
3481 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3484 while (digitp
!= dp
->precision_end
)
3485 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3490 /* POSIX specifies the default precision to be 6 for %f, %F,
3491 %e, %E, but not for %g, %G. Implementations appear to use
3492 the same default precision also for %g, %G. But for %a, %A,
3493 the default precision is 0. */
3495 if (!(dp
->conversion
== 'a' || dp
->conversion
== 'A'))
3498 /* Allocate a temporary buffer of sufficient size. */
3499 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3500 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
3501 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3502 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
3503 # elif NEED_PRINTF_LONG_DOUBLE
3504 tmp_length
= LDBL_DIG
+ 1;
3505 # elif NEED_PRINTF_DOUBLE
3506 tmp_length
= DBL_DIG
+ 1;
3510 if (tmp_length
< precision
)
3511 tmp_length
= precision
;
3512 # if NEED_PRINTF_LONG_DOUBLE
3513 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3514 if (type
== TYPE_LONGDOUBLE
)
3516 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3518 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3519 if (!(isnanl (arg
) || arg
+ arg
== arg
))
3521 /* arg is finite and nonzero. */
3522 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
3523 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3524 tmp_length
= exponent
+ precision
;
3528 # if NEED_PRINTF_DOUBLE
3529 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3530 if (type
== TYPE_DOUBLE
)
3532 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3534 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3535 if (!(isnand (arg
) || arg
+ arg
== arg
))
3537 /* arg is finite and nonzero. */
3538 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
3539 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3540 tmp_length
= exponent
+ precision
;
3544 /* Account for sign, decimal point etc. */
3545 tmp_length
= xsum (tmp_length
, 12);
3547 if (tmp_length
< width
)
3550 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3552 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3556 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3558 if (size_overflow_p (tmp_memsize
))
3559 /* Overflow, would lead to out of memory. */
3561 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3563 /* Out of memory. */
3570 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3571 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3572 if (type
== TYPE_LONGDOUBLE
)
3575 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3579 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3581 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3585 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3591 DECL_LONG_DOUBLE_ROUNDING
3593 BEGIN_LONG_DOUBLE_ROUNDING ();
3595 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3603 else if (flags
& FLAG_SHOWSIGN
)
3605 else if (flags
& FLAG_SPACE
)
3608 if (arg
> 0.0L && arg
+ arg
== arg
)
3610 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3612 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3616 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3621 # if NEED_PRINTF_LONG_DOUBLE
3624 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3630 scale10_round_decimal_long_double (arg
, precision
);
3633 END_LONG_DOUBLE_ROUNDING ();
3636 ndigits
= strlen (digits
);
3638 if (ndigits
> precision
)
3642 *p
++ = digits
[ndigits
];
3644 while (ndigits
> precision
);
3647 /* Here ndigits <= precision. */
3648 if ((flags
& FLAG_ALT
) || precision
> 0)
3650 *p
++ = decimal_point_char ();
3651 for (; precision
> ndigits
; precision
--)
3656 *p
++ = digits
[ndigits
];
3662 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3670 if ((flags
& FLAG_ALT
) || precision
> 0)
3672 *p
++ = decimal_point_char ();
3673 for (; precision
> 0; precision
--)
3684 exponent
= floorlog10l (arg
);
3689 scale10_round_decimal_long_double (arg
,
3690 (int)precision
- exponent
);
3693 END_LONG_DOUBLE_ROUNDING ();
3696 ndigits
= strlen (digits
);
3698 if (ndigits
== precision
+ 1)
3700 if (ndigits
< precision
3701 || ndigits
> precision
+ 2)
3702 /* The exponent was not guessed
3703 precisely enough. */
3706 /* None of two values of exponent is
3707 the right one. Prevent an endless
3711 if (ndigits
== precision
)
3717 /* Here ndigits = precision+1. */
3718 if (is_borderline (digits
, precision
))
3720 /* Maybe the exponent guess was too high
3721 and a smaller exponent can be reached
3722 by turning a 10...0 into 9...9x. */
3724 scale10_round_decimal_long_double (arg
,
3725 (int)precision
- exponent
+ 1);
3726 if (digits2
== NULL
)
3729 END_LONG_DOUBLE_ROUNDING ();
3732 if (strlen (digits2
) == precision
+ 1)
3741 /* Here ndigits = precision+1. */
3743 *p
++ = digits
[--ndigits
];
3744 if ((flags
& FLAG_ALT
) || precision
> 0)
3746 *p
++ = decimal_point_char ();
3750 *p
++ = digits
[ndigits
];
3757 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3758 # if WIDE_CHAR_VERSION
3760 static const wchar_t decimal_format
[] =
3761 { '%', '+', '.', '2', 'd', '\0' };
3762 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3767 if (sizeof (DCHAR_T
) == 1)
3769 sprintf ((char *) p
, "%+.2d", exponent
);
3777 sprintf (expbuf
, "%+.2d", exponent
);
3778 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3783 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3787 /* precision >= 1. */
3790 /* The exponent is 0, >= -4, < precision.
3791 Use fixed-point notation. */
3793 size_t ndigits
= precision
;
3794 /* Number of trailing zeroes that have to be
3797 (flags
& FLAG_ALT
? 0 : precision
- 1);
3801 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3803 *p
++ = decimal_point_char ();
3804 while (ndigits
> nzeroes
)
3820 exponent
= floorlog10l (arg
);
3825 scale10_round_decimal_long_double (arg
,
3826 (int)(precision
- 1) - exponent
);
3829 END_LONG_DOUBLE_ROUNDING ();
3832 ndigits
= strlen (digits
);
3834 if (ndigits
== precision
)
3836 if (ndigits
< precision
- 1
3837 || ndigits
> precision
+ 1)
3838 /* The exponent was not guessed
3839 precisely enough. */
3842 /* None of two values of exponent is
3843 the right one. Prevent an endless
3847 if (ndigits
< precision
)
3853 /* Here ndigits = precision. */
3854 if (is_borderline (digits
, precision
- 1))
3856 /* Maybe the exponent guess was too high
3857 and a smaller exponent can be reached
3858 by turning a 10...0 into 9...9x. */
3860 scale10_round_decimal_long_double (arg
,
3861 (int)(precision
- 1) - exponent
+ 1);
3862 if (digits2
== NULL
)
3865 END_LONG_DOUBLE_ROUNDING ();
3868 if (strlen (digits2
) == precision
)
3877 /* Here ndigits = precision. */
3879 /* Determine the number of trailing zeroes
3880 that have to be dropped. */
3882 if ((flags
& FLAG_ALT
) == 0)
3883 while (nzeroes
< ndigits
3884 && digits
[nzeroes
] == '0')
3887 /* The exponent is now determined. */
3889 && exponent
< (long)precision
)
3891 /* Fixed-point notation:
3892 max(exponent,0)+1 digits, then the
3893 decimal point, then the remaining
3894 digits without trailing zeroes. */
3897 size_t ecount
= exponent
+ 1;
3898 /* Note: count <= precision = ndigits. */
3899 for (; ecount
> 0; ecount
--)
3900 *p
++ = digits
[--ndigits
];
3901 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3903 *p
++ = decimal_point_char ();
3904 while (ndigits
> nzeroes
)
3907 *p
++ = digits
[ndigits
];
3913 size_t ecount
= -exponent
- 1;
3915 *p
++ = decimal_point_char ();
3916 for (; ecount
> 0; ecount
--)
3918 while (ndigits
> nzeroes
)
3921 *p
++ = digits
[ndigits
];
3927 /* Exponential notation. */
3928 *p
++ = digits
[--ndigits
];
3929 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3931 *p
++ = decimal_point_char ();
3932 while (ndigits
> nzeroes
)
3935 *p
++ = digits
[ndigits
];
3938 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
3939 # if WIDE_CHAR_VERSION
3941 static const wchar_t decimal_format
[] =
3942 { '%', '+', '.', '2', 'd', '\0' };
3943 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3948 if (sizeof (DCHAR_T
) == 1)
3950 sprintf ((char *) p
, "%+.2d", exponent
);
3958 sprintf (expbuf
, "%+.2d", exponent
);
3959 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3971 /* arg is finite. */
3977 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3980 if ((flags
& FLAG_ALT
) || precision
> 0)
3982 *p
++ = decimal_point_char ();
3983 for (; precision
> 0; precision
--)
3987 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3990 if ((flags
& FLAG_ALT
) || precision
> 0)
3992 *p
++ = decimal_point_char ();
3993 for (; precision
> 0; precision
--)
3996 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4001 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4004 if (flags
& FLAG_ALT
)
4007 (precision
> 0 ? precision
- 1 : 0);
4008 *p
++ = decimal_point_char ();
4009 for (; ndigits
> 0; --ndigits
)
4013 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
4016 *p
++ = dp
->conversion
- 'A' + 'X';
4019 if ((flags
& FLAG_ALT
) || precision
> 0)
4021 *p
++ = decimal_point_char ();
4022 for (; precision
> 0; precision
--)
4025 *p
++ = dp
->conversion
- 'A' + 'P';
4034 END_LONG_DOUBLE_ROUNDING ();
4037 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4041 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4043 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4047 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4049 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
4053 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
4060 if (signbit (arg
)) /* arg < 0.0 or negative zero */
4068 else if (flags
& FLAG_SHOWSIGN
)
4070 else if (flags
& FLAG_SPACE
)
4073 if (arg
> 0.0 && arg
+ arg
== arg
)
4075 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4077 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
4081 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
4086 # if NEED_PRINTF_DOUBLE
4089 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4095 scale10_round_decimal_double (arg
, precision
);
4098 ndigits
= strlen (digits
);
4100 if (ndigits
> precision
)
4104 *p
++ = digits
[ndigits
];
4106 while (ndigits
> precision
);
4109 /* Here ndigits <= precision. */
4110 if ((flags
& FLAG_ALT
) || precision
> 0)
4112 *p
++ = decimal_point_char ();
4113 for (; precision
> ndigits
; precision
--)
4118 *p
++ = digits
[ndigits
];
4124 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4132 if ((flags
& FLAG_ALT
) || precision
> 0)
4134 *p
++ = decimal_point_char ();
4135 for (; precision
> 0; precision
--)
4146 exponent
= floorlog10 (arg
);
4151 scale10_round_decimal_double (arg
,
4152 (int)precision
- exponent
);
4155 ndigits
= strlen (digits
);
4157 if (ndigits
== precision
+ 1)
4159 if (ndigits
< precision
4160 || ndigits
> precision
+ 2)
4161 /* The exponent was not guessed
4162 precisely enough. */
4165 /* None of two values of exponent is
4166 the right one. Prevent an endless
4170 if (ndigits
== precision
)
4176 /* Here ndigits = precision+1. */
4177 if (is_borderline (digits
, precision
))
4179 /* Maybe the exponent guess was too high
4180 and a smaller exponent can be reached
4181 by turning a 10...0 into 9...9x. */
4183 scale10_round_decimal_double (arg
,
4184 (int)precision
- exponent
+ 1);
4185 if (digits2
== NULL
)
4190 if (strlen (digits2
) == precision
+ 1)
4199 /* Here ndigits = precision+1. */
4201 *p
++ = digits
[--ndigits
];
4202 if ((flags
& FLAG_ALT
) || precision
> 0)
4204 *p
++ = decimal_point_char ();
4208 *p
++ = digits
[ndigits
];
4215 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4216 # if WIDE_CHAR_VERSION
4218 static const wchar_t decimal_format
[] =
4219 /* Produce the same number of exponent digits
4220 as the native printf implementation. */
4221 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4222 { '%', '+', '.', '3', 'd', '\0' };
4224 { '%', '+', '.', '2', 'd', '\0' };
4226 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4232 static const char decimal_format
[] =
4233 /* Produce the same number of exponent digits
4234 as the native printf implementation. */
4235 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4240 if (sizeof (DCHAR_T
) == 1)
4242 sprintf ((char *) p
, decimal_format
, exponent
);
4250 sprintf (expbuf
, decimal_format
, exponent
);
4251 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4257 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4261 /* precision >= 1. */
4264 /* The exponent is 0, >= -4, < precision.
4265 Use fixed-point notation. */
4267 size_t ndigits
= precision
;
4268 /* Number of trailing zeroes that have to be
4271 (flags
& FLAG_ALT
? 0 : precision
- 1);
4275 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4277 *p
++ = decimal_point_char ();
4278 while (ndigits
> nzeroes
)
4294 exponent
= floorlog10 (arg
);
4299 scale10_round_decimal_double (arg
,
4300 (int)(precision
- 1) - exponent
);
4303 ndigits
= strlen (digits
);
4305 if (ndigits
== precision
)
4307 if (ndigits
< precision
- 1
4308 || ndigits
> precision
+ 1)
4309 /* The exponent was not guessed
4310 precisely enough. */
4313 /* None of two values of exponent is
4314 the right one. Prevent an endless
4318 if (ndigits
< precision
)
4324 /* Here ndigits = precision. */
4325 if (is_borderline (digits
, precision
- 1))
4327 /* Maybe the exponent guess was too high
4328 and a smaller exponent can be reached
4329 by turning a 10...0 into 9...9x. */
4331 scale10_round_decimal_double (arg
,
4332 (int)(precision
- 1) - exponent
+ 1);
4333 if (digits2
== NULL
)
4338 if (strlen (digits2
) == precision
)
4347 /* Here ndigits = precision. */
4349 /* Determine the number of trailing zeroes
4350 that have to be dropped. */
4352 if ((flags
& FLAG_ALT
) == 0)
4353 while (nzeroes
< ndigits
4354 && digits
[nzeroes
] == '0')
4357 /* The exponent is now determined. */
4359 && exponent
< (long)precision
)
4361 /* Fixed-point notation:
4362 max(exponent,0)+1 digits, then the
4363 decimal point, then the remaining
4364 digits without trailing zeroes. */
4367 size_t ecount
= exponent
+ 1;
4368 /* Note: ecount <= precision = ndigits. */
4369 for (; ecount
> 0; ecount
--)
4370 *p
++ = digits
[--ndigits
];
4371 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4373 *p
++ = decimal_point_char ();
4374 while (ndigits
> nzeroes
)
4377 *p
++ = digits
[ndigits
];
4383 size_t ecount
= -exponent
- 1;
4385 *p
++ = decimal_point_char ();
4386 for (; ecount
> 0; ecount
--)
4388 while (ndigits
> nzeroes
)
4391 *p
++ = digits
[ndigits
];
4397 /* Exponential notation. */
4398 *p
++ = digits
[--ndigits
];
4399 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4401 *p
++ = decimal_point_char ();
4402 while (ndigits
> nzeroes
)
4405 *p
++ = digits
[ndigits
];
4408 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4409 # if WIDE_CHAR_VERSION
4411 static const wchar_t decimal_format
[] =
4412 /* Produce the same number of exponent digits
4413 as the native printf implementation. */
4414 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4415 { '%', '+', '.', '3', 'd', '\0' };
4417 { '%', '+', '.', '2', 'd', '\0' };
4419 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4425 static const char decimal_format
[] =
4426 /* Produce the same number of exponent digits
4427 as the native printf implementation. */
4428 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4433 if (sizeof (DCHAR_T
) == 1)
4435 sprintf ((char *) p
, decimal_format
, exponent
);
4443 sprintf (expbuf
, decimal_format
, exponent
);
4444 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4457 /* arg is finite. */
4463 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4466 if ((flags
& FLAG_ALT
) || precision
> 0)
4468 *p
++ = decimal_point_char ();
4469 for (; precision
> 0; precision
--)
4473 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4476 if ((flags
& FLAG_ALT
) || precision
> 0)
4478 *p
++ = decimal_point_char ();
4479 for (; precision
> 0; precision
--)
4482 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4484 /* Produce the same number of exponent digits as
4485 the native printf implementation. */
4486 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4492 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4495 if (flags
& FLAG_ALT
)
4498 (precision
> 0 ? precision
- 1 : 0);
4499 *p
++ = decimal_point_char ();
4500 for (; ndigits
> 0; --ndigits
)
4512 /* The generated string now extends from tmp to p, with the
4513 zero padding insertion point being at pad_ptr. */
4518 size_t pad
= width
- count
;
4519 DCHAR_T
*end
= p
+ pad
;
4521 if (flags
& FLAG_LEFT
)
4523 /* Pad with spaces on the right. */
4524 for (; pad
> 0; pad
--)
4527 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4529 /* Pad with zeroes. */
4534 for (; pad
> 0; pad
--)
4539 /* Pad with spaces on the left. */
4544 for (; pad
> 0; pad
--)
4553 if (count
>= tmp_length
)
4554 /* tmp_length was incorrectly calculated - fix the
4558 /* Make room for the result. */
4559 if (count
>= allocated
- length
)
4561 size_t n
= xsum (length
, count
);
4563 ENSURE_ALLOCATION (n
);
4566 /* Append the result. */
4567 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4575 arg_type type
= a
.arg
[dp
->arg_index
].type
;
4576 int flags
= dp
->flags
;
4577 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4580 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4583 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4587 #if NEED_PRINTF_UNBOUNDED_PRECISION
4590 # define prec_ourselves 0
4592 #if NEED_PRINTF_FLAG_LEFTADJUST
4593 # define pad_ourselves 1
4594 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4597 # define pad_ourselves 0
4600 unsigned int prefix_count
;
4601 int prefixes
[2] IF_LINT (= { 0 });
4605 TCHAR_T tmpbuf
[700];
4609 #if !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 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4614 if (dp
->width_start
!= dp
->width_end
)
4616 if (dp
->width_arg_index
!= ARG_NONE
)
4620 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4622 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
4626 /* "A negative field width is taken as a '-' flag
4627 followed by a positive field width." */
4634 const FCHAR_T
*digitp
= dp
->width_start
;
4637 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
4638 while (digitp
!= dp
->width_end
);
4640 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4646 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4649 if (dp
->precision_start
!= dp
->precision_end
)
4651 if (dp
->precision_arg_index
!= ARG_NONE
)
4655 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4657 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4658 /* "A negative precision is taken as if the precision
4668 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
4671 while (digitp
!= dp
->precision_end
)
4672 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
4678 /* Decide whether to handle the precision ourselves. */
4679 #if NEED_PRINTF_UNBOUNDED_PRECISION
4680 switch (dp
->conversion
)
4682 case 'd': case 'i': case 'u':
4684 case 'x': case 'X': case 'p':
4685 prec_ourselves
= has_precision
&& (precision
> 0);
4693 /* Decide whether to perform the padding ourselves. */
4694 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4695 switch (dp
->conversion
)
4697 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4698 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4699 to perform the padding after this conversion. Functions
4700 with unistdio extensions perform the padding based on
4701 character count rather than element count. */
4704 # if NEED_PRINTF_FLAG_ZERO
4705 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4711 pad_ourselves
= prec_ourselves
;
4717 /* Allocate a temporary buffer of sufficient size for calling
4720 MAX_ROOM_NEEDED (&a
, dp
->arg_index
, dp
->conversion
, type
,
4721 flags
, width
, has_precision
, precision
,
4724 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
4728 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
4730 if (size_overflow_p (tmp_memsize
))
4731 /* Overflow, would lead to out of memory. */
4733 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
4735 /* Out of memory. */
4740 /* Construct the format string for calling snprintf or
4744 #if NEED_PRINTF_FLAG_GROUPING
4745 /* The underlying implementation doesn't support the ' flag.
4746 Produce no grouping characters in this case; this is
4747 acceptable because the grouping is locale dependent. */
4749 if (flags
& FLAG_GROUP
)
4752 if (flags
& FLAG_LEFT
)
4754 if (flags
& FLAG_SHOWSIGN
)
4756 if (flags
& FLAG_SPACE
)
4758 if (flags
& FLAG_ALT
)
4760 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4761 if (flags
& FLAG_LOCALIZED
)
4766 if (flags
& FLAG_ZERO
)
4768 if (dp
->width_start
!= dp
->width_end
)
4770 size_t n
= dp
->width_end
- dp
->width_start
;
4771 /* The width specification is known to consist only
4772 of standard ASCII characters. */
4773 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4775 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
4780 const FCHAR_T
*mp
= dp
->width_start
;
4787 if (!prec_ourselves
)
4789 if (dp
->precision_start
!= dp
->precision_end
)
4791 size_t n
= dp
->precision_end
- dp
->precision_start
;
4792 /* The precision specification is known to consist only
4793 of standard ASCII characters. */
4794 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4796 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
4801 const FCHAR_T
*mp
= dp
->precision_start
;
4812 case TYPE_LONGLONGINT
:
4813 case TYPE_ULONGLONGINT
:
4814 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4827 case TYPE_WIDE_CHAR
:
4830 case TYPE_WIDE_STRING
:
4834 case TYPE_LONGDOUBLE
:
4840 #if NEED_PRINTF_DIRECTIVE_F
4841 if (dp
->conversion
== 'F')
4845 *fbp
= dp
->conversion
;
4847 # if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4852 /* On glibc2 systems from glibc >= 2.3 - probably also older
4853 ones - we know that snprintf's return value conforms to
4854 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
4855 gl_SNPRINTF_TRUNCATION_C99 pass.
4856 Therefore we can avoid using %n in this situation.
4857 On glibc2 systems from 2004-10-18 or newer, the use of %n
4858 in format strings in writable memory may crash the program
4859 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4860 in this situation. */
4861 /* On native Windows systems (such as mingw), we can avoid using
4863 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4864 snprintf does not write more than the specified number
4865 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4866 '4', '5', '6' into buf, not '4', '5', '\0'.)
4867 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4868 allows us to recognize the case of an insufficient
4869 buffer size: it returns -1 in this case.
4870 On native Windows systems (such as mingw) where the OS is
4871 Windows Vista, the use of %n in format strings by default
4872 crashes the program. See
4873 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4874 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4875 So we should avoid %n in this situation. */
4882 /* Construct the arguments for calling snprintf or sprintf. */
4884 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
4886 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4888 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
4890 if (!prec_ourselves
&& dp
->precision_arg_index
!= ARG_NONE
)
4892 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4894 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4898 /* The SNPRINTF result is appended after result[0..length].
4899 The latter is an array of DCHAR_T; SNPRINTF appends an
4900 array of TCHAR_T to it. This is possible because
4901 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4902 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4903 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4904 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4905 where an snprintf() with maxlen==1 acts like sprintf(). */
4906 ENSURE_ALLOCATION (xsum (length
,
4907 (2 + TCHARS_PER_DCHAR
- 1)
4908 / TCHARS_PER_DCHAR
));
4909 /* Prepare checking whether snprintf returns the count
4911 *(TCHAR_T
*) (result
+ length
) = '\0';
4922 size_t maxlen
= allocated
- length
;
4923 /* SNPRINTF can fail if its second argument is
4925 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
4926 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
4927 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
4928 # define SNPRINTF_BUF(arg) \
4929 switch (prefix_count) \
4932 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4937 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4939 prefixes[0], arg, &count); \
4942 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4944 prefixes[0], prefixes[1], arg, \
4951 # define SNPRINTF_BUF(arg) \
4952 switch (prefix_count) \
4955 count = sprintf (tmp, buf, arg); \
4958 count = sprintf (tmp, buf, prefixes[0], arg); \
4961 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4974 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
4980 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
4986 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
4992 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
4998 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
5004 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
5010 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
5016 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
5021 case TYPE_LONGLONGINT
:
5023 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
5027 case TYPE_ULONGLONGINT
:
5029 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
5036 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
5040 case TYPE_LONGDOUBLE
:
5042 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
5048 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
5053 case TYPE_WIDE_CHAR
:
5055 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
5062 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
5067 case TYPE_WIDE_STRING
:
5069 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
5076 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
5085 /* Portability: Not all implementations of snprintf()
5086 are ISO C 99 compliant. Determine the number of
5087 bytes that snprintf() has produced or would have
5091 /* Verify that snprintf() has NUL-terminated its
5094 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
5096 /* Portability hack. */
5097 if (retcount
> count
)
5102 /* snprintf() doesn't understand the '%n'
5106 /* Don't use the '%n' directive; instead, look
5107 at the snprintf() return value. */
5113 /* Look at the snprintf() return value. */
5116 # if !HAVE_SNPRINTF_RETVAL_C99
5117 /* HP-UX 10.20 snprintf() is doubly deficient:
5118 It doesn't understand the '%n' directive,
5119 *and* it returns -1 (rather than the length
5120 that would have been required) when the
5121 buffer is too small.
5122 But a failure at this point can also come
5123 from other reasons than a too small buffer,
5124 such as an invalid wide string argument to
5125 the %ls directive, or possibly an invalid
5126 floating-point argument. */
5128 MAX_ROOM_NEEDED (&a
, dp
->arg_index
,
5129 dp
->conversion
, type
, flags
,
5132 precision
, pad_ourselves
);
5134 if (maxlen
< tmp_length
)
5136 /* Make more room. But try to do through
5137 this reallocation only once. */
5138 size_t bigger_need
=
5141 TCHARS_PER_DCHAR
- 1)
5142 / TCHARS_PER_DCHAR
);
5143 /* And always grow proportionally.
5144 (There may be several arguments, each
5145 needing a little more room than the
5147 size_t bigger_need2
=
5148 xsum (xtimes (allocated
, 2), 12);
5149 if (bigger_need
< bigger_need2
)
5150 bigger_need
= bigger_need2
;
5151 ENSURE_ALLOCATION (bigger_need
);
5162 /* Attempt to handle failure. */
5165 /* SNPRINTF or sprintf failed. Save and use the errno
5166 that it has set, if any. */
5167 int saved_errno
= errno
;
5168 if (saved_errno
== 0)
5170 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5171 saved_errno
= EILSEQ
;
5173 saved_errno
= EINVAL
;
5176 if (!(result
== resultbuf
|| result
== NULL
))
5178 if (buf_malloced
!= NULL
)
5179 free (buf_malloced
);
5182 errno
= saved_errno
;
5187 /* Handle overflow of the allocated buffer.
5188 If such an overflow occurs, a C99 compliant snprintf()
5189 returns a count >= maxlen. However, a non-compliant
5190 snprintf() function returns only count = maxlen - 1. To
5191 cover both cases, test whether count >= maxlen - 1. */
5192 if ((unsigned int) count
+ 1 >= maxlen
)
5194 /* If maxlen already has attained its allowed maximum,
5195 allocating more memory will not increase maxlen.
5196 Instead of looping, bail out. */
5197 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
5201 /* Need at least (count + 1) * sizeof (TCHAR_T)
5202 bytes. (The +1 is for the trailing NUL.)
5203 But ask for (count + 2) * sizeof (TCHAR_T)
5204 bytes, so that in the next round, we likely get
5205 maxlen > (unsigned int) count + 1
5206 and so we don't get here again.
5207 And allocate proportionally, to avoid looping
5208 eternally if snprintf() reports a too small
5212 ((unsigned int) count
+ 2
5213 + TCHARS_PER_DCHAR
- 1)
5214 / TCHARS_PER_DCHAR
),
5215 xtimes (allocated
, 2));
5217 ENSURE_ALLOCATION (n
);
5223 #if NEED_PRINTF_UNBOUNDED_PRECISION
5226 /* Handle the precision. */
5229 (TCHAR_T
*) (result
+ length
);
5233 size_t prefix_count
;
5237 /* Put the additional zeroes after the sign. */
5239 && (*prec_ptr
== '-' || *prec_ptr
== '+'
5240 || *prec_ptr
== ' '))
5242 /* Put the additional zeroes after the 0x prefix if
5243 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5245 && prec_ptr
[0] == '0'
5246 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
5249 move
= count
- prefix_count
;
5250 if (precision
> move
)
5252 /* Insert zeroes. */
5253 size_t insert
= precision
- move
;
5259 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
5260 / TCHARS_PER_DCHAR
);
5261 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5262 ENSURE_ALLOCATION (n
);
5263 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5264 prec_ptr
= (TCHAR_T
*) (result
+ length
);
5267 prec_end
= prec_ptr
+ count
;
5268 prec_ptr
+= prefix_count
;
5270 while (prec_end
> prec_ptr
)
5273 prec_end
[insert
] = prec_end
[0];
5279 while (prec_end
> prec_ptr
);
5287 if (count
>= tmp_length
)
5288 /* tmp_length was incorrectly calculated - fix the
5294 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5295 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5297 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5299 The result string is not certainly ASCII. */
5300 const TCHAR_T
*tmpsrc
;
5303 /* This code assumes that TCHAR_T is 'char'. */
5304 verify (sizeof (TCHAR_T
) == 1);
5306 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5311 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5312 iconveh_question_mark
,
5318 int saved_errno
= errno
;
5319 if (!(result
== resultbuf
|| result
== NULL
))
5321 if (buf_malloced
!= NULL
)
5322 free (buf_malloced
);
5324 errno
= saved_errno
;
5327 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
5328 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
5334 /* The result string is ASCII.
5335 Simple 1:1 conversion. */
5337 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5338 no-op conversion, in-place on the array starting
5339 at (result + length). */
5340 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
5343 const TCHAR_T
*tmpsrc
;
5348 if (result
== resultbuf
)
5350 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5351 /* ENSURE_ALLOCATION will not move tmpsrc
5352 (because it's part of resultbuf). */
5353 ENSURE_ALLOCATION (xsum (length
, count
));
5357 /* ENSURE_ALLOCATION will move the array
5358 (because it uses realloc(). */
5359 ENSURE_ALLOCATION (xsum (length
, count
));
5360 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5364 ENSURE_ALLOCATION (xsum (length
, count
));
5366 tmpdst
= result
+ length
;
5367 /* Copy backwards, because of overlapping. */
5370 for (n
= count
; n
> 0; n
--)
5371 *--tmpdst
= *--tmpsrc
;
5376 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5377 /* Make room for the result. */
5378 if (count
> allocated
- length
)
5380 /* Need at least count elements. But allocate
5383 xmax (xsum (length
, count
), xtimes (allocated
, 2));
5385 ENSURE_ALLOCATION (n
);
5389 /* Here count <= allocated - length. */
5391 /* Perform padding. */
5392 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5393 if (pad_ourselves
&& has_width
)
5396 # if ENABLE_UNISTDIO
5397 /* Outside POSIX, it's preferable to compare the width
5398 against the number of _characters_ of the converted
5400 w
= DCHAR_MBSNLEN (result
+ length
, count
);
5402 /* The width is compared against the number of _bytes_
5403 of the converted value, says POSIX. */
5408 size_t pad
= width
- w
;
5410 /* Make room for the result. */
5411 if (xsum (count
, pad
) > allocated
- length
)
5413 /* Need at least count + pad elements. But
5414 allocate proportionally. */
5416 xmax (xsum3 (length
, count
, pad
),
5417 xtimes (allocated
, 2));
5421 ENSURE_ALLOCATION (n
);
5424 ENSURE_ALLOCATION (n
);
5427 /* Here count + pad <= allocated - length. */
5430 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5431 DCHAR_T
* const rp
= result
+ length
;
5433 DCHAR_T
* const rp
= tmp
;
5435 DCHAR_T
*p
= rp
+ count
;
5436 DCHAR_T
*end
= p
+ pad
;
5438 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5439 if (dp
->conversion
== 'c'
5440 || dp
->conversion
== 's')
5441 /* No zero-padding for string directives. */
5446 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
5447 /* No zero-padding of "inf" and "nan". */
5448 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
5449 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
5452 /* The generated string now extends from rp to p,
5453 with the zero padding insertion point being at
5456 count
= count
+ pad
; /* = end - rp */
5458 if (flags
& FLAG_LEFT
)
5460 /* Pad with spaces on the right. */
5461 for (; pad
> 0; pad
--)
5464 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
5466 /* Pad with zeroes. */
5471 for (; pad
> 0; pad
--)
5476 /* Pad with spaces on the left. */
5481 for (; pad
> 0; pad
--)
5489 /* Here still count <= allocated - length. */
5491 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5492 /* The snprintf() result did fit. */
5494 /* Append the sprintf() result. */
5495 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
5502 #if NEED_PRINTF_DIRECTIVE_F
5503 if (dp
->conversion
== 'F')
5505 /* Convert the %f result to upper case for %F. */
5506 DCHAR_T
*rp
= result
+ length
;
5508 for (rc
= count
; rc
> 0; rc
--, rp
++)
5509 if (*rp
>= 'a' && *rp
<= 'z')
5510 *rp
= *rp
- 'a' + 'A';
5518 #undef pad_ourselves
5519 #undef prec_ourselves
5524 /* Add the final NUL. */
5525 ENSURE_ALLOCATION (xsum (length
, 1));
5526 result
[length
] = '\0';
5528 if (result
!= resultbuf
&& length
+ 1 < allocated
)
5530 /* Shrink the allocated memory if possible. */
5533 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
5538 if (buf_malloced
!= NULL
)
5539 free (buf_malloced
);
5542 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5543 says that snprintf() fails with errno = EOVERFLOW in this case, but
5544 that's only because snprintf() returns an 'int'. This function does
5545 not have this limitation. */
5550 if (!(result
== resultbuf
|| result
== NULL
))
5552 if (buf_malloced
!= NULL
)
5553 free (buf_malloced
);
5560 if (!(result
== resultbuf
|| result
== NULL
))
5562 if (buf_malloced
!= NULL
)
5563 free (buf_malloced
);
5571 #undef MAX_ROOM_NEEDED
5572 #undef TCHARS_PER_DCHAR
5580 #undef DCHAR_IS_TCHAR