1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* This file can be parametrized with the following macros:
19 VASNPRINTF The name of the function being defined.
20 FCHAR_T The element type of the format string.
21 DCHAR_T The element type of the destination (result) string.
22 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
23 in the format string are ASCII. MUST be set if
24 FCHAR_T and DCHAR_T are not the same type.
25 DIRECTIVE Structure denoting a format directive.
27 DIRECTIVES Structure denoting the set of format directives of a
28 format string. Depends on FCHAR_T.
29 PRINTF_PARSE Function that parses a format string.
31 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
32 DCHAR_SET memset like function for DCHAR_T[] arrays.
33 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
34 SNPRINTF The system's snprintf (or similar) function.
35 This may be either snprintf or swprintf.
36 TCHAR_T The element type of the argument and result string
37 of the said SNPRINTF function. This may be either
38 char or wchar_t. The code exploits that
39 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
40 alignof (TCHAR_T) <= alignof (DCHAR_T).
41 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
42 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
43 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
44 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
45 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
47 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
48 This must come before <config.h> because <config.h> may include
49 <features.h>, and once <features.h> has been included, it's too late. */
51 # define _GNU_SOURCE 1
63 # if WIDE_CHAR_VERSION
64 # include "vasnwprintf.h"
66 # include "vasnprintf.h"
70 #include <locale.h> /* localeconv() */
71 #include <stdio.h> /* snprintf(), sprintf() */
72 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
73 #include <string.h> /* memcpy(), strlen() */
74 #include <errno.h> /* errno */
75 #include <limits.h> /* CHAR_BIT */
76 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
78 # include <langinfo.h>
81 # if WIDE_CHAR_VERSION
82 # include "wprintf-parse.h"
84 # include "printf-parse.h"
88 /* Checked size_t computations. */
91 #if NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
97 #if NEED_PRINTF_INFINITE_DOUBLE && !defined IN_LIBINTL
102 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !defined IN_LIBINTL
104 # include "isnanl-nolibm.h"
108 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
111 # include "printf-frexp.h"
112 # include "isnanl-nolibm.h"
113 # include "printf-frexpl.h"
117 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
119 # define EOVERFLOW E2BIG
124 # define local_wcslen wcslen
126 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
127 a dependency towards this library, here is a local substitute.
128 Define this substitute only once, even if this file is included
129 twice in the same compilation unit. */
130 # ifndef local_wcslen_defined
131 # define local_wcslen_defined 1
133 local_wcslen (const wchar_t *s
)
137 for (ptr
= s
; *ptr
!= (wchar_t) 0; ptr
++)
145 /* Default parameters. */
147 # if WIDE_CHAR_VERSION
148 # define VASNPRINTF vasnwprintf
149 # define FCHAR_T wchar_t
150 # define DCHAR_T wchar_t
151 # define TCHAR_T wchar_t
152 # define DCHAR_IS_TCHAR 1
153 # define DIRECTIVE wchar_t_directive
154 # define DIRECTIVES wchar_t_directives
155 # define PRINTF_PARSE wprintf_parse
156 # define DCHAR_CPY wmemcpy
158 # define VASNPRINTF vasnprintf
159 # define FCHAR_T char
160 # define DCHAR_T char
161 # define TCHAR_T char
162 # define DCHAR_IS_TCHAR 1
163 # define DIRECTIVE char_directive
164 # define DIRECTIVES char_directives
165 # define PRINTF_PARSE printf_parse
166 # define DCHAR_CPY memcpy
169 #if WIDE_CHAR_VERSION
170 /* TCHAR_T is wchar_t. */
171 # define USE_SNPRINTF 1
172 # if HAVE_DECL__SNWPRINTF
173 /* On Windows, the function swprintf() has a different signature than
174 on Unix; we use the _snwprintf() function instead. */
175 # define SNPRINTF _snwprintf
178 # define SNPRINTF swprintf
181 /* TCHAR_T is char. */
182 # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
183 But don't use it on BeOS, since BeOS snprintf produces no output if the
184 size argument is >= 0x3000000. */
185 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
186 # define USE_SNPRINTF 1
188 # define USE_SNPRINTF 0
190 # if HAVE_DECL__SNPRINTF
192 # define SNPRINTF _snprintf
195 # define SNPRINTF snprintf
196 /* Here we need to call the native snprintf, not rpl_snprintf. */
200 /* Here we need to call the native sprintf, not rpl_sprintf. */
203 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
204 /* Determine the decimal-point character according to the current locale. */
205 # ifndef decimal_point_char_defined
206 # define decimal_point_char_defined 1
208 decimal_point_char ()
211 /* Determine it in a multithread-safe way. We know nl_langinfo is
212 multithread-safe on glibc systems, but is not required to be multithread-
213 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
214 is rarely multithread-safe. */
215 # if HAVE_NL_LANGINFO && __GLIBC__
216 point
= nl_langinfo (RADIXCHAR
);
219 sprintf (pointbuf
, "%#.0f", 1.0);
220 point
= &pointbuf
[1];
222 point
= localeconv () -> decimal_point
;
224 /* The decimal point is always a single byte: either '.' or ','. */
225 return (point
[0] != '\0' ? point
[0] : '.');
230 #if NEED_PRINTF_INFINITE_DOUBLE && !defined IN_LIBINTL
232 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
234 is_infinite_or_zero (double x
)
236 return isnan (x
) || x
+ x
== x
;
241 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !defined IN_LIBINTL
243 /* Equivalent to !isfinite(x), but does not require libm. */
245 is_infinitel (long double x
)
247 return isnanl (x
) || (x
+ x
== x
&& x
!= 0.0L);
252 #if NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
254 /* Converting 'long double' to decimal without rare rounding bugs requires
255 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
256 (and slower) algorithms. */
258 typedef unsigned int mp_limb_t
;
259 # define GMP_LIMB_BITS 32
260 typedef int mp_limb_verify
[2 * (sizeof (mp_limb_t
) * CHAR_BIT
== GMP_LIMB_BITS
) - 1];
262 typedef unsigned long long mp_twolimb_t
;
263 # define GMP_TWOLIMB_BITS 64
264 typedef int mp_twolimb_verify
[2 * (sizeof (mp_twolimb_t
) * CHAR_BIT
== GMP_TWOLIMB_BITS
) - 1];
266 /* Representation of a bignum >= 0. */
270 mp_limb_t
*limbs
; /* Bits in little-endian order, allocated with malloc(). */
273 /* Compute the product of two bignums >= 0.
274 Return the allocated memory in case of success, NULL in case of memory
275 allocation failure. */
277 multiply (mpn_t src1
, mpn_t src2
, mpn_t
*dest
)
284 if (src1
.nlimbs
<= src2
.nlimbs
)
298 /* Now 0 <= len1 <= len2. */
301 /* src1 or src2 is zero. */
303 dest
->limbs
= (mp_limb_t
*) malloc (1);
307 /* Here 1 <= len1 <= len2. */
313 dp
= (mp_limb_t
*) malloc (dlen
* sizeof (mp_limb_t
));
316 for (k
= len2
; k
> 0; )
318 for (i
= 0; i
< len1
; i
++)
320 mp_limb_t digit1
= p1
[i
];
321 mp_twolimb_t carry
= 0;
322 for (j
= 0; j
< len2
; j
++)
324 mp_limb_t digit2
= p2
[j
];
325 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
327 dp
[i
+ j
] = (mp_limb_t
) carry
;
328 carry
= carry
>> GMP_LIMB_BITS
;
330 dp
[i
+ len2
] = (mp_limb_t
) carry
;
333 while (dlen
> 0 && dp
[dlen
- 1] == 0)
341 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
342 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
344 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
346 Return the allocated memory in case of success, NULL in case of memory
347 allocation failure. */
349 divide (mpn_t a
, mpn_t b
, mpn_t
*q
)
352 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
353 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
354 If m<n, then q:=0 and r:=a.
355 If m>=n=1, perform a single-precision division:
358 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
359 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
360 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
361 Normalise [q[m-1],...,q[0]], yields q.
362 If m>=n>1, perform a multiple-precision division:
363 We have a/b < beta^(m-n+1).
364 s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
365 Shift a and b left by s bits, copying them. r:=a.
366 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
367 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
369 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
370 In case of overflow (q* >= beta) set q* := beta-1.
371 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
372 and c3 := b[n-2] * q*.
373 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
374 occurred. Furthermore 0 <= c3 < beta^2.
375 If there was overflow and
376 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
377 the next test can be skipped.}
378 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
379 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
381 Put r := r - b * q* * beta^j. In detail:
382 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
383 hence: u:=0, for i:=0 to n-1 do
385 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
386 u:=u div beta (+ 1, if carry in subtraction)
388 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
390 the carry u does not overflow.}
391 If a negative carry occurs, put q* := q* - 1
392 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
394 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
395 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
397 The room for q[j] can be allocated at the memory location of r[n+j].
398 Finally, round-to-even:
399 Shift r left by 1 bit.
400 If r > b or if r = b and q[0] is odd, q := q+1.
402 const mp_limb_t
*a_ptr
= a
.limbs
;
403 size_t a_len
= a
.nlimbs
;
404 const mp_limb_t
*b_ptr
= b
.limbs
;
405 size_t b_len
= b
.nlimbs
;
407 mp_limb_t
*tmp_roomptr
= NULL
;
413 /* Allocate room for a_len+2 digits.
414 (Need a_len+1 digits for the real division and 1 more digit for the
415 final rounding of q.) */
416 roomptr
= (mp_limb_t
*) malloc ((a_len
+ 2) * sizeof (mp_limb_t
));
421 while (a_len
> 0 && a_ptr
[a_len
- 1] == 0)
428 /* Division by zero. */
430 if (b_ptr
[b_len
- 1] == 0)
436 /* Here m = a_len >= 0 and n = b_len > 0. */
440 /* m<n: trivial case. q=0, r := copy of a. */
443 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
444 q_ptr
= roomptr
+ a_len
;
449 /* n=1: single precision division.
450 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
454 mp_limb_t den
= b_ptr
[0];
455 mp_limb_t remainder
= 0;
456 const mp_limb_t
*sourceptr
= a_ptr
+ a_len
;
457 mp_limb_t
*destptr
= q_ptr
+ a_len
;
459 for (count
= a_len
; count
> 0; count
--)
462 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--sourceptr
;
463 *--destptr
= num
/ den
;
464 remainder
= num
% den
;
466 /* Normalise and store r. */
469 r_ptr
[0] = remainder
;
476 if (q_ptr
[q_len
- 1] == 0)
482 /* n>1: multiple precision division.
483 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
484 beta^(m-n-1) <= a/b < beta^(m-n+1). */
488 mp_limb_t msd
= b_ptr
[b_len
- 1]; /* = b[n-1], > 0 */
516 /* 0 <= s < GMP_LIMB_BITS.
517 Copy b, shifting it left by s bits. */
520 tmp_roomptr
= (mp_limb_t
*) malloc (b_len
* sizeof (mp_limb_t
));
521 if (tmp_roomptr
== NULL
)
527 const mp_limb_t
*sourceptr
= b_ptr
;
528 mp_limb_t
*destptr
= tmp_roomptr
;
529 mp_twolimb_t accu
= 0;
531 for (count
= b_len
; count
> 0; count
--)
533 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
534 *destptr
++ = (mp_limb_t
) accu
;
535 accu
= accu
>> GMP_LIMB_BITS
;
537 /* accu must be zero, since that was how s was determined. */
543 /* Copy a, shifting it left by s bits, yields r.
545 At the beginning: r = roomptr[0..a_len],
546 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
550 memcpy (r_ptr
, a_ptr
, a_len
* sizeof (mp_limb_t
));
555 const mp_limb_t
*sourceptr
= a_ptr
;
556 mp_limb_t
*destptr
= r_ptr
;
557 mp_twolimb_t accu
= 0;
559 for (count
= a_len
; count
> 0; count
--)
561 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
562 *destptr
++ = (mp_limb_t
) accu
;
563 accu
= accu
>> GMP_LIMB_BITS
;
565 *destptr
++ = (mp_limb_t
) accu
;
567 q_ptr
= roomptr
+ b_len
;
568 q_len
= a_len
- b_len
+ 1; /* q will have m-n+1 limbs */
570 size_t j
= a_len
- b_len
; /* m-n */
571 mp_limb_t b_msd
= b_ptr
[b_len
- 1]; /* b[n-1] */
572 mp_limb_t b_2msd
= b_ptr
[b_len
- 2]; /* b[n-2] */
573 mp_twolimb_t b_msdd
= /* b[n-1]*beta+b[n-2] */
574 ((mp_twolimb_t
) b_msd
<< GMP_LIMB_BITS
) | b_2msd
;
575 /* Division loop, traversed m-n+1 times.
576 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
581 if (r_ptr
[j
+ b_len
] < b_msd
) /* r[j+n] < b[n-1] ? */
583 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
585 ((mp_twolimb_t
) r_ptr
[j
+ b_len
] << GMP_LIMB_BITS
)
586 | r_ptr
[j
+ b_len
- 1];
587 q_star
= num
/ b_msd
;
592 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
593 q_star
= (mp_limb_t
)~(mp_limb_t
)0; /* q* = beta-1 */
594 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
595 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
596 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
598 If yes, jump directly to the subtraction loop.
599 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
600 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
601 if (r_ptr
[j
+ b_len
] > b_msd
602 || (c1
= r_ptr
[j
+ b_len
- 1] + b_msd
) < b_msd
)
603 /* r[j+n] >= b[n-1]+1 or
604 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
609 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
611 mp_twolimb_t c2
= /* c1*beta+r[j+n-2] */
612 ((mp_twolimb_t
) c1
<< GMP_LIMB_BITS
) | r_ptr
[j
+ b_len
- 2];
613 mp_twolimb_t c3
= /* b[n-2] * q* */
614 (mp_twolimb_t
) b_2msd
* (mp_twolimb_t
) q_star
;
615 /* While c2 < c3, increase c2 and decrease c3.
616 Consider c3-c2. While it is > 0, decrease it by
617 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
618 this can happen only twice. */
621 q_star
= q_star
- 1; /* q* := q* - 1 */
622 if (c3
- c2
> b_msdd
)
623 q_star
= q_star
- 1; /* q* := q* - 1 */
629 /* Subtract r := r - b * q* * beta^j. */
632 const mp_limb_t
*sourceptr
= b_ptr
;
633 mp_limb_t
*destptr
= r_ptr
+ j
;
634 mp_twolimb_t carry
= 0;
636 for (count
= b_len
; count
> 0; count
--)
638 /* Here 0 <= carry <= q*. */
641 + (mp_twolimb_t
) q_star
* (mp_twolimb_t
) *sourceptr
++
642 + (mp_limb_t
) ~(*destptr
);
643 /* Here 0 <= carry <= beta*q* + beta-1. */
644 *destptr
++ = ~(mp_limb_t
) carry
;
645 carry
= carry
>> GMP_LIMB_BITS
; /* <= q* */
647 cr
= (mp_limb_t
) carry
;
649 /* Subtract cr from r_ptr[j + b_len], then forget about
651 if (cr
> r_ptr
[j
+ b_len
])
653 /* Subtraction gave a carry. */
654 q_star
= q_star
- 1; /* q* := q* - 1 */
657 const mp_limb_t
*sourceptr
= b_ptr
;
658 mp_limb_t
*destptr
= r_ptr
+ j
;
661 for (count
= b_len
; count
> 0; count
--)
663 mp_limb_t source1
= *sourceptr
++;
664 mp_limb_t source2
= *destptr
;
665 *destptr
++ = source1
+ source2
+ carry
;
668 ? source1
>= (mp_limb_t
) ~source2
669 : source1
> (mp_limb_t
) ~source2
);
672 /* Forget about the carry and about r[j+n]. */
675 /* q* is determined. Store it as q[j]. */
684 if (q_ptr
[q_len
- 1] == 0)
686 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
687 b is shifted left by s bits. */
688 /* Shift r right by s bits. */
691 mp_limb_t ptr
= r_ptr
+ r_len
;
692 mp_twolimb_t accu
= 0;
694 for (count
= r_len
; count
> 0; count
--)
696 accu
= (mp_twolimb_t
) (mp_limb_t
) accu
<< GMP_LIMB_BITS
;
697 accu
+= (mp_twolimb_t
) *--ptr
<< (GMP_LIMB_BITS
- s
);
698 *ptr
= (mp_limb_t
) (accu
>> GMP_LIMB_BITS
);
703 while (r_len
> 0 && r_ptr
[r_len
- 1] == 0)
706 /* Compare r << 1 with b. */
714 (i
<= r_len
&& i
> 0 ? r_ptr
[i
- 1] >> (GMP_LIMB_BITS
- 1) : 0)
715 | (i
< r_len
? r_ptr
[i
] << 1 : 0);
716 mp_limb_t b_i
= (i
< b_len
? b_ptr
[i
] : 0);
726 if (q_len
> 0 && ((q_ptr
[0] & 1) != 0))
731 for (i
= 0; i
< q_len
; i
++)
732 if (++(q_ptr
[i
]) != 0)
737 if (tmp_roomptr
!= NULL
)
744 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
746 Destroys the contents of a.
747 Return the allocated memory - containing the decimal digits in low-to-high
748 order, terminated with a NUL character - in case of success, NULL in case
749 of memory allocation failure. */
751 convert_to_decimal (mpn_t a
, size_t extra_zeroes
)
753 mp_limb_t
*a_ptr
= a
.limbs
;
754 size_t a_len
= a
.nlimbs
;
755 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
756 size_t c_len
= 9 * ((size_t)(a_len
* (GMP_LIMB_BITS
* 0.03345f
)) + 1);
757 char *c_ptr
= (char *) malloc (xsum (c_len
, extra_zeroes
));
761 for (; extra_zeroes
> 0; extra_zeroes
--)
765 /* Divide a by 10^9, in-place. */
766 mp_limb_t remainder
= 0;
767 mp_limb_t
*ptr
= a_ptr
+ a_len
;
769 for (count
= a_len
; count
> 0; count
--)
772 ((mp_twolimb_t
) remainder
<< GMP_LIMB_BITS
) | *--ptr
;
773 *ptr
= num
/ 1000000000;
774 remainder
= num
% 1000000000;
776 /* Store the remainder as 9 decimal digits. */
777 for (count
= 9; count
> 0; count
--)
779 *d_ptr
++ = '0' + (remainder
% 10);
780 remainder
= remainder
/ 10;
783 if (a_ptr
[a_len
- 1] == 0)
786 /* Remove leading zeroes. */
787 while (d_ptr
> c_ptr
&& d_ptr
[-1] == '0')
789 /* But keep at least one zero. */
792 /* Terminate the string. */
798 /* Assuming x is finite and >= 0:
799 write x as x = 2^e * m, where m is a bignum.
800 Return the allocated memory in case of success, NULL in case of memory
801 allocation failure. */
803 decode_long_double (long double x
, int *ep
, mpn_t
*mp
)
810 /* Allocate memory for result. */
811 m
.nlimbs
= (LDBL_MANT_BIT
+ GMP_LIMB_BITS
- 1) / GMP_LIMB_BITS
;
812 m
.limbs
= (mp_limb_t
*) malloc (m
.nlimbs
* sizeof (mp_limb_t
));
815 /* Split into exponential part and mantissa. */
816 y
= frexpl (x
, &exp
);
817 if (!(y
>= 0.0L && y
< 1.0L))
819 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
820 latter is an integer. */
821 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
822 I'm not sure whether it's safe to cast a 'long double' value between
823 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
824 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
826 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
827 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
830 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% (GMP_LIMB_BITS
/ 2));
833 if (!(y
>= 0.0L && y
< 1.0L))
835 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
838 if (!(y
>= 0.0L && y
< 1.0L))
840 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
845 y
*= (mp_limb_t
) 1 << (LDBL_MANT_BIT
% GMP_LIMB_BITS
);
848 if (!(y
>= 0.0L && y
< 1.0L))
850 m
.limbs
[LDBL_MANT_BIT
/ GMP_LIMB_BITS
] = d
;
854 for (i
= LDBL_MANT_BIT
/ GMP_LIMB_BITS
; i
> 0; )
857 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
860 if (!(y
>= 0.0L && y
< 1.0L))
862 y
*= (mp_limb_t
) 1 << (GMP_LIMB_BITS
/ 2);
865 if (!(y
>= 0.0L && y
< 1.0L))
867 m
.limbs
[--i
] = (hi
<< (GMP_LIMB_BITS
/ 2)) | lo
;
872 while (m
.nlimbs
> 0 && m
.limbs
[m
.nlimbs
- 1] == 0)
875 *ep
= exp
- LDBL_MANT_BIT
;
879 /* Assuming x is finite and >= 0, and n is an integer:
880 Returns the decimal representation of round (x * 10^n).
881 Return the allocated memory - containing the decimal digits in low-to-high
882 order, terminated with a NUL character - in case of success, NULL in case
883 of memory allocation failure. */
885 scale10_round_decimal_long_double (long double x
, int n
)
889 void *memory
= decode_long_double (x
, &e
, &m
);
896 unsigned int s_limbs
;
905 /* x = 2^e * m, hence
906 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
907 = round (2^s * 5^n * m). */
910 /* Factor out a common power of 10 if possible. */
913 extra_zeroes
= (s
< n
? s
: n
);
917 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
918 Before converting to decimal, we need to compute
919 z = round (2^s * 5^n * m). */
920 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
921 sign. 2.322 is slightly larger than log(5)/log(2). */
922 abs_n
= (n
>= 0 ? n
: -n
);
923 abs_s
= (s
>= 0 ? s
: -s
);
924 pow5_ptr
= (mp_limb_t
*) malloc (((int)(abs_n
* (2.322f
/ GMP_LIMB_BITS
)) + 1
925 + abs_s
/ GMP_LIMB_BITS
+ 1)
926 * sizeof (mp_limb_t
));
927 if (pow5_ptr
== NULL
)
932 /* Initialize with 1. */
935 /* Multiply with 5^|n|. */
938 static mp_limb_t
const small_pow5
[13 + 1] =
940 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
941 48828125, 244140625, 1220703125
944 for (n13
= 0; n13
<= abs_n
; n13
+= 13)
946 mp_limb_t digit1
= small_pow5
[n13
+ 13 <= abs_n
? 13 : abs_n
- n13
];
948 mp_twolimb_t carry
= 0;
949 for (j
= 0; j
< pow5_len
; j
++)
951 mp_limb_t digit2
= pow5_ptr
[j
];
952 carry
+= (mp_twolimb_t
) digit1
* (mp_twolimb_t
) digit2
;
953 pow5_ptr
[j
] = (mp_limb_t
) carry
;
954 carry
= carry
>> GMP_LIMB_BITS
;
957 pow5_ptr
[pow5_len
++] = (mp_limb_t
) carry
;
960 s_limbs
= abs_s
/ GMP_LIMB_BITS
;
961 s_bits
= abs_s
% GMP_LIMB_BITS
;
962 if (n
>= 0 ? s
>= 0 : s
<= 0)
964 /* Multiply with 2^|s|. */
967 mp_limb_t
*ptr
= pow5_ptr
;
968 mp_twolimb_t accu
= 0;
970 for (count
= pow5_len
; count
> 0; count
--)
972 accu
+= (mp_twolimb_t
) *ptr
<< s_bits
;
973 *ptr
++ = (mp_limb_t
) accu
;
974 accu
= accu
>> GMP_LIMB_BITS
;
978 *ptr
= (mp_limb_t
) accu
;
985 for (count
= pow5_len
; count
> 0;)
988 pow5_ptr
[s_limbs
+ count
] = pow5_ptr
[count
];
990 for (count
= s_limbs
; count
> 0;)
997 pow5
.limbs
= pow5_ptr
;
998 pow5
.nlimbs
= pow5_len
;
1001 /* Multiply m with pow5. No division needed. */
1002 z_memory
= multiply (m
, pow5
, &z
);
1006 /* Divide m by pow5 and round. */
1007 z_memory
= divide (m
, pow5
, &z
);
1012 pow5
.limbs
= pow5_ptr
;
1013 pow5
.nlimbs
= pow5_len
;
1017 Multiply m with pow5, then divide by 2^|s|. */
1021 tmp_memory
= multiply (m
, pow5
, &numerator
);
1022 if (tmp_memory
== NULL
)
1028 /* Construct 2^|s|. */
1030 mp_limb_t
*ptr
= pow5_ptr
+ pow5_len
;
1032 for (i
= 0; i
< s_limbs
; i
++)
1034 ptr
[s_limbs
] = (mp_limb_t
) 1 << s_bits
;
1035 denominator
.limbs
= ptr
;
1036 denominator
.nlimbs
= s_limbs
+ 1;
1038 z_memory
= divide (numerator
, denominator
, &z
);
1044 Multiply m with 2^s, then divide by pow5. */
1047 num_ptr
= (mp_limb_t
*) malloc ((m
.nlimbs
+ s_limbs
+ 1)
1048 * sizeof (mp_limb_t
));
1049 if (num_ptr
== NULL
)
1056 mp_limb_t
*destptr
= num_ptr
;
1059 for (i
= 0; i
< s_limbs
; i
++)
1064 const mp_limb_t
*sourceptr
= m
.limbs
;
1065 mp_twolimb_t accu
= 0;
1067 for (count
= m
.nlimbs
; count
> 0; count
--)
1069 accu
+= (mp_twolimb_t
) *sourceptr
++ << s
;
1070 *destptr
++ = (mp_limb_t
) accu
;
1071 accu
= accu
>> GMP_LIMB_BITS
;
1074 *destptr
++ = (mp_limb_t
) accu
;
1078 const mp_limb_t
*sourceptr
= m
.limbs
;
1080 for (count
= m
.nlimbs
; count
> 0; count
--)
1081 *destptr
++ = *sourceptr
++;
1083 numerator
.limbs
= num_ptr
;
1084 numerator
.nlimbs
= destptr
- num_ptr
;
1086 z_memory
= divide (numerator
, pow5
, &z
);
1093 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1095 if (z_memory
== NULL
)
1097 digits
= convert_to_decimal (z
, extra_zeroes
);
1102 /* Assuming x is finite and > 0:
1103 Return an approximation for n with 10^n <= x < 10^(n+1).
1104 The approximation is usually the right n, but may be off by 1 sometimes. */
1106 floorlog10l (long double x
)
1113 /* Split into exponential part and mantissa. */
1114 y
= frexpl (x
, &exp
);
1115 if (!(y
>= 0.0L && y
< 1.0L))
1121 while (y
< (1.0L / (1 << (GMP_LIMB_BITS
/ 2)) / (1 << (GMP_LIMB_BITS
/ 2))))
1123 y
*= 1.0L * (1 << (GMP_LIMB_BITS
/ 2)) * (1 << (GMP_LIMB_BITS
/ 2));
1124 exp
-= GMP_LIMB_BITS
;
1126 if (y
< (1.0L / (1 << 16)))
1128 y
*= 1.0L * (1 << 16);
1131 if (y
< (1.0L / (1 << 8)))
1133 y
*= 1.0L * (1 << 8);
1136 if (y
< (1.0L / (1 << 4)))
1138 y
*= 1.0L * (1 << 4);
1141 if (y
< (1.0L / (1 << 2)))
1143 y
*= 1.0L * (1 << 2);
1146 if (y
< (1.0L / (1 << 1)))
1148 y
*= 1.0L * (1 << 1);
1152 if (!(y
>= 0.5L && y
< 1.0L))
1154 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1157 if (z
< 0.70710678118654752444)
1159 z
*= 1.4142135623730950488;
1162 if (z
< 0.8408964152537145431)
1164 z
*= 1.1892071150027210667;
1167 if (z
< 0.91700404320467123175)
1169 z
*= 1.0905077326652576592;
1172 if (z
< 0.9576032806985736469)
1174 z
*= 1.0442737824274138403;
1177 /* Now 0.95 <= z <= 1.01. */
1179 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1180 Four terms are enough to get an approximation with error < 10^-7. */
1181 l
-= z
* (1.0 + z
* (0.5 + z
* ((1.0 / 3) + z
* 0.25)));
1182 /* Finally multiply with log(2)/log(10), yields an approximation for
1184 l
*= 0.30102999566398119523;
1185 /* Round down to the next integer. */
1186 return (int) l
+ (l
< 0 ? -1 : 0);
1192 VASNPRINTF (DCHAR_T
*resultbuf
, size_t *lengthp
,
1193 const FCHAR_T
*format
, va_list args
)
1198 if (PRINTF_PARSE (format
, &d
, &a
) < 0)
1209 if (PRINTF_FETCHARGS (args
, &a
) < 0)
1217 size_t buf_neededlength
;
1219 TCHAR_T
*buf_malloced
;
1223 /* Output string accumulator. */
1228 /* Allocate a small buffer that will hold a directive passed to
1229 sprintf or snprintf. */
1231 xsum4 (7, d
.max_width_length
, d
.max_precision_length
, 6);
1233 if (buf_neededlength
< 4000 / sizeof (TCHAR_T
))
1235 buf
= (TCHAR_T
*) alloca (buf_neededlength
* sizeof (TCHAR_T
));
1236 buf_malloced
= NULL
;
1241 size_t buf_memsize
= xtimes (buf_neededlength
, sizeof (TCHAR_T
));
1242 if (size_overflow_p (buf_memsize
))
1243 goto out_of_memory_1
;
1244 buf
= (TCHAR_T
*) malloc (buf_memsize
);
1246 goto out_of_memory_1
;
1250 if (resultbuf
!= NULL
)
1253 allocated
= *lengthp
;
1262 result is either == resultbuf or == NULL or malloc-allocated.
1263 If length > 0, then result != NULL. */
1265 /* Ensures that allocated >= needed. Aborts through a jump to
1266 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1267 #define ENSURE_ALLOCATION(needed) \
1268 if ((needed) > allocated) \
1270 size_t memory_size; \
1273 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1274 if ((needed) > allocated) \
1275 allocated = (needed); \
1276 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1277 if (size_overflow_p (memory_size)) \
1278 goto out_of_memory; \
1279 if (result == resultbuf || result == NULL) \
1280 memory = (DCHAR_T *) malloc (memory_size); \
1282 memory = (DCHAR_T *) realloc (result, memory_size); \
1283 if (memory == NULL) \
1284 goto out_of_memory; \
1285 if (result == resultbuf && length > 0) \
1286 DCHAR_CPY (memory, result, length); \
1290 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
1292 if (cp
!= dp
->dir_start
)
1294 size_t n
= dp
->dir_start
- cp
;
1295 size_t augmented_length
= xsum (length
, n
);
1297 ENSURE_ALLOCATION (augmented_length
);
1298 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1299 need that the format string contains only ASCII characters
1300 if FCHAR_T and DCHAR_T are not the same type. */
1301 if (sizeof (FCHAR_T
) == sizeof (DCHAR_T
))
1303 DCHAR_CPY (result
+ length
, (const DCHAR_T
*) cp
, n
);
1304 length
= augmented_length
;
1309 result
[length
++] = (unsigned char) *cp
++;
1316 /* Execute a single directive. */
1317 if (dp
->conversion
== '%')
1319 size_t augmented_length
;
1321 if (!(dp
->arg_index
== ARG_NONE
))
1323 augmented_length
= xsum (length
, 1);
1324 ENSURE_ALLOCATION (augmented_length
);
1325 result
[length
] = '%';
1326 length
= augmented_length
;
1330 if (!(dp
->arg_index
!= ARG_NONE
))
1333 if (dp
->conversion
== 'n')
1335 switch (a
.arg
[dp
->arg_index
].type
)
1337 case TYPE_COUNT_SCHAR_POINTER
:
1338 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
1340 case TYPE_COUNT_SHORT_POINTER
:
1341 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
1343 case TYPE_COUNT_INT_POINTER
:
1344 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
1346 case TYPE_COUNT_LONGINT_POINTER
:
1347 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
1349 #if HAVE_LONG_LONG_INT
1350 case TYPE_COUNT_LONGLONGINT_POINTER
:
1351 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
1359 /* The unistdio extensions. */
1360 else if (dp
->conversion
== 'U')
1362 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1363 int flags
= dp
->flags
;
1371 if (dp
->width_start
!= dp
->width_end
)
1373 if (dp
->width_arg_index
!= ARG_NONE
)
1377 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1379 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1382 /* "A negative field width is taken as a '-' flag
1383 followed by a positive field width." */
1385 width
= (unsigned int) (-arg
);
1392 const FCHAR_T
*digitp
= dp
->width_start
;
1395 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1396 while (digitp
!= dp
->width_end
);
1403 if (dp
->precision_start
!= dp
->precision_end
)
1405 if (dp
->precision_arg_index
!= ARG_NONE
)
1409 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
1411 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
1412 /* "A negative precision is taken as if the precision
1422 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
1425 while (digitp
!= dp
->precision_end
)
1426 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
1433 case TYPE_U8_STRING
:
1435 const uint8_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u8_string
;
1436 const uint8_t *arg_end
;
1441 /* Use only PRECISION characters, from the left. */
1444 for (; precision
> 0; precision
--)
1446 int count
= u8_strmblen (arg_end
);
1451 if (!(result
== resultbuf
|| result
== NULL
))
1453 if (buf_malloced
!= NULL
)
1454 free (buf_malloced
);
1465 /* Use the entire string, and count the number of
1471 int count
= u8_strmblen (arg_end
);
1476 if (!(result
== resultbuf
|| result
== NULL
))
1478 if (buf_malloced
!= NULL
)
1479 free (buf_malloced
);
1490 /* Use the entire string. */
1491 arg_end
= arg
+ u8_strlen (arg
);
1492 /* The number of characters doesn't matter. */
1496 if (has_width
&& width
> characters
1497 && !(dp
->flags
& FLAG_LEFT
))
1499 size_t n
= width
- characters
;
1500 ENSURE_ALLOCATION (xsum (length
, n
));
1501 DCHAR_SET (result
+ length
, ' ', n
);
1505 # if DCHAR_IS_UINT8_T
1507 size_t n
= arg_end
- arg
;
1508 ENSURE_ALLOCATION (xsum (length
, n
));
1509 DCHAR_CPY (result
+ length
, arg
, n
);
1514 DCHAR_T
*converted
= result
+ length
;
1515 size_t converted_len
= allocated
- length
;
1517 /* Convert from UTF-8 to locale encoding. */
1518 if (u8_conv_to_encoding (locale_charset (),
1519 iconveh_question_mark
,
1520 arg
, arg_end
- arg
, NULL
,
1521 &converted
, &converted_len
)
1524 /* Convert from UTF-8 to UTF-16/UTF-32. */
1526 U8_TO_DCHAR (arg
, arg_end
- arg
,
1527 converted
, &converted_len
);
1528 if (converted
== NULL
)
1531 int saved_errno
= errno
;
1532 if (!(result
== resultbuf
|| result
== NULL
))
1534 if (buf_malloced
!= NULL
)
1535 free (buf_malloced
);
1537 errno
= saved_errno
;
1540 if (converted
!= result
+ length
)
1542 ENSURE_ALLOCATION (xsum (length
, converted_len
));
1543 DCHAR_CPY (result
+ length
, converted
, converted_len
);
1546 length
+= converted_len
;
1550 if (has_width
&& width
> characters
1551 && (dp
->flags
& FLAG_LEFT
))
1553 size_t n
= width
- characters
;
1554 ENSURE_ALLOCATION (xsum (length
, n
));
1555 DCHAR_SET (result
+ length
, ' ', n
);
1561 case TYPE_U16_STRING
:
1563 const uint16_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u16_string
;
1564 const uint16_t *arg_end
;
1569 /* Use only PRECISION characters, from the left. */
1572 for (; precision
> 0; precision
--)
1574 int count
= u16_strmblen (arg_end
);
1579 if (!(result
== resultbuf
|| result
== NULL
))
1581 if (buf_malloced
!= NULL
)
1582 free (buf_malloced
);
1593 /* Use the entire string, and count the number of
1599 int count
= u16_strmblen (arg_end
);
1604 if (!(result
== resultbuf
|| result
== NULL
))
1606 if (buf_malloced
!= NULL
)
1607 free (buf_malloced
);
1618 /* Use the entire string. */
1619 arg_end
= arg
+ u16_strlen (arg
);
1620 /* The number of characters doesn't matter. */
1624 if (has_width
&& width
> characters
1625 && !(dp
->flags
& FLAG_LEFT
))
1627 size_t n
= width
- characters
;
1628 ENSURE_ALLOCATION (xsum (length
, n
));
1629 DCHAR_SET (result
+ length
, ' ', n
);
1633 # if DCHAR_IS_UINT16_T
1635 size_t n
= arg_end
- arg
;
1636 ENSURE_ALLOCATION (xsum (length
, n
));
1637 DCHAR_CPY (result
+ length
, arg
, n
);
1642 DCHAR_T
*converted
= result
+ length
;
1643 size_t converted_len
= allocated
- length
;
1645 /* Convert from UTF-16 to locale encoding. */
1646 if (u16_conv_to_encoding (locale_charset (),
1647 iconveh_question_mark
,
1648 arg
, arg_end
- arg
, NULL
,
1649 &converted
, &converted_len
)
1652 /* Convert from UTF-16 to UTF-8/UTF-32. */
1654 U16_TO_DCHAR (arg
, arg_end
- arg
,
1655 converted
, &converted_len
);
1656 if (converted
== NULL
)
1659 int saved_errno
= errno
;
1660 if (!(result
== resultbuf
|| result
== NULL
))
1662 if (buf_malloced
!= NULL
)
1663 free (buf_malloced
);
1665 errno
= saved_errno
;
1668 if (converted
!= result
+ length
)
1670 ENSURE_ALLOCATION (xsum (length
, converted_len
));
1671 DCHAR_CPY (result
+ length
, converted
, converted_len
);
1674 length
+= converted_len
;
1678 if (has_width
&& width
> characters
1679 && (dp
->flags
& FLAG_LEFT
))
1681 size_t n
= width
- characters
;
1682 ENSURE_ALLOCATION (xsum (length
, n
));
1683 DCHAR_SET (result
+ length
, ' ', n
);
1689 case TYPE_U32_STRING
:
1691 const uint32_t *arg
= a
.arg
[dp
->arg_index
].a
.a_u32_string
;
1692 const uint32_t *arg_end
;
1697 /* Use only PRECISION characters, from the left. */
1700 for (; precision
> 0; precision
--)
1702 int count
= u32_strmblen (arg_end
);
1707 if (!(result
== resultbuf
|| result
== NULL
))
1709 if (buf_malloced
!= NULL
)
1710 free (buf_malloced
);
1721 /* Use the entire string, and count the number of
1727 int count
= u32_strmblen (arg_end
);
1732 if (!(result
== resultbuf
|| result
== NULL
))
1734 if (buf_malloced
!= NULL
)
1735 free (buf_malloced
);
1746 /* Use the entire string. */
1747 arg_end
= arg
+ u32_strlen (arg
);
1748 /* The number of characters doesn't matter. */
1752 if (has_width
&& width
> characters
1753 && !(dp
->flags
& FLAG_LEFT
))
1755 size_t n
= width
- characters
;
1756 ENSURE_ALLOCATION (xsum (length
, n
));
1757 DCHAR_SET (result
+ length
, ' ', n
);
1761 # if DCHAR_IS_UINT32_T
1763 size_t n
= arg_end
- arg
;
1764 ENSURE_ALLOCATION (xsum (length
, n
));
1765 DCHAR_CPY (result
+ length
, arg
, n
);
1770 DCHAR_T
*converted
= result
+ length
;
1771 size_t converted_len
= allocated
- length
;
1773 /* Convert from UTF-32 to locale encoding. */
1774 if (u32_conv_to_encoding (locale_charset (),
1775 iconveh_question_mark
,
1776 arg
, arg_end
- arg
, NULL
,
1777 &converted
, &converted_len
)
1780 /* Convert from UTF-32 to UTF-8/UTF-16. */
1782 U32_TO_DCHAR (arg
, arg_end
- arg
,
1783 converted
, &converted_len
);
1784 if (converted
== NULL
)
1787 int saved_errno
= errno
;
1788 if (!(result
== resultbuf
|| result
== NULL
))
1790 if (buf_malloced
!= NULL
)
1791 free (buf_malloced
);
1793 errno
= saved_errno
;
1796 if (converted
!= result
+ length
)
1798 ENSURE_ALLOCATION (xsum (length
, converted_len
));
1799 DCHAR_CPY (result
+ length
, converted
, converted_len
);
1802 length
+= converted_len
;
1806 if (has_width
&& width
> characters
1807 && (dp
->flags
& FLAG_LEFT
))
1809 size_t n
= width
- characters
;
1810 ENSURE_ALLOCATION (xsum (length
, n
));
1811 DCHAR_SET (result
+ length
, ' ', n
);
1822 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
1823 else if (dp
->conversion
== 'a' || dp
->conversion
== 'A')
1825 arg_type type
= a
.arg
[dp
->arg_index
].type
;
1826 int flags
= dp
->flags
;
1832 DCHAR_T tmpbuf
[700];
1839 if (dp
->width_start
!= dp
->width_end
)
1841 if (dp
->width_arg_index
!= ARG_NONE
)
1845 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
1847 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
1850 /* "A negative field width is taken as a '-' flag
1851 followed by a positive field width." */
1853 width
= (unsigned int) (-arg
);
1860 const FCHAR_T
*digitp
= dp
->width_start
;
1863 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
1864 while (digitp
!= dp
->width_end
);
1871 if (dp
->precision_start
!= dp
->precision_end
)
1873 if (dp
->precision_arg_index
!= ARG_NONE
)
1877 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
1879 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
1880 /* "A negative precision is taken as if the precision
1890 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
1893 while (digitp
!= dp
->precision_end
)
1894 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
1899 /* Allocate a temporary buffer of sufficient size. */
1900 if (type
== TYPE_LONGDOUBLE
)
1902 (unsigned int) ((LDBL_DIG
+ 1)
1903 * 0.831 /* decimal -> hexadecimal */
1905 + 1; /* turn floor into ceil */
1908 (unsigned int) ((DBL_DIG
+ 1)
1909 * 0.831 /* decimal -> hexadecimal */
1911 + 1; /* turn floor into ceil */
1912 if (tmp_length
< precision
)
1913 tmp_length
= precision
;
1914 /* Account for sign, decimal point etc. */
1915 tmp_length
= xsum (tmp_length
, 12);
1917 if (tmp_length
< width
)
1920 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
1922 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
1926 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
1928 if (size_overflow_p (tmp_memsize
))
1929 /* Overflow, would lead to out of memory. */
1931 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
1933 /* Out of memory. */
1939 if (type
== TYPE_LONGDOUBLE
)
1941 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
1945 if (dp
->conversion
== 'A')
1947 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
1951 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
1957 DECL_LONG_DOUBLE_ROUNDING
1959 BEGIN_LONG_DOUBLE_ROUNDING ();
1961 if (signbit (arg
)) /* arg < 0.0L or negative zero */
1969 else if (flags
& FLAG_SHOWSIGN
)
1971 else if (flags
& FLAG_SPACE
)
1974 if (arg
> 0.0L && arg
+ arg
== arg
)
1976 if (dp
->conversion
== 'A')
1978 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
1982 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
1988 long double mantissa
;
1991 mantissa
= printf_frexpl (arg
, &exponent
);
1999 && precision
< (unsigned int) ((LDBL_DIG
+ 1) * 0.831) + 1)
2001 /* Round the mantissa. */
2002 long double tail
= mantissa
;
2005 for (q
= precision
; ; q
--)
2007 int digit
= (int) tail
;
2011 if (digit
& 1 ? tail
>= 0.5L : tail
> 0.5L)
2020 for (q
= precision
; q
> 0; q
--)
2026 *p
++ = dp
->conversion
- 'A' + 'X';
2031 digit
= (int) mantissa
;
2034 if ((flags
& FLAG_ALT
)
2035 || mantissa
> 0.0L || precision
> 0)
2037 *p
++ = decimal_point_char ();
2038 /* This loop terminates because we assume
2039 that FLT_RADIX is a power of 2. */
2040 while (mantissa
> 0.0L)
2043 digit
= (int) mantissa
;
2048 : dp
->conversion
- 10);
2052 while (precision
> 0)
2059 *p
++ = dp
->conversion
- 'A' + 'P';
2060 # if WIDE_CHAR_VERSION
2062 static const wchar_t decimal_format
[] =
2063 { '%', '+', 'd', '\0' };
2064 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2069 if (sizeof (DCHAR_T
) == 1)
2071 sprintf ((char *) p
, "%+d", exponent
);
2079 sprintf (expbuf
, "%+d", exponent
);
2080 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2086 END_LONG_DOUBLE_ROUNDING ();
2091 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
2095 if (dp
->conversion
== 'A')
2097 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2101 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2108 if (signbit (arg
)) /* arg < 0.0 or negative zero */
2116 else if (flags
& FLAG_SHOWSIGN
)
2118 else if (flags
& FLAG_SPACE
)
2121 if (arg
> 0.0 && arg
+ arg
== arg
)
2123 if (dp
->conversion
== 'A')
2125 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2129 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2138 mantissa
= printf_frexp (arg
, &exponent
);
2146 && precision
< (unsigned int) ((DBL_DIG
+ 1) * 0.831) + 1)
2148 /* Round the mantissa. */
2149 double tail
= mantissa
;
2152 for (q
= precision
; ; q
--)
2154 int digit
= (int) tail
;
2158 if (digit
& 1 ? tail
>= 0.5 : tail
> 0.5)
2167 for (q
= precision
; q
> 0; q
--)
2173 *p
++ = dp
->conversion
- 'A' + 'X';
2178 digit
= (int) mantissa
;
2181 if ((flags
& FLAG_ALT
)
2182 || mantissa
> 0.0 || precision
> 0)
2184 *p
++ = decimal_point_char ();
2185 /* This loop terminates because we assume
2186 that FLT_RADIX is a power of 2. */
2187 while (mantissa
> 0.0)
2190 digit
= (int) mantissa
;
2195 : dp
->conversion
- 10);
2199 while (precision
> 0)
2206 *p
++ = dp
->conversion
- 'A' + 'P';
2207 # if WIDE_CHAR_VERSION
2209 static const wchar_t decimal_format
[] =
2210 { '%', '+', 'd', '\0' };
2211 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2216 if (sizeof (DCHAR_T
) == 1)
2218 sprintf ((char *) p
, "%+d", exponent
);
2226 sprintf (expbuf
, "%+d", exponent
);
2227 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2234 /* The generated string now extends from tmp to p, with the
2235 zero padding insertion point being at pad_ptr. */
2236 if (has_width
&& p
- tmp
< width
)
2238 size_t pad
= width
- (p
- tmp
);
2239 DCHAR_T
*end
= p
+ pad
;
2241 if (flags
& FLAG_LEFT
)
2243 /* Pad with spaces on the right. */
2244 for (; pad
> 0; pad
--)
2247 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
2249 /* Pad with zeroes. */
2254 for (; pad
> 0; pad
--)
2259 /* Pad with spaces on the left. */
2264 for (; pad
> 0; pad
--)
2272 size_t count
= p
- tmp
;
2274 if (count
>= tmp_length
)
2275 /* tmp_length was incorrectly calculated - fix the
2279 /* Make room for the result. */
2280 if (count
>= allocated
- length
)
2282 size_t n
= xsum (length
, count
);
2284 ENSURE_ALLOCATION (n
);
2287 /* Append the result. */
2288 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
2295 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2296 else if ((dp
->conversion
== 'f' || dp
->conversion
== 'F'
2297 || dp
->conversion
== 'e' || dp
->conversion
== 'E'
2298 || dp
->conversion
== 'g' || dp
->conversion
== 'G'
2299 || dp
->conversion
== 'a' || dp
->conversion
== 'A')
2301 # if NEED_PRINTF_INFINITE_DOUBLE
2302 || (a
.arg
[dp
->arg_index
].type
== TYPE_DOUBLE
2303 /* The systems (mingw) which produce wrong output
2304 for Inf, -Inf, and NaN also do so for -0.0.
2305 Therefore we treat this case here as well. */
2306 && is_infinite_or_zero (a
.arg
[dp
->arg_index
].a
.a_double
))
2308 # if NEED_PRINTF_LONG_DOUBLE
2309 || a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2310 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2311 || (a
.arg
[dp
->arg_index
].type
== TYPE_LONGDOUBLE
2312 /* Some systems produce wrong output for Inf,
2314 && is_infinitel (a
.arg
[dp
->arg_index
].a
.a_longdouble
))
2318 # if NEED_PRINTF_INFINITE_DOUBLE && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2319 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2321 int flags
= dp
->flags
;
2327 DCHAR_T tmpbuf
[700];
2334 if (dp
->width_start
!= dp
->width_end
)
2336 if (dp
->width_arg_index
!= ARG_NONE
)
2340 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
2342 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
2345 /* "A negative field width is taken as a '-' flag
2346 followed by a positive field width." */
2348 width
= (unsigned int) (-arg
);
2355 const FCHAR_T
*digitp
= dp
->width_start
;
2358 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
2359 while (digitp
!= dp
->width_end
);
2366 if (dp
->precision_start
!= dp
->precision_end
)
2368 if (dp
->precision_arg_index
!= ARG_NONE
)
2372 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
2374 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
2375 /* "A negative precision is taken as if the precision
2385 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
2388 while (digitp
!= dp
->precision_end
)
2389 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
2394 /* POSIX specifies the default precision to be 6 for %f, %F,
2395 %e, %E, but not for %g, %G. Implementations appear to use
2396 the same default precision also for %g, %G. */
2400 /* Allocate a temporary buffer of sufficient size. */
2401 # if NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2402 tmp_length
= (type
== TYPE_LONGDOUBLE
? LDBL_DIG
+ 1 : 0);
2403 # elif NEED_PRINTF_LONG_DOUBLE
2404 tmp_length
= LDBL_DIG
+ 1;
2408 if (tmp_length
< precision
)
2409 tmp_length
= precision
;
2410 # if NEED_PRINTF_LONG_DOUBLE
2411 # if NEED_PRINTF_INFINITE_DOUBLE
2412 if (type
== TYPE_LONGDOUBLE
)
2414 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2416 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
2417 if (!(isnanl (arg
) || arg
+ arg
== arg
))
2419 /* arg is finite and nonzero. */
2420 int exponent
= floorlog10l (arg
< 0 ? -arg
: arg
);
2421 if (exponent
>= 0 && tmp_length
< exponent
+ precision
)
2422 tmp_length
= exponent
+ precision
;
2426 /* Account for sign, decimal point etc. */
2427 tmp_length
= xsum (tmp_length
, 12);
2429 if (tmp_length
< width
)
2432 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
2434 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (DCHAR_T
))
2438 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (DCHAR_T
));
2440 if (size_overflow_p (tmp_memsize
))
2441 /* Overflow, would lead to out of memory. */
2443 tmp
= (DCHAR_T
*) malloc (tmp_memsize
);
2445 /* Out of memory. */
2452 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2453 # if NEED_PRINTF_INFINITE_DOUBLE
2454 if (type
== TYPE_LONGDOUBLE
)
2457 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
2461 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2463 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2467 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2473 DECL_LONG_DOUBLE_ROUNDING
2475 BEGIN_LONG_DOUBLE_ROUNDING ();
2477 if (signbit (arg
)) /* arg < 0.0L or negative zero */
2485 else if (flags
& FLAG_SHOWSIGN
)
2487 else if (flags
& FLAG_SPACE
)
2490 if (arg
> 0.0L && arg
+ arg
== arg
)
2492 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2494 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2498 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2503 # if NEED_PRINTF_LONG_DOUBLE
2506 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2512 scale10_round_decimal_long_double (arg
, precision
);
2515 END_LONG_DOUBLE_ROUNDING ();
2518 ndigits
= strlen (digits
);
2520 if (ndigits
> precision
)
2524 *p
++ = digits
[ndigits
];
2526 while (ndigits
> precision
);
2529 /* Here ndigits <= precision. */
2530 if ((flags
& FLAG_ALT
) || precision
> 0)
2532 *p
++ = decimal_point_char ();
2533 for (; precision
> ndigits
; precision
--)
2538 *p
++ = digits
[ndigits
];
2544 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
2552 if ((flags
& FLAG_ALT
) || precision
> 0)
2554 *p
++ = decimal_point_char ();
2555 for (; precision
> 0; precision
--)
2566 exponent
= floorlog10l (arg
);
2571 scale10_round_decimal_long_double (arg
,
2572 (int)precision
- exponent
);
2575 END_LONG_DOUBLE_ROUNDING ();
2578 ndigits
= strlen (digits
);
2580 if (ndigits
== precision
+ 1)
2582 if (ndigits
< precision
2583 || ndigits
> precision
+ 2)
2584 /* The exponent was not guessed
2585 precisely enough. */
2588 /* None of two values of exponent is
2589 the right one. Prevent an endless
2593 if (ndigits
== precision
)
2600 /* Here ndigits = precision+1. */
2601 *p
++ = digits
[--ndigits
];
2602 if ((flags
& FLAG_ALT
) || precision
> 0)
2604 *p
++ = decimal_point_char ();
2608 *p
++ = digits
[ndigits
];
2615 *p
++ = dp
->conversion
; /* 'e' or 'E' */
2616 # if WIDE_CHAR_VERSION
2618 static const wchar_t decimal_format
[] =
2619 { '%', '+', '.', '2', 'd', '\0' };
2620 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2625 if (sizeof (DCHAR_T
) == 1)
2627 sprintf ((char *) p
, "%+.2d", exponent
);
2635 sprintf (expbuf
, "%+.2d", exponent
);
2636 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2641 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
2645 /* precision >= 1. */
2648 /* The exponent is 0, >= -4, < precision.
2649 Use fixed-point notation. */
2651 size_t ndigits
= precision
;
2652 /* Number of trailing zeroes that have to be
2655 (flags
& FLAG_ALT
? 0 : precision
- 1);
2659 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
2661 *p
++ = decimal_point_char ();
2662 while (ndigits
> nzeroes
)
2678 exponent
= floorlog10l (arg
);
2683 scale10_round_decimal_long_double (arg
,
2684 (int)(precision
- 1) - exponent
);
2687 END_LONG_DOUBLE_ROUNDING ();
2690 ndigits
= strlen (digits
);
2692 if (ndigits
== precision
)
2694 if (ndigits
< precision
- 1
2695 || ndigits
> precision
+ 1)
2696 /* The exponent was not guessed
2697 precisely enough. */
2700 /* None of two values of exponent is
2701 the right one. Prevent an endless
2705 if (ndigits
< precision
)
2711 /* Here ndigits = precision. */
2713 /* Determine the number of trailing zeroes
2714 that have to be dropped. */
2716 if ((flags
& FLAG_ALT
) == 0)
2717 while (nzeroes
< ndigits
2718 && digits
[nzeroes
] == '0')
2721 /* The exponent is now determined. */
2723 && exponent
< (long)precision
)
2725 /* Fixed-point notation:
2726 max(exponent,0)+1 digits, then the
2727 decimal point, then the remaining
2728 digits without trailing zeroes. */
2731 size_t count
= exponent
+ 1;
2732 /* Note: count <= precision = ndigits. */
2733 for (; count
> 0; count
--)
2734 *p
++ = digits
[--ndigits
];
2735 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
2737 *p
++ = decimal_point_char ();
2738 while (ndigits
> nzeroes
)
2741 *p
++ = digits
[ndigits
];
2747 size_t count
= -exponent
- 1;
2749 *p
++ = decimal_point_char ();
2750 for (; count
> 0; count
--)
2752 while (ndigits
> nzeroes
)
2755 *p
++ = digits
[ndigits
];
2761 /* Exponential notation. */
2762 *p
++ = digits
[--ndigits
];
2763 if ((flags
& FLAG_ALT
) || ndigits
> nzeroes
)
2765 *p
++ = decimal_point_char ();
2766 while (ndigits
> nzeroes
)
2769 *p
++ = digits
[ndigits
];
2772 *p
++ = dp
->conversion
- 'G' + 'E'; /* 'e' or 'E' */
2773 # if WIDE_CHAR_VERSION
2775 static const wchar_t decimal_format
[] =
2776 { '%', '+', '.', '2', 'd', '\0' };
2777 SNPRINTF (p
, 6 + 1, decimal_format
, exponent
);
2782 if (sizeof (DCHAR_T
) == 1)
2784 sprintf ((char *) p
, "%+.2d", exponent
);
2792 sprintf (expbuf
, "%+.2d", exponent
);
2793 for (ep
= expbuf
; (*p
= *ep
) != '\0'; ep
++)
2805 /* arg is finite. */
2810 END_LONG_DOUBLE_ROUNDING ();
2813 # if NEED_PRINTF_INFINITE_DOUBLE
2817 # if NEED_PRINTF_INFINITE_DOUBLE
2819 /* Simpler than above: handle only NaN, Infinity, zero. */
2820 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
2824 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2826 *p
++ = 'N'; *p
++ = 'A'; *p
++ = 'N';
2830 *p
++ = 'n'; *p
++ = 'a'; *p
++ = 'n';
2837 if (signbit (arg
)) /* arg < 0.0L or negative zero */
2845 else if (flags
& FLAG_SHOWSIGN
)
2847 else if (flags
& FLAG_SPACE
)
2850 if (arg
> 0.0 && arg
+ arg
== arg
)
2852 if (dp
->conversion
>= 'A' && dp
->conversion
<= 'Z')
2854 *p
++ = 'I'; *p
++ = 'N'; *p
++ = 'F';
2858 *p
++ = 'i'; *p
++ = 'n'; *p
++ = 'f';
2868 if (dp
->conversion
== 'f' || dp
->conversion
== 'F')
2871 if ((flags
& FLAG_ALT
) || precision
> 0)
2873 *p
++ = decimal_point_char ();
2874 for (; precision
> 0; precision
--)
2878 else if (dp
->conversion
== 'e' || dp
->conversion
== 'E')
2881 if ((flags
& FLAG_ALT
) || precision
> 0)
2883 *p
++ = decimal_point_char ();
2884 for (; precision
> 0; precision
--)
2887 *p
++ = dp
->conversion
; /* 'e' or 'E' */
2889 /* Produce the same number of exponent digits as
2890 the native printf implementation. */
2891 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
2897 else if (dp
->conversion
== 'g' || dp
->conversion
== 'G')
2900 if (flags
& FLAG_ALT
)
2903 (precision
> 0 ? precision
- 1 : 0);
2904 *p
++ = decimal_point_char ();
2905 for (; ndigits
> 0; --ndigits
)
2916 /* The generated string now extends from tmp to p, with the
2917 zero padding insertion point being at pad_ptr. */
2918 if (has_width
&& p
- tmp
< width
)
2920 size_t pad
= width
- (p
- tmp
);
2921 DCHAR_T
*end
= p
+ pad
;
2923 if (flags
& FLAG_LEFT
)
2925 /* Pad with spaces on the right. */
2926 for (; pad
> 0; pad
--)
2929 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
2931 /* Pad with zeroes. */
2936 for (; pad
> 0; pad
--)
2941 /* Pad with spaces on the left. */
2946 for (; pad
> 0; pad
--)
2954 size_t count
= p
- tmp
;
2956 if (count
>= tmp_length
)
2957 /* tmp_length was incorrectly calculated - fix the
2961 /* Make room for the result. */
2962 if (count
>= allocated
- length
)
2964 size_t n
= xsum (length
, count
);
2966 ENSURE_ALLOCATION (n
);
2969 /* Append the result. */
2970 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
2979 arg_type type
= a
.arg
[dp
->arg_index
].type
;
2980 int flags
= dp
->flags
;
2981 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
2985 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
2988 # define pad_ourselves 0
2991 unsigned int prefix_count
;
2995 TCHAR_T tmpbuf
[700];
2999 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
3002 if (dp
->width_start
!= dp
->width_end
)
3004 if (dp
->width_arg_index
!= ARG_NONE
)
3008 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3010 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
3013 /* "A negative field width is taken as a '-' flag
3014 followed by a positive field width." */
3016 width
= (unsigned int) (-arg
);
3023 const FCHAR_T
*digitp
= dp
->width_start
;
3026 width
= xsum (xtimes (width
, 10), *digitp
++ - '0');
3027 while (digitp
!= dp
->width_end
);
3034 /* Allocate a temporary buffer of sufficient size for calling
3040 if (dp
->precision_start
!= dp
->precision_end
)
3042 if (dp
->precision_arg_index
!= ARG_NONE
)
3046 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3048 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3049 precision
= (arg
< 0 ? 0 : arg
);
3053 const FCHAR_T
*digitp
= dp
->precision_start
+ 1;
3056 while (digitp
!= dp
->precision_end
)
3057 precision
= xsum (xtimes (precision
, 10), *digitp
++ - '0');
3061 switch (dp
->conversion
)
3064 case 'd': case 'i': case 'u':
3065 # if HAVE_LONG_LONG_INT
3066 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3068 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3069 * 0.30103 /* binary -> decimal */
3071 + 1; /* turn floor into ceil */
3074 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3076 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3077 * 0.30103 /* binary -> decimal */
3079 + 1; /* turn floor into ceil */
3082 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3083 * 0.30103 /* binary -> decimal */
3085 + 1; /* turn floor into ceil */
3086 if (tmp_length
< precision
)
3087 tmp_length
= precision
;
3088 /* Multiply by 2, as an estimate for FLAG_GROUP. */
3089 tmp_length
= xsum (tmp_length
, tmp_length
);
3090 /* Add 1, to account for a leading sign. */
3091 tmp_length
= xsum (tmp_length
, 1);
3095 # if HAVE_LONG_LONG_INT
3096 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3098 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3099 * 0.333334 /* binary -> octal */
3101 + 1; /* turn floor into ceil */
3104 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3106 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3107 * 0.333334 /* binary -> octal */
3109 + 1; /* turn floor into ceil */
3112 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3113 * 0.333334 /* binary -> octal */
3115 + 1; /* turn floor into ceil */
3116 if (tmp_length
< precision
)
3117 tmp_length
= precision
;
3118 /* Add 1, to account for a leading sign. */
3119 tmp_length
= xsum (tmp_length
, 1);
3123 # if HAVE_LONG_LONG_INT
3124 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
3126 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3127 * 0.25 /* binary -> hexadecimal */
3129 + 1; /* turn floor into ceil */
3132 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
3134 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3135 * 0.25 /* binary -> hexadecimal */
3137 + 1; /* turn floor into ceil */
3140 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3141 * 0.25 /* binary -> hexadecimal */
3143 + 1; /* turn floor into ceil */
3144 if (tmp_length
< precision
)
3145 tmp_length
= precision
;
3146 /* Add 2, to account for a leading sign or alternate form. */
3147 tmp_length
= xsum (tmp_length
, 2);
3151 if (type
== TYPE_LONGDOUBLE
)
3153 (unsigned int) (LDBL_MAX_EXP
3154 * 0.30103 /* binary -> decimal */
3155 * 2 /* estimate for FLAG_GROUP */
3157 + 1 /* turn floor into ceil */
3158 + 10; /* sign, decimal point etc. */
3161 (unsigned int) (DBL_MAX_EXP
3162 * 0.30103 /* binary -> decimal */
3163 * 2 /* estimate for FLAG_GROUP */
3165 + 1 /* turn floor into ceil */
3166 + 10; /* sign, decimal point etc. */
3167 tmp_length
= xsum (tmp_length
, precision
);
3170 case 'e': case 'E': case 'g': case 'G':
3172 12; /* sign, decimal point, exponent etc. */
3173 tmp_length
= xsum (tmp_length
, precision
);
3177 if (type
== TYPE_LONGDOUBLE
)
3179 (unsigned int) (LDBL_DIG
3180 * 0.831 /* decimal -> hexadecimal */
3182 + 1; /* turn floor into ceil */
3185 (unsigned int) (DBL_DIG
3186 * 0.831 /* decimal -> hexadecimal */
3188 + 1; /* turn floor into ceil */
3189 if (tmp_length
< precision
)
3190 tmp_length
= precision
;
3191 /* Account for sign, decimal point etc. */
3192 tmp_length
= xsum (tmp_length
, 12);
3196 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
3197 if (type
== TYPE_WIDE_CHAR
)
3198 tmp_length
= MB_CUR_MAX
;
3206 if (type
== TYPE_WIDE_STRING
)
3209 local_wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
);
3211 # if !WIDE_CHAR_VERSION
3212 tmp_length
= xtimes (tmp_length
, MB_CUR_MAX
);
3217 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
3222 (unsigned int) (sizeof (void *) * CHAR_BIT
3223 * 0.25 /* binary -> hexadecimal */
3225 + 1 /* turn floor into ceil */
3226 + 2; /* account for leading 0x */
3233 # if ENABLE_UNISTDIO
3234 /* Padding considers the number of characters, therefore the
3235 number of elements after padding may be
3236 > max (tmp_length, width)
3238 <= tmp_length + width. */
3239 tmp_length
= xsum (tmp_length
, width
);
3241 /* Padding considers the number of elements, says POSIX. */
3242 if (tmp_length
< width
)
3246 tmp_length
= xsum (tmp_length
, 1); /* account for trailing NUL */
3249 if (tmp_length
<= sizeof (tmpbuf
) / sizeof (TCHAR_T
))
3253 size_t tmp_memsize
= xtimes (tmp_length
, sizeof (TCHAR_T
));
3255 if (size_overflow_p (tmp_memsize
))
3256 /* Overflow, would lead to out of memory. */
3258 tmp
= (TCHAR_T
*) malloc (tmp_memsize
);
3260 /* Out of memory. */
3265 /* Decide whether to perform the padding ourselves. */
3266 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
3267 switch (dp
->conversion
)
3269 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3270 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
3271 to perform the padding after this conversion. Functions
3272 with unistdio extensions perform the padding based on
3273 character count rather than element count. */
3276 # if NEED_PRINTF_FLAG_ZERO
3277 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
3288 /* Construct the format string for calling snprintf or
3292 #if NEED_PRINTF_FLAG_GROUPING
3293 /* The underlying implementation doesn't support the ' flag.
3294 Produce no grouping characters in this case; this is
3295 acceptable because the grouping is locale dependent. */
3297 if (flags
& FLAG_GROUP
)
3300 if (flags
& FLAG_LEFT
)
3302 if (flags
& FLAG_SHOWSIGN
)
3304 if (flags
& FLAG_SPACE
)
3306 if (flags
& FLAG_ALT
)
3310 if (flags
& FLAG_ZERO
)
3312 if (dp
->width_start
!= dp
->width_end
)
3314 size_t n
= dp
->width_end
- dp
->width_start
;
3315 /* The width specification is known to consist only
3316 of standard ASCII characters. */
3317 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
3319 memcpy (fbp
, dp
->width_start
, n
* sizeof (TCHAR_T
));
3324 const FCHAR_T
*mp
= dp
->width_start
;
3326 *fbp
++ = (unsigned char) *mp
++;
3331 if (dp
->precision_start
!= dp
->precision_end
)
3333 size_t n
= dp
->precision_end
- dp
->precision_start
;
3334 /* The precision specification is known to consist only
3335 of standard ASCII characters. */
3336 if (sizeof (FCHAR_T
) == sizeof (TCHAR_T
))
3338 memcpy (fbp
, dp
->precision_start
, n
* sizeof (TCHAR_T
));
3343 const FCHAR_T
*mp
= dp
->precision_start
;
3345 *fbp
++ = (unsigned char) *mp
++;
3352 #if HAVE_LONG_LONG_INT
3353 case TYPE_LONGLONGINT
:
3354 case TYPE_ULONGLONGINT
:
3355 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3368 case TYPE_WIDE_CHAR
:
3371 case TYPE_WIDE_STRING
:
3375 case TYPE_LONGDOUBLE
:
3381 #if NEED_PRINTF_DIRECTIVE_F
3382 if (dp
->conversion
== 'F')
3386 *fbp
= dp
->conversion
;
3395 /* Construct the arguments for calling snprintf or sprintf. */
3397 if (!pad_ourselves
&& dp
->width_arg_index
!= ARG_NONE
)
3399 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
3401 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
3403 if (dp
->precision_arg_index
!= ARG_NONE
)
3405 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
3407 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
3411 /* The SNPRINTF result is appended after result[0..length].
3412 The latter is an array of DCHAR_T; SNPRINTF appends an
3413 array of TCHAR_T to it. This is possible because
3414 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
3415 alignof (TCHAR_T) <= alignof (DCHAR_T). */
3416 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
3417 /* Prepare checking whether snprintf returns the count
3419 ENSURE_ALLOCATION (xsum (length
, 1));
3420 *(TCHAR_T
*) (result
+ length
) = '\0';
3429 size_t maxlen
= allocated
- length
;
3430 /* SNPRINTF can fail if its second argument is
3432 if (maxlen
> INT_MAX
/ TCHARS_PER_DCHAR
)
3434 maxlen
= maxlen
* TCHARS_PER_DCHAR
;
3435 # define SNPRINTF_BUF(arg) \
3436 switch (prefix_count) \
3439 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3444 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3446 prefixes[0], arg, &count); \
3449 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3451 prefixes[0], prefixes[1], arg, \
3458 # define SNPRINTF_BUF(arg) \
3459 switch (prefix_count) \
3462 count = sprintf (tmp, buf, arg); \
3465 count = sprintf (tmp, buf, prefixes[0], arg); \
3468 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
3480 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
3486 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
3492 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
3498 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
3504 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
3510 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
3516 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
3522 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
3526 #if HAVE_LONG_LONG_INT
3527 case TYPE_LONGLONGINT
:
3529 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
3533 case TYPE_ULONGLONGINT
:
3535 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
3542 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
3546 case TYPE_LONGDOUBLE
:
3548 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
3554 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
3559 case TYPE_WIDE_CHAR
:
3561 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
3568 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
3573 case TYPE_WIDE_STRING
:
3575 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
3582 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
3591 /* Portability: Not all implementations of snprintf()
3592 are ISO C 99 compliant. Determine the number of
3593 bytes that snprintf() has produced or would have
3597 /* Verify that snprintf() has NUL-terminated its
3600 && ((TCHAR_T
*) (result
+ length
)) [count
] != '\0')
3602 /* Portability hack. */
3603 if (retcount
> count
)
3608 /* snprintf() doesn't understand the '%n'
3612 /* Don't use the '%n' directive; instead, look
3613 at the snprintf() return value. */
3619 /* Look at the snprintf() return value. */
3622 /* HP-UX 10.20 snprintf() is doubly deficient:
3623 It doesn't understand the '%n' directive,
3624 *and* it returns -1 (rather than the length
3625 that would have been required) when the
3626 buffer is too small. */
3627 size_t bigger_need
=
3628 xsum (xtimes (allocated
, 2), 12);
3629 ENSURE_ALLOCATION (bigger_need
);
3638 /* Attempt to handle failure. */
3641 if (!(result
== resultbuf
|| result
== NULL
))
3643 if (buf_malloced
!= NULL
)
3644 free (buf_malloced
);
3651 /* Handle overflow of the allocated buffer. */
3652 if (count
>= maxlen
)
3654 /* Need at least count * sizeof (TCHAR_T) bytes. But
3655 allocate proportionally, to avoid looping eternally
3656 if snprintf() reports a too small count. */
3659 (count
+ TCHARS_PER_DCHAR
- 1)
3660 / TCHARS_PER_DCHAR
),
3661 xtimes (allocated
, 2));
3663 ENSURE_ALLOCATION (n
);
3670 if (count
>= tmp_length
)
3671 /* tmp_length was incorrectly calculated - fix the
3676 /* Convert from TCHAR_T[] to DCHAR_T[]. */
3677 if (dp
->conversion
== 'c' || dp
->conversion
== 's')
3679 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
3681 The result string is not certainly ASCII. */
3682 const TCHAR_T
*tmpsrc
;
3685 /* This code assumes that TCHAR_T is 'char'. */
3686 typedef int TCHAR_T_verify
3687 [2 * (sizeof (TCHAR_T
) == 1) - 1];
3689 tmpsrc
= (TCHAR_T
*) (result
+ length
);
3695 if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
3696 iconveh_question_mark
,
3699 &tmpdst
, &tmpdst_len
)
3702 int saved_errno
= errno
;
3703 if (!(result
== resultbuf
|| result
== NULL
))
3705 if (buf_malloced
!= NULL
)
3706 free (buf_malloced
);
3708 errno
= saved_errno
;
3711 ENSURE_ALLOCATION (xsum (length
, tmpdst_len
));
3712 DCHAR_CPY (result
+ length
, tmpdst
, tmpdst_len
);
3718 /* The result string is ASCII.
3719 Simple 1:1 conversion. */
3721 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
3722 no-op conversion, in-place on the array starting
3723 at (result + length). */
3724 if (sizeof (DCHAR_T
) != sizeof (TCHAR_T
))
3727 const TCHAR_T
*tmpsrc
;
3732 if (result
== resultbuf
)
3734 tmpsrc
= (TCHAR_T
*) (result
+ length
);
3735 /* ENSURE_ALLOCATION will not move tmpsrc
3736 (because it's part of resultbuf). */
3737 ENSURE_ALLOCATION (xsum (length
, count
));
3741 /* ENSURE_ALLOCATION will move the array
3742 (because it uses realloc(). */
3743 ENSURE_ALLOCATION (xsum (length
, count
));
3744 tmpsrc
= (TCHAR_T
*) (result
+ length
);
3748 ENSURE_ALLOCATION (xsum (length
, count
));
3750 tmpdst
= result
+ length
;
3751 /* Copy backwards, because of overlapping. */
3754 for (n
= count
; n
> 0; n
--)
3755 *--tmpdst
= (unsigned char) *--tmpsrc
;
3760 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
3761 /* Make room for the result. */
3762 if (count
> allocated
- length
)
3764 /* Need at least count elements. But allocate
3767 xmax (xsum (length
, count
), xtimes (allocated
, 2));
3769 ENSURE_ALLOCATION (n
);
3773 /* Here count <= allocated - length. */
3775 /* Perform padding. */
3776 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
3777 if (pad_ourselves
&& has_width
)
3780 # if ENABLE_UNISTDIO
3781 /* Outside POSIX, it's preferrable to compare the width
3782 against the number of _characters_ of the converted
3784 w
= DCHAR_MBSNLEN (result
+ length
, count
);
3786 /* The width is compared against the number of _bytes_
3787 of the converted value, says POSIX. */
3792 size_t pad
= width
- w
;
3794 /* Make room for the result. */
3795 if (xsum (count
, pad
) > allocated
- length
)
3797 /* Need at least count + pad elements. But
3798 allocate proportionally. */
3800 xmax (xsum3 (length
, count
, pad
),
3801 xtimes (allocated
, 2));
3804 ENSURE_ALLOCATION (n
);
3807 /* Here count + pad <= allocated - length. */
3810 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
3811 DCHAR_T
* const rp
= result
+ length
;
3813 DCHAR_T
* const rp
= tmp
;
3815 DCHAR_T
*p
= rp
+ count
;
3816 DCHAR_T
*end
= p
+ pad
;
3817 # if NEED_PRINTF_FLAG_ZERO
3819 # if !DCHAR_IS_TCHAR
3820 if (dp
->conversion
== 'c'
3821 || dp
->conversion
== 's')
3822 /* No zero-padding for string directives. */
3827 pad_ptr
= (*rp
== '-' ? rp
+ 1 : rp
);
3828 /* No zero-padding of "inf" and "nan". */
3829 if ((*pad_ptr
>= 'A' && *pad_ptr
<= 'Z')
3830 || (*pad_ptr
>= 'a' && *pad_ptr
<= 'z'))
3834 /* The generated string now extends from rp to p,
3835 with the zero padding insertion point being at
3838 count
= count
+ pad
; /* = end - rp */
3840 if (flags
& FLAG_LEFT
)
3842 /* Pad with spaces on the right. */
3843 for (; pad
> 0; pad
--)
3846 # if NEED_PRINTF_FLAG_ZERO
3847 else if ((flags
& FLAG_ZERO
) && pad_ptr
!= NULL
)
3849 /* Pad with zeroes. */
3854 for (; pad
> 0; pad
--)
3860 /* Pad with spaces on the left. */
3865 for (; pad
> 0; pad
--)
3873 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
3874 if (count
>= tmp_length
)
3875 /* tmp_length was incorrectly calculated - fix the
3880 /* Here still count <= allocated - length. */
3882 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
3883 /* The snprintf() result did fit. */
3885 /* Append the sprintf() result. */
3886 memcpy (result
+ length
, tmp
, count
* sizeof (DCHAR_T
));
3893 #if NEED_PRINTF_DIRECTIVE_F
3894 if (dp
->conversion
== 'F')
3896 /* Convert the %f result to upper case for %F. */
3897 DCHAR_T
*rp
= result
+ length
;
3899 for (rc
= count
; rc
> 0; rc
--, rp
++)
3900 if (*rp
>= 'a' && *rp
<= 'z')
3901 *rp
= *rp
- 'a' + 'A';
3912 /* Add the final NUL. */
3913 ENSURE_ALLOCATION (xsum (length
, 1));
3914 result
[length
] = '\0';
3916 if (result
!= resultbuf
&& length
+ 1 < allocated
)
3918 /* Shrink the allocated memory if possible. */
3921 memory
= (DCHAR_T
*) realloc (result
, (length
+ 1) * sizeof (DCHAR_T
));
3926 if (buf_malloced
!= NULL
)
3927 free (buf_malloced
);
3930 /* Note that we can produce a big string of a length > INT_MAX. POSIX
3931 says that snprintf() fails with errno = EOVERFLOW in this case, but
3932 that's only because snprintf() returns an 'int'. This function does
3933 not have this limitation. */
3937 if (!(result
== resultbuf
|| result
== NULL
))
3939 if (buf_malloced
!= NULL
)
3940 free (buf_malloced
);
3946 if (!(result
== resultbuf
|| result
== NULL
))
3948 if (buf_malloced
!= NULL
)
3949 free (buf_malloced
);
3957 #undef TCHARS_PER_DCHAR