1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2023 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* This file can be parametrized with the following macros:
18 VASNPRINTF The name of the function being defined.
19 FCHAR_T The element type of the format string.
20 DCHAR_T The element type of the destination (result) string.
21 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
22 in the format string are ASCII. MUST be set if
23 FCHAR_T and DCHAR_T are not the same type.
24 DIRECTIVE Structure denoting a format directive.
26 DIRECTIVES Structure denoting the set of format directives of a
27 format string. Depends on FCHAR_T.
28 PRINTF_PARSE Function that parses a format string.
30 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
31 DCHAR_SET memset like function for DCHAR_T[] arrays.
32 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
33 SNPRINTF The system's snprintf (or similar) function.
34 This may be either snprintf or swprintf.
35 TCHAR_T The element type of the argument and result string
36 of the said SNPRINTF function. This may be either
37 char or wchar_t. The code exploits that
38 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
39 alignof (TCHAR_T) <= alignof (DCHAR_T).
40 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
41 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
42 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
43 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
44 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t.
45 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
46 ENABLE_WCHAR_FALLBACK Set to 1 to avoid EILSEQ during conversion of wide
47 characters (wchar_t) and wide character strings
48 (wchar_t[]) to multibyte sequences. The fallback is the
49 hexadecimal escape syntax (\unnnn or \Unnnnnnnn) or,
50 if wchar_t is not Unicode encoded, \wnnnn or \Wnnnnnnnn.
53 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
54 This must come before <config.h> because <config.h> may include
55 <features.h>, and once <features.h> has been included, it's too late. */
57 # define _GNU_SOURCE 1
64 /* As of GCC 11.2.1, gcc -Wanalyzer-too-complex reports that main's
65 use of CHECK macros expands to code that is too complicated for gcc
66 -fanalyzer. Suppress the resulting bogus warnings. */
68 # pragma GCC diagnostic ignored "-Wanalyzer-null-argument"
75 # if WIDE_CHAR_VERSION
76 # include "vasnwprintf.h"
78 # include "vasnprintf.h"
82 #include <locale.h> /* localeconv() */
83 #include <stdio.h> /* snprintf(), sprintf() */
84 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
85 #include <string.h> /* memcpy(), strlen() */
86 #include <wchar.h> /* mbstate_t, mbrtowc(), mbrlen(), wcrtomb() */
87 #include <errno.h> /* errno */
88 #include <limits.h> /* CHAR_BIT */
89 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
91 # include <langinfo.h>
94 # if WIDE_CHAR_VERSION
95 # include "wprintf-parse.h"
97 # include "printf-parse.h"
101 /* Checked size_t computations. */
104 #include "attribute.h"
106 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
111 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
113 # include "isnand-nolibm.h"
116 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
118 # include "isnanl-nolibm.h"
122 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
124 # include "isnand-nolibm.h"
125 # include "printf-frexp.h"
128 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
130 # include "isnanl-nolibm.h"
131 # include "printf-frexpl.h"
135 /* Default parameters. */
137 # if WIDE_CHAR_VERSION
138 # define VASNPRINTF vasnwprintf
139 # define FCHAR_T wchar_t
140 # define DCHAR_T wchar_t
141 # define TCHAR_T wchar_t
142 # define DCHAR_IS_TCHAR 1
143 # define DIRECTIVE wchar_t_directive
144 # define DIRECTIVES wchar_t_directives
145 # define PRINTF_PARSE wprintf_parse
146 # define DCHAR_CPY wmemcpy
147 # define DCHAR_SET wmemset
149 # define VASNPRINTF vasnprintf
150 # define FCHAR_T char
151 # define DCHAR_T char
152 # define TCHAR_T char
153 # define DCHAR_IS_TCHAR 1
154 # define DIRECTIVE char_directive
155 # define DIRECTIVES char_directives
156 # define PRINTF_PARSE printf_parse
157 # define DCHAR_CPY memcpy
158 # define DCHAR_SET memset
161 #if WIDE_CHAR_VERSION
162 /* TCHAR_T is wchar_t. */
163 # define USE_SNPRINTF 1
164 # if HAVE_DECL__SNWPRINTF
165 /* On Windows, the function swprintf() has a different signature than
166 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
167 instead. The mingw function snwprintf() has fewer bugs than the
168 MSVCRT function _snwprintf(), so prefer that. */
169 # if defined __MINGW32__
170 # define SNPRINTF snwprintf
172 # define SNPRINTF _snwprintf
173 # define USE_MSVC__SNPRINTF 1
177 # define SNPRINTF swprintf
180 /* TCHAR_T is char. */
181 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
182 But don't use it on BeOS, since BeOS snprintf produces no output if the
183 size argument is >= 0x3000000.
184 Also don't use it on Linux libc5, since there snprintf with size = 1
185 writes any output without bounds, like sprintf. */
186 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
187 # define USE_SNPRINTF 1
189 # define USE_SNPRINTF 0
191 # if HAVE_DECL__SNPRINTF
192 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
193 function _snprintf(), so prefer that. */
194 # if defined __MINGW32__
195 # define SNPRINTF snprintf
196 /* Here we need to call the native snprintf, not rpl_snprintf. */
199 /* MSVC versions < 14 did not have snprintf, only _snprintf. */
200 # define SNPRINTF _snprintf
201 # define USE_MSVC__SNPRINTF 1
205 # define SNPRINTF snprintf
206 /* Here we need to call the native snprintf, not rpl_snprintf. */
210 /* Here we need to call the native sprintf, not rpl_sprintf. */
213 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
214 warnings in this file. Use -Dlint to suppress them. */
215 #if defined GCC_LINT || defined lint
216 # define IF_LINT(Code) Code
218 # define IF_LINT(Code) /* empty */
221 /* Avoid some warnings from "gcc -Wshadow".
222 This file doesn't use the exp() and remainder() functions. */
226 #define remainder rem
228 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && !WIDE_CHAR_VERSION
229 # if (HAVE_STRNLEN && !defined _AIX)
230 # define local_strnlen strnlen
232 # ifndef local_strnlen_defined
233 # define local_strnlen_defined 1
235 local_strnlen (const char *string
, size_t maxlen
)
237 const char *end
= memchr (string
, '\0', maxlen
);
238 return end
? (size_t) (end
- string
) : maxlen
;
244 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
246 # define local_wcslen wcslen
248 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
249 a dependency towards this library, here is a local substitute.
250 Define this substitute only once, even if this file is included
251 twice in the same compilation unit. */
252 # ifndef local_wcslen_defined
253 # define local_wcslen_defined 1
255 local_wcslen (const wchar_t *s
)
259 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
267 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
269 # define local_wcsnlen wcsnlen
271 # ifndef local_wcsnlen_defined
272 # define local_wcsnlen_defined 1
274 local_wcsnlen (const wchar_t *s
, size_t maxlen
)
278 for (ptr
= s
; maxlen
> 0 && *ptr
!= (wchar_t) 0; ptr
++, maxlen
--)
286 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T) || (ENABLE_WCHAR_FALLBACK && HAVE_WINT_T)) && !WIDE_CHAR_VERSION
287 # if ENABLE_WCHAR_FALLBACK
289 wctomb_fallback (char *s
, wchar_t wc
)
291 static char hex
[16] = "0123456789ABCDEF";
294 if (sizeof (wchar_t) > 2 && wc
> 0xffff)
296 # if __STDC_ISO_10646__ || (__GLIBC__ >= 2) || (defined _WIN32 || defined __CYGWIN__)
301 s
[2] = hex
[(wc
& 0xf0000000U
) >> 28];
302 s
[3] = hex
[(wc
& 0xf000000U
) >> 24];
303 s
[4] = hex
[(wc
& 0xf00000U
) >> 20];
304 s
[5] = hex
[(wc
& 0xf0000U
) >> 16];
305 s
[6] = hex
[(wc
& 0xf000U
) >> 12];
306 s
[7] = hex
[(wc
& 0xf00U
) >> 8];
307 s
[8] = hex
[(wc
& 0xf0U
) >> 4];
308 s
[9] = hex
[wc
& 0xfU
];
313 # if __STDC_ISO_10646__ || (__GLIBC__ >= 2) || (defined _WIN32 || defined __CYGWIN__)
318 s
[2] = hex
[(wc
& 0xf000U
) >> 12];
319 s
[3] = hex
[(wc
& 0xf00U
) >> 8];
320 s
[4] = hex
[(wc
& 0xf0U
) >> 4];
321 s
[5] = hex
[wc
& 0xfU
];
325 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
327 local_wcrtomb (char *s
, wchar_t wc
, mbstate_t *ps
)
329 size_t count
= wcrtomb (s
, wc
, ps
);
330 if (count
== (size_t)(-1))
331 count
= wctomb_fallback (s
, wc
);
336 local_wctomb (char *s
, wchar_t wc
)
338 int count
= wctomb (s
, wc
);
340 count
= wctomb_fallback (s
, wc
);
343 # define local_wcrtomb(S, WC, PS) local_wctomb ((S), (WC))
346 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
347 # define local_wcrtomb(S, WC, PS) wcrtomb ((S), (WC), (PS))
349 # define local_wcrtomb(S, WC, PS) wctomb ((S), (WC))
354 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
355 /* Determine the decimal-point character according to the current locale. */
356 # ifndef decimal_point_char_defined
357 # define decimal_point_char_defined 1
359 decimal_point_char (void)
362 /* Determine it in a multithread-safe way. We know nl_langinfo is
363 multithread-safe on glibc systems and Mac OS X systems, but is not required
364 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
365 localeconv() is rarely multithread-safe. */
366 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
367 point
= nl_langinfo (RADIXCHAR
);
370 sprintf (pointbuf
, "%#.0f", 1.0);
371 point
= &pointbuf
[1];
373 point
= localeconv () -> decimal_point
;
375 /* The decimal point is always a single byte: either '.' or ','. */
376 return (point
[0] != '\0' ? point
[0] : '.');
381 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
383 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
385 is_infinite_or_zero (double x
)
387 return isnand (x
) || x
+ x
== x
;
392 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
394 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
396 is_infinite_or_zerol (long double x
)
398 return isnanl (x
) || x
+ x
== x
;
403 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
405 /* Converting 'long double' to decimal without rare rounding bugs requires
406 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
407 (and slower) algorithms. */
409 typedef unsigned int mp_limb_t
;
410 # define GMP_LIMB_BITS 32
411 static_assert (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
);
413 typedef unsigned long long mp_twolimb_t
;
414 # define GMP_TWOLIMB_BITS 64
415 static_assert (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
);
417 /* Representation of a bignum >= 0. */
421 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
424 /* Compute the product of two bignums >= 0.
425 Return the allocated memory in case of success, NULL in case of memory
426 allocation failure. */
428 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
435 if (src1
.nlimbs
<= src2
.nlimbs
)
449 /* Now 0 <= len1 <= len2. */
452 /* src1 or src2 is zero. */
454 dest
->limbs
= (mp_limb_t
*) malloc (1);
458 /* Here 1 <= len1 <= len2. */
464 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
467 for (k
= len2
; k
> 0; )
469 for (i
= 0; i
< len1
; i
++)
471 mp_limb_t digit1
= p1
[i
];
472 mp_twolimb_t carry
= 0;
473 for (j
= 0; j
< len2
; j
++)
475 mp_limb_t digit2
= p2
[j
];
476 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
478 dp
[i
+ j
] = (mp_limb_t
) carry
;
479 carry
= carry
>> GMP_LIMB_BITS
;
481 dp
[i
+ len2
] = (mp_limb_t
) carry
;
484 while (dlen
> 0 && dp
[dlen
- 1] == 0)
492 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
493 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
495 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
497 Return the allocated memory in case of success, NULL in case of memory
498 allocation failure. */
500 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
503 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
504 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
505 If m<n, then q:=0 and r:=a.
506 If m>=n=1, perform a single-precision division:
509 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
510 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
511 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
512 Normalise [q[m-1],...,q[0]], yields q.
513 If m>=n>1, perform a multiple-precision division:
514 We have a/b < beta^(m-n+1).
515 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
516 Shift a and b left by s bits, copying them. r:=a.
517 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
518 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
520 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
521 In case of overflow (q* >= beta) set q* := beta-1.
522 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
523 and c3 := b[n-2] * q*.
524 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
525 occurred. Furthermore 0 <= c3 < beta^2.
526 If there was overflow and
527 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
528 the next test can be skipped.}
529 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
530 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
532 Put r := r - b * q* * beta^j. In detail:
533 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
534 hence: u:=0, for i:=0 to n-1 do
536 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
537 u:=u div beta (+ 1, if carry in subtraction)
539 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
541 the carry u does not overflow.}
542 If a negative carry occurs, put q* := q* - 1
543 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
545 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
546 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
548 The room for q[j] can be allocated at the memory location of r[n+j].
549 Finally, round-to-even:
550 Shift r left by 1 bit.
551 If r > b or if r = b and q[0] is odd, q := q+1.
553 const mp_limb_t
*a_ptr
= a
.limbs
;
554 size_t a_len
= a
.nlimbs
;
555 const mp_limb_t
*b_ptr
= b
.limbs
;
556 size_t b_len
= b
.nlimbs
;
558 mp_limb_t
*tmp_roomptr
= NULL
;
564 /* Allocate room for a_len+2 digits.
565 (Need a_len+1 digits for the real division and 1 more digit for the
566 final rounding of q.) */
567 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
572 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
579 /* Division by zero. */
581 if (b_ptr
[b_len
- 1] == 0)
587 /* Here m = a_len >= 0 and n = b_len > 0. */
591 /* m<n: trivial case. q=0, r := copy of a. */
594 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
595 q_ptr
= roomptr
+ a_len
;
600 /* n=1: single precision division.
601 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
605 mp_limb_t den
= b_ptr
[0];
606 mp_limb_t remainder
= 0;
607 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
608 mp_limb_t
*destptr
= q_ptr
+ a_len
;
610 for (count
= a_len
; count
> 0; count
--)
613 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
614 *--destptr
= num
/ den
;
615 remainder
= num
% den
;
617 /* Normalise and store r. */
620 r_ptr
[0] = remainder
;
627 if (q_ptr
[q_len
- 1] == 0)
633 /* n>1: multiple precision division.
634 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
635 beta^(m-n-1) <= a/b < beta^(m-n+1). */
639 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
640 /* Determine s = GMP_LIMB_BITS - integer_length (msd).
641 Code copied from gnulib's integer_length.c. */
642 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) \
643 || (__clang_major__ >= 4)
644 s
= __builtin_clz (msd
);
646 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
647 if (GMP_LIMB_BITS
<= DBL_MANT_BIT
)
649 /* Use 'double' operations.
650 Assumes an IEEE 754 'double' implementation. */
651 # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
652 # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
654 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
655 union { double value
; unsigned int word
[NWORDS
]; } m
;
657 /* Use a single integer to floating-point conversion. */
661 - (((m
.word
[DBL_EXPBIT0_WORD
] >> DBL_EXPBIT0_BIT
) & DBL_EXP_MASK
)
697 /* 0 <= s < GMP_LIMB_BITS.
698 Copy b, shifting it left by s bits. */
701 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
702 if (tmp_roomptr
== NULL
)
708 const mp_limb_t
*sourceptr
= b_ptr
;
709 mp_limb_t
*destptr
= tmp_roomptr
;
710 mp_twolimb_t accu
= 0;
712 for (count
= b_len
; count
> 0; count
--)
714 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
715 *destptr
++ = (mp_limb_t
) accu
;
716 accu
= accu
>> GMP_LIMB_BITS
;
718 /* accu must be zero, since that was how s was determined. */
724 /* Copy a, shifting it left by s bits, yields r.
726 At the beginning: r = roomptr[0..a_len],
727 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
731 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
736 const mp_limb_t
*sourceptr
= a_ptr
;
737 mp_limb_t
*destptr
= r_ptr
;
738 mp_twolimb_t accu
= 0;
740 for (count
= a_len
; count
> 0; count
--)
742 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
743 *destptr
++ = (mp_limb_t
) accu
;
744 accu
= accu
>> GMP_LIMB_BITS
;
746 *destptr
++ = (mp_limb_t
) accu
;
748 q_ptr
= roomptr
+ b_len
;
749 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
751 size_t j
= a_len
- b_len
; /* m-n */
752 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
753 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
754 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
755 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
756 /* Division loop, traversed m-n+1 times.
757 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
762 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
764 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
766 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
767 | r_ptr
[j
+ b_len
- 1];
768 q_star
= num
/ b_msd
;
773 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
774 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
775 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
776 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
777 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
779 If yes, jump directly to the subtraction loop.
780 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
781 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
782 if (r_ptr
[j
+ b_len
] > b_msd
783 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
784 /* r[j+n] >= b[n-1]+1 or
785 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
790 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
792 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
793 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
794 mp_twolimb_t c3
= /* b[n-2] * q* */
795 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
796 /* While c2 < c3, increase c2 and decrease c3.
797 Consider c3-c2. While it is > 0, decrease it by
798 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
799 this can happen only twice. */
802 q_star
= q_star
- 1; /* q* := q* - 1 */
803 if (c3
- c2
> b_msdd
)
804 q_star
= q_star
- 1; /* q* := q* - 1 */
810 /* Subtract r := r - b * q* * beta^j. */
813 const mp_limb_t
*sourceptr
= b_ptr
;
814 mp_limb_t
*destptr
= r_ptr
+ j
;
815 mp_twolimb_t carry
= 0;
817 for (count
= b_len
; count
> 0; count
--)
819 /* Here 0 <= carry <= q*. */
822 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
823 + (mp_limb_t
) ~(*destptr
);
824 /* Here 0 <= carry <= beta*q* + beta-1. */
825 *destptr
++ = ~(mp_limb_t
) carry
;
826 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
828 cr
= (mp_limb_t
) carry
;
830 /* Subtract cr from r_ptr[j + b_len], then forget about
832 if (cr
> r_ptr
[j
+ b_len
])
834 /* Subtraction gave a carry. */
835 q_star
= q_star
- 1; /* q* := q* - 1 */
838 const mp_limb_t
*sourceptr
= b_ptr
;
839 mp_limb_t
*destptr
= r_ptr
+ j
;
842 for (count
= b_len
; count
> 0; count
--)
844 mp_limb_t source1
= *sourceptr
++;
845 mp_limb_t source2
= *destptr
;
846 *destptr
++ = source1
+ source2
+ carry
;
849 ? source1
>= (mp_limb_t
) ~source2
850 : source1
> (mp_limb_t
) ~source2
);
853 /* Forget about the carry and about r[j+n]. */
856 /* q* is determined. Store it as q[j]. */
865 if (q_ptr
[q_len
- 1] == 0)
867 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
868 b is shifted left by s bits. */
869 /* Shift r right by s bits. */
872 mp_limb_t ptr
= r_ptr
+ r_len
;
873 mp_twolimb_t accu
= 0;
875 for (count
= r_len
; count
> 0; count
--)
877 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
878 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
879 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
884 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
887 /* Compare r << 1 with b. */
895 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
896 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
897 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
907 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
912 for (i
= 0; i
< q_len
; i
++)
913 if (++(q_ptr
[i
]) != 0)
924 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
926 Destroys the contents of a.
927 Return the allocated memory - containing the decimal digits in low-to-high
928 order, terminated with a NUL character - in case of success, NULL in case
929 of memory allocation failure. */
931 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
933 mp_limb_t
*a_ptr
= a
.limbs
;
934 size_t a_len
= a
.nlimbs
;
935 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
936 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
937 /* We need extra_zeroes bytes for zeroes, followed by c_len bytes for the
938 digits of a, followed by 1 byte for the terminating NUL. */
939 char *c_ptr
= (char *) malloc (xsum (xsum (extra_zeroes
, c_len
), 1));
943 for (; extra_zeroes
> 0; extra_zeroes
--)
947 /* Divide a by 10^9, in-place. */
948 mp_limb_t remainder
= 0;
949 mp_limb_t
*ptr
= a_ptr
+ a_len
;
951 for (count
= a_len
; count
> 0; count
--)
954 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
955 *ptr
= num
/ 1000000000;
956 remainder
= num
% 1000000000;
958 /* Store the remainder as 9 decimal digits. */
959 for (count
= 9; count
> 0; count
--)
961 *d_ptr
++ = '0' + (remainder
% 10);
962 remainder
= remainder
/ 10;
965 if (a_ptr
[a_len
- 1] == 0)
968 /* Remove leading zeroes. */
969 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
971 /* But keep at least one zero. */
974 /* Terminate the string. */
980 # if NEED_PRINTF_LONG_DOUBLE
982 /* Assuming x is finite and >= 0:
983 write x as x = 2^e * m, where m is a bignum.
984 Return the allocated memory in case of success, NULL in case of memory
985 allocation failure. */
987 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
994 /* Allocate memory for result. */
995 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
996 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
999 /* Split into exponential part and mantissa. */
1000 y
= frexpl (x
, &exp
);
1001 if (!(y
>= 0.0L && y
< 1.0L))
1003 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
1004 latter is an integer. */
1005 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
1006 I'm not sure whether it's safe to cast a 'long double' value between
1007 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1008 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1010 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
1011 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1014 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1017 if (!(y
>= 0.0L && y
< 1.0L))
1019 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1022 if (!(y
>= 0.0L && y
< 1.0L))
1024 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1029 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
1032 if (!(y
>= 0.0L && y
< 1.0L))
1034 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1038 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1041 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1044 if (!(y
>= 0.0L && y
< 1.0L))
1046 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1049 if (!(y
>= 0.0L && y
< 1.0L))
1051 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1053 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
1059 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1062 *ep
= exp
- LDBL_MANT_BIT
;
1068 # if NEED_PRINTF_DOUBLE
1070 /* Assuming x is finite and >= 0:
1071 write x as x = 2^e * m, where m is a bignum.
1072 Return the allocated memory in case of success, NULL in case of memory
1073 allocation failure. */
1075 decode_double (double x
, int *ep
, mpn_t
*mp
)
1082 /* Allocate memory for result. */
1083 m
.nlimbs
= (DBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
1084 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
1085 if (m
.limbs
== NULL
)
1087 /* Split into exponential part and mantissa. */
1088 y
= frexp (x
, &exp
);
1089 if (!(y
>= 0.0 && y
< 1.0))
1091 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
1092 latter is an integer. */
1093 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
1094 I'm not sure whether it's safe to cast a 'double' value between
1095 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1096 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1098 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
1099 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
1102 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
1105 if (!(y
>= 0.0 && y
< 1.0))
1107 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1110 if (!(y
>= 0.0 && y
< 1.0))
1112 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1117 y
*= (mp_limb_t
) 1 << (DBL_MANT_BIT
% GMP_LIMB_BITS
);
1120 if (!(y
>= 0.0 && y
< 1.0))
1122 m
.limbs
[DBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
1126 for (i
= DBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
1129 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1132 if (!(y
>= 0.0 && y
< 1.0))
1134 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
1137 if (!(y
>= 0.0 && y
< 1.0))
1139 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
1144 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
1147 *ep
= exp
- DBL_MANT_BIT
;
1153 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1154 Returns the decimal representation of round (x * 10^n).
1155 Return the allocated memory - containing the decimal digits in low-to-high
1156 order, terminated with a NUL character - in case of success, NULL in case
1157 of memory allocation failure. */
1159 scale10_round_decimal_decoded (int e
, mpn_t m
, void *memory
, int n
)
1162 size_t extra_zeroes
;
1165 mp_limb_t
*pow5_ptr
;
1167 unsigned int s_limbs
;
1168 unsigned int s_bits
;
1176 /* x = 2^e * m, hence
1177 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1178 = round (2^s * 5^n * m). */
1181 /* Factor out a common power of 10 if possible. */
1184 extra_zeroes
= (s
< n
? s
: n
);
1188 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1189 Before converting to decimal, we need to compute
1190 z = round (2^s * 5^n * m). */
1191 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1192 sign. 2.322 is slightly larger than log(5)/log(2). */
1193 abs_n
= (n
>= 0 ? n
: -n
);
1194 abs_s
= (s
>= 0 ? s
: -s
);
1195 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
1196 + abs_s
/ GMP_LIMB_BITS
+ 1)
1197 * sizeof (mp_limb_t
));
1198 if (pow5_ptr
== NULL
)
1203 /* Initialize with 1. */
1206 /* Multiply with 5^|n|. */
1209 static mp_limb_t
const small_pow5
[13 + 1] =
1211 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1212 48828125, 244140625, 1220703125
1215 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
1217 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
1219 mp_twolimb_t carry
= 0;
1220 for (j
= 0; j
< pow5_len
; j
++)
1222 mp_limb_t digit2
= pow5_ptr
[j
];
1223 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
1224 pow5_ptr
[j
] = (mp_limb_t
) carry
;
1225 carry
= carry
>> GMP_LIMB_BITS
;
1228 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
1231 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
1232 s_bits
= abs_s
% GMP_LIMB_BITS
;
1233 if (n
>= 0 ? s
>= 0 : s
<= 0)
1235 /* Multiply with 2^|s|. */
1238 mp_limb_t
*ptr
= pow5_ptr
;
1239 mp_twolimb_t accu
= 0;
1241 for (count
= pow5_len
; count
> 0; count
--)
1243 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
1244 *ptr
++ = (mp_limb_t
) accu
;
1245 accu
= accu
>> GMP_LIMB_BITS
;
1249 *ptr
= (mp_limb_t
) accu
;
1256 for (count
= pow5_len
; count
> 0;)
1259 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
1261 for (count
= s_limbs
; count
> 0;)
1264 pow5_ptr
[count
] = 0;
1266 pow5_len
+= s_limbs
;
1268 pow5
.limbs
= pow5_ptr
;
1269 pow5
.nlimbs
= pow5_len
;
1272 /* Multiply m with pow5. No division needed. */
1273 z_memory
= multiply (m
, pow5
, &z
);
1277 /* Divide m by pow5 and round. */
1278 z_memory
= divide (m
, pow5
, &z
);
1283 pow5
.limbs
= pow5_ptr
;
1284 pow5
.nlimbs
= pow5_len
;
1288 Multiply m with pow5, then divide by 2^|s|. */
1292 tmp_memory
= multiply (m
, pow5
, &numerator
);
1293 if (tmp_memory
== NULL
)
1299 /* Construct 2^|s|. */
1301 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1303 for (i
= 0; i
< s_limbs
; i
++)
1305 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1306 denominator
.limbs
= ptr
;
1307 denominator
.nlimbs
= s_limbs
+ 1;
1309 z_memory
= divide (numerator
, denominator
, &z
);
1315 Multiply m with 2^s, then divide by pow5. */
1318 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1319 * sizeof (mp_limb_t
));
1320 if (num_ptr
== NULL
)
1327 mp_limb_t
*destptr
= num_ptr
;
1330 for (i
= 0; i
< s_limbs
; i
++)
1335 const mp_limb_t
*sourceptr
= m
.limbs
;
1336 mp_twolimb_t accu
= 0;
1338 for (count
= m
.nlimbs
; count
> 0; count
--)
1340 accu
+= (mp_twolimb_t
) *sourceptr
++ << s_bits
;
1341 *destptr
++ = (mp_limb_t
) accu
;
1342 accu
= accu
>> GMP_LIMB_BITS
;
1345 *destptr
++ = (mp_limb_t
) accu
;
1349 const mp_limb_t
*sourceptr
= m
.limbs
;
1351 for (count
= m
.nlimbs
; count
> 0; count
--)
1352 *destptr
++ = *sourceptr
++;
1354 numerator
.limbs
= num_ptr
;
1355 numerator
.nlimbs
= destptr
- num_ptr
;
1357 z_memory
= divide (numerator
, pow5
, &z
);
1364 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1366 if (z_memory
== NULL
)
1368 digits
= convert_to_decimal (z
, extra_zeroes
);
1373 # if NEED_PRINTF_LONG_DOUBLE
1375 /* Assuming x is finite and >= 0, and n is an integer:
1376 Returns the decimal representation of round (x * 10^n).
1377 Return the allocated memory - containing the decimal digits in low-to-high
1378 order, terminated with a NUL character - in case of success, NULL in case
1379 of memory allocation failure. */
1381 scale10_round_decimal_long_double (long double x
, int n
)
1385 void *memory
= decode_long_double (x
, &e
, &m
);
1386 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1391 # if NEED_PRINTF_DOUBLE
1393 /* Assuming x is finite and >= 0, and n is an integer:
1394 Returns the decimal representation of round (x * 10^n).
1395 Return the allocated memory - containing the decimal digits in low-to-high
1396 order, terminated with a NUL character - in case of success, NULL in case
1397 of memory allocation failure. */
1399 scale10_round_decimal_double (double x
, int n
)
1403 void *memory
= decode_double (x
, &e
, &m
);
1404 return scale10_round_decimal_decoded (e
, m
, memory
, n
);
1409 # if NEED_PRINTF_LONG_DOUBLE
1411 /* Assuming x is finite and > 0:
1412 Return an approximation for n with 10^n <= x < 10^(n+1).
1413 The approximation is usually the right n, but may be off by 1 sometimes. */
1415 floorlog10l (long double x
)
1422 /* Split into exponential part and mantissa. */
1423 y
= frexpl (x
, &exp
);
1424 if (!(y
>= 0.0L && y
< 1.0L))
1430 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1432 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1433 exp
-= GMP_LIMB_BITS
;
1435 if (y
< (1.0L / (1 << 16)))
1437 y
*= 1.0L * (1 << 16);
1440 if (y
< (1.0L / (1 << 8)))
1442 y
*= 1.0L * (1 << 8);
1445 if (y
< (1.0L / (1 << 4)))
1447 y
*= 1.0L * (1 << 4);
1450 if (y
< (1.0L / (1 << 2)))
1452 y
*= 1.0L * (1 << 2);
1455 if (y
< (1.0L / (1 << 1)))
1457 y
*= 1.0L * (1 << 1);
1461 if (!(y
>= 0.5L && y
< 1.0L))
1463 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1466 if (z
< 0.70710678118654752444)
1468 z
*= 1.4142135623730950488;
1471 if (z
< 0.8408964152537145431)
1473 z
*= 1.1892071150027210667;
1476 if (z
< 0.91700404320467123175)
1478 z
*= 1.0905077326652576592;
1481 if (z
< 0.9576032806985736469)
1483 z
*= 1.0442737824274138403;
1486 /* Now 0.95 <= z <= 1.01. */
1488 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1489 Four terms are enough to get an approximation with error < 10^-7. */
1490 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1491 /* Finally multiply with log(2)/log(10), yields an approximation for
1493 l
*= 0.30102999566398119523;
1494 /* Round down to the next integer. */
1495 return (int) l
+ (l
< 0 ? -1 : 0);
1500 # if NEED_PRINTF_DOUBLE
1502 /* Assuming x is finite and > 0:
1503 Return an approximation for n with 10^n <= x < 10^(n+1).
1504 The approximation is usually the right n, but may be off by 1 sometimes. */
1506 floorlog10 (double x
)
1513 /* Split into exponential part and mantissa. */
1514 y
= frexp (x
, &exp
);
1515 if (!(y
>= 0.0 && y
< 1.0))
1521 while (y
< (1.0 / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1523 y
*= 1.0 * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1524 exp
-= GMP_LIMB_BITS
;
1526 if (y
< (1.0 / (1 << 16)))
1528 y
*= 1.0 * (1 << 16);
1531 if (y
< (1.0 / (1 << 8)))
1533 y
*= 1.0 * (1 << 8);
1536 if (y
< (1.0 / (1 << 4)))
1538 y
*= 1.0 * (1 << 4);
1541 if (y
< (1.0 / (1 << 2)))
1543 y
*= 1.0 * (1 << 2);
1546 if (y
< (1.0 / (1 << 1)))
1548 y
*= 1.0 * (1 << 1);
1552 if (!(y
>= 0.5 && y
< 1.0))
1554 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1557 if (z
< 0.70710678118654752444)
1559 z
*= 1.4142135623730950488;
1562 if (z
< 0.8408964152537145431)
1564 z
*= 1.1892071150027210667;
1567 if (z
< 0.91700404320467123175)
1569 z
*= 1.0905077326652576592;
1572 if (z
< 0.9576032806985736469)
1574 z
*= 1.0442737824274138403;
1577 /* Now 0.95 <= z <= 1.01. */
1579 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1580 Four terms are enough to get an approximation with error < 10^-7. */
1581 l
-= 1.4426950408889634074 * z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1582 /* Finally multiply with log(2)/log(10), yields an approximation for
1584 l
*= 0.30102999566398119523;
1585 /* Round down to the next integer. */
1586 return (int) l
+ (l
< 0 ? -1 : 0);
1591 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
1592 a single '1' digit. */
1594 is_borderline (const char *digits
, size_t precision
)
1596 for (; precision
> 0; precision
--, digits
++)
1602 return *digits
== '\0';
1607 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
1609 /* Use a different function name, to make it possible that the 'wchar_t'
1610 parametrization and the 'char' parametrization get compiled in the same
1611 translation unit. */
1612 # if WIDE_CHAR_VERSION
1613 # define MAX_ROOM_NEEDED wmax_room_needed
1615 # define MAX_ROOM_NEEDED max_room_needed
1618 /* Returns the number of TCHAR_T units needed as temporary space for the result
1619 of sprintf or SNPRINTF of a single conversion directive. */
1621 MAX_ROOM_NEEDED (const arguments
*ap
, size_t arg_index
, FCHAR_T conversion
,
1622 arg_type type
, int flags
, size_t width
, int has_precision
,
1623 size_t precision
, int pad_ourselves
)
1629 case 'd': case 'i': case 'u':
1630 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1632 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1633 * 0.30103 /* binary -> decimal */
1635 + 1; /* turn floor into ceil */
1636 else if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1638 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1639 * 0.30103 /* binary -> decimal */
1641 + 1; /* turn floor into ceil */
1644 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1645 * 0.30103 /* binary -> decimal */
1647 + 1; /* turn floor into ceil */
1648 if (tmp_length
< precision
)
1649 tmp_length
= precision
;
1650 /* Multiply by 2, as an estimate for FLAG_GROUP. */
1651 tmp_length
= xsum (tmp_length
, tmp_length
);
1652 /* Add 1, to account for a leading sign. */
1653 tmp_length
= xsum (tmp_length
, 1);
1657 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1659 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1660 * 0.333334 /* binary -> octal */
1662 + 1; /* turn floor into ceil */
1663 else if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1665 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1666 * 0.333334 /* binary -> octal */
1668 + 1; /* turn floor into ceil */
1671 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1672 * 0.333334 /* binary -> octal */
1674 + 1; /* turn floor into ceil */
1675 if (tmp_length
< precision
)
1676 tmp_length
= precision
;
1677 /* Add 1, to account for a leading sign. */
1678 tmp_length
= xsum (tmp_length
, 1);
1682 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
1684 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1685 * 0.25 /* binary -> hexadecimal */
1687 + 1; /* turn floor into ceil */
1688 else if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
1690 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
1691 * 0.25 /* binary -> hexadecimal */
1693 + 1; /* turn floor into ceil */
1696 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
1697 * 0.25 /* binary -> hexadecimal */
1699 + 1; /* turn floor into ceil */
1700 if (tmp_length
< precision
)
1701 tmp_length
= precision
;
1702 /* Add 2, to account for a leading sign or alternate form. */
1703 tmp_length
= xsum (tmp_length
, 2);
1707 if (type
== TYPE_LONGDOUBLE
)
1709 (unsigned int) (LDBL_MAX_EXP
1710 * 0.30103 /* binary -> decimal */
1711 * 2 /* estimate for FLAG_GROUP */
1713 + 1 /* turn floor into ceil */
1714 + 10; /* sign, decimal point etc. */
1717 (unsigned int) (DBL_MAX_EXP
1718 * 0.30103 /* binary -> decimal */
1719 * 2 /* estimate for FLAG_GROUP */
1721 + 1 /* turn floor into ceil */
1722 + 10; /* sign, decimal point etc. */
1723 tmp_length
= xsum (tmp_length
, precision
);
1726 case 'e': case 'E': case 'g': case 'G':
1728 12; /* sign, decimal point, exponent etc. */
1729 tmp_length
= xsum (tmp_length
, precision
);
1733 if (type
== TYPE_LONGDOUBLE
)
1735 (unsigned int) (LDBL_DIG
1736 * 0.831 /* decimal -> hexadecimal */
1738 + 1; /* turn floor into ceil */
1741 (unsigned int) (DBL_DIG
1742 * 0.831 /* decimal -> hexadecimal */
1744 + 1; /* turn floor into ceil */
1745 if (tmp_length
< precision
)
1746 tmp_length
= precision
;
1747 /* Account for sign, decimal point etc. */
1748 tmp_length
= xsum (tmp_length
, 12);
1752 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
1753 if (type
== TYPE_WIDE_CHAR
)
1755 tmp_length
= MB_CUR_MAX
;
1756 # if ENABLE_WCHAR_FALLBACK
1757 if (tmp_length
< (sizeof (wchar_t) > 2 ? 10 : 6))
1758 tmp_length
= (sizeof (wchar_t) > 2 ? 10 : 6);
1768 if (type
== TYPE_WIDE_STRING
)
1770 # if WIDE_CHAR_VERSION
1771 /* ISO C says about %ls in fwprintf:
1772 "If the precision is not specified or is greater than the size
1773 of the array, the array shall contain a null wide character."
1774 So if there is a precision, we must not use wcslen. */
1775 const wchar_t *arg
= ap
->arg
[arg_index
].a
.a_wide_string
;
1778 tmp_length
= local_wcsnlen (arg
, precision
);
1780 tmp_length
= local_wcslen (arg
);
1782 /* ISO C says about %ls in fprintf:
1783 "If a precision is specified, no more than that many bytes are
1784 written (including shift sequences, if any), and the array
1785 shall contain a null wide character if, to equal the multibyte
1786 character sequence length given by the precision, the function
1787 would need to access a wide character one past the end of the
1789 So if there is a precision, we must not use wcslen. */
1790 /* This case has already been handled separately in VASNPRINTF. */
1797 # if WIDE_CHAR_VERSION
1798 /* ISO C says about %s in fwprintf:
1799 "If the precision is not specified or is greater than the size
1800 of the converted array, the converted array shall contain a
1801 null wide character."
1802 So if there is a precision, we must not use strlen. */
1803 /* This case has already been handled separately in VASNPRINTF. */
1806 /* ISO C says about %s in fprintf:
1807 "If the precision is not specified or greater than the size of
1808 the array, the array shall contain a null character."
1809 So if there is a precision, we must not use strlen. */
1810 const char *arg
= ap
->arg
[arg_index
].a
.a_string
;
1813 tmp_length
= local_strnlen (arg
, precision
);
1815 tmp_length
= strlen (arg
);
1822 (unsigned int) (sizeof (void *) * CHAR_BIT
1823 * 0.25 /* binary -> hexadecimal */
1825 + 1 /* turn floor into ceil */
1826 + 2; /* account for leading 0x */
1835 # if ENABLE_UNISTDIO
1836 /* Padding considers the number of characters, therefore the number of
1837 elements after padding may be
1838 > max (tmp_length, width)
1840 <= tmp_length + width. */
1841 tmp_length
= xsum (tmp_length
, width
);
1843 /* Padding considers the number of elements, says POSIX. */
1844 if (tmp_length
< width
)
1849 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1857 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1858 const FCHAR_T
*format
, va_list args
)
1863 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1864 /* errno is already set. */
1867 /* Frees the memory allocated by this function. Preserves errno. */
1869 if (d.dir != d.direct_alloc_dir) \
1871 if (a.arg != a.direct_alloc_arg) \
1874 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1875 goto fail_1_with_EINVAL
;
1878 size_t buf_neededlength
;
1880 TCHAR_T
*buf_malloced
;
1884 /* Output string accumulator. */
1889 /* Allocate a small buffer that will hold a directive passed to
1890 sprintf or snprintf. */
1892 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1894 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1896 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1897 buf_malloced
= NULL
;
1902 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1903 if (size_overflow_p (buf_memsize
))
1904 goto out_of_memory_1
;
1905 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1907 goto out_of_memory_1
;
1912 allocated
= (resultbuf
!= NULL
? *lengthp
: 0);
1915 result is either == resultbuf or malloc-allocated.
1916 If result == NULL, resultbuf is == NULL as well.
1917 If length > 0, then result != NULL. */
1919 /* Ensures that allocated >= needed. Aborts through a jump to
1920 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1921 #define ENSURE_ALLOCATION_ELSE(needed, oom_statement) \
1922 if ((needed) > allocated) \
1924 size_t memory_size; \
1927 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1928 if ((needed) > allocated) \
1929 allocated = (needed); \
1930 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1931 if (size_overflow_p (memory_size)) \
1933 if (result == resultbuf) \
1934 memory = (DCHAR_T *) malloc (memory_size); \
1936 memory = (DCHAR_T *) realloc (result, memory_size); \
1937 if (memory == NULL) \
1939 if (result == resultbuf && length > 0) \
1940 DCHAR_CPY (memory, result, length); \
1943 #define ENSURE_ALLOCATION(needed) \
1944 ENSURE_ALLOCATION_ELSE((needed), goto out_of_memory; )
1946 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1948 if (cp
!= dp
->dir_start
)
1950 size_t n
= dp
->dir_start
- cp
;
1951 size_t augmented_length
= xsum (length
, n
);
1953 ENSURE_ALLOCATION (augmented_length
);
1954 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1955 need that the format string contains only ASCII characters
1956 if FCHAR_T and DCHAR_T are not the same type. */
1957 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1959 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1960 length
= augmented_length
;
1965 result
[length
++] = *cp
++;
1972 /* Execute a single directive. */
1973 if (dp
->conversion
== '%')
1975 size_t augmented_length
;
1977 if (!(dp
->arg_index
== ARG_NONE
))
1979 augmented_length
= xsum (length
, 1);
1980 ENSURE_ALLOCATION (augmented_length
);
1981 result
[length
] = '%';
1982 length
= augmented_length
;
1986 if (!(dp
->arg_index
!= ARG_NONE
))
1989 if (dp
->conversion
== 'n')
1991 switch (a
.arg
[dp
->arg_index
].type
)
1993 case TYPE_COUNT_SCHAR_POINTER
:
1994 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1996 case TYPE_COUNT_SHORT_POINTER
:
1997 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1999 case TYPE_COUNT_INT_POINTER
:
2000 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
2002 case TYPE_COUNT_LONGINT_POINTER
:
2003 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
2005 case TYPE_COUNT_LONGLONGINT_POINTER
:
2006 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
2013 /* The unistdio extensions. */
2014 else if (dp
->conversion
== 'U')
2016 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2017 int flags
= dp
->flags
;
2025 if (dp
->width_start
!= dp
->width_end
)
2027 if (dp
->width_arg_index
!= ARG_NONE
)
2031 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2033 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2037 /* "A negative field width is taken as a '-' flag
2038 followed by a positive field width." */
2045 const FCHAR_T
*digitp
= dp
->width_start
;
2048 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2049 while (digitp
!= dp
->width_end
);
2056 if (dp
->precision_start
!= dp
->precision_end
)
2058 if (dp
->precision_arg_index
!= ARG_NONE
)
2062 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2064 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2065 /* "A negative precision is taken as if the precision
2075 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2078 while (digitp
!= dp
->precision_end
)
2079 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2086 case TYPE_U8_STRING
:
2088 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
2089 const uint8_t *arg_end
;
2094 /* Use only PRECISION characters, from the left. */
2097 for (; precision
> 0; precision
--)
2099 int count
= u8_strmblen (arg_end
);
2103 goto fail_with_EILSEQ
;
2110 /* Use the entire string, and count the number of
2116 int count
= u8_strmblen (arg_end
);
2120 goto fail_with_EILSEQ
;
2127 /* Use the entire string. */
2128 arg_end
= arg
+ u8_strlen (arg
);
2129 /* The number of characters doesn't matter. */
2133 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2135 size_t n
= width
- characters
;
2136 ENSURE_ALLOCATION (xsum (length
, n
));
2137 DCHAR_SET (result
+ length
, ' ', n
);
2141 # if DCHAR_IS_UINT8_T
2143 size_t n
= arg_end
- arg
;
2144 ENSURE_ALLOCATION (xsum (length
, n
));
2145 DCHAR_CPY (result
+ length
, arg
, n
);
2150 DCHAR_T
*converted
= result
+ length
;
2151 size_t converted_len
= allocated
- length
;
2153 /* Convert from UTF-8 to locale encoding. */
2155 u8_conv_to_encoding (locale_charset (),
2156 iconveh_question_mark
,
2157 arg
, arg_end
- arg
, NULL
,
2158 converted
, &converted_len
);
2160 /* Convert from UTF-8 to UTF-16/UTF-32. */
2162 U8_TO_DCHAR (arg
, arg_end
- arg
,
2163 converted
, &converted_len
);
2165 if (converted
== NULL
)
2166 goto fail_with_errno
;
2167 if (converted
!= result
+ length
)
2169 ENSURE_ALLOCATION_ELSE (xsum (length
, converted_len
),
2170 { free (converted
); goto out_of_memory
; });
2171 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2174 length
+= converted_len
;
2178 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2180 size_t n
= width
- characters
;
2181 ENSURE_ALLOCATION (xsum (length
, n
));
2182 DCHAR_SET (result
+ length
, ' ', n
);
2188 case TYPE_U16_STRING
:
2190 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
2191 const uint16_t *arg_end
;
2196 /* Use only PRECISION characters, from the left. */
2199 for (; precision
> 0; precision
--)
2201 int count
= u16_strmblen (arg_end
);
2205 goto fail_with_EILSEQ
;
2212 /* Use the entire string, and count the number of
2218 int count
= u16_strmblen (arg_end
);
2222 goto fail_with_EILSEQ
;
2229 /* Use the entire string. */
2230 arg_end
= arg
+ u16_strlen (arg
);
2231 /* The number of characters doesn't matter. */
2235 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2237 size_t n
= width
- characters
;
2238 ENSURE_ALLOCATION (xsum (length
, n
));
2239 DCHAR_SET (result
+ length
, ' ', n
);
2243 # if DCHAR_IS_UINT16_T
2245 size_t n
= arg_end
- arg
;
2246 ENSURE_ALLOCATION (xsum (length
, n
));
2247 DCHAR_CPY (result
+ length
, arg
, n
);
2252 DCHAR_T
*converted
= result
+ length
;
2253 size_t converted_len
= allocated
- length
;
2255 /* Convert from UTF-16 to locale encoding. */
2257 u16_conv_to_encoding (locale_charset (),
2258 iconveh_question_mark
,
2259 arg
, arg_end
- arg
, NULL
,
2260 converted
, &converted_len
);
2262 /* Convert from UTF-16 to UTF-8/UTF-32. */
2264 U16_TO_DCHAR (arg
, arg_end
- arg
,
2265 converted
, &converted_len
);
2267 if (converted
== NULL
)
2268 goto fail_with_errno
;
2269 if (converted
!= result
+ length
)
2271 ENSURE_ALLOCATION_ELSE (xsum (length
, converted_len
),
2272 { free (converted
); goto out_of_memory
; });
2273 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2276 length
+= converted_len
;
2280 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2282 size_t n
= width
- characters
;
2283 ENSURE_ALLOCATION (xsum (length
, n
));
2284 DCHAR_SET (result
+ length
, ' ', n
);
2290 case TYPE_U32_STRING
:
2292 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
2293 const uint32_t *arg_end
;
2298 /* Use only PRECISION characters, from the left. */
2301 for (; precision
> 0; precision
--)
2303 int count
= u32_strmblen (arg_end
);
2307 goto fail_with_EILSEQ
;
2314 /* Use the entire string, and count the number of
2320 int count
= u32_strmblen (arg_end
);
2324 goto fail_with_EILSEQ
;
2331 /* Use the entire string. */
2332 arg_end
= arg
+ u32_strlen (arg
);
2333 /* The number of characters doesn't matter. */
2337 if (characters
< width
&& !(dp
->flags
& FLAG_LEFT
))
2339 size_t n
= width
- characters
;
2340 ENSURE_ALLOCATION (xsum (length
, n
));
2341 DCHAR_SET (result
+ length
, ' ', n
);
2345 # if DCHAR_IS_UINT32_T
2347 size_t n
= arg_end
- arg
;
2348 ENSURE_ALLOCATION (xsum (length
, n
));
2349 DCHAR_CPY (result
+ length
, arg
, n
);
2354 DCHAR_T
*converted
= result
+ length
;
2355 size_t converted_len
= allocated
- length
;
2357 /* Convert from UTF-32 to locale encoding. */
2359 u32_conv_to_encoding (locale_charset (),
2360 iconveh_question_mark
,
2361 arg
, arg_end
- arg
, NULL
,
2362 converted
, &converted_len
);
2364 /* Convert from UTF-32 to UTF-8/UTF-16. */
2366 U32_TO_DCHAR (arg
, arg_end
- arg
,
2367 converted
, &converted_len
);
2369 if (converted
== NULL
)
2370 goto fail_with_errno
;
2371 if (converted
!= result
+ length
)
2373 ENSURE_ALLOCATION_ELSE (xsum (length
, converted_len
),
2374 { free (converted
); goto out_of_memory
; });
2375 DCHAR_CPY (result
+ length
, converted
, converted_len
);
2378 length
+= converted_len
;
2382 if (characters
< width
&& (dp
->flags
& FLAG_LEFT
))
2384 size_t n
= width
- characters
;
2385 ENSURE_ALLOCATION (xsum (length
, n
));
2386 DCHAR_SET (result
+ length
, ' ', n
);
2397 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T
2398 else if (dp
->conversion
== 's'
2399 # if WIDE_CHAR_VERSION
2400 && a
.arg
[dp
->arg_index
].type
!= TYPE_WIDE_STRING
2402 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_STRING
2406 /* The normal handling of the 's' directive below requires
2407 allocating a temporary buffer. The determination of its
2408 length (tmp_length), in the case when a precision is
2409 specified, below requires a conversion between a char[]
2410 string and a wchar_t[] wide string. It could be done, but
2411 we have no guarantee that the implementation of sprintf will
2412 use the exactly same algorithm. Without this guarantee, it
2413 is possible to have buffer overrun bugs. In order to avoid
2414 such bugs, we implement the entire processing of the 's'
2415 directive ourselves. */
2416 int flags
= dp
->flags
;
2424 if (dp
->width_start
!= dp
->width_end
)
2426 if (dp
->width_arg_index
!= ARG_NONE
)
2430 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2432 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2436 /* "A negative field width is taken as a '-' flag
2437 followed by a positive field width." */
2444 const FCHAR_T
*digitp
= dp
->width_start
;
2447 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2448 while (digitp
!= dp
->width_end
);
2455 if (dp
->precision_start
!= dp
->precision_end
)
2457 if (dp
->precision_arg_index
!= ARG_NONE
)
2461 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2463 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2464 /* "A negative precision is taken as if the precision
2474 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2477 while (digitp
!= dp
->precision_end
)
2478 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2483 # if WIDE_CHAR_VERSION
2484 /* %s in vasnwprintf. See the specification of fwprintf. */
2486 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
2487 const char *arg_end
;
2492 /* Use only as many bytes as needed to produce PRECISION
2493 wide characters, from the left. */
2496 memset (&state
, '\0', sizeof (mbstate_t));
2500 for (; precision
> 0; precision
--)
2504 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2506 count
= mblen (arg_end
, MB_CUR_MAX
);
2509 /* Found the terminating NUL. */
2512 /* Invalid or incomplete multibyte character. */
2513 goto fail_with_EILSEQ
;
2520 /* Use the entire string, and count the number of wide
2524 memset (&state
, '\0', sizeof (mbstate_t));
2532 count
= mbrlen (arg_end
, MB_CUR_MAX
, &state
);
2534 count
= mblen (arg_end
, MB_CUR_MAX
);
2537 /* Found the terminating NUL. */
2540 /* Invalid or incomplete multibyte character. */
2541 goto fail_with_EILSEQ
;
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 static_assert (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 count
= local_wcrtomb (cbuf
, *arg_end
, &state
);
2658 /* Cannot convert. */
2659 goto fail_with_EILSEQ
;
2660 if (precision
< (unsigned int) count
)
2663 characters
+= count
;
2673 /* Use the entire string, and count the number of
2675 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2677 memset (&state
, '\0', sizeof (mbstate_t));
2683 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2687 /* Found the terminating null wide character. */
2689 count
= local_wcrtomb (cbuf
, *arg_end
, &state
);
2691 /* Cannot convert. */
2692 goto fail_with_EILSEQ
;
2694 characters
+= count
;
2700 /* Use the entire string. */
2701 arg_end
= arg
+ local_wcslen (arg
);
2702 /* The number of bytes doesn't matter. */
2707 # if !DCHAR_IS_TCHAR
2708 /* Convert the string into a piece of temporary memory. */
2709 tmpsrc
= (TCHAR_T
*) malloc (characters
* sizeof (TCHAR_T
));
2713 TCHAR_T
*tmpptr
= tmpsrc
;
2715 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2717 memset (&state
, '\0', sizeof (mbstate_t));
2719 for (remaining
= characters
; remaining
> 0; )
2721 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2726 count
= local_wcrtomb (cbuf
, *arg
, &state
);
2728 /* Inconsistency. */
2730 memcpy (tmpptr
, cbuf
, count
);
2735 if (!(arg
== arg_end
))
2739 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2741 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2742 iconveh_question_mark
,
2749 goto fail_with_errno
;
2756 # if ENABLE_UNISTDIO
2757 /* Outside POSIX, it's preferable to compare the width
2758 against the number of _characters_ of the converted
2760 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2762 /* The width is compared against the number of _bytes_
2763 of the converted value, says POSIX. */
2768 /* w doesn't matter. */
2771 if (w
< width
&& !(dp
->flags
& FLAG_LEFT
))
2773 size_t n
= width
- w
;
2774 ENSURE_ALLOCATION (xsum (length
, n
));
2775 DCHAR_SET (result
+ length
, ' ', n
);
2780 if (has_precision
|| has_width
)
2782 /* We know the number of bytes in advance. */
2784 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2786 memset (&state
, '\0', sizeof (mbstate_t));
2788 ENSURE_ALLOCATION (xsum (length
, characters
));
2789 for (remaining
= characters
; remaining
> 0; )
2791 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2796 count
= local_wcrtomb (cbuf
, *arg
, &state
);
2798 /* Inconsistency. */
2800 memcpy (result
+ length
, cbuf
, count
);
2805 if (!(arg
== arg_end
))
2810 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2812 memset (&state
, '\0', sizeof (mbstate_t));
2814 while (arg
< arg_end
)
2816 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2821 count
= local_wcrtomb (cbuf
, *arg
, &state
);
2823 /* Cannot convert. */
2824 goto fail_with_EILSEQ
;
2825 ENSURE_ALLOCATION (xsum (length
, count
));
2826 memcpy (result
+ length
, cbuf
, count
);
2832 ENSURE_ALLOCATION_ELSE (xsum (length
, tmpdst_len
),
2833 { free (tmpdst
); goto out_of_memory
; });
2834 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
2836 length
+= tmpdst_len
;
2839 if (w
< width
&& (dp
->flags
& FLAG_LEFT
))
2841 size_t n
= width
- w
;
2842 ENSURE_ALLOCATION (xsum (length
, n
));
2843 DCHAR_SET (result
+ length
, ' ', n
);
2850 #if ENABLE_WCHAR_FALLBACK && HAVE_WINT_T && !WIDE_CHAR_VERSION
2851 else if (dp
->conversion
== 'c'
2852 && a
.arg
[dp
->arg_index
].type
== TYPE_WIDE_CHAR
)
2854 /* Implement the 'lc' directive ourselves, in order to provide
2855 the fallback that avoids EILSEQ. */
2856 int flags
= dp
->flags
;
2862 if (dp
->width_start
!= dp
->width_end
)
2864 if (dp
->width_arg_index
!= ARG_NONE
)
2868 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2870 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2874 /* "A negative field width is taken as a '-' flag
2875 followed by a positive field width." */
2882 const FCHAR_T
*digitp
= dp
->width_start
;
2885 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2886 while (digitp
!= dp
->width_end
);
2891 /* %lc in vasnprintf. See the specification of fprintf. */
2893 wchar_t arg
= (wchar_t) a
.arg
[dp
->arg_index
].a
.a_wide_char
;
2895 # if !DCHAR_IS_TCHAR
2896 /* This code assumes that TCHAR_T is 'char'. */
2897 static_assert (sizeof (TCHAR_T
) == 1);
2898 TCHAR_T tmpsrc
[64]; /* Assume MB_CUR_MAX <= 64. */
2908 /* Count the number of bytes. */
2912 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2914 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2916 memset (&state
, '\0', sizeof (mbstate_t));
2919 count
= local_wcrtomb (cbuf
, arg
, &state
);
2921 /* Inconsistency. */
2929 /* The number of bytes doesn't matter. */
2934 # if !DCHAR_IS_TCHAR
2935 /* Convert the string into a piece of temporary memory. */
2936 if (characters
> 0) /* implies arg != 0 */
2938 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
2940 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2942 memset (&state
, '\0', sizeof (mbstate_t));
2945 count
= local_wcrtomb (cbuf
, arg
, &state
);
2947 /* Inconsistency. */
2949 memcpy (tmpsrc
, cbuf
, count
);
2952 /* Convert from TCHAR_T[] to DCHAR_T[]. */
2954 DCHAR_CONV_FROM_ENCODING (locale_charset (),
2955 iconveh_question_mark
,
2960 goto fail_with_errno
;
2965 # if ENABLE_UNISTDIO
2966 /* Outside POSIX, it's preferable to compare the width
2967 against the number of _characters_ of the converted
2969 w
= DCHAR_MBSNLEN (result
+ length
, characters
);
2971 /* The width is compared against the number of _bytes_
2972 of the converted value, says POSIX. */
2977 /* w doesn't matter. */
2980 if (w
< width
&& !(dp
->flags
& FLAG_LEFT
))
2982 size_t n
= width
- w
;
2983 ENSURE_ALLOCATION (xsum (length
, n
));
2984 DCHAR_SET (result
+ length
, ' ', n
);
2991 /* We know the number of bytes in advance. */
2992 ENSURE_ALLOCATION (xsum (length
, characters
));
2993 if (characters
> 0) /* implies arg != 0 */
2996 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2998 memset (&state
, '\0', sizeof (mbstate_t));
3001 count
= local_wcrtomb (result
+ length
, arg
, &state
);
3003 /* Inconsistency. */
3012 char cbuf
[64]; /* Assume MB_CUR_MAX <= 64. */
3014 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3016 memset (&state
, '\0', sizeof (mbstate_t));
3019 count
= local_wcrtomb (cbuf
, arg
, &state
);
3021 /* Inconsistency. */
3023 ENSURE_ALLOCATION (xsum (length
, count
));
3024 memcpy (result
+ length
, cbuf
, count
);
3029 ENSURE_ALLOCATION_ELSE (xsum (length
, tmpdst_len
),
3030 { free (tmpdst
); goto out_of_memory
; });
3031 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
3033 length
+= tmpdst_len
;
3036 if (w
< width
&& (dp
->flags
& FLAG_LEFT
))
3038 size_t n
= width
- w
;
3039 ENSURE_ALLOCATION (xsum (length
, n
));
3040 DCHAR_SET (result
+ length
, ' ', n
);
3046 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
3047 else if ((dp
->conversion
== 'a' || dp
->conversion
== 'A')
3048 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
3050 # if NEED_PRINTF_DOUBLE
3051 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3053 # if NEED_PRINTF_LONG_DOUBLE
3054 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3060 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3061 int flags
= dp
->flags
;
3067 DCHAR_T tmpbuf
[700];
3073 if (dp
->width_start
!= dp
->width_end
)
3075 if (dp
->width_arg_index
!= ARG_NONE
)
3079 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3081 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3085 /* "A negative field width is taken as a '-' flag
3086 followed by a positive field width." */
3093 const FCHAR_T
*digitp
= dp
->width_start
;
3096 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3097 while (digitp
!= dp
->width_end
);
3103 if (dp
->precision_start
!= dp
->precision_end
)
3105 if (dp
->precision_arg_index
!= ARG_NONE
)
3109 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3111 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3112 /* "A negative precision is taken as if the precision
3122 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3125 while (digitp
!= dp
->precision_end
)
3126 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3131 /* Allocate a temporary buffer of sufficient size. */
3132 if (type
== TYPE_LONGDOUBLE
)
3134 (unsigned int) ((LDBL_DIG
+ 1)
3135 * 0.831 /* decimal -> hexadecimal */
3137 + 1; /* turn floor into ceil */
3140 (unsigned int) ((DBL_DIG
+ 1)
3141 * 0.831 /* decimal -> hexadecimal */
3143 + 1; /* turn floor into ceil */
3144 if (tmp_length
< precision
)
3145 tmp_length
= precision
;
3146 /* Account for sign, decimal point etc. */
3147 tmp_length
= xsum (tmp_length
, 12);
3149 if (tmp_length
< width
)
3152 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3154 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3158 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3160 if (size_overflow_p (tmp_memsize
))
3161 /* Overflow, would lead to out of memory. */
3163 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3165 /* Out of memory. */
3171 if (type
== TYPE_LONGDOUBLE
)
3173 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3174 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3178 if (dp
->conversion
== 'A')
3180 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3184 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3190 DECL_LONG_DOUBLE_ROUNDING
3192 BEGIN_LONG_DOUBLE_ROUNDING ();
3194 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3202 else if (flags
& FLAG_SHOWSIGN
)
3204 else if (flags
& FLAG_SPACE
)
3207 if (arg
> 0.0L && arg
+ arg
== arg
)
3209 if (dp
->conversion
== 'A')
3211 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3215 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3221 long double mantissa
;
3224 mantissa
= printf_frexpl (arg
, &exponent
);
3232 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
3234 /* Round the mantissa. */
3235 long double tail
= mantissa
;
3238 for (q
= precision
; ; q
--)
3240 int digit
= (int) tail
;
3244 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
3253 for (q
= precision
; q
> 0; q
--)
3259 *p
++ = dp
->conversion
- 'A' + 'X';
3264 digit
= (int) mantissa
;
3267 if ((flags
& FLAG_ALT
)
3268 || mantissa
> 0.0L || precision
> 0)
3270 *p
++ = decimal_point_char ();
3271 /* This loop terminates because we assume
3272 that FLT_RADIX is a power of 2. */
3273 while (mantissa
> 0.0L)
3276 digit
= (int) mantissa
;
3281 : dp
->conversion
- 10);
3285 while (precision
> 0)
3292 *p
++ = dp
->conversion
- 'A' + 'P';
3293 # if WIDE_CHAR_VERSION
3295 static const wchar_t decimal_format
[] =
3296 { '%', '+', 'd', '\0' };
3297 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3302 if (sizeof (DCHAR_T
) == 1)
3304 sprintf ((char *) p
, "%+d", exponent
);
3312 sprintf (expbuf
, "%+d", exponent
);
3313 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3319 END_LONG_DOUBLE_ROUNDING ();
3327 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3328 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3332 if (dp
->conversion
== 'A')
3334 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3338 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3345 if (signbit (arg
)) /* arg < 0.0 or negative zero */
3353 else if (flags
& FLAG_SHOWSIGN
)
3355 else if (flags
& FLAG_SPACE
)
3358 if (arg
> 0.0 && arg
+ arg
== arg
)
3360 if (dp
->conversion
== 'A')
3362 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3366 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3375 mantissa
= printf_frexp (arg
, &exponent
);
3383 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
3385 /* Round the mantissa. */
3386 double tail
= mantissa
;
3389 for (q
= precision
; ; q
--)
3391 int digit
= (int) tail
;
3395 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
3404 for (q
= precision
; q
> 0; q
--)
3410 *p
++ = dp
->conversion
- 'A' + 'X';
3415 digit
= (int) mantissa
;
3418 if ((flags
& FLAG_ALT
)
3419 || mantissa
> 0.0 || precision
> 0)
3421 *p
++ = decimal_point_char ();
3422 /* This loop terminates because we assume
3423 that FLT_RADIX is a power of 2. */
3424 while (mantissa
> 0.0)
3427 digit
= (int) mantissa
;
3432 : dp
->conversion
- 10);
3436 while (precision
> 0)
3443 *p
++ = dp
->conversion
- 'A' + 'P';
3444 # if WIDE_CHAR_VERSION
3446 static const wchar_t decimal_format
[] =
3447 { '%', '+', 'd', '\0' };
3448 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3453 if (sizeof (DCHAR_T
) == 1)
3455 sprintf ((char *) p
, "%+d", exponent
);
3463 sprintf (expbuf
, "%+d", exponent
);
3464 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3475 /* The generated string now extends from tmp to p, with the
3476 zero padding insertion point being at pad_ptr. */
3481 size_t pad
= width
- count
;
3482 DCHAR_T
*end
= p
+ pad
;
3484 if (flags
& FLAG_LEFT
)
3486 /* Pad with spaces on the right. */
3487 for (; pad
> 0; pad
--)
3490 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3492 /* Pad with zeroes. */
3497 for (; pad
> 0; pad
--)
3502 /* Pad with spaces on the left. */
3507 for (; pad
> 0; pad
--)
3516 if (count
>= tmp_length
)
3517 /* tmp_length was incorrectly calculated - fix the
3521 /* Make room for the result. */
3522 if (count
>= allocated
- length
)
3524 size_t n
= xsum (length
, count
);
3526 ENSURE_ALLOCATION (n
);
3529 /* Append the result. */
3530 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3536 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3537 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
3538 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
3539 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
3540 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
3542 # if NEED_PRINTF_DOUBLE
3543 || a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3544 # elif NEED_PRINTF_INFINITE_DOUBLE
3545 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
3546 /* The systems (mingw) which produce wrong output
3547 for Inf, -Inf, and NaN also do so for -0.0.
3548 Therefore we treat this case here as well. */
3549 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
3551 # if NEED_PRINTF_LONG_DOUBLE
3552 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3553 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3554 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
3555 /* Some systems produce wrong output for Inf,
3556 -Inf, and NaN. Some systems in this category
3557 (IRIX 5.3) also do so for -0.0. Therefore we
3558 treat this case here as well. */
3559 && is_infinite_or_zerol (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
3563 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3564 arg_type type
= a
.arg
[dp
->arg_index
].type
;
3566 int flags
= dp
->flags
;
3572 DCHAR_T tmpbuf
[700];
3578 if (dp
->width_start
!= dp
->width_end
)
3580 if (dp
->width_arg_index
!= ARG_NONE
)
3584 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3586 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3590 /* "A negative field width is taken as a '-' flag
3591 followed by a positive field width." */
3598 const FCHAR_T
*digitp
= dp
->width_start
;
3601 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3602 while (digitp
!= dp
->width_end
);
3608 if (dp
->precision_start
!= dp
->precision_end
)
3610 if (dp
->precision_arg_index
!= ARG_NONE
)
3614 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3616 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3617 /* "A negative precision is taken as if the precision
3627 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3630 while (digitp
!= dp
->precision_end
)
3631 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3636 /* POSIX specifies the default precision to be 6 for %f, %F,
3637 %e, %E, but not for %g, %G. Implementations appear to use
3638 the same default precision also for %g, %G. But for %a, %A,
3639 the default precision is 0. */
3641 if (!(dp
->conversion
== 'a' || dp
->conversion
== 'A'))
3644 /* Allocate a temporary buffer of sufficient size. */
3645 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3646 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : DBL_DIG
+ 1);
3647 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3648 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
3649 # elif NEED_PRINTF_LONG_DOUBLE
3650 tmp_length
= LDBL_DIG
+ 1;
3651 # elif NEED_PRINTF_DOUBLE
3652 tmp_length
= DBL_DIG
+ 1;
3656 if (tmp_length
< precision
)
3657 tmp_length
= precision
;
3658 # if NEED_PRINTF_LONG_DOUBLE
3659 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3660 if (type
== TYPE_LONGDOUBLE
)
3662 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3664 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3665 if (!(isnanl (arg
) || arg
+ arg
== arg
))
3667 /* arg is finite and nonzero. */
3668 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
3669 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3670 tmp_length
= exponent
+ precision
;
3674 # if NEED_PRINTF_DOUBLE
3675 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3676 if (type
== TYPE_DOUBLE
)
3678 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3680 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3681 if (!(isnand (arg
) || arg
+ arg
== arg
))
3683 /* arg is finite and nonzero. */
3684 int exponent
= floorlog10 (arg
< 0 ? -arg
: arg
);
3685 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
3686 tmp_length
= exponent
+ precision
;
3690 /* Account for sign, decimal point etc. */
3691 tmp_length
= xsum (tmp_length
, 12);
3693 if (tmp_length
< width
)
3696 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3698 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
3702 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
3704 if (size_overflow_p (tmp_memsize
))
3705 /* Overflow, would lead to out of memory. */
3707 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
3709 /* Out of memory. */
3716 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3717 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3718 if (type
== TYPE_LONGDOUBLE
)
3721 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3725 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3727 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
3731 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
3737 DECL_LONG_DOUBLE_ROUNDING
3739 BEGIN_LONG_DOUBLE_ROUNDING ();
3741 if (signbit (arg
)) /* arg < 0.0L or negative zero */
3749 else if (flags
& FLAG_SHOWSIGN
)
3751 else if (flags
& FLAG_SPACE
)
3754 if (arg
> 0.0L && arg
+ arg
== arg
)
3756 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
3758 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
3762 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
3767 # if NEED_PRINTF_LONG_DOUBLE
3770 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
3776 scale10_round_decimal_long_double (arg
, precision
);
3779 END_LONG_DOUBLE_ROUNDING ();
3782 ndigits
= strlen (digits
);
3784 if (ndigits
> precision
)
3788 *p
++ = digits
[ndigits
];
3790 while (ndigits
> precision
);
3793 /* Here ndigits <= precision. */
3794 if ((flags
& FLAG_ALT
) || precision
> 0)
3796 *p
++ = decimal_point_char ();
3797 for (; precision
> ndigits
; precision
--)
3802 *p
++ = digits
[ndigits
];
3808 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
3816 if ((flags
& FLAG_ALT
) || precision
> 0)
3818 *p
++ = decimal_point_char ();
3819 for (; precision
> 0; precision
--)
3830 exponent
= floorlog10l (arg
);
3835 scale10_round_decimal_long_double (arg
,
3836 (int)precision
- exponent
);
3839 END_LONG_DOUBLE_ROUNDING ();
3842 ndigits
= strlen (digits
);
3844 if (ndigits
== precision
+ 1)
3846 if (ndigits
< precision
3847 || ndigits
> precision
+ 2)
3848 /* The exponent was not guessed
3849 precisely enough. */
3852 /* None of two values of exponent is
3853 the right one. Prevent an endless
3857 if (ndigits
== precision
)
3863 /* Here ndigits = precision+1. */
3864 if (is_borderline (digits
, precision
))
3866 /* Maybe the exponent guess was too high
3867 and a smaller exponent can be reached
3868 by turning a 10...0 into 9...9x. */
3870 scale10_round_decimal_long_double (arg
,
3871 (int)precision
- exponent
+ 1);
3872 if (digits2
== NULL
)
3875 END_LONG_DOUBLE_ROUNDING ();
3878 if (strlen (digits2
) == precision
+ 1)
3887 /* Here ndigits = precision+1. */
3889 *p
++ = digits
[--ndigits
];
3890 if ((flags
& FLAG_ALT
) || precision
> 0)
3892 *p
++ = decimal_point_char ();
3896 *p
++ = digits
[ndigits
];
3903 *p
++ = dp
->conversion
; /* 'e' or 'E' */
3904 # if WIDE_CHAR_VERSION
3906 static const wchar_t decimal_format
[] =
3907 { '%', '+', '.', '2', 'd', '\0' };
3908 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
3913 if (sizeof (DCHAR_T
) == 1)
3915 sprintf ((char *) p
, "%+.2d", exponent
);
3923 sprintf (expbuf
, "%+.2d", exponent
);
3924 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
3929 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
3933 /* precision >= 1. */
3936 /* The exponent is 0, >= -4, < precision.
3937 Use fixed-point notation. */
3939 size_t ndigits
= precision
;
3940 /* Number of trailing zeroes that have to be
3943 (flags
& FLAG_ALT
? 0 : precision
- 1);
3947 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
3949 *p
++ = decimal_point_char ();
3950 while (ndigits
> nzeroes
)
3966 exponent
= floorlog10l (arg
);
3971 scale10_round_decimal_long_double (arg
,
3972 (int)(precision
- 1) - exponent
);
3975 END_LONG_DOUBLE_ROUNDING ();
3978 ndigits
= strlen (digits
);
3980 if (ndigits
== precision
)
3982 if (ndigits
< precision
- 1
3983 || ndigits
> precision
+ 1)
3984 /* The exponent was not guessed
3985 precisely enough. */
3988 /* None of two values of exponent is
3989 the right one. Prevent an endless
3993 if (ndigits
< precision
)
3999 /* Here ndigits = precision. */
4000 if (is_borderline (digits
, precision
- 1))
4002 /* Maybe the exponent guess was too high
4003 and a smaller exponent can be reached
4004 by turning a 10...0 into 9...9x. */
4006 scale10_round_decimal_long_double (arg
,
4007 (int)(precision
- 1) - exponent
+ 1);
4008 if (digits2
== NULL
)
4011 END_LONG_DOUBLE_ROUNDING ();
4014 if (strlen (digits2
) == precision
)
4023 /* Here ndigits = precision. */
4025 /* Determine the number of trailing zeroes
4026 that have to be dropped. */
4028 if ((flags
& FLAG_ALT
) == 0)
4029 while (nzeroes
< ndigits
4030 && digits
[nzeroes
] == '0')
4033 /* The exponent is now determined. */
4035 && exponent
< (long)precision
)
4037 /* Fixed-point notation:
4038 max(exponent,0)+1 digits, then the
4039 decimal point, then the remaining
4040 digits without trailing zeroes. */
4043 size_t ecount
= exponent
+ 1;
4044 /* Note: count <= precision = ndigits. */
4045 for (; ecount
> 0; ecount
--)
4046 *p
++ = digits
[--ndigits
];
4047 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4049 *p
++ = decimal_point_char ();
4050 while (ndigits
> nzeroes
)
4053 *p
++ = digits
[ndigits
];
4059 size_t ecount
= -exponent
- 1;
4061 *p
++ = decimal_point_char ();
4062 for (; ecount
> 0; ecount
--)
4064 while (ndigits
> nzeroes
)
4067 *p
++ = digits
[ndigits
];
4073 /* Exponential notation. */
4074 *p
++ = digits
[--ndigits
];
4075 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4077 *p
++ = decimal_point_char ();
4078 while (ndigits
> nzeroes
)
4081 *p
++ = digits
[ndigits
];
4084 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4085 # if WIDE_CHAR_VERSION
4087 static const wchar_t decimal_format
[] =
4088 { '%', '+', '.', '2', 'd', '\0' };
4089 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4094 if (sizeof (DCHAR_T
) == 1)
4096 sprintf ((char *) p
, "%+.2d", exponent
);
4104 sprintf (expbuf
, "%+.2d", exponent
);
4105 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4117 /* arg is finite. */
4123 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4126 if ((flags
& FLAG_ALT
) || precision
> 0)
4128 *p
++ = decimal_point_char ();
4129 for (; precision
> 0; precision
--)
4133 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4136 if ((flags
& FLAG_ALT
) || precision
> 0)
4138 *p
++ = decimal_point_char ();
4139 for (; precision
> 0; precision
--)
4142 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4147 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4150 if (flags
& FLAG_ALT
)
4153 (precision
> 0 ? precision
- 1 : 0);
4154 *p
++ = decimal_point_char ();
4155 for (; ndigits
> 0; --ndigits
)
4159 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
4162 *p
++ = dp
->conversion
- 'A' + 'X';
4165 if ((flags
& FLAG_ALT
) || precision
> 0)
4167 *p
++ = decimal_point_char ();
4168 for (; precision
> 0; precision
--)
4171 *p
++ = dp
->conversion
- 'A' + 'P';
4180 END_LONG_DOUBLE_ROUNDING ();
4183 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4187 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4189 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
4193 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4195 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
4199 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
4206 if (signbit (arg
)) /* arg < 0.0 or negative zero */
4214 else if (flags
& FLAG_SHOWSIGN
)
4216 else if (flags
& FLAG_SPACE
)
4219 if (arg
> 0.0 && arg
+ arg
== arg
)
4221 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
4223 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
4227 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
4232 # if NEED_PRINTF_DOUBLE
4235 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4241 scale10_round_decimal_double (arg
, precision
);
4244 ndigits
= strlen (digits
);
4246 if (ndigits
> precision
)
4250 *p
++ = digits
[ndigits
];
4252 while (ndigits
> precision
);
4255 /* Here ndigits <= precision. */
4256 if ((flags
& FLAG_ALT
) || precision
> 0)
4258 *p
++ = decimal_point_char ();
4259 for (; precision
> ndigits
; precision
--)
4264 *p
++ = digits
[ndigits
];
4270 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4278 if ((flags
& FLAG_ALT
) || precision
> 0)
4280 *p
++ = decimal_point_char ();
4281 for (; precision
> 0; precision
--)
4292 exponent
= floorlog10 (arg
);
4297 scale10_round_decimal_double (arg
,
4298 (int)precision
- exponent
);
4301 ndigits
= strlen (digits
);
4303 if (ndigits
== precision
+ 1)
4305 if (ndigits
< precision
4306 || ndigits
> precision
+ 2)
4307 /* The exponent was not guessed
4308 precisely enough. */
4311 /* None of two values of exponent is
4312 the right one. Prevent an endless
4316 if (ndigits
== precision
)
4322 /* Here ndigits = precision+1. */
4323 if (is_borderline (digits
, precision
))
4325 /* Maybe the exponent guess was too high
4326 and a smaller exponent can be reached
4327 by turning a 10...0 into 9...9x. */
4329 scale10_round_decimal_double (arg
,
4330 (int)precision
- exponent
+ 1);
4331 if (digits2
== NULL
)
4336 if (strlen (digits2
) == precision
+ 1)
4345 /* Here ndigits = precision+1. */
4347 *p
++ = digits
[--ndigits
];
4348 if ((flags
& FLAG_ALT
) || precision
> 0)
4350 *p
++ = decimal_point_char ();
4354 *p
++ = digits
[ndigits
];
4361 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4362 # if WIDE_CHAR_VERSION
4364 static const wchar_t decimal_format
[] =
4365 /* Produce the same number of exponent digits
4366 as the native printf implementation. */
4367 # if defined _WIN32 && ! defined __CYGWIN__
4368 { '%', '+', '.', '3', 'd', '\0' };
4370 { '%', '+', '.', '2', 'd', '\0' };
4372 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4378 static const char decimal_format
[] =
4379 /* Produce the same number of exponent digits
4380 as the native printf implementation. */
4381 # if defined _WIN32 && ! defined __CYGWIN__
4386 if (sizeof (DCHAR_T
) == 1)
4388 sprintf ((char *) p
, decimal_format
, exponent
);
4396 sprintf (expbuf
, decimal_format
, exponent
);
4397 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4403 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4407 /* precision >= 1. */
4410 /* The exponent is 0, >= -4, < precision.
4411 Use fixed-point notation. */
4413 size_t ndigits
= precision
;
4414 /* Number of trailing zeroes that have to be
4417 (flags
& FLAG_ALT
? 0 : precision
- 1);
4421 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4423 *p
++ = decimal_point_char ();
4424 while (ndigits
> nzeroes
)
4440 exponent
= floorlog10 (arg
);
4445 scale10_round_decimal_double (arg
,
4446 (int)(precision
- 1) - exponent
);
4449 ndigits
= strlen (digits
);
4451 if (ndigits
== precision
)
4453 if (ndigits
< precision
- 1
4454 || ndigits
> precision
+ 1)
4455 /* The exponent was not guessed
4456 precisely enough. */
4459 /* None of two values of exponent is
4460 the right one. Prevent an endless
4464 if (ndigits
< precision
)
4470 /* Here ndigits = precision. */
4471 if (is_borderline (digits
, precision
- 1))
4473 /* Maybe the exponent guess was too high
4474 and a smaller exponent can be reached
4475 by turning a 10...0 into 9...9x. */
4477 scale10_round_decimal_double (arg
,
4478 (int)(precision
- 1) - exponent
+ 1);
4479 if (digits2
== NULL
)
4484 if (strlen (digits2
) == precision
)
4493 /* Here ndigits = precision. */
4495 /* Determine the number of trailing zeroes
4496 that have to be dropped. */
4498 if ((flags
& FLAG_ALT
) == 0)
4499 while (nzeroes
< ndigits
4500 && digits
[nzeroes
] == '0')
4503 /* The exponent is now determined. */
4505 && exponent
< (long)precision
)
4507 /* Fixed-point notation:
4508 max(exponent,0)+1 digits, then the
4509 decimal point, then the remaining
4510 digits without trailing zeroes. */
4513 size_t ecount
= exponent
+ 1;
4514 /* Note: ecount <= precision = ndigits. */
4515 for (; ecount
> 0; ecount
--)
4516 *p
++ = digits
[--ndigits
];
4517 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4519 *p
++ = decimal_point_char ();
4520 while (ndigits
> nzeroes
)
4523 *p
++ = digits
[ndigits
];
4529 size_t ecount
= -exponent
- 1;
4531 *p
++ = decimal_point_char ();
4532 for (; ecount
> 0; ecount
--)
4534 while (ndigits
> nzeroes
)
4537 *p
++ = digits
[ndigits
];
4543 /* Exponential notation. */
4544 *p
++ = digits
[--ndigits
];
4545 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
4547 *p
++ = decimal_point_char ();
4548 while (ndigits
> nzeroes
)
4551 *p
++ = digits
[ndigits
];
4554 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
4555 # if WIDE_CHAR_VERSION
4557 static const wchar_t decimal_format
[] =
4558 /* Produce the same number of exponent digits
4559 as the native printf implementation. */
4560 # if defined _WIN32 && ! defined __CYGWIN__
4561 { '%', '+', '.', '3', 'd', '\0' };
4563 { '%', '+', '.', '2', 'd', '\0' };
4565 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
4571 static const char decimal_format
[] =
4572 /* Produce the same number of exponent digits
4573 as the native printf implementation. */
4574 # if defined _WIN32 && ! defined __CYGWIN__
4579 if (sizeof (DCHAR_T
) == 1)
4581 sprintf ((char *) p
, decimal_format
, exponent
);
4589 sprintf (expbuf
, decimal_format
, exponent
);
4590 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
4603 /* arg is finite. */
4609 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
4612 if ((flags
& FLAG_ALT
) || precision
> 0)
4614 *p
++ = decimal_point_char ();
4615 for (; precision
> 0; precision
--)
4619 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
4622 if ((flags
& FLAG_ALT
) || precision
> 0)
4624 *p
++ = decimal_point_char ();
4625 for (; precision
> 0; precision
--)
4628 *p
++ = dp
->conversion
; /* 'e' or 'E' */
4630 /* Produce the same number of exponent digits as
4631 the native printf implementation. */
4632 # if defined _WIN32 && ! defined __CYGWIN__
4638 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
4641 if (flags
& FLAG_ALT
)
4644 (precision
> 0 ? precision
- 1 : 0);
4645 *p
++ = decimal_point_char ();
4646 for (; ndigits
> 0; --ndigits
)
4658 /* The generated string now extends from tmp to p, with the
4659 zero padding insertion point being at pad_ptr. */
4664 size_t pad
= width
- count
;
4665 DCHAR_T
*end
= p
+ pad
;
4667 if (flags
& FLAG_LEFT
)
4669 /* Pad with spaces on the right. */
4670 for (; pad
> 0; pad
--)
4673 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
4675 /* Pad with zeroes. */
4680 for (; pad
> 0; pad
--)
4685 /* Pad with spaces on the left. */
4690 for (; pad
> 0; pad
--)
4699 if (count
>= tmp_length
)
4700 /* tmp_length was incorrectly calculated - fix the
4704 /* Make room for the result. */
4705 if (count
>= allocated
- length
)
4707 size_t n
= xsum (length
, count
);
4709 ENSURE_ALLOCATION (n
);
4712 /* Append the result. */
4713 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
4721 arg_type type
= a
.arg
[dp
->arg_index
].type
;
4722 int flags
= dp
->flags
;
4723 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4726 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4729 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4733 #if NEED_PRINTF_UNBOUNDED_PRECISION
4736 # define prec_ourselves 0
4738 #if NEED_PRINTF_FLAG_LEFTADJUST
4739 # define pad_ourselves 1
4740 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4743 # define pad_ourselves 0
4746 unsigned int prefix_count
;
4747 int prefixes
[2] IF_LINT (= { 0 });
4751 TCHAR_T tmpbuf
[700];
4755 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4758 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4760 if (dp
->width_start
!= dp
->width_end
)
4762 if (dp
->width_arg_index
!= ARG_NONE
)
4766 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
4768 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
4772 /* "A negative field width is taken as a '-' flag
4773 followed by a positive field width." */
4780 const FCHAR_T
*digitp
= dp
->width_start
;
4783 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
4784 while (digitp
!= dp
->width_end
);
4786 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4792 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4795 if (dp
->precision_start
!= dp
->precision_end
)
4797 if (dp
->precision_arg_index
!= ARG_NONE
)
4801 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
4803 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
4804 /* "A negative precision is taken as if the precision
4814 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
4817 while (digitp
!= dp
->precision_end
)
4818 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
4824 /* Decide whether to handle the precision ourselves. */
4825 #if NEED_PRINTF_UNBOUNDED_PRECISION
4826 switch (dp
->conversion
)
4828 case 'd': case 'i': case 'u':
4830 case 'x': case 'X': case 'p':
4831 prec_ourselves
= has_precision
&& (precision
> 0);
4839 /* Decide whether to perform the padding ourselves. */
4840 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4841 switch (dp
->conversion
)
4843 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4844 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4845 to perform the padding after this conversion. Functions
4846 with unistdio extensions perform the padding based on
4847 character count rather than element count. */
4850 # if NEED_PRINTF_FLAG_ZERO
4851 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4857 pad_ourselves
= prec_ourselves
;
4863 /* Allocate a temporary buffer of sufficient size for calling
4866 MAX_ROOM_NEEDED (&a
, dp
->arg_index
, dp
->conversion
, type
,
4867 flags
, width
, has_precision
, precision
,
4870 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
4874 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
4876 if (size_overflow_p (tmp_memsize
))
4877 /* Overflow, would lead to out of memory. */
4879 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
4881 /* Out of memory. */
4886 /* Construct the format string for calling snprintf or
4890 #if NEED_PRINTF_FLAG_GROUPING
4891 /* The underlying implementation doesn't support the ' flag.
4892 Produce no grouping characters in this case; this is
4893 acceptable because the grouping is locale dependent. */
4895 if (flags
& FLAG_GROUP
)
4898 if (flags
& FLAG_LEFT
)
4900 if (flags
& FLAG_SHOWSIGN
)
4902 if (flags
& FLAG_SPACE
)
4904 if (flags
& FLAG_ALT
)
4906 #if __GLIBC__ >= 2 && !defined __UCLIBC__
4907 if (flags
& FLAG_LOCALIZED
)
4912 if (flags
& FLAG_ZERO
)
4914 if (dp
->width_start
!= dp
->width_end
)
4916 size_t n
= dp
->width_end
- dp
->width_start
;
4917 /* The width specification is known to consist only
4918 of standard ASCII characters. */
4919 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4921 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
4926 const FCHAR_T
*mp
= dp
->width_start
;
4933 if (!prec_ourselves
)
4935 if (dp
->precision_start
!= dp
->precision_end
)
4937 size_t n
= dp
->precision_end
- dp
->precision_start
;
4938 /* The precision specification is known to consist only
4939 of standard ASCII characters. */
4940 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
4942 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
4947 const FCHAR_T
*mp
= dp
->precision_start
;
4957 case TYPE_LONGLONGINT
:
4958 case TYPE_ULONGLONGINT
:
4959 #if defined _WIN32 && ! defined __CYGWIN__
4971 case TYPE_WIDE_CHAR
:
4974 case TYPE_WIDE_STRING
:
4978 case TYPE_LONGDOUBLE
:
4984 #if NEED_PRINTF_DIRECTIVE_F
4985 if (dp
->conversion
== 'F')
4989 *fbp
= dp
->conversion
;
4991 # if ((HAVE_SNPRINTF_RETVAL_C99 && HAVE_SNPRINTF_TRUNCATION_C99) \
4992 || ((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) \
4993 && !defined __UCLIBC__) \
4994 || (defined __APPLE__ && defined __MACH__) \
4995 || defined __ANDROID__ \
4996 || (defined _WIN32 && ! defined __CYGWIN__))
4997 /* On systems where we know that snprintf's return value
4998 conforms to ISO C 99 (HAVE_SNPRINTF_RETVAL_C99) and that
4999 snprintf always produces NUL-terminated strings
5000 (HAVE_SNPRINTF_TRUNCATION_C99), it is possible to avoid
5001 using %n. And it is desirable to do so, because more and
5002 more platforms no longer support %n, for "security reasons".
5003 In particular, the following platforms:
5004 - On glibc2 systems from 2004-10-18 or newer, the use of
5005 %n in format strings in writable memory may crash the
5006 program (if compiled with _FORTIFY_SOURCE=2).
5007 - On Mac OS X 10.13 or newer, the use of %n in format
5008 strings in writable memory by default crashes the
5010 - On Android, starting on 2018-03-07, the use of %n in
5011 format strings produces a fatal error (see
5012 <https://android.googlesource.com/platform/bionic/+/41398d03b7e8e0dfb951660ae713e682e9fc0336>).
5013 On these platforms, HAVE_SNPRINTF_RETVAL_C99 and
5014 HAVE_SNPRINTF_TRUNCATION_C99 are 1. We have listed them
5015 explicitly in the condition above, in case of cross-
5016 compilation (just to be sure). */
5017 /* On native Windows systems (such as mingw), we can avoid using
5019 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
5020 snprintf does not write more than the specified number
5021 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
5022 '4', '5', '6' into buf, not '4', '5', '\0'.)
5023 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
5024 allows us to recognize the case of an insufficient
5025 buffer size: it returns -1 in this case.
5026 On native Windows systems (such as mingw) where the OS is
5027 Windows Vista, the use of %n in format strings by default
5028 crashes the program. See
5029 <https://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
5030 <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-printf-count-output>
5031 So we should avoid %n in this situation. */
5033 # else /* AIX <= 5.1, HP-UX, IRIX, OSF/1, Solaris <= 9, BeOS */
5042 /* Construct the arguments for calling snprintf or sprintf. */
5044 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
5046 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
5048 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
5050 if (!prec_ourselves
&& dp
->precision_arg_index
!= ARG_NONE
)
5052 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
5054 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
5058 /* The SNPRINTF result is appended after result[0..length].
5059 The latter is an array of DCHAR_T; SNPRINTF appends an
5060 array of TCHAR_T to it. This is possible because
5061 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
5062 alignof (TCHAR_T) <= alignof (DCHAR_T). */
5063 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
5064 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
5065 where an snprintf() with maxlen==1 acts like sprintf(). */
5066 ENSURE_ALLOCATION (xsum (length
,
5067 (2 + TCHARS_PER_DCHAR
- 1)
5068 / TCHARS_PER_DCHAR
));
5069 /* Prepare checking whether snprintf returns the count
5071 *(TCHAR_T
*) (result
+ length
) = '\0';
5082 size_t maxlen
= allocated
- length
;
5083 /* SNPRINTF can fail if its second argument is
5085 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
5086 maxlen
= INT_MAX
/ TCHARS_PER_DCHAR
;
5087 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
5088 # define SNPRINTF_BUF(arg) \
5089 switch (prefix_count) \
5092 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
5097 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
5099 prefixes[0], arg, &count); \
5102 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
5104 prefixes[0], prefixes[1], arg, \
5111 # define SNPRINTF_BUF(arg) \
5112 switch (prefix_count) \
5115 count = sprintf (tmp, buf, arg); \
5118 count = sprintf (tmp, buf, prefixes[0], arg); \
5121 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
5134 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
5140 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
5146 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
5152 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
5158 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
5164 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
5170 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
5176 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
5180 case TYPE_LONGLONGINT
:
5182 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
5186 case TYPE_ULONGLONGINT
:
5188 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
5194 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
5198 case TYPE_LONGDOUBLE
:
5200 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
5206 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
5211 case TYPE_WIDE_CHAR
:
5213 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
5220 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
5225 case TYPE_WIDE_STRING
:
5227 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
5234 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
5243 /* Portability: Not all implementations of snprintf()
5244 are ISO C 99 compliant. Determine the number of
5245 bytes that snprintf() has produced or would have
5249 /* Verify that snprintf() has NUL-terminated its
5251 if ((unsigned int) count
< maxlen
5252 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
5254 /* Portability hack. */
5255 if (retcount
> count
)
5260 /* snprintf() doesn't understand the '%n'
5264 /* Don't use the '%n' directive; instead, look
5265 at the snprintf() return value. */
5271 /* Look at the snprintf() return value. */
5274 # if !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
5275 /* HP-UX 10.20 snprintf() is doubly deficient:
5276 It doesn't understand the '%n' directive,
5277 *and* it returns -1 (rather than the length
5278 that would have been required) when the
5279 buffer is too small.
5280 But a failure at this point can also come
5281 from other reasons than a too small buffer,
5282 such as an invalid wide string argument to
5283 the %ls directive, or possibly an invalid
5284 floating-point argument. */
5286 MAX_ROOM_NEEDED (&a
, dp
->arg_index
,
5287 dp
->conversion
, type
, flags
,
5290 precision
, pad_ourselves
);
5292 if (maxlen
< tmp_length
)
5294 /* Make more room. But try to do through
5295 this reallocation only once. */
5296 size_t bigger_need
=
5299 TCHARS_PER_DCHAR
- 1)
5300 / TCHARS_PER_DCHAR
);
5301 /* And always grow proportionally.
5302 (There may be several arguments, each
5303 needing a little more room than the
5305 size_t bigger_need2
=
5306 xsum (xtimes (allocated
, 2), 12);
5307 if (bigger_need
< bigger_need2
)
5308 bigger_need
= bigger_need2
;
5309 ENSURE_ALLOCATION (bigger_need
);
5320 /* Attempt to handle failure. */
5323 /* SNPRINTF or sprintf failed. Use the errno that it
5327 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5333 goto fail_with_errno
;
5337 /* Handle overflow of the allocated buffer.
5338 If such an overflow occurs, a C99 compliant snprintf()
5339 returns a count >= maxlen. However, a non-compliant
5340 snprintf() function returns only count = maxlen - 1. To
5341 cover both cases, test whether count >= maxlen - 1. */
5342 if ((unsigned int) count
+ 1 >= maxlen
)
5344 /* If maxlen already has attained its allowed maximum,
5345 allocating more memory will not increase maxlen.
5346 Instead of looping, bail out. */
5347 if (maxlen
== INT_MAX
/ TCHARS_PER_DCHAR
)
5351 /* Need at least (count + 1) * sizeof (TCHAR_T)
5352 bytes. (The +1 is for the trailing NUL.)
5353 But ask for (count + 2) * sizeof (TCHAR_T)
5354 bytes, so that in the next round, we likely get
5355 maxlen > (unsigned int) count + 1
5356 and so we don't get here again.
5357 And allocate proportionally, to avoid looping
5358 eternally if snprintf() reports a too small
5362 ((unsigned int) count
+ 2
5363 + TCHARS_PER_DCHAR
- 1)
5364 / TCHARS_PER_DCHAR
),
5365 xtimes (allocated
, 2));
5367 ENSURE_ALLOCATION (n
);
5373 #if NEED_PRINTF_UNBOUNDED_PRECISION
5376 /* Handle the precision. */
5379 (TCHAR_T
*) (result
+ length
);
5383 size_t prefix_count
;
5387 /* Put the additional zeroes after the sign. */
5389 && (*prec_ptr
== '-' || *prec_ptr
== '+'
5390 || *prec_ptr
== ' '))
5392 /* Put the additional zeroes after the 0x prefix if
5393 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
5395 && prec_ptr
[0] == '0'
5396 && (prec_ptr
[1] == 'x' || prec_ptr
[1] == 'X'))
5399 move
= count
- prefix_count
;
5400 if (precision
> move
)
5402 /* Insert zeroes. */
5403 size_t insert
= precision
- move
;
5409 (count
+ insert
+ TCHARS_PER_DCHAR
- 1)
5410 / TCHARS_PER_DCHAR
);
5411 length
+= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5412 ENSURE_ALLOCATION (n
);
5413 length
-= (count
+ TCHARS_PER_DCHAR
- 1) / TCHARS_PER_DCHAR
;
5414 prec_ptr
= (TCHAR_T
*) (result
+ length
);
5417 prec_end
= prec_ptr
+ count
;
5418 prec_ptr
+= prefix_count
;
5420 while (prec_end
> prec_ptr
)
5423 prec_end
[insert
] = prec_end
[0];
5429 while (prec_end
> prec_ptr
);
5437 if (count
>= tmp_length
)
5438 /* tmp_length was incorrectly calculated - fix the
5444 /* Convert from TCHAR_T[] to DCHAR_T[]. */
5445 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
5447 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5449 The result string is not certainly ASCII. */
5450 const TCHAR_T
*tmpsrc
;
5453 /* This code assumes that TCHAR_T is 'char'. */
5454 static_assert (sizeof (TCHAR_T
) == 1);
5456 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5461 DCHAR_CONV_FROM_ENCODING (locale_charset (),
5462 iconveh_question_mark
,
5467 goto fail_with_errno
;
5468 ENSURE_ALLOCATION_ELSE (xsum (length
, tmpdst_len
),
5469 { free (tmpdst
); goto out_of_memory
; });
5470 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
5476 /* The result string is ASCII.
5477 Simple 1:1 conversion. */
5479 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5480 no-op conversion, in-place on the array starting
5481 at (result + length). */
5482 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
5485 const TCHAR_T
*tmpsrc
;
5490 if (result
== resultbuf
)
5492 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5493 /* ENSURE_ALLOCATION will not move tmpsrc
5494 (because it's part of resultbuf). */
5495 ENSURE_ALLOCATION (xsum (length
, count
));
5499 /* ENSURE_ALLOCATION will move the array
5500 (because it uses realloc(). */
5501 ENSURE_ALLOCATION (xsum (length
, count
));
5502 tmpsrc
= (TCHAR_T
*) (result
+ length
);
5506 ENSURE_ALLOCATION (xsum (length
, count
));
5508 tmpdst
= result
+ length
;
5509 /* Copy backwards, because of overlapping. */
5512 for (n
= count
; n
> 0; n
--)
5513 *--tmpdst
= *--tmpsrc
;
5518 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
5519 /* Make room for the result. */
5520 if (count
> allocated
- length
)
5522 /* Need at least count elements. But allocate
5525 xmax (xsum (length
, count
), xtimes (allocated
, 2));
5527 ENSURE_ALLOCATION (n
);
5531 /* Here count <= allocated - length. */
5533 /* Perform padding. */
5534 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5535 if (pad_ourselves
&& has_width
)
5538 # if ENABLE_UNISTDIO
5539 /* Outside POSIX, it's preferable to compare the width
5540 against the number of _characters_ of the converted
5542 w
= DCHAR_MBSNLEN (result
+ length
, count
);
5544 /* The width is compared against the number of _bytes_
5545 of the converted value, says POSIX. */
5550 size_t pad
= width
- w
;
5552 /* Make room for the result. */
5553 if (xsum (count
, pad
) > allocated
- length
)
5555 /* Need at least count + pad elements. But
5556 allocate proportionally. */
5558 xmax (xsum3 (length
, count
, pad
),
5559 xtimes (allocated
, 2));
5563 ENSURE_ALLOCATION (n
);
5566 ENSURE_ALLOCATION (n
);
5569 /* Here count + pad <= allocated - length. */
5572 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
5573 DCHAR_T
* const rp
= result
+ length
;
5575 DCHAR_T
* const rp
= tmp
;
5577 DCHAR_T
*p
= rp
+ count
;
5578 DCHAR_T
*end
= p
+ pad
;
5580 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5581 if (dp
->conversion
== 'c'
5582 || dp
->conversion
== 's')
5583 /* No zero-padding for string directives. */
5588 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
5589 /* No zero-padding of "inf" and "nan". */
5590 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
5591 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
5594 /* The generated string now extends from rp to p,
5595 with the zero padding insertion point being at
5598 count
= count
+ pad
; /* = end - rp */
5600 if (flags
& FLAG_LEFT
)
5602 /* Pad with spaces on the right. */
5603 for (; pad
> 0; pad
--)
5606 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
5608 /* Pad with zeroes. */
5613 for (; pad
> 0; pad
--)
5618 /* Pad with spaces on the left. */
5623 for (; pad
> 0; pad
--)
5631 /* Here still count <= allocated - length. */
5633 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
5634 /* The snprintf() result did fit. */
5636 /* Append the sprintf() result. */
5637 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
5644 #if NEED_PRINTF_DIRECTIVE_F
5645 if (dp
->conversion
== 'F')
5647 /* Convert the %f result to upper case for %F. */
5648 DCHAR_T
*rp
= result
+ length
;
5650 for (rc
= count
; rc
> 0; rc
--, rp
++)
5651 if (*rp
>= 'a' && *rp
<= 'z')
5652 *rp
= *rp
- 'a' + 'A';
5660 #undef pad_ourselves
5661 #undef prec_ourselves
5666 /* Add the final NUL. */
5667 ENSURE_ALLOCATION (xsum (length
, 1));
5668 result
[length
] = '\0';
5670 if (result
!= resultbuf
&& length
+ 1 < allocated
)
5672 /* Shrink the allocated memory if possible. */
5675 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
5680 if (buf_malloced
!= NULL
)
5681 free (buf_malloced
);
5684 /* Note that we can produce a big string of a length > INT_MAX. POSIX
5685 says that snprintf() fails with errno = EOVERFLOW in this case, but
5686 that's only because snprintf() returns an 'int'. This function does
5687 not have this limitation. */
5693 goto fail_with_errno
;
5698 goto fail_with_errno
;
5700 #if ENABLE_UNISTDIO || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T)
5703 goto fail_with_errno
;
5707 if (result
!= resultbuf
)
5709 if (buf_malloced
!= NULL
)
5710 free (buf_malloced
);
5717 goto fail_1_with_errno
;
5721 goto fail_1_with_errno
;
5728 #undef MAX_ROOM_NEEDED
5729 #undef TCHARS_PER_DCHAR
5737 #undef DCHAR_IS_TCHAR