1
/* -*- buffer-read-only: t -*- vi: set ro: */
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
/* vsprintf with automatic memory allocation.
4
Copyright (C) 1999, 2002-2011 Free Software Foundation, Inc.
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3, or (at your option)
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation,
18
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
/* This file can be parametrized with the following macros:
21
VASNPRINTF The name of the function being defined.
22
FCHAR_T The element type of the format string.
23
DCHAR_T The element type of the destination (result) string.
24
FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
25
in the format string are ASCII. MUST be set if
26
FCHAR_T and DCHAR_T are not the same type.
27
DIRECTIVE Structure denoting a format directive.
29
DIRECTIVES Structure denoting the set of format directives of a
30
format string. Depends on FCHAR_T.
31
PRINTF_PARSE Function that parses a format string.
33
DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
34
DCHAR_SET memset like function for DCHAR_T[] arrays.
35
DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
36
SNPRINTF The system's snprintf (or similar) function.
37
This may be either snprintf or swprintf.
38
TCHAR_T The element type of the argument and result string
39
of the said SNPRINTF function. This may be either
40
char or wchar_t. The code exploits that
41
sizeof (TCHAR_T) | sizeof (DCHAR_T) and
42
alignof (TCHAR_T) <= alignof (DCHAR_T).
43
DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
44
DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
45
DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
46
DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
47
DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
49
/* Tell glibc's <stdio.h> to provide a prototype for snprintf().
50
This must come before <config.h> because <config.h> may include
51
<features.h>, and once <features.h> has been included, it's too late. */
53
# define _GNU_SOURCE 1
65
# if WIDE_CHAR_VERSION
66
# include "vasnwprintf.h"
68
# include "vasnprintf.h"
72
#include <locale.h> /* localeconv() */
73
#include <stdio.h> /* snprintf(), sprintf() */
74
#include <stdlib.h> /* abort(), malloc(), realloc(), free() */
75
#include <string.h> /* memcpy(), strlen() */
76
#include <errno.h> /* errno */
77
#include <limits.h> /* CHAR_BIT */
78
#include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
80
# include <langinfo.h>
83
# if WIDE_CHAR_VERSION
84
# include "wprintf-parse.h"
86
# include "printf-parse.h"
90
/* Checked size_t computations. */
95
#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
100
#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
102
# include "isnand-nolibm.h"
105
#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
107
# include "isnanl-nolibm.h"
111
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
113
# include "isnand-nolibm.h"
114
# include "printf-frexp.h"
117
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
119
# include "isnanl-nolibm.h"
120
# include "printf-frexpl.h"
124
/* Default parameters. */
126
# if WIDE_CHAR_VERSION
127
# define VASNPRINTF vasnwprintf
128
# define FCHAR_T wchar_t
129
# define DCHAR_T wchar_t
130
# define TCHAR_T wchar_t
131
# define DCHAR_IS_TCHAR 1
132
# define DIRECTIVE wchar_t_directive
133
# define DIRECTIVES wchar_t_directives
134
# define PRINTF_PARSE wprintf_parse
135
# define DCHAR_CPY wmemcpy
136
# define DCHAR_SET wmemset
138
# define VASNPRINTF vasnprintf
139
# define FCHAR_T char
140
# define DCHAR_T char
141
# define TCHAR_T char
142
# define DCHAR_IS_TCHAR 1
143
# define DIRECTIVE char_directive
144
# define DIRECTIVES char_directives
145
# define PRINTF_PARSE printf_parse
146
# define DCHAR_CPY memcpy
147
# define DCHAR_SET memset
150
#if WIDE_CHAR_VERSION
151
/* TCHAR_T is wchar_t. */
152
# define USE_SNPRINTF 1
153
# if HAVE_DECL__SNWPRINTF
154
/* On Windows, the function swprintf() has a different signature than
155
on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
156
instead. The mingw function snwprintf() has fewer bugs than the
157
MSVCRT function _snwprintf(), so prefer that. */
158
# if defined __MINGW32__
159
# define SNPRINTF snwprintf
161
# define SNPRINTF _snwprintf
165
# define SNPRINTF swprintf
168
/* TCHAR_T is char. */
169
/* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
170
But don't use it on BeOS, since BeOS snprintf produces no output if the
171
size argument is >= 0x3000000.
172
Also don't use it on Linux libc5, since there snprintf with size = 1
173
writes any output without bounds, like sprintf. */
174
# if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
175
# define USE_SNPRINTF 1
177
# define USE_SNPRINTF 0
179
# if HAVE_DECL__SNPRINTF
180
/* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
181
function _snprintf(), so prefer that. */
182
# if defined __MINGW32__
183
# define SNPRINTF snprintf
184
/* Here we need to call the native snprintf, not rpl_snprintf. */
187
# define SNPRINTF _snprintf
191
# define SNPRINTF snprintf
192
/* Here we need to call the native snprintf, not rpl_snprintf. */
196
/* Here we need to call the native sprintf, not rpl_sprintf. */
199
/* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
200
warnings in this file. Use -Dlint to suppress them. */
202
# define IF_LINT(Code) Code
204
# define IF_LINT(Code) /* empty */
207
/* Avoid some warnings from "gcc -Wshadow".
208
This file doesn't use the exp() and remainder() functions. */
212
#define remainder rem
214
#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
215
# if (HAVE_STRNLEN && !defined _AIX)
216
# define local_strnlen strnlen
218
# ifndef local_strnlen_defined
219
# define local_strnlen_defined 1
221
local_strnlen (const char *string, size_t maxlen)
223
const char *end = memchr (string, '\0', maxlen);
224
return end ? (size_t) (end - string) : maxlen;
230
#if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
232
# define local_wcslen wcslen
234
/* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
235
a dependency towards this library, here is a local substitute.
236
Define this substitute only once, even if this file is included
237
twice in the same compilation unit. */
238
# ifndef local_wcslen_defined
239
# define local_wcslen_defined 1
241
local_wcslen (const wchar_t *s)
245
for (ptr = s; *ptr != (wchar_t) 0; ptr++)
253
#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
255
# define local_wcsnlen wcsnlen
257
# ifndef local_wcsnlen_defined
258
# define local_wcsnlen_defined 1
260
local_wcsnlen (const wchar_t *s, size_t maxlen)
264
for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
272
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
273
/* Determine the decimal-point character according to the current locale. */
274
# ifndef decimal_point_char_defined
275
# define decimal_point_char_defined 1
277
decimal_point_char (void)
280
/* Determine it in a multithread-safe way. We know nl_langinfo is
281
multithread-safe on glibc systems and MacOS X systems, but is not required
282
to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
283
localeconv() is rarely multithread-safe. */
284
# if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
285
point = nl_langinfo (RADIXCHAR);
288
sprintf (pointbuf, "%#.0f", 1.0);
289
point = &pointbuf[1];
291
point = localeconv () -> decimal_point;
293
/* The decimal point is always a single byte: either '.' or ','. */
294
return (point[0] != '\0' ? point[0] : '.');
299
#if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
301
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
303
is_infinite_or_zero (double x)
305
return isnand (x) || x + x == x;
310
#if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
312
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
314
is_infinite_or_zerol (long double x)
316
return isnanl (x) || x + x == x;
321
#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
323
/* Converting 'long double' to decimal without rare rounding bugs requires
324
real bignums. We use the naming conventions of GNU gmp, but vastly simpler
325
(and slower) algorithms. */
327
typedef unsigned int mp_limb_t;
328
# define GMP_LIMB_BITS 32
329
verify (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS);
331
typedef unsigned long long mp_twolimb_t;
332
# define GMP_TWOLIMB_BITS 64
333
verify (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS);
335
/* Representation of a bignum >= 0. */
339
mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
342
/* Compute the product of two bignums >= 0.
343
Return the allocated memory in case of success, NULL in case of memory
344
allocation failure. */
346
multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
353
if (src1.nlimbs <= src2.nlimbs)
367
/* Now 0 <= len1 <= len2. */
370
/* src1 or src2 is zero. */
372
dest->limbs = (mp_limb_t *) malloc (1);
376
/* Here 1 <= len1 <= len2. */
382
dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
385
for (k = len2; k > 0; )
387
for (i = 0; i < len1; i++)
389
mp_limb_t digit1 = p1[i];
390
mp_twolimb_t carry = 0;
391
for (j = 0; j < len2; j++)
393
mp_limb_t digit2 = p2[j];
394
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
396
dp[i + j] = (mp_limb_t) carry;
397
carry = carry >> GMP_LIMB_BITS;
399
dp[i + len2] = (mp_limb_t) carry;
402
while (dlen > 0 && dp[dlen - 1] == 0)
410
/* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
411
a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
413
Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
415
Return the allocated memory in case of success, NULL in case of memory
416
allocation failure. */
418
divide (mpn_t a, mpn_t b, mpn_t *q)
421
First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
422
with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
423
If m<n, then q:=0 and r:=a.
424
If m>=n=1, perform a single-precision division:
427
{Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
428
= a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
429
j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
430
Normalise [q[m-1],...,q[0]], yields q.
431
If m>=n>1, perform a multiple-precision division:
432
We have a/b < beta^(m-n+1).
433
s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
434
Shift a and b left by s bits, copying them. r:=a.
435
r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
436
For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
438
q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
439
In case of overflow (q* >= beta) set q* := beta-1.
440
Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
441
and c3 := b[n-2] * q*.
442
{We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
443
occurred. Furthermore 0 <= c3 < beta^2.
444
If there was overflow and
445
r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
446
the next test can be skipped.}
447
While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
448
Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
450
Put r := r - b * q* * beta^j. In detail:
451
[r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
452
hence: u:=0, for i:=0 to n-1 do
454
r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
455
u:=u div beta (+ 1, if carry in subtraction)
457
{Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
459
the carry u does not overflow.}
460
If a negative carry occurs, put q* := q* - 1
461
and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
463
Normalise [q[m-n],..,q[0]]; this yields the quotient q.
464
Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
466
The room for q[j] can be allocated at the memory location of r[n+j].
467
Finally, round-to-even:
468
Shift r left by 1 bit.
469
If r > b or if r = b and q[0] is odd, q := q+1.
471
const mp_limb_t *a_ptr = a.limbs;
472
size_t a_len = a.nlimbs;
473
const mp_limb_t *b_ptr = b.limbs;
474
size_t b_len = b.nlimbs;
476
mp_limb_t *tmp_roomptr = NULL;
482
/* Allocate room for a_len+2 digits.
483
(Need a_len+1 digits for the real division and 1 more digit for the
484
final rounding of q.) */
485
roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
490
while (a_len > 0 && a_ptr[a_len - 1] == 0)
497
/* Division by zero. */
499
if (b_ptr[b_len - 1] == 0)
505
/* Here m = a_len >= 0 and n = b_len > 0. */
509
/* m<n: trivial case. q=0, r := copy of a. */
512
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
513
q_ptr = roomptr + a_len;
518
/* n=1: single precision division.
519
beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
523
mp_limb_t den = b_ptr[0];
524
mp_limb_t remainder = 0;
525
const mp_limb_t *sourceptr = a_ptr + a_len;
526
mp_limb_t *destptr = q_ptr + a_len;
528
for (count = a_len; count > 0; count--)
531
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
532
*--destptr = num / den;
533
remainder = num % den;
535
/* Normalise and store r. */
538
r_ptr[0] = remainder;
545
if (q_ptr[q_len - 1] == 0)
551
/* n>1: multiple precision division.
552
beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
553
beta^(m-n-1) <= a/b < beta^(m-n+1). */
557
mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
585
/* 0 <= s < GMP_LIMB_BITS.
586
Copy b, shifting it left by s bits. */
589
tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
590
if (tmp_roomptr == NULL)
596
const mp_limb_t *sourceptr = b_ptr;
597
mp_limb_t *destptr = tmp_roomptr;
598
mp_twolimb_t accu = 0;
600
for (count = b_len; count > 0; count--)
602
accu += (mp_twolimb_t) *sourceptr++ << s;
603
*destptr++ = (mp_limb_t) accu;
604
accu = accu >> GMP_LIMB_BITS;
606
/* accu must be zero, since that was how s was determined. */
612
/* Copy a, shifting it left by s bits, yields r.
614
At the beginning: r = roomptr[0..a_len],
615
at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
619
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
624
const mp_limb_t *sourceptr = a_ptr;
625
mp_limb_t *destptr = r_ptr;
626
mp_twolimb_t accu = 0;
628
for (count = a_len; count > 0; count--)
630
accu += (mp_twolimb_t) *sourceptr++ << s;
631
*destptr++ = (mp_limb_t) accu;
632
accu = accu >> GMP_LIMB_BITS;
634
*destptr++ = (mp_limb_t) accu;
636
q_ptr = roomptr + b_len;
637
q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
639
size_t j = a_len - b_len; /* m-n */
640
mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
641
mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
642
mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
643
((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
644
/* Division loop, traversed m-n+1 times.
645
j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
650
if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
652
/* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
654
((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
655
| r_ptr[j + b_len - 1];
656
q_star = num / b_msd;
661
/* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
662
q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
663
/* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
664
<==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
665
<==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
667
If yes, jump directly to the subtraction loop.
668
(Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
669
<==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
670
if (r_ptr[j + b_len] > b_msd
671
|| (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
672
/* r[j+n] >= b[n-1]+1 or
673
r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
678
c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
680
mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
681
((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
682
mp_twolimb_t c3 = /* b[n-2] * q* */
683
(mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
684
/* While c2 < c3, increase c2 and decrease c3.
685
Consider c3-c2. While it is > 0, decrease it by
686
b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
687
this can happen only twice. */
690
q_star = q_star - 1; /* q* := q* - 1 */
691
if (c3 - c2 > b_msdd)
692
q_star = q_star - 1; /* q* := q* - 1 */
698
/* Subtract r := r - b * q* * beta^j. */
701
const mp_limb_t *sourceptr = b_ptr;
702
mp_limb_t *destptr = r_ptr + j;
703
mp_twolimb_t carry = 0;
705
for (count = b_len; count > 0; count--)
707
/* Here 0 <= carry <= q*. */
710
+ (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
711
+ (mp_limb_t) ~(*destptr);
712
/* Here 0 <= carry <= beta*q* + beta-1. */
713
*destptr++ = ~(mp_limb_t) carry;
714
carry = carry >> GMP_LIMB_BITS; /* <= q* */
716
cr = (mp_limb_t) carry;
718
/* Subtract cr from r_ptr[j + b_len], then forget about
720
if (cr > r_ptr[j + b_len])
722
/* Subtraction gave a carry. */
723
q_star = q_star - 1; /* q* := q* - 1 */
726
const mp_limb_t *sourceptr = b_ptr;
727
mp_limb_t *destptr = r_ptr + j;
730
for (count = b_len; count > 0; count--)
732
mp_limb_t source1 = *sourceptr++;
733
mp_limb_t source2 = *destptr;
734
*destptr++ = source1 + source2 + carry;
737
? source1 >= (mp_limb_t) ~source2
738
: source1 > (mp_limb_t) ~source2);
741
/* Forget about the carry and about r[j+n]. */
744
/* q* is determined. Store it as q[j]. */
753
if (q_ptr[q_len - 1] == 0)
755
# if 0 /* Not needed here, since we need r only to compare it with b/2, and
756
b is shifted left by s bits. */
757
/* Shift r right by s bits. */
760
mp_limb_t ptr = r_ptr + r_len;
761
mp_twolimb_t accu = 0;
763
for (count = r_len; count > 0; count--)
765
accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
766
accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
767
*ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
772
while (r_len > 0 && r_ptr[r_len - 1] == 0)
775
/* Compare r << 1 with b. */
783
(i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
784
| (i < r_len ? r_ptr[i] << 1 : 0);
785
mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
795
if (q_len > 0 && ((q_ptr[0] & 1) != 0))
800
for (i = 0; i < q_len; i++)
801
if (++(q_ptr[i]) != 0)
806
if (tmp_roomptr != NULL)
813
/* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
815
Destroys the contents of a.
816
Return the allocated memory - containing the decimal digits in low-to-high
817
order, terminated with a NUL character - in case of success, NULL in case
818
of memory allocation failure. */
820
convert_to_decimal (mpn_t a, size_t extra_zeroes)
822
mp_limb_t *a_ptr = a.limbs;
823
size_t a_len = a.nlimbs;
824
/* 0.03345 is slightly larger than log(2)/(9*log(10)). */
825
size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
826
char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
830
for (; extra_zeroes > 0; extra_zeroes--)
834
/* Divide a by 10^9, in-place. */
835
mp_limb_t remainder = 0;
836
mp_limb_t *ptr = a_ptr + a_len;
838
for (count = a_len; count > 0; count--)
841
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
842
*ptr = num / 1000000000;
843
remainder = num % 1000000000;
845
/* Store the remainder as 9 decimal digits. */
846
for (count = 9; count > 0; count--)
848
*d_ptr++ = '0' + (remainder % 10);
849
remainder = remainder / 10;
852
if (a_ptr[a_len - 1] == 0)
855
/* Remove leading zeroes. */
856
while (d_ptr > c_ptr && d_ptr[-1] == '0')
858
/* But keep at least one zero. */
861
/* Terminate the string. */
867
# if NEED_PRINTF_LONG_DOUBLE
869
/* Assuming x is finite and >= 0:
870
write x as x = 2^e * m, where m is a bignum.
871
Return the allocated memory in case of success, NULL in case of memory
872
allocation failure. */
874
decode_long_double (long double x, int *ep, mpn_t *mp)
881
/* Allocate memory for result. */
882
m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
883
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
886
/* Split into exponential part and mantissa. */
887
y = frexpl (x, &exp);
888
if (!(y >= 0.0L && y < 1.0L))
890
/* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
891
latter is an integer. */
892
/* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
893
I'm not sure whether it's safe to cast a 'long double' value between
894
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
895
'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
897
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
898
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
901
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
904
if (!(y >= 0.0L && y < 1.0L))
906
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
909
if (!(y >= 0.0L && y < 1.0L))
911
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
916
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
919
if (!(y >= 0.0L && y < 1.0L))
921
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
925
for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
928
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
931
if (!(y >= 0.0L && y < 1.0L))
933
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
936
if (!(y >= 0.0L && y < 1.0L))
938
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
940
# if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
946
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
949
*ep = exp - LDBL_MANT_BIT;
955
# if NEED_PRINTF_DOUBLE
957
/* Assuming x is finite and >= 0:
958
write x as x = 2^e * m, where m is a bignum.
959
Return the allocated memory in case of success, NULL in case of memory
960
allocation failure. */
962
decode_double (double x, int *ep, mpn_t *mp)
969
/* Allocate memory for result. */
970
m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
971
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
974
/* Split into exponential part and mantissa. */
976
if (!(y >= 0.0 && y < 1.0))
978
/* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
979
latter is an integer. */
980
/* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
981
I'm not sure whether it's safe to cast a 'double' value between
982
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
983
'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
985
# if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
986
# if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
989
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
992
if (!(y >= 0.0 && y < 1.0))
994
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
997
if (!(y >= 0.0 && y < 1.0))
999
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1004
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
1007
if (!(y >= 0.0 && y < 1.0))
1009
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
1013
for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1016
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1019
if (!(y >= 0.0 && y < 1.0))
1021
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1024
if (!(y >= 0.0 && y < 1.0))
1026
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1031
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
1034
*ep = exp - DBL_MANT_BIT;
1040
/* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1041
Returns the decimal representation of round (x * 10^n).
1042
Return the allocated memory - containing the decimal digits in low-to-high
1043
order, terminated with a NUL character - in case of success, NULL in case
1044
of memory allocation failure. */
1046
scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1049
size_t extra_zeroes;
1052
mp_limb_t *pow5_ptr;
1054
unsigned int s_limbs;
1055
unsigned int s_bits;
1063
/* x = 2^e * m, hence
1064
y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1065
= round (2^s * 5^n * m). */
1068
/* Factor out a common power of 10 if possible. */
1071
extra_zeroes = (s < n ? s : n);
1075
/* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1076
Before converting to decimal, we need to compute
1077
z = round (2^s * 5^n * m). */
1078
/* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1079
sign. 2.322 is slightly larger than log(5)/log(2). */
1080
abs_n = (n >= 0 ? n : -n);
1081
abs_s = (s >= 0 ? s : -s);
1082
pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1083
+ abs_s / GMP_LIMB_BITS + 1)
1084
* sizeof (mp_limb_t));
1085
if (pow5_ptr == NULL)
1090
/* Initialize with 1. */
1093
/* Multiply with 5^|n|. */
1096
static mp_limb_t const small_pow5[13 + 1] =
1098
1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1099
48828125, 244140625, 1220703125
1102
for (n13 = 0; n13 <= abs_n; n13 += 13)
1104
mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1106
mp_twolimb_t carry = 0;
1107
for (j = 0; j < pow5_len; j++)
1109
mp_limb_t digit2 = pow5_ptr[j];
1110
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1111
pow5_ptr[j] = (mp_limb_t) carry;
1112
carry = carry >> GMP_LIMB_BITS;
1115
pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1118
s_limbs = abs_s / GMP_LIMB_BITS;
1119
s_bits = abs_s % GMP_LIMB_BITS;
1120
if (n >= 0 ? s >= 0 : s <= 0)
1122
/* Multiply with 2^|s|. */
1125
mp_limb_t *ptr = pow5_ptr;
1126
mp_twolimb_t accu = 0;
1128
for (count = pow5_len; count > 0; count--)
1130
accu += (mp_twolimb_t) *ptr << s_bits;
1131
*ptr++ = (mp_limb_t) accu;
1132
accu = accu >> GMP_LIMB_BITS;
1136
*ptr = (mp_limb_t) accu;
1143
for (count = pow5_len; count > 0;)
1146
pow5_ptr[s_limbs + count] = pow5_ptr[count];
1148
for (count = s_limbs; count > 0;)
1151
pow5_ptr[count] = 0;
1153
pow5_len += s_limbs;
1155
pow5.limbs = pow5_ptr;
1156
pow5.nlimbs = pow5_len;
1159
/* Multiply m with pow5. No division needed. */
1160
z_memory = multiply (m, pow5, &z);
1164
/* Divide m by pow5 and round. */
1165
z_memory = divide (m, pow5, &z);
1170
pow5.limbs = pow5_ptr;
1171
pow5.nlimbs = pow5_len;
1175
Multiply m with pow5, then divide by 2^|s|. */
1179
tmp_memory = multiply (m, pow5, &numerator);
1180
if (tmp_memory == NULL)
1186
/* Construct 2^|s|. */
1188
mp_limb_t *ptr = pow5_ptr + pow5_len;
1190
for (i = 0; i < s_limbs; i++)
1192
ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1193
denominator.limbs = ptr;
1194
denominator.nlimbs = s_limbs + 1;
1196
z_memory = divide (numerator, denominator, &z);
1202
Multiply m with 2^s, then divide by pow5. */
1205
num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1206
* sizeof (mp_limb_t));
1207
if (num_ptr == NULL)
1214
mp_limb_t *destptr = num_ptr;
1217
for (i = 0; i < s_limbs; i++)
1222
const mp_limb_t *sourceptr = m.limbs;
1223
mp_twolimb_t accu = 0;
1225
for (count = m.nlimbs; count > 0; count--)
1227
accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1228
*destptr++ = (mp_limb_t) accu;
1229
accu = accu >> GMP_LIMB_BITS;
1232
*destptr++ = (mp_limb_t) accu;
1236
const mp_limb_t *sourceptr = m.limbs;
1238
for (count = m.nlimbs; count > 0; count--)
1239
*destptr++ = *sourceptr++;
1241
numerator.limbs = num_ptr;
1242
numerator.nlimbs = destptr - num_ptr;
1244
z_memory = divide (numerator, pow5, &z);
1251
/* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1253
if (z_memory == NULL)
1255
digits = convert_to_decimal (z, extra_zeroes);
1260
# if NEED_PRINTF_LONG_DOUBLE
1262
/* Assuming x is finite and >= 0, and n is an integer:
1263
Returns the decimal representation of round (x * 10^n).
1264
Return the allocated memory - containing the decimal digits in low-to-high
1265
order, terminated with a NUL character - in case of success, NULL in case
1266
of memory allocation failure. */
1268
scale10_round_decimal_long_double (long double x, int n)
1272
void *memory = decode_long_double (x, &e, &m);
1273
return scale10_round_decimal_decoded (e, m, memory, n);
1278
# if NEED_PRINTF_DOUBLE
1280
/* Assuming x is finite and >= 0, and n is an integer:
1281
Returns the decimal representation of round (x * 10^n).
1282
Return the allocated memory - containing the decimal digits in low-to-high
1283
order, terminated with a NUL character - in case of success, NULL in case
1284
of memory allocation failure. */
1286
scale10_round_decimal_double (double x, int n)
1290
void *memory = decode_double (x, &e, &m);
1291
return scale10_round_decimal_decoded (e, m, memory, n);
1296
# if NEED_PRINTF_LONG_DOUBLE
1298
/* Assuming x is finite and > 0:
1299
Return an approximation for n with 10^n <= x < 10^(n+1).
1300
The approximation is usually the right n, but may be off by 1 sometimes. */
1302
floorlog10l (long double x)
1309
/* Split into exponential part and mantissa. */
1310
y = frexpl (x, &exp);
1311
if (!(y >= 0.0L && y < 1.0L))
1317
while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1319
y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1320
exp -= GMP_LIMB_BITS;
1322
if (y < (1.0L / (1 << 16)))
1324
y *= 1.0L * (1 << 16);
1327
if (y < (1.0L / (1 << 8)))
1329
y *= 1.0L * (1 << 8);
1332
if (y < (1.0L / (1 << 4)))
1334
y *= 1.0L * (1 << 4);
1337
if (y < (1.0L / (1 << 2)))
1339
y *= 1.0L * (1 << 2);
1342
if (y < (1.0L / (1 << 1)))
1344
y *= 1.0L * (1 << 1);
1348
if (!(y >= 0.5L && y < 1.0L))
1350
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1353
if (z < 0.70710678118654752444)
1355
z *= 1.4142135623730950488;
1358
if (z < 0.8408964152537145431)
1360
z *= 1.1892071150027210667;
1363
if (z < 0.91700404320467123175)
1365
z *= 1.0905077326652576592;
1368
if (z < 0.9576032806985736469)
1370
z *= 1.0442737824274138403;
1373
/* Now 0.95 <= z <= 1.01. */
1375
/* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1376
Four terms are enough to get an approximation with error < 10^-7. */
1377
l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1378
/* Finally multiply with log(2)/log(10), yields an approximation for
1380
l *= 0.30102999566398119523;
1381
/* Round down to the next integer. */
1382
return (int) l + (l < 0 ? -1 : 0);
1387
# if NEED_PRINTF_DOUBLE
1389
/* Assuming x is finite and > 0:
1390
Return an approximation for n with 10^n <= x < 10^(n+1).
1391
The approximation is usually the right n, but may be off by 1 sometimes. */
1393
floorlog10 (double x)
1400
/* Split into exponential part and mantissa. */
1401
y = frexp (x, &exp);
1402
if (!(y >= 0.0 && y < 1.0))
1408
while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1410
y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1411
exp -= GMP_LIMB_BITS;
1413
if (y < (1.0 / (1 << 16)))
1415
y *= 1.0 * (1 << 16);
1418
if (y < (1.0 / (1 << 8)))
1420
y *= 1.0 * (1 << 8);
1423
if (y < (1.0 / (1 << 4)))
1425
y *= 1.0 * (1 << 4);
1428
if (y < (1.0 / (1 << 2)))
1430
y *= 1.0 * (1 << 2);
1433
if (y < (1.0 / (1 << 1)))
1435
y *= 1.0 * (1 << 1);
1439
if (!(y >= 0.5 && y < 1.0))
1441
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1444
if (z < 0.70710678118654752444)
1446
z *= 1.4142135623730950488;
1449
if (z < 0.8408964152537145431)
1451
z *= 1.1892071150027210667;
1454
if (z < 0.91700404320467123175)
1456
z *= 1.0905077326652576592;
1459
if (z < 0.9576032806985736469)
1461
z *= 1.0442737824274138403;
1464
/* Now 0.95 <= z <= 1.01. */
1466
/* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1467
Four terms are enough to get an approximation with error < 10^-7. */
1468
l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1469
/* Finally multiply with log(2)/log(10), yields an approximation for
1471
l *= 0.30102999566398119523;
1472
/* Round down to the next integer. */
1473
return (int) l + (l < 0 ? -1 : 0);
1478
/* Tests whether a string of digits consists of exactly PRECISION zeroes and
1479
a single '1' digit. */
1481
is_borderline (const char *digits, size_t precision)
1483
for (; precision > 0; precision--, digits++)
1489
return *digits == '\0';
1494
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
1496
/* Use a different function name, to make it possible that the 'wchar_t'
1497
parametrization and the 'char' parametrization get compiled in the same
1498
translation unit. */
1499
# if WIDE_CHAR_VERSION
1500
# define MAX_ROOM_NEEDED wmax_room_needed
1502
# define MAX_ROOM_NEEDED max_room_needed
1505
/* Returns the number of TCHAR_T units needed as temporary space for the result
1506
of sprintf or SNPRINTF of a single conversion directive. */
1507
static inline size_t
1508
MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
1509
arg_type type, int flags, size_t width, int has_precision,
1510
size_t precision, int pad_ourselves)
1516
case 'd': case 'i': case 'u':
1517
# if HAVE_LONG_LONG_INT
1518
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1520
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1521
* 0.30103 /* binary -> decimal */
1523
+ 1; /* turn floor into ceil */
1526
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1528
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
1529
* 0.30103 /* binary -> decimal */
1531
+ 1; /* turn floor into ceil */
1534
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
1535
* 0.30103 /* binary -> decimal */
1537
+ 1; /* turn floor into ceil */
1538
if (tmp_length < precision)
1539
tmp_length = precision;
1540
/* Multiply by 2, as an estimate for FLAG_GROUP. */
1541
tmp_length = xsum (tmp_length, tmp_length);
1542
/* Add 1, to account for a leading sign. */
1543
tmp_length = xsum (tmp_length, 1);
1547
# if HAVE_LONG_LONG_INT
1548
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1550
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1551
* 0.333334 /* binary -> octal */
1553
+ 1; /* turn floor into ceil */
1556
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1558
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
1559
* 0.333334 /* binary -> octal */
1561
+ 1; /* turn floor into ceil */
1564
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
1565
* 0.333334 /* binary -> octal */
1567
+ 1; /* turn floor into ceil */
1568
if (tmp_length < precision)
1569
tmp_length = precision;
1570
/* Add 1, to account for a leading sign. */
1571
tmp_length = xsum (tmp_length, 1);
1575
# if HAVE_LONG_LONG_INT
1576
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
1578
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
1579
* 0.25 /* binary -> hexadecimal */
1581
+ 1; /* turn floor into ceil */
1584
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
1586
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
1587
* 0.25 /* binary -> hexadecimal */
1589
+ 1; /* turn floor into ceil */
1592
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
1593
* 0.25 /* binary -> hexadecimal */
1595
+ 1; /* turn floor into ceil */
1596
if (tmp_length < precision)
1597
tmp_length = precision;
1598
/* Add 2, to account for a leading sign or alternate form. */
1599
tmp_length = xsum (tmp_length, 2);
1603
if (type == TYPE_LONGDOUBLE)
1605
(unsigned int) (LDBL_MAX_EXP
1606
* 0.30103 /* binary -> decimal */
1607
* 2 /* estimate for FLAG_GROUP */
1609
+ 1 /* turn floor into ceil */
1610
+ 10; /* sign, decimal point etc. */
1613
(unsigned int) (DBL_MAX_EXP
1614
* 0.30103 /* binary -> decimal */
1615
* 2 /* estimate for FLAG_GROUP */
1617
+ 1 /* turn floor into ceil */
1618
+ 10; /* sign, decimal point etc. */
1619
tmp_length = xsum (tmp_length, precision);
1622
case 'e': case 'E': case 'g': case 'G':
1624
12; /* sign, decimal point, exponent etc. */
1625
tmp_length = xsum (tmp_length, precision);
1629
if (type == TYPE_LONGDOUBLE)
1631
(unsigned int) (LDBL_DIG
1632
* 0.831 /* decimal -> hexadecimal */
1634
+ 1; /* turn floor into ceil */
1637
(unsigned int) (DBL_DIG
1638
* 0.831 /* decimal -> hexadecimal */
1640
+ 1; /* turn floor into ceil */
1641
if (tmp_length < precision)
1642
tmp_length = precision;
1643
/* Account for sign, decimal point etc. */
1644
tmp_length = xsum (tmp_length, 12);
1648
# if HAVE_WINT_T && !WIDE_CHAR_VERSION
1649
if (type == TYPE_WIDE_CHAR)
1650
tmp_length = MB_CUR_MAX;
1658
if (type == TYPE_WIDE_STRING)
1660
# if WIDE_CHAR_VERSION
1661
/* ISO C says about %ls in fwprintf:
1662
"If the precision is not specified or is greater than the size
1663
of the array, the array shall contain a null wide character."
1664
So if there is a precision, we must not use wcslen. */
1665
const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
1668
tmp_length = local_wcsnlen (arg, precision);
1670
tmp_length = local_wcslen (arg);
1672
/* ISO C says about %ls in fprintf:
1673
"If a precision is specified, no more than that many bytes are
1674
written (including shift sequences, if any), and the array
1675
shall contain a null wide character if, to equal the multibyte
1676
character sequence length given by the precision, the function
1677
would need to access a wide character one past the end of the
1679
So if there is a precision, we must not use wcslen. */
1680
/* This case has already been handled separately in VASNPRINTF. */
1687
# if WIDE_CHAR_VERSION
1688
/* ISO C says about %s in fwprintf:
1689
"If the precision is not specified or is greater than the size
1690
of the converted array, the converted array shall contain a
1691
null wide character."
1692
So if there is a precision, we must not use strlen. */
1693
/* This case has already been handled separately in VASNPRINTF. */
1696
/* ISO C says about %s in fprintf:
1697
"If the precision is not specified or greater than the size of
1698
the array, the array shall contain a null character."
1699
So if there is a precision, we must not use strlen. */
1700
const char *arg = ap->arg[arg_index].a.a_string;
1703
tmp_length = local_strnlen (arg, precision);
1705
tmp_length = strlen (arg);
1712
(unsigned int) (sizeof (void *) * CHAR_BIT
1713
* 0.25 /* binary -> hexadecimal */
1715
+ 1 /* turn floor into ceil */
1716
+ 2; /* account for leading 0x */
1725
# if ENABLE_UNISTDIO
1726
/* Padding considers the number of characters, therefore the number of
1727
elements after padding may be
1728
> max (tmp_length, width)
1730
<= tmp_length + width. */
1731
tmp_length = xsum (tmp_length, width);
1733
/* Padding considers the number of elements, says POSIX. */
1734
if (tmp_length < width)
1739
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1747
VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1748
const FCHAR_T *format, va_list args)
1753
if (PRINTF_PARSE (format, &d, &a) < 0)
1754
/* errno is already set. */
1758
if (d.dir != d.direct_alloc_dir) \
1760
if (a.arg != a.direct_alloc_arg) \
1763
if (PRINTF_FETCHARGS (args, &a) < 0)
1771
size_t buf_neededlength;
1773
TCHAR_T *buf_malloced;
1777
/* Output string accumulator. */
1782
/* Allocate a small buffer that will hold a directive passed to
1783
sprintf or snprintf. */
1785
xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1787
if (buf_neededlength < 4000 / sizeof (TCHAR_T))
1789
buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
1790
buf_malloced = NULL;
1795
size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
1796
if (size_overflow_p (buf_memsize))
1797
goto out_of_memory_1;
1798
buf = (TCHAR_T *) malloc (buf_memsize);
1800
goto out_of_memory_1;
1804
if (resultbuf != NULL)
1807
allocated = *lengthp;
1816
result is either == resultbuf or == NULL or malloc-allocated.
1817
If length > 0, then result != NULL. */
1819
/* Ensures that allocated >= needed. Aborts through a jump to
1820
out_of_memory if needed is SIZE_MAX or otherwise too big. */
1821
#define ENSURE_ALLOCATION(needed) \
1822
if ((needed) > allocated) \
1824
size_t memory_size; \
1827
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1828
if ((needed) > allocated) \
1829
allocated = (needed); \
1830
memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1831
if (size_overflow_p (memory_size)) \
1832
goto out_of_memory; \
1833
if (result == resultbuf || result == NULL) \
1834
memory = (DCHAR_T *) malloc (memory_size); \
1836
memory = (DCHAR_T *) realloc (result, memory_size); \
1837
if (memory == NULL) \
1838
goto out_of_memory; \
1839
if (result == resultbuf && length > 0) \
1840
DCHAR_CPY (memory, result, length); \
1844
for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1846
if (cp != dp->dir_start)
1848
size_t n = dp->dir_start - cp;
1849
size_t augmented_length = xsum (length, n);
1851
ENSURE_ALLOCATION (augmented_length);
1852
/* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1853
need that the format string contains only ASCII characters
1854
if FCHAR_T and DCHAR_T are not the same type. */
1855
if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1857
DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1858
length = augmented_length;
1863
result[length++] = (unsigned char) *cp++;
1870
/* Execute a single directive. */
1871
if (dp->conversion == '%')
1873
size_t augmented_length;
1875
if (!(dp->arg_index == ARG_NONE))
1877
augmented_length = xsum (length, 1);
1878
ENSURE_ALLOCATION (augmented_length);
1879
result[length] = '%';
1880
length = augmented_length;
1884
if (!(dp->arg_index != ARG_NONE))
1887
if (dp->conversion == 'n')
1889
switch (a.arg[dp->arg_index].type)
1891
case TYPE_COUNT_SCHAR_POINTER:
1892
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1894
case TYPE_COUNT_SHORT_POINTER:
1895
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
1897
case TYPE_COUNT_INT_POINTER:
1898
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
1900
case TYPE_COUNT_LONGINT_POINTER:
1901
*a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1903
#if HAVE_LONG_LONG_INT
1904
case TYPE_COUNT_LONGLONGINT_POINTER:
1905
*a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1913
/* The unistdio extensions. */
1914
else if (dp->conversion == 'U')
1916
arg_type type = a.arg[dp->arg_index].type;
1917
int flags = dp->flags;
1925
if (dp->width_start != dp->width_end)
1927
if (dp->width_arg_index != ARG_NONE)
1931
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1933
arg = a.arg[dp->width_arg_index].a.a_int;
1936
/* "A negative field width is taken as a '-' flag
1937
followed by a positive field width." */
1939
width = (unsigned int) (-arg);
1946
const FCHAR_T *digitp = dp->width_start;
1949
width = xsum (xtimes (width, 10), *digitp++ - '0');
1950
while (digitp != dp->width_end);
1957
if (dp->precision_start != dp->precision_end)
1959
if (dp->precision_arg_index != ARG_NONE)
1963
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1965
arg = a.arg[dp->precision_arg_index].a.a_int;
1966
/* "A negative precision is taken as if the precision
1976
const FCHAR_T *digitp = dp->precision_start + 1;
1979
while (digitp != dp->precision_end)
1980
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1987
case TYPE_U8_STRING:
1989
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1990
const uint8_t *arg_end;
1995
/* Use only PRECISION characters, from the left. */
1998
for (; precision > 0; precision--)
2000
int count = u8_strmblen (arg_end);
2005
if (!(result == resultbuf || result == NULL))
2007
if (buf_malloced != NULL)
2008
free (buf_malloced);
2019
/* Use the entire string, and count the number of
2025
int count = u8_strmblen (arg_end);
2030
if (!(result == resultbuf || result == NULL))
2032
if (buf_malloced != NULL)
2033
free (buf_malloced);
2044
/* Use the entire string. */
2045
arg_end = arg + u8_strlen (arg);
2046
/* The number of characters doesn't matter. */
2050
if (has_width && width > characters
2051
&& !(dp->flags & FLAG_LEFT))
2053
size_t n = width - characters;
2054
ENSURE_ALLOCATION (xsum (length, n));
2055
DCHAR_SET (result + length, ' ', n);
2059
# if DCHAR_IS_UINT8_T
2061
size_t n = arg_end - arg;
2062
ENSURE_ALLOCATION (xsum (length, n));
2063
DCHAR_CPY (result + length, arg, n);
2068
DCHAR_T *converted = result + length;
2069
size_t converted_len = allocated - length;
2071
/* Convert from UTF-8 to locale encoding. */
2073
u8_conv_to_encoding (locale_charset (),
2074
iconveh_question_mark,
2075
arg, arg_end - arg, NULL,
2076
converted, &converted_len);
2078
/* Convert from UTF-8 to UTF-16/UTF-32. */
2080
U8_TO_DCHAR (arg, arg_end - arg,
2081
converted, &converted_len);
2083
if (converted == NULL)
2085
int saved_errno = errno;
2086
if (!(result == resultbuf || result == NULL))
2088
if (buf_malloced != NULL)
2089
free (buf_malloced);
2091
errno = saved_errno;
2094
if (converted != result + length)
2096
ENSURE_ALLOCATION (xsum (length, converted_len));
2097
DCHAR_CPY (result + length, converted, converted_len);
2100
length += converted_len;
2104
if (has_width && width > characters
2105
&& (dp->flags & FLAG_LEFT))
2107
size_t n = width - characters;
2108
ENSURE_ALLOCATION (xsum (length, n));
2109
DCHAR_SET (result + length, ' ', n);
2115
case TYPE_U16_STRING:
2117
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
2118
const uint16_t *arg_end;
2123
/* Use only PRECISION characters, from the left. */
2126
for (; precision > 0; precision--)
2128
int count = u16_strmblen (arg_end);
2133
if (!(result == resultbuf || result == NULL))
2135
if (buf_malloced != NULL)
2136
free (buf_malloced);
2147
/* Use the entire string, and count the number of
2153
int count = u16_strmblen (arg_end);
2158
if (!(result == resultbuf || result == NULL))
2160
if (buf_malloced != NULL)
2161
free (buf_malloced);
2172
/* Use the entire string. */
2173
arg_end = arg + u16_strlen (arg);
2174
/* The number of characters doesn't matter. */
2178
if (has_width && width > characters
2179
&& !(dp->flags & FLAG_LEFT))
2181
size_t n = width - characters;
2182
ENSURE_ALLOCATION (xsum (length, n));
2183
DCHAR_SET (result + length, ' ', n);
2187
# if DCHAR_IS_UINT16_T
2189
size_t n = arg_end - arg;
2190
ENSURE_ALLOCATION (xsum (length, n));
2191
DCHAR_CPY (result + length, arg, n);
2196
DCHAR_T *converted = result + length;
2197
size_t converted_len = allocated - length;
2199
/* Convert from UTF-16 to locale encoding. */
2201
u16_conv_to_encoding (locale_charset (),
2202
iconveh_question_mark,
2203
arg, arg_end - arg, NULL,
2204
converted, &converted_len);
2206
/* Convert from UTF-16 to UTF-8/UTF-32. */
2208
U16_TO_DCHAR (arg, arg_end - arg,
2209
converted, &converted_len);
2211
if (converted == NULL)
2213
int saved_errno = errno;
2214
if (!(result == resultbuf || result == NULL))
2216
if (buf_malloced != NULL)
2217
free (buf_malloced);
2219
errno = saved_errno;
2222
if (converted != result + length)
2224
ENSURE_ALLOCATION (xsum (length, converted_len));
2225
DCHAR_CPY (result + length, converted, converted_len);
2228
length += converted_len;
2232
if (has_width && width > characters
2233
&& (dp->flags & FLAG_LEFT))
2235
size_t n = width - characters;
2236
ENSURE_ALLOCATION (xsum (length, n));
2237
DCHAR_SET (result + length, ' ', n);
2243
case TYPE_U32_STRING:
2245
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
2246
const uint32_t *arg_end;
2251
/* Use only PRECISION characters, from the left. */
2254
for (; precision > 0; precision--)
2256
int count = u32_strmblen (arg_end);
2261
if (!(result == resultbuf || result == NULL))
2263
if (buf_malloced != NULL)
2264
free (buf_malloced);
2275
/* Use the entire string, and count the number of
2281
int count = u32_strmblen (arg_end);
2286
if (!(result == resultbuf || result == NULL))
2288
if (buf_malloced != NULL)
2289
free (buf_malloced);
2300
/* Use the entire string. */
2301
arg_end = arg + u32_strlen (arg);
2302
/* The number of characters doesn't matter. */
2306
if (has_width && width > characters
2307
&& !(dp->flags & FLAG_LEFT))
2309
size_t n = width - characters;
2310
ENSURE_ALLOCATION (xsum (length, n));
2311
DCHAR_SET (result + length, ' ', n);
2315
# if DCHAR_IS_UINT32_T
2317
size_t n = arg_end - arg;
2318
ENSURE_ALLOCATION (xsum (length, n));
2319
DCHAR_CPY (result + length, arg, n);
2324
DCHAR_T *converted = result + length;
2325
size_t converted_len = allocated - length;
2327
/* Convert from UTF-32 to locale encoding. */
2329
u32_conv_to_encoding (locale_charset (),
2330
iconveh_question_mark,
2331
arg, arg_end - arg, NULL,
2332
converted, &converted_len);
2334
/* Convert from UTF-32 to UTF-8/UTF-16. */
2336
U32_TO_DCHAR (arg, arg_end - arg,
2337
converted, &converted_len);
2339
if (converted == NULL)
2341
int saved_errno = errno;
2342
if (!(result == resultbuf || result == NULL))
2344
if (buf_malloced != NULL)
2345
free (buf_malloced);
2347
errno = saved_errno;
2350
if (converted != result + length)
2352
ENSURE_ALLOCATION (xsum (length, converted_len));
2353
DCHAR_CPY (result + length, converted, converted_len);
2356
length += converted_len;
2360
if (has_width && width > characters
2361
&& (dp->flags & FLAG_LEFT))
2363
size_t n = width - characters;
2364
ENSURE_ALLOCATION (xsum (length, n));
2365
DCHAR_SET (result + length, ' ', n);
2376
#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2377
else if (dp->conversion == 's'
2378
# if WIDE_CHAR_VERSION
2379
&& a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2381
&& a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2385
/* The normal handling of the 's' directive below requires
2386
allocating a temporary buffer. The determination of its
2387
length (tmp_length), in the case when a precision is
2388
specified, below requires a conversion between a char[]
2389
string and a wchar_t[] wide string. It could be done, but
2390
we have no guarantee that the implementation of sprintf will
2391
use the exactly same algorithm. Without this guarantee, it
2392
is possible to have buffer overrun bugs. In order to avoid
2393
such bugs, we implement the entire processing of the 's'
2394
directive ourselves. */
2395
int flags = dp->flags;
2403
if (dp->width_start != dp->width_end)
2405
if (dp->width_arg_index != ARG_NONE)
2409
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2411
arg = a.arg[dp->width_arg_index].a.a_int;
2414
/* "A negative field width is taken as a '-' flag
2415
followed by a positive field width." */
2417
width = (unsigned int) (-arg);
2424
const FCHAR_T *digitp = dp->width_start;
2427
width = xsum (xtimes (width, 10), *digitp++ - '0');
2428
while (digitp != dp->width_end);
2435
if (dp->precision_start != dp->precision_end)
2437
if (dp->precision_arg_index != ARG_NONE)
2441
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2443
arg = a.arg[dp->precision_arg_index].a.a_int;
2444
/* "A negative precision is taken as if the precision
2454
const FCHAR_T *digitp = dp->precision_start + 1;
2457
while (digitp != dp->precision_end)
2458
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2463
# if WIDE_CHAR_VERSION
2464
/* %s in vasnwprintf. See the specification of fwprintf. */
2466
const char *arg = a.arg[dp->arg_index].a.a_string;
2467
const char *arg_end;
2472
/* Use only as many bytes as needed to produce PRECISION
2473
wide characters, from the left. */
2476
memset (&state, '\0', sizeof (mbstate_t));
2480
for (; precision > 0; precision--)
2484
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2486
count = mblen (arg_end, MB_CUR_MAX);
2489
/* Found the terminating NUL. */
2493
/* Invalid or incomplete multibyte character. */
2494
if (!(result == resultbuf || result == NULL))
2496
if (buf_malloced != NULL)
2497
free (buf_malloced);
2508
/* Use the entire string, and count the number of wide
2512
memset (&state, '\0', sizeof (mbstate_t));
2520
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2522
count = mblen (arg_end, MB_CUR_MAX);
2525
/* Found the terminating NUL. */
2529
/* Invalid or incomplete multibyte character. */
2530
if (!(result == resultbuf || result == NULL))
2532
if (buf_malloced != NULL)
2533
free (buf_malloced);
2544
/* Use the entire string. */
2545
arg_end = arg + strlen (arg);
2546
/* The number of characters doesn't matter. */
2550
if (has_width && width > characters
2551
&& !(dp->flags & FLAG_LEFT))
2553
size_t n = width - characters;
2554
ENSURE_ALLOCATION (xsum (length, n));
2555
DCHAR_SET (result + length, ' ', n);
2559
if (has_precision || has_width)
2561
/* We know the number of wide characters in advance. */
2565
memset (&state, '\0', sizeof (mbstate_t));
2567
ENSURE_ALLOCATION (xsum (length, characters));
2568
for (remaining = characters; remaining > 0; remaining--)
2573
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2575
count = mbtowc (&wc, arg, arg_end - arg);
2578
/* mbrtowc not consistent with mbrlen, or mbtowc
2579
not consistent with mblen. */
2581
result[length++] = wc;
2584
if (!(arg == arg_end))
2591
memset (&state, '\0', sizeof (mbstate_t));
2593
while (arg < arg_end)
2598
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2600
count = mbtowc (&wc, arg, arg_end - arg);
2603
/* mbrtowc not consistent with mbrlen, or mbtowc
2604
not consistent with mblen. */
2606
ENSURE_ALLOCATION (xsum (length, 1));
2607
result[length++] = wc;
2612
if (has_width && width > characters
2613
&& (dp->flags & FLAG_LEFT))
2615
size_t n = width - characters;
2616
ENSURE_ALLOCATION (xsum (length, n));
2617
DCHAR_SET (result + length, ' ', n);
2622
/* %ls in vasnprintf. See the specification of fprintf. */
2624
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
2625
const wchar_t *arg_end;
2627
# if !DCHAR_IS_TCHAR
2628
/* This code assumes that TCHAR_T is 'char'. */
2629
verify (sizeof (TCHAR_T) == 1);
2638
/* Use only as many wide characters as needed to produce
2639
at most PRECISION bytes, from the left. */
2640
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2642
memset (&state, '\0', sizeof (mbstate_t));
2646
while (precision > 0)
2648
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2652
/* Found the terminating null wide character. */
2654
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2655
count = wcrtomb (cbuf, *arg_end, &state);
2657
count = wctomb (cbuf, *arg_end);
2661
/* Cannot convert. */
2662
if (!(result == resultbuf || result == NULL))
2664
if (buf_malloced != NULL)
2665
free (buf_malloced);
2670
if (precision < count)
2673
characters += count;
2683
/* Use the entire string, and count the number of
2685
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2687
memset (&state, '\0', sizeof (mbstate_t));
2693
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2697
/* Found the terminating null wide character. */
2699
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2700
count = wcrtomb (cbuf, *arg_end, &state);
2702
count = wctomb (cbuf, *arg_end);
2706
/* Cannot convert. */
2707
if (!(result == resultbuf || result == NULL))
2709
if (buf_malloced != NULL)
2710
free (buf_malloced);
2716
characters += count;
2722
/* Use the entire string. */
2723
arg_end = arg + local_wcslen (arg);
2724
/* The number of bytes doesn't matter. */
2729
# if !DCHAR_IS_TCHAR
2730
/* Convert the string into a piece of temporary memory. */
2731
tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
2735
TCHAR_T *tmpptr = tmpsrc;
2737
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2739
memset (&state, '\0', sizeof (mbstate_t));
2741
for (remaining = characters; remaining > 0; )
2743
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2748
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2749
count = wcrtomb (cbuf, *arg, &state);
2751
count = wctomb (cbuf, *arg);
2754
/* Inconsistency. */
2756
memcpy (tmpptr, cbuf, count);
2761
if (!(arg == arg_end))
2765
/* Convert from TCHAR_T[] to DCHAR_T[]. */
2767
DCHAR_CONV_FROM_ENCODING (locale_charset (),
2768
iconveh_question_mark,
2774
int saved_errno = errno;
2776
if (!(result == resultbuf || result == NULL))
2778
if (buf_malloced != NULL)
2779
free (buf_malloced);
2781
errno = saved_errno;
2789
# if ENABLE_UNISTDIO
2790
/* Outside POSIX, it's preferrable to compare the width
2791
against the number of _characters_ of the converted
2793
w = DCHAR_MBSNLEN (result + length, characters);
2795
/* The width is compared against the number of _bytes_
2796
of the converted value, says POSIX. */
2801
/* w doesn't matter. */
2804
if (has_width && width > w
2805
&& !(dp->flags & FLAG_LEFT))
2807
size_t n = width - w;
2808
ENSURE_ALLOCATION (xsum (length, n));
2809
DCHAR_SET (result + length, ' ', n);
2814
if (has_precision || has_width)
2816
/* We know the number of bytes in advance. */
2818
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2820
memset (&state, '\0', sizeof (mbstate_t));
2822
ENSURE_ALLOCATION (xsum (length, characters));
2823
for (remaining = characters; remaining > 0; )
2825
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2830
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2831
count = wcrtomb (cbuf, *arg, &state);
2833
count = wctomb (cbuf, *arg);
2836
/* Inconsistency. */
2838
memcpy (result + length, cbuf, count);
2843
if (!(arg == arg_end))
2848
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2850
memset (&state, '\0', sizeof (mbstate_t));
2852
while (arg < arg_end)
2854
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2859
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2860
count = wcrtomb (cbuf, *arg, &state);
2862
count = wctomb (cbuf, *arg);
2866
/* Cannot convert. */
2867
if (!(result == resultbuf || result == NULL))
2869
if (buf_malloced != NULL)
2870
free (buf_malloced);
2875
ENSURE_ALLOCATION (xsum (length, count));
2876
memcpy (result + length, cbuf, count);
2882
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2883
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2885
length += tmpdst_len;
2888
if (has_width && width > w
2889
&& (dp->flags & FLAG_LEFT))
2891
size_t n = width - w;
2892
ENSURE_ALLOCATION (xsum (length, n));
2893
DCHAR_SET (result + length, ' ', n);
2900
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2901
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2902
# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2904
# if NEED_PRINTF_DOUBLE
2905
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2907
# if NEED_PRINTF_LONG_DOUBLE
2908
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2914
arg_type type = a.arg[dp->arg_index].type;
2915
int flags = dp->flags;
2921
DCHAR_T tmpbuf[700];
2928
if (dp->width_start != dp->width_end)
2930
if (dp->width_arg_index != ARG_NONE)
2934
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2936
arg = a.arg[dp->width_arg_index].a.a_int;
2939
/* "A negative field width is taken as a '-' flag
2940
followed by a positive field width." */
2942
width = (unsigned int) (-arg);
2949
const FCHAR_T *digitp = dp->width_start;
2952
width = xsum (xtimes (width, 10), *digitp++ - '0');
2953
while (digitp != dp->width_end);
2960
if (dp->precision_start != dp->precision_end)
2962
if (dp->precision_arg_index != ARG_NONE)
2966
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2968
arg = a.arg[dp->precision_arg_index].a.a_int;
2969
/* "A negative precision is taken as if the precision
2979
const FCHAR_T *digitp = dp->precision_start + 1;
2982
while (digitp != dp->precision_end)
2983
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2988
/* Allocate a temporary buffer of sufficient size. */
2989
if (type == TYPE_LONGDOUBLE)
2991
(unsigned int) ((LDBL_DIG + 1)
2992
* 0.831 /* decimal -> hexadecimal */
2994
+ 1; /* turn floor into ceil */
2997
(unsigned int) ((DBL_DIG + 1)
2998
* 0.831 /* decimal -> hexadecimal */
3000
+ 1; /* turn floor into ceil */
3001
if (tmp_length < precision)
3002
tmp_length = precision;
3003
/* Account for sign, decimal point etc. */
3004
tmp_length = xsum (tmp_length, 12);
3006
if (tmp_length < width)
3009
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3011
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3015
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3017
if (size_overflow_p (tmp_memsize))
3018
/* Overflow, would lead to out of memory. */
3020
tmp = (DCHAR_T *) malloc (tmp_memsize);
3022
/* Out of memory. */
3028
if (type == TYPE_LONGDOUBLE)
3030
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3031
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3035
if (dp->conversion == 'A')
3037
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3041
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3047
DECL_LONG_DOUBLE_ROUNDING
3049
BEGIN_LONG_DOUBLE_ROUNDING ();
3051
if (signbit (arg)) /* arg < 0.0L or negative zero */
3059
else if (flags & FLAG_SHOWSIGN)
3061
else if (flags & FLAG_SPACE)
3064
if (arg > 0.0L && arg + arg == arg)
3066
if (dp->conversion == 'A')
3068
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3072
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3078
long double mantissa;
3081
mantissa = printf_frexpl (arg, &exponent);
3089
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
3091
/* Round the mantissa. */
3092
long double tail = mantissa;
3095
for (q = precision; ; q--)
3097
int digit = (int) tail;
3101
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
3110
for (q = precision; q > 0; q--)
3116
*p++ = dp->conversion - 'A' + 'X';
3121
digit = (int) mantissa;
3124
if ((flags & FLAG_ALT)
3125
|| mantissa > 0.0L || precision > 0)
3127
*p++ = decimal_point_char ();
3128
/* This loop terminates because we assume
3129
that FLT_RADIX is a power of 2. */
3130
while (mantissa > 0.0L)
3133
digit = (int) mantissa;
3138
: dp->conversion - 10);
3142
while (precision > 0)
3149
*p++ = dp->conversion - 'A' + 'P';
3150
# if WIDE_CHAR_VERSION
3152
static const wchar_t decimal_format[] =
3153
{ '%', '+', 'd', '\0' };
3154
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3159
if (sizeof (DCHAR_T) == 1)
3161
sprintf ((char *) p, "%+d", exponent);
3169
sprintf (expbuf, "%+d", exponent);
3170
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3176
END_LONG_DOUBLE_ROUNDING ();
3184
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3185
double arg = a.arg[dp->arg_index].a.a_double;
3189
if (dp->conversion == 'A')
3191
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3195
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3202
if (signbit (arg)) /* arg < 0.0 or negative zero */
3210
else if (flags & FLAG_SHOWSIGN)
3212
else if (flags & FLAG_SPACE)
3215
if (arg > 0.0 && arg + arg == arg)
3217
if (dp->conversion == 'A')
3219
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3223
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3232
mantissa = printf_frexp (arg, &exponent);
3240
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
3242
/* Round the mantissa. */
3243
double tail = mantissa;
3246
for (q = precision; ; q--)
3248
int digit = (int) tail;
3252
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
3261
for (q = precision; q > 0; q--)
3267
*p++ = dp->conversion - 'A' + 'X';
3272
digit = (int) mantissa;
3275
if ((flags & FLAG_ALT)
3276
|| mantissa > 0.0 || precision > 0)
3278
*p++ = decimal_point_char ();
3279
/* This loop terminates because we assume
3280
that FLT_RADIX is a power of 2. */
3281
while (mantissa > 0.0)
3284
digit = (int) mantissa;
3289
: dp->conversion - 10);
3293
while (precision > 0)
3300
*p++ = dp->conversion - 'A' + 'P';
3301
# if WIDE_CHAR_VERSION
3303
static const wchar_t decimal_format[] =
3304
{ '%', '+', 'd', '\0' };
3305
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3310
if (sizeof (DCHAR_T) == 1)
3312
sprintf ((char *) p, "%+d", exponent);
3320
sprintf (expbuf, "%+d", exponent);
3321
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3331
/* The generated string now extends from tmp to p, with the
3332
zero padding insertion point being at pad_ptr. */
3333
if (has_width && p - tmp < width)
3335
size_t pad = width - (p - tmp);
3336
DCHAR_T *end = p + pad;
3338
if (flags & FLAG_LEFT)
3340
/* Pad with spaces on the right. */
3341
for (; pad > 0; pad--)
3344
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3346
/* Pad with zeroes. */
3351
for (; pad > 0; pad--)
3356
/* Pad with spaces on the left. */
3361
for (; pad > 0; pad--)
3369
size_t count = p - tmp;
3371
if (count >= tmp_length)
3372
/* tmp_length was incorrectly calculated - fix the
3376
/* Make room for the result. */
3377
if (count >= allocated - length)
3379
size_t n = xsum (length, count);
3381
ENSURE_ALLOCATION (n);
3384
/* Append the result. */
3385
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3392
#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3393
else if ((dp->conversion == 'f' || dp->conversion == 'F'
3394
|| dp->conversion == 'e' || dp->conversion == 'E'
3395
|| dp->conversion == 'g' || dp->conversion == 'G'
3396
|| dp->conversion == 'a' || dp->conversion == 'A')
3398
# if NEED_PRINTF_DOUBLE
3399
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
3400
# elif NEED_PRINTF_INFINITE_DOUBLE
3401
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
3402
/* The systems (mingw) which produce wrong output
3403
for Inf, -Inf, and NaN also do so for -0.0.
3404
Therefore we treat this case here as well. */
3405
&& is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
3407
# if NEED_PRINTF_LONG_DOUBLE
3408
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3409
# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3410
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3411
/* Some systems produce wrong output for Inf,
3412
-Inf, and NaN. Some systems in this category
3413
(IRIX 5.3) also do so for -0.0. Therefore we
3414
treat this case here as well. */
3415
&& is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
3419
# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3420
arg_type type = a.arg[dp->arg_index].type;
3422
int flags = dp->flags;
3428
DCHAR_T tmpbuf[700];
3435
if (dp->width_start != dp->width_end)
3437
if (dp->width_arg_index != ARG_NONE)
3441
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3443
arg = a.arg[dp->width_arg_index].a.a_int;
3446
/* "A negative field width is taken as a '-' flag
3447
followed by a positive field width." */
3449
width = (unsigned int) (-arg);
3456
const FCHAR_T *digitp = dp->width_start;
3459
width = xsum (xtimes (width, 10), *digitp++ - '0');
3460
while (digitp != dp->width_end);
3467
if (dp->precision_start != dp->precision_end)
3469
if (dp->precision_arg_index != ARG_NONE)
3473
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3475
arg = a.arg[dp->precision_arg_index].a.a_int;
3476
/* "A negative precision is taken as if the precision
3486
const FCHAR_T *digitp = dp->precision_start + 1;
3489
while (digitp != dp->precision_end)
3490
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3495
/* POSIX specifies the default precision to be 6 for %f, %F,
3496
%e, %E, but not for %g, %G. Implementations appear to use
3497
the same default precision also for %g, %G. But for %a, %A,
3498
the default precision is 0. */
3500
if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3503
/* Allocate a temporary buffer of sufficient size. */
3504
# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3505
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3506
# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3507
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3508
# elif NEED_PRINTF_LONG_DOUBLE
3509
tmp_length = LDBL_DIG + 1;
3510
# elif NEED_PRINTF_DOUBLE
3511
tmp_length = DBL_DIG + 1;
3515
if (tmp_length < precision)
3516
tmp_length = precision;
3517
# if NEED_PRINTF_LONG_DOUBLE
3518
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3519
if (type == TYPE_LONGDOUBLE)
3521
if (dp->conversion == 'f' || dp->conversion == 'F')
3523
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3524
if (!(isnanl (arg) || arg + arg == arg))
3526
/* arg is finite and nonzero. */
3527
int exponent = floorlog10l (arg < 0 ? -arg : arg);
3528
if (exponent >= 0 && tmp_length < exponent + precision)
3529
tmp_length = exponent + precision;
3533
# if NEED_PRINTF_DOUBLE
3534
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3535
if (type == TYPE_DOUBLE)
3537
if (dp->conversion == 'f' || dp->conversion == 'F')
3539
double arg = a.arg[dp->arg_index].a.a_double;
3540
if (!(isnand (arg) || arg + arg == arg))
3542
/* arg is finite and nonzero. */
3543
int exponent = floorlog10 (arg < 0 ? -arg : arg);
3544
if (exponent >= 0 && tmp_length < exponent + precision)
3545
tmp_length = exponent + precision;
3549
/* Account for sign, decimal point etc. */
3550
tmp_length = xsum (tmp_length, 12);
3552
if (tmp_length < width)
3555
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3557
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3561
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3563
if (size_overflow_p (tmp_memsize))
3564
/* Overflow, would lead to out of memory. */
3566
tmp = (DCHAR_T *) malloc (tmp_memsize);
3568
/* Out of memory. */
3575
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3576
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3577
if (type == TYPE_LONGDOUBLE)
3580
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3584
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3586
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3590
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3596
DECL_LONG_DOUBLE_ROUNDING
3598
BEGIN_LONG_DOUBLE_ROUNDING ();
3600
if (signbit (arg)) /* arg < 0.0L or negative zero */
3608
else if (flags & FLAG_SHOWSIGN)
3610
else if (flags & FLAG_SPACE)
3613
if (arg > 0.0L && arg + arg == arg)
3615
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3617
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3621
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3626
# if NEED_PRINTF_LONG_DOUBLE
3629
if (dp->conversion == 'f' || dp->conversion == 'F')
3635
scale10_round_decimal_long_double (arg, precision);
3638
END_LONG_DOUBLE_ROUNDING ();
3641
ndigits = strlen (digits);
3643
if (ndigits > precision)
3647
*p++ = digits[ndigits];
3649
while (ndigits > precision);
3652
/* Here ndigits <= precision. */
3653
if ((flags & FLAG_ALT) || precision > 0)
3655
*p++ = decimal_point_char ();
3656
for (; precision > ndigits; precision--)
3661
*p++ = digits[ndigits];
3667
else if (dp->conversion == 'e' || dp->conversion == 'E')
3675
if ((flags & FLAG_ALT) || precision > 0)
3677
*p++ = decimal_point_char ();
3678
for (; precision > 0; precision--)
3689
exponent = floorlog10l (arg);
3694
scale10_round_decimal_long_double (arg,
3695
(int)precision - exponent);
3698
END_LONG_DOUBLE_ROUNDING ();
3701
ndigits = strlen (digits);
3703
if (ndigits == precision + 1)
3705
if (ndigits < precision
3706
|| ndigits > precision + 2)
3707
/* The exponent was not guessed
3708
precisely enough. */
3711
/* None of two values of exponent is
3712
the right one. Prevent an endless
3716
if (ndigits == precision)
3722
/* Here ndigits = precision+1. */
3723
if (is_borderline (digits, precision))
3725
/* Maybe the exponent guess was too high
3726
and a smaller exponent can be reached
3727
by turning a 10...0 into 9...9x. */
3729
scale10_round_decimal_long_double (arg,
3730
(int)precision - exponent + 1);
3731
if (digits2 == NULL)
3734
END_LONG_DOUBLE_ROUNDING ();
3737
if (strlen (digits2) == precision + 1)
3746
/* Here ndigits = precision+1. */
3748
*p++ = digits[--ndigits];
3749
if ((flags & FLAG_ALT) || precision > 0)
3751
*p++ = decimal_point_char ();
3755
*p++ = digits[ndigits];
3762
*p++ = dp->conversion; /* 'e' or 'E' */
3763
# if WIDE_CHAR_VERSION
3765
static const wchar_t decimal_format[] =
3766
{ '%', '+', '.', '2', 'd', '\0' };
3767
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3772
if (sizeof (DCHAR_T) == 1)
3774
sprintf ((char *) p, "%+.2d", exponent);
3782
sprintf (expbuf, "%+.2d", exponent);
3783
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3788
else if (dp->conversion == 'g' || dp->conversion == 'G')
3792
/* precision >= 1. */
3795
/* The exponent is 0, >= -4, < precision.
3796
Use fixed-point notation. */
3798
size_t ndigits = precision;
3799
/* Number of trailing zeroes that have to be
3802
(flags & FLAG_ALT ? 0 : precision - 1);
3806
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3808
*p++ = decimal_point_char ();
3809
while (ndigits > nzeroes)
3825
exponent = floorlog10l (arg);
3830
scale10_round_decimal_long_double (arg,
3831
(int)(precision - 1) - exponent);
3834
END_LONG_DOUBLE_ROUNDING ();
3837
ndigits = strlen (digits);
3839
if (ndigits == precision)
3841
if (ndigits < precision - 1
3842
|| ndigits > precision + 1)
3843
/* The exponent was not guessed
3844
precisely enough. */
3847
/* None of two values of exponent is
3848
the right one. Prevent an endless
3852
if (ndigits < precision)
3858
/* Here ndigits = precision. */
3859
if (is_borderline (digits, precision - 1))
3861
/* Maybe the exponent guess was too high
3862
and a smaller exponent can be reached
3863
by turning a 10...0 into 9...9x. */
3865
scale10_round_decimal_long_double (arg,
3866
(int)(precision - 1) - exponent + 1);
3867
if (digits2 == NULL)
3870
END_LONG_DOUBLE_ROUNDING ();
3873
if (strlen (digits2) == precision)
3882
/* Here ndigits = precision. */
3884
/* Determine the number of trailing zeroes
3885
that have to be dropped. */
3887
if ((flags & FLAG_ALT) == 0)
3888
while (nzeroes < ndigits
3889
&& digits[nzeroes] == '0')
3892
/* The exponent is now determined. */
3894
&& exponent < (long)precision)
3896
/* Fixed-point notation:
3897
max(exponent,0)+1 digits, then the
3898
decimal point, then the remaining
3899
digits without trailing zeroes. */
3902
size_t count = exponent + 1;
3903
/* Note: count <= precision = ndigits. */
3904
for (; count > 0; count--)
3905
*p++ = digits[--ndigits];
3906
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3908
*p++ = decimal_point_char ();
3909
while (ndigits > nzeroes)
3912
*p++ = digits[ndigits];
3918
size_t count = -exponent - 1;
3920
*p++ = decimal_point_char ();
3921
for (; count > 0; count--)
3923
while (ndigits > nzeroes)
3926
*p++ = digits[ndigits];
3932
/* Exponential notation. */
3933
*p++ = digits[--ndigits];
3934
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3936
*p++ = decimal_point_char ();
3937
while (ndigits > nzeroes)
3940
*p++ = digits[ndigits];
3943
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3944
# if WIDE_CHAR_VERSION
3946
static const wchar_t decimal_format[] =
3947
{ '%', '+', '.', '2', 'd', '\0' };
3948
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3953
if (sizeof (DCHAR_T) == 1)
3955
sprintf ((char *) p, "%+.2d", exponent);
3963
sprintf (expbuf, "%+.2d", exponent);
3964
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3976
/* arg is finite. */
3982
if (dp->conversion == 'f' || dp->conversion == 'F')
3985
if ((flags & FLAG_ALT) || precision > 0)
3987
*p++ = decimal_point_char ();
3988
for (; precision > 0; precision--)
3992
else if (dp->conversion == 'e' || dp->conversion == 'E')
3995
if ((flags & FLAG_ALT) || precision > 0)
3997
*p++ = decimal_point_char ();
3998
for (; precision > 0; precision--)
4001
*p++ = dp->conversion; /* 'e' or 'E' */
4006
else if (dp->conversion == 'g' || dp->conversion == 'G')
4009
if (flags & FLAG_ALT)
4012
(precision > 0 ? precision - 1 : 0);
4013
*p++ = decimal_point_char ();
4014
for (; ndigits > 0; --ndigits)
4018
else if (dp->conversion == 'a' || dp->conversion == 'A')
4021
*p++ = dp->conversion - 'A' + 'X';
4024
if ((flags & FLAG_ALT) || precision > 0)
4026
*p++ = decimal_point_char ();
4027
for (; precision > 0; precision--)
4030
*p++ = dp->conversion - 'A' + 'P';
4039
END_LONG_DOUBLE_ROUNDING ();
4042
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4046
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
4048
double arg = a.arg[dp->arg_index].a.a_double;
4052
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4054
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
4058
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
4065
if (signbit (arg)) /* arg < 0.0 or negative zero */
4073
else if (flags & FLAG_SHOWSIGN)
4075
else if (flags & FLAG_SPACE)
4078
if (arg > 0.0 && arg + arg == arg)
4080
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4082
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
4086
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
4091
# if NEED_PRINTF_DOUBLE
4094
if (dp->conversion == 'f' || dp->conversion == 'F')
4100
scale10_round_decimal_double (arg, precision);
4103
ndigits = strlen (digits);
4105
if (ndigits > precision)
4109
*p++ = digits[ndigits];
4111
while (ndigits > precision);
4114
/* Here ndigits <= precision. */
4115
if ((flags & FLAG_ALT) || precision > 0)
4117
*p++ = decimal_point_char ();
4118
for (; precision > ndigits; precision--)
4123
*p++ = digits[ndigits];
4129
else if (dp->conversion == 'e' || dp->conversion == 'E')
4137
if ((flags & FLAG_ALT) || precision > 0)
4139
*p++ = decimal_point_char ();
4140
for (; precision > 0; precision--)
4151
exponent = floorlog10 (arg);
4156
scale10_round_decimal_double (arg,
4157
(int)precision - exponent);
4160
ndigits = strlen (digits);
4162
if (ndigits == precision + 1)
4164
if (ndigits < precision
4165
|| ndigits > precision + 2)
4166
/* The exponent was not guessed
4167
precisely enough. */
4170
/* None of two values of exponent is
4171
the right one. Prevent an endless
4175
if (ndigits == precision)
4181
/* Here ndigits = precision+1. */
4182
if (is_borderline (digits, precision))
4184
/* Maybe the exponent guess was too high
4185
and a smaller exponent can be reached
4186
by turning a 10...0 into 9...9x. */
4188
scale10_round_decimal_double (arg,
4189
(int)precision - exponent + 1);
4190
if (digits2 == NULL)
4195
if (strlen (digits2) == precision + 1)
4204
/* Here ndigits = precision+1. */
4206
*p++ = digits[--ndigits];
4207
if ((flags & FLAG_ALT) || precision > 0)
4209
*p++ = decimal_point_char ();
4213
*p++ = digits[ndigits];
4220
*p++ = dp->conversion; /* 'e' or 'E' */
4221
# if WIDE_CHAR_VERSION
4223
static const wchar_t decimal_format[] =
4224
/* Produce the same number of exponent digits
4225
as the native printf implementation. */
4226
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4227
{ '%', '+', '.', '3', 'd', '\0' };
4229
{ '%', '+', '.', '2', 'd', '\0' };
4231
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4237
static const char decimal_format[] =
4238
/* Produce the same number of exponent digits
4239
as the native printf implementation. */
4240
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4245
if (sizeof (DCHAR_T) == 1)
4247
sprintf ((char *) p, decimal_format, exponent);
4255
sprintf (expbuf, decimal_format, exponent);
4256
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4262
else if (dp->conversion == 'g' || dp->conversion == 'G')
4266
/* precision >= 1. */
4269
/* The exponent is 0, >= -4, < precision.
4270
Use fixed-point notation. */
4272
size_t ndigits = precision;
4273
/* Number of trailing zeroes that have to be
4276
(flags & FLAG_ALT ? 0 : precision - 1);
4280
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4282
*p++ = decimal_point_char ();
4283
while (ndigits > nzeroes)
4299
exponent = floorlog10 (arg);
4304
scale10_round_decimal_double (arg,
4305
(int)(precision - 1) - exponent);
4308
ndigits = strlen (digits);
4310
if (ndigits == precision)
4312
if (ndigits < precision - 1
4313
|| ndigits > precision + 1)
4314
/* The exponent was not guessed
4315
precisely enough. */
4318
/* None of two values of exponent is
4319
the right one. Prevent an endless
4323
if (ndigits < precision)
4329
/* Here ndigits = precision. */
4330
if (is_borderline (digits, precision - 1))
4332
/* Maybe the exponent guess was too high
4333
and a smaller exponent can be reached
4334
by turning a 10...0 into 9...9x. */
4336
scale10_round_decimal_double (arg,
4337
(int)(precision - 1) - exponent + 1);
4338
if (digits2 == NULL)
4343
if (strlen (digits2) == precision)
4352
/* Here ndigits = precision. */
4354
/* Determine the number of trailing zeroes
4355
that have to be dropped. */
4357
if ((flags & FLAG_ALT) == 0)
4358
while (nzeroes < ndigits
4359
&& digits[nzeroes] == '0')
4362
/* The exponent is now determined. */
4364
&& exponent < (long)precision)
4366
/* Fixed-point notation:
4367
max(exponent,0)+1 digits, then the
4368
decimal point, then the remaining
4369
digits without trailing zeroes. */
4372
size_t count = exponent + 1;
4373
/* Note: count <= precision = ndigits. */
4374
for (; count > 0; count--)
4375
*p++ = digits[--ndigits];
4376
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4378
*p++ = decimal_point_char ();
4379
while (ndigits > nzeroes)
4382
*p++ = digits[ndigits];
4388
size_t count = -exponent - 1;
4390
*p++ = decimal_point_char ();
4391
for (; count > 0; count--)
4393
while (ndigits > nzeroes)
4396
*p++ = digits[ndigits];
4402
/* Exponential notation. */
4403
*p++ = digits[--ndigits];
4404
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4406
*p++ = decimal_point_char ();
4407
while (ndigits > nzeroes)
4410
*p++ = digits[ndigits];
4413
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
4414
# if WIDE_CHAR_VERSION
4416
static const wchar_t decimal_format[] =
4417
/* Produce the same number of exponent digits
4418
as the native printf implementation. */
4419
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4420
{ '%', '+', '.', '3', 'd', '\0' };
4422
{ '%', '+', '.', '2', 'd', '\0' };
4424
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4430
static const char decimal_format[] =
4431
/* Produce the same number of exponent digits
4432
as the native printf implementation. */
4433
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4438
if (sizeof (DCHAR_T) == 1)
4440
sprintf ((char *) p, decimal_format, exponent);
4448
sprintf (expbuf, decimal_format, exponent);
4449
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4462
/* arg is finite. */
4468
if (dp->conversion == 'f' || dp->conversion == 'F')
4471
if ((flags & FLAG_ALT) || precision > 0)
4473
*p++ = decimal_point_char ();
4474
for (; precision > 0; precision--)
4478
else if (dp->conversion == 'e' || dp->conversion == 'E')
4481
if ((flags & FLAG_ALT) || precision > 0)
4483
*p++ = decimal_point_char ();
4484
for (; precision > 0; precision--)
4487
*p++ = dp->conversion; /* 'e' or 'E' */
4489
/* Produce the same number of exponent digits as
4490
the native printf implementation. */
4491
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4497
else if (dp->conversion == 'g' || dp->conversion == 'G')
4500
if (flags & FLAG_ALT)
4503
(precision > 0 ? precision - 1 : 0);
4504
*p++ = decimal_point_char ();
4505
for (; ndigits > 0; --ndigits)
4517
/* The generated string now extends from tmp to p, with the
4518
zero padding insertion point being at pad_ptr. */
4519
if (has_width && p - tmp < width)
4521
size_t pad = width - (p - tmp);
4522
DCHAR_T *end = p + pad;
4524
if (flags & FLAG_LEFT)
4526
/* Pad with spaces on the right. */
4527
for (; pad > 0; pad--)
4530
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4532
/* Pad with zeroes. */
4537
for (; pad > 0; pad--)
4542
/* Pad with spaces on the left. */
4547
for (; pad > 0; pad--)
4555
size_t count = p - tmp;
4557
if (count >= tmp_length)
4558
/* tmp_length was incorrectly calculated - fix the
4562
/* Make room for the result. */
4563
if (count >= allocated - length)
4565
size_t n = xsum (length, count);
4567
ENSURE_ALLOCATION (n);
4570
/* Append the result. */
4571
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4580
arg_type type = a.arg[dp->arg_index].type;
4581
int flags = dp->flags;
4582
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4586
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4590
#if NEED_PRINTF_UNBOUNDED_PRECISION
4593
# define prec_ourselves 0
4595
#if NEED_PRINTF_FLAG_LEFTADJUST
4596
# define pad_ourselves 1
4597
#elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4600
# define pad_ourselves 0
4603
unsigned int prefix_count;
4604
int prefixes[2] IF_LINT (= { 0 });
4608
TCHAR_T tmpbuf[700];
4612
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4615
if (dp->width_start != dp->width_end)
4617
if (dp->width_arg_index != ARG_NONE)
4621
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4623
arg = a.arg[dp->width_arg_index].a.a_int;
4626
/* "A negative field width is taken as a '-' flag
4627
followed by a positive field width." */
4629
width = (unsigned int) (-arg);
4636
const FCHAR_T *digitp = dp->width_start;
4639
width = xsum (xtimes (width, 10), *digitp++ - '0');
4640
while (digitp != dp->width_end);
4646
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4649
if (dp->precision_start != dp->precision_end)
4651
if (dp->precision_arg_index != ARG_NONE)
4655
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4657
arg = a.arg[dp->precision_arg_index].a.a_int;
4658
/* "A negative precision is taken as if the precision
4668
const FCHAR_T *digitp = dp->precision_start + 1;
4671
while (digitp != dp->precision_end)
4672
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
4678
/* Decide whether to handle the precision ourselves. */
4679
#if NEED_PRINTF_UNBOUNDED_PRECISION
4680
switch (dp->conversion)
4682
case 'd': case 'i': case 'u':
4684
case 'x': case 'X': case 'p':
4685
prec_ourselves = has_precision && (precision > 0);
4693
/* Decide whether to perform the padding ourselves. */
4694
#if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4695
switch (dp->conversion)
4697
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4698
/* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4699
to perform the padding after this conversion. Functions
4700
with unistdio extensions perform the padding based on
4701
character count rather than element count. */
4704
# if NEED_PRINTF_FLAG_ZERO
4705
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4711
pad_ourselves = prec_ourselves;
4717
/* Allocate a temporary buffer of sufficient size for calling
4720
MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
4721
flags, width, has_precision, precision,
4724
if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4728
size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4730
if (size_overflow_p (tmp_memsize))
4731
/* Overflow, would lead to out of memory. */
4733
tmp = (TCHAR_T *) malloc (tmp_memsize);
4735
/* Out of memory. */
4740
/* Construct the format string for calling snprintf or
4744
#if NEED_PRINTF_FLAG_GROUPING
4745
/* The underlying implementation doesn't support the ' flag.
4746
Produce no grouping characters in this case; this is
4747
acceptable because the grouping is locale dependent. */
4749
if (flags & FLAG_GROUP)
4752
if (flags & FLAG_LEFT)
4754
if (flags & FLAG_SHOWSIGN)
4756
if (flags & FLAG_SPACE)
4758
if (flags & FLAG_ALT)
4760
#if __GLIBC__ >= 2 && !defined __UCLIBC__
4761
if (flags & FLAG_LOCALIZED)
4766
if (flags & FLAG_ZERO)
4768
if (dp->width_start != dp->width_end)
4770
size_t n = dp->width_end - dp->width_start;
4771
/* The width specification is known to consist only
4772
of standard ASCII characters. */
4773
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4775
memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4780
const FCHAR_T *mp = dp->width_start;
4782
*fbp++ = (unsigned char) *mp++;
4787
if (!prec_ourselves)
4789
if (dp->precision_start != dp->precision_end)
4791
size_t n = dp->precision_end - dp->precision_start;
4792
/* The precision specification is known to consist only
4793
of standard ASCII characters. */
4794
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4796
memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4801
const FCHAR_T *mp = dp->precision_start;
4803
*fbp++ = (unsigned char) *mp++;
4811
#if HAVE_LONG_LONG_INT
4812
case TYPE_LONGLONGINT:
4813
case TYPE_ULONGLONGINT:
4814
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4827
case TYPE_WIDE_CHAR:
4830
case TYPE_WIDE_STRING:
4834
case TYPE_LONGDOUBLE:
4840
#if NEED_PRINTF_DIRECTIVE_F
4841
if (dp->conversion == 'F')
4845
*fbp = dp->conversion;
4847
# if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4852
/* On glibc2 systems from glibc >= 2.3 - probably also older
4853
ones - we know that snprintf's return value conforms to
4854
ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
4855
gl_SNPRINTF_TRUNCATION_C99 pass.
4856
Therefore we can avoid using %n in this situation.
4857
On glibc2 systems from 2004-10-18 or newer, the use of %n
4858
in format strings in writable memory may crash the program
4859
(if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4860
in this situation. */
4861
/* On native Win32 systems (such as mingw), we can avoid using
4863
- Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4864
snprintf does not write more than the specified number
4865
of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4866
'4', '5', '6' into buf, not '4', '5', '\0'.)
4867
- Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4868
allows us to recognize the case of an insufficient
4869
buffer size: it returns -1 in this case.
4870
On native Win32 systems (such as mingw) where the OS is
4871
Windows Vista, the use of %n in format strings by default
4872
crashes the program. See
4873
<http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4874
<http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4875
So we should avoid %n in this situation. */
4882
/* Construct the arguments for calling snprintf or sprintf. */
4884
if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4886
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4888
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4890
if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4892
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4894
prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4898
/* The SNPRINTF result is appended after result[0..length].
4899
The latter is an array of DCHAR_T; SNPRINTF appends an
4900
array of TCHAR_T to it. This is possible because
4901
sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4902
alignof (TCHAR_T) <= alignof (DCHAR_T). */
4903
# define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4904
/* Ensure that maxlen below will be >= 2. Needed on BeOS,
4905
where an snprintf() with maxlen==1 acts like sprintf(). */
4906
ENSURE_ALLOCATION (xsum (length,
4907
(2 + TCHARS_PER_DCHAR - 1)
4908
/ TCHARS_PER_DCHAR));
4909
/* Prepare checking whether snprintf returns the count
4911
*(TCHAR_T *) (result + length) = '\0';
4922
size_t maxlen = allocated - length;
4923
/* SNPRINTF can fail if its second argument is
4925
if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4926
maxlen = INT_MAX / TCHARS_PER_DCHAR;
4927
maxlen = maxlen * TCHARS_PER_DCHAR;
4928
# define SNPRINTF_BUF(arg) \
4929
switch (prefix_count) \
4932
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4937
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4939
prefixes[0], arg, &count); \
4942
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4944
prefixes[0], prefixes[1], arg, \
4951
# define SNPRINTF_BUF(arg) \
4952
switch (prefix_count) \
4955
count = sprintf (tmp, buf, arg); \
4958
count = sprintf (tmp, buf, prefixes[0], arg); \
4961
count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4974
int arg = a.arg[dp->arg_index].a.a_schar;
4980
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4986
int arg = a.arg[dp->arg_index].a.a_short;
4992
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4998
int arg = a.arg[dp->arg_index].a.a_int;
5004
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
5010
long int arg = a.arg[dp->arg_index].a.a_longint;
5016
unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
5020
#if HAVE_LONG_LONG_INT
5021
case TYPE_LONGLONGINT:
5023
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
5027
case TYPE_ULONGLONGINT:
5029
unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
5036
double arg = a.arg[dp->arg_index].a.a_double;
5040
case TYPE_LONGDOUBLE:
5042
long double arg = a.arg[dp->arg_index].a.a_longdouble;
5048
int arg = a.arg[dp->arg_index].a.a_char;
5053
case TYPE_WIDE_CHAR:
5055
wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
5062
const char *arg = a.arg[dp->arg_index].a.a_string;
5067
case TYPE_WIDE_STRING:
5069
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
5076
void *arg = a.arg[dp->arg_index].a.a_pointer;
5085
/* Portability: Not all implementations of snprintf()
5086
are ISO C 99 compliant. Determine the number of
5087
bytes that snprintf() has produced or would have
5091
/* Verify that snprintf() has NUL-terminated its
5094
&& ((TCHAR_T *) (result + length)) [count] != '\0')
5096
/* Portability hack. */
5097
if (retcount > count)
5102
/* snprintf() doesn't understand the '%n'
5106
/* Don't use the '%n' directive; instead, look
5107
at the snprintf() return value. */
5113
/* Look at the snprintf() return value. */
5116
# if !HAVE_SNPRINTF_RETVAL_C99
5117
/* HP-UX 10.20 snprintf() is doubly deficient:
5118
It doesn't understand the '%n' directive,
5119
*and* it returns -1 (rather than the length
5120
that would have been required) when the
5121
buffer is too small.
5122
But a failure at this point can also come
5123
from other reasons than a too small buffer,
5124
such as an invalid wide string argument to
5125
the %ls directive, or possibly an invalid
5126
floating-point argument. */
5128
MAX_ROOM_NEEDED (&a, dp->arg_index,
5129
dp->conversion, type, flags,
5130
width, has_precision,
5131
precision, pad_ourselves);
5133
if (maxlen < tmp_length)
5135
/* Make more room. But try to do through
5136
this reallocation only once. */
5137
size_t bigger_need =
5140
TCHARS_PER_DCHAR - 1)
5141
/ TCHARS_PER_DCHAR);
5142
/* And always grow proportionally.
5143
(There may be several arguments, each
5144
needing a little more room than the
5146
size_t bigger_need2 =
5147
xsum (xtimes (allocated, 2), 12);
5148
if (bigger_need < bigger_need2)
5149
bigger_need = bigger_need2;
5150
ENSURE_ALLOCATION (bigger_need);
5161
/* Attempt to handle failure. */
5164
/* SNPRINTF or sprintf failed. Save and use the errno
5165
that it has set, if any. */
5166
int saved_errno = errno;
5168
if (!(result == resultbuf || result == NULL))
5170
if (buf_malloced != NULL)
5171
free (buf_malloced);
5176
: (dp->conversion == 'c' || dp->conversion == 's'
5183
/* Handle overflow of the allocated buffer.
5184
If such an overflow occurs, a C99 compliant snprintf()
5185
returns a count >= maxlen. However, a non-compliant
5186
snprintf() function returns only count = maxlen - 1. To
5187
cover both cases, test whether count >= maxlen - 1. */
5188
if ((unsigned int) count + 1 >= maxlen)
5190
/* If maxlen already has attained its allowed maximum,
5191
allocating more memory will not increase maxlen.
5192
Instead of looping, bail out. */
5193
if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5197
/* Need at least (count + 1) * sizeof (TCHAR_T)
5198
bytes. (The +1 is for the trailing NUL.)
5199
But ask for (count + 2) * sizeof (TCHAR_T)
5200
bytes, so that in the next round, we likely get
5201
maxlen > (unsigned int) count + 1
5202
and so we don't get here again.
5203
And allocate proportionally, to avoid looping
5204
eternally if snprintf() reports a too small
5208
((unsigned int) count + 2
5209
+ TCHARS_PER_DCHAR - 1)
5210
/ TCHARS_PER_DCHAR),
5211
xtimes (allocated, 2));
5213
ENSURE_ALLOCATION (n);
5219
#if NEED_PRINTF_UNBOUNDED_PRECISION
5222
/* Handle the precision. */
5225
(TCHAR_T *) (result + length);
5229
size_t prefix_count;
5233
/* Put the additional zeroes after the sign. */
5235
&& (*prec_ptr == '-' || *prec_ptr == '+'
5236
|| *prec_ptr == ' '))
5238
/* Put the additional zeroes after the 0x prefix if
5239
(flags & FLAG_ALT) || (dp->conversion == 'p'). */
5241
&& prec_ptr[0] == '0'
5242
&& (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
5245
move = count - prefix_count;
5246
if (precision > move)
5248
/* Insert zeroes. */
5249
size_t insert = precision - move;
5255
(count + insert + TCHARS_PER_DCHAR - 1)
5256
/ TCHARS_PER_DCHAR);
5257
length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5258
ENSURE_ALLOCATION (n);
5259
length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5260
prec_ptr = (TCHAR_T *) (result + length);
5263
prec_end = prec_ptr + count;
5264
prec_ptr += prefix_count;
5266
while (prec_end > prec_ptr)
5269
prec_end[insert] = prec_end[0];
5275
while (prec_end > prec_ptr);
5283
if (count >= tmp_length)
5284
/* tmp_length was incorrectly calculated - fix the
5290
/* Convert from TCHAR_T[] to DCHAR_T[]. */
5291
if (dp->conversion == 'c' || dp->conversion == 's')
5293
/* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5295
The result string is not certainly ASCII. */
5296
const TCHAR_T *tmpsrc;
5299
/* This code assumes that TCHAR_T is 'char'. */
5300
verify (sizeof (TCHAR_T) == 1);
5302
tmpsrc = (TCHAR_T *) (result + length);
5307
DCHAR_CONV_FROM_ENCODING (locale_charset (),
5308
iconveh_question_mark,
5314
int saved_errno = errno;
5315
if (!(result == resultbuf || result == NULL))
5317
if (buf_malloced != NULL)
5318
free (buf_malloced);
5320
errno = saved_errno;
5323
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
5324
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5330
/* The result string is ASCII.
5331
Simple 1:1 conversion. */
5333
/* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5334
no-op conversion, in-place on the array starting
5335
at (result + length). */
5336
if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
5339
const TCHAR_T *tmpsrc;
5344
if (result == resultbuf)
5346
tmpsrc = (TCHAR_T *) (result + length);
5347
/* ENSURE_ALLOCATION will not move tmpsrc
5348
(because it's part of resultbuf). */
5349
ENSURE_ALLOCATION (xsum (length, count));
5353
/* ENSURE_ALLOCATION will move the array
5354
(because it uses realloc(). */
5355
ENSURE_ALLOCATION (xsum (length, count));
5356
tmpsrc = (TCHAR_T *) (result + length);
5360
ENSURE_ALLOCATION (xsum (length, count));
5362
tmpdst = result + length;
5363
/* Copy backwards, because of overlapping. */
5366
for (n = count; n > 0; n--)
5367
*--tmpdst = (unsigned char) *--tmpsrc;
5372
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
5373
/* Make room for the result. */
5374
if (count > allocated - length)
5376
/* Need at least count elements. But allocate
5379
xmax (xsum (length, count), xtimes (allocated, 2));
5381
ENSURE_ALLOCATION (n);
5385
/* Here count <= allocated - length. */
5387
/* Perform padding. */
5388
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5389
if (pad_ourselves && has_width)
5392
# if ENABLE_UNISTDIO
5393
/* Outside POSIX, it's preferrable to compare the width
5394
against the number of _characters_ of the converted
5396
w = DCHAR_MBSNLEN (result + length, count);
5398
/* The width is compared against the number of _bytes_
5399
of the converted value, says POSIX. */
5404
size_t pad = width - w;
5406
/* Make room for the result. */
5407
if (xsum (count, pad) > allocated - length)
5409
/* Need at least count + pad elements. But
5410
allocate proportionally. */
5412
xmax (xsum3 (length, count, pad),
5413
xtimes (allocated, 2));
5417
ENSURE_ALLOCATION (n);
5420
ENSURE_ALLOCATION (n);
5423
/* Here count + pad <= allocated - length. */
5426
# if !DCHAR_IS_TCHAR || USE_SNPRINTF
5427
DCHAR_T * const rp = result + length;
5429
DCHAR_T * const rp = tmp;
5431
DCHAR_T *p = rp + count;
5432
DCHAR_T *end = p + pad;
5434
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5435
if (dp->conversion == 'c'
5436
|| dp->conversion == 's')
5437
/* No zero-padding for string directives. */
5442
pad_ptr = (*rp == '-' ? rp + 1 : rp);
5443
/* No zero-padding of "inf" and "nan". */
5444
if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
5445
|| (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
5448
/* The generated string now extends from rp to p,
5449
with the zero padding insertion point being at
5452
count = count + pad; /* = end - rp */
5454
if (flags & FLAG_LEFT)
5456
/* Pad with spaces on the right. */
5457
for (; pad > 0; pad--)
5460
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
5462
/* Pad with zeroes. */
5467
for (; pad > 0; pad--)
5472
/* Pad with spaces on the left. */
5477
for (; pad > 0; pad--)
5485
/* Here still count <= allocated - length. */
5487
#if !DCHAR_IS_TCHAR || USE_SNPRINTF
5488
/* The snprintf() result did fit. */
5490
/* Append the sprintf() result. */
5491
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5498
#if NEED_PRINTF_DIRECTIVE_F
5499
if (dp->conversion == 'F')
5501
/* Convert the %f result to upper case for %F. */
5502
DCHAR_T *rp = result + length;
5504
for (rc = count; rc > 0; rc--, rp++)
5505
if (*rp >= 'a' && *rp <= 'z')
5506
*rp = *rp - 'a' + 'A';
5514
#undef pad_ourselves
5515
#undef prec_ourselves
5520
/* Add the final NUL. */
5521
ENSURE_ALLOCATION (xsum (length, 1));
5522
result[length] = '\0';
5524
if (result != resultbuf && length + 1 < allocated)
5526
/* Shrink the allocated memory if possible. */
5529
memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
5534
if (buf_malloced != NULL)
5535
free (buf_malloced);
5538
/* Note that we can produce a big string of a length > INT_MAX. POSIX
5539
says that snprintf() fails with errno = EOVERFLOW in this case, but
5540
that's only because snprintf() returns an 'int'. This function does
5541
not have this limitation. */
5546
if (!(result == resultbuf || result == NULL))
5548
if (buf_malloced != NULL)
5549
free (buf_malloced);
5556
if (!(result == resultbuf || result == NULL))
5558
if (buf_malloced != NULL)
5559
free (buf_malloced);
5567
#undef MAX_ROOM_NEEDED
5568
#undef TCHARS_PER_DCHAR
5576
#undef DCHAR_IS_TCHAR