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-2010 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. */
93
#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
98
#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
100
# include "isnand-nolibm.h"
103
#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
105
# include "isnanl-nolibm.h"
109
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
111
# include "isnand-nolibm.h"
112
# include "printf-frexp.h"
115
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
117
# include "isnanl-nolibm.h"
118
# include "printf-frexpl.h"
122
/* Default parameters. */
124
# if WIDE_CHAR_VERSION
125
# define VASNPRINTF vasnwprintf
126
# define FCHAR_T wchar_t
127
# define DCHAR_T wchar_t
128
# define TCHAR_T wchar_t
129
# define DCHAR_IS_TCHAR 1
130
# define DIRECTIVE wchar_t_directive
131
# define DIRECTIVES wchar_t_directives
132
# define PRINTF_PARSE wprintf_parse
133
# define DCHAR_CPY wmemcpy
134
# define DCHAR_SET wmemset
136
# define VASNPRINTF vasnprintf
137
# define FCHAR_T char
138
# define DCHAR_T char
139
# define TCHAR_T char
140
# define DCHAR_IS_TCHAR 1
141
# define DIRECTIVE char_directive
142
# define DIRECTIVES char_directives
143
# define PRINTF_PARSE printf_parse
144
# define DCHAR_CPY memcpy
145
# define DCHAR_SET memset
148
#if WIDE_CHAR_VERSION
149
/* TCHAR_T is wchar_t. */
150
# define USE_SNPRINTF 1
151
# if HAVE_DECL__SNWPRINTF
152
/* On Windows, the function swprintf() has a different signature than
153
on Unix; we use the _snwprintf() function instead. */
154
# define SNPRINTF _snwprintf
157
# define SNPRINTF swprintf
160
/* TCHAR_T is char. */
161
/* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
162
But don't use it on BeOS, since BeOS snprintf produces no output if the
163
size argument is >= 0x3000000.
164
Also don't use it on Linux libc5, since there snprintf with size = 1
165
writes any output without bounds, like sprintf. */
166
# if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
167
# define USE_SNPRINTF 1
169
# define USE_SNPRINTF 0
171
# if HAVE_DECL__SNPRINTF
173
# define SNPRINTF _snprintf
176
# define SNPRINTF snprintf
177
/* Here we need to call the native snprintf, not rpl_snprintf. */
181
/* Here we need to call the native sprintf, not rpl_sprintf. */
184
/* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
185
warnings in this file. Use -Dlint to suppress them. */
187
# define IF_LINT(Code) Code
189
# define IF_LINT(Code) /* empty */
192
/* Avoid some warnings from "gcc -Wshadow".
193
This file doesn't use the exp() and remainder() functions. */
197
#define remainder rem
199
#if !USE_SNPRINTF && !WIDE_CHAR_VERSION
200
# if (HAVE_STRNLEN && !defined _AIX)
201
# define local_strnlen strnlen
203
# ifndef local_strnlen_defined
204
# define local_strnlen_defined 1
206
local_strnlen (const char *string, size_t maxlen)
208
const char *end = memchr (string, '\0', maxlen);
209
return end ? (size_t) (end - string) : maxlen;
215
#if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T && (WIDE_CHAR_VERSION || DCHAR_IS_TCHAR)
217
# define local_wcslen wcslen
219
/* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
220
a dependency towards this library, here is a local substitute.
221
Define this substitute only once, even if this file is included
222
twice in the same compilation unit. */
223
# ifndef local_wcslen_defined
224
# define local_wcslen_defined 1
226
local_wcslen (const wchar_t *s)
230
for (ptr = s; *ptr != (wchar_t) 0; ptr++)
238
#if !USE_SNPRINTF && HAVE_WCHAR_T && WIDE_CHAR_VERSION
240
# define local_wcsnlen wcsnlen
242
# ifndef local_wcsnlen_defined
243
# define local_wcsnlen_defined 1
245
local_wcsnlen (const wchar_t *s, size_t maxlen)
249
for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
257
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
258
/* Determine the decimal-point character according to the current locale. */
259
# ifndef decimal_point_char_defined
260
# define decimal_point_char_defined 1
262
decimal_point_char (void)
265
/* Determine it in a multithread-safe way. We know nl_langinfo is
266
multithread-safe on glibc systems and MacOS X systems, but is not required
267
to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
268
localeconv() is rarely multithread-safe. */
269
# if HAVE_NL_LANGINFO && (__GLIBC__ || (defined __APPLE__ && defined __MACH__))
270
point = nl_langinfo (RADIXCHAR);
273
sprintf (pointbuf, "%#.0f", 1.0);
274
point = &pointbuf[1];
276
point = localeconv () -> decimal_point;
278
/* The decimal point is always a single byte: either '.' or ','. */
279
return (point[0] != '\0' ? point[0] : '.');
284
#if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
286
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
288
is_infinite_or_zero (double x)
290
return isnand (x) || x + x == x;
295
#if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
297
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
299
is_infinite_or_zerol (long double x)
301
return isnanl (x) || x + x == x;
306
#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
308
/* Converting 'long double' to decimal without rare rounding bugs requires
309
real bignums. We use the naming conventions of GNU gmp, but vastly simpler
310
(and slower) algorithms. */
312
typedef unsigned int mp_limb_t;
313
# define GMP_LIMB_BITS 32
314
typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
316
typedef unsigned long long mp_twolimb_t;
317
# define GMP_TWOLIMB_BITS 64
318
typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
320
/* Representation of a bignum >= 0. */
324
mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
327
/* Compute the product of two bignums >= 0.
328
Return the allocated memory in case of success, NULL in case of memory
329
allocation failure. */
331
multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
338
if (src1.nlimbs <= src2.nlimbs)
352
/* Now 0 <= len1 <= len2. */
355
/* src1 or src2 is zero. */
357
dest->limbs = (mp_limb_t *) malloc (1);
361
/* Here 1 <= len1 <= len2. */
367
dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
370
for (k = len2; k > 0; )
372
for (i = 0; i < len1; i++)
374
mp_limb_t digit1 = p1[i];
375
mp_twolimb_t carry = 0;
376
for (j = 0; j < len2; j++)
378
mp_limb_t digit2 = p2[j];
379
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
381
dp[i + j] = (mp_limb_t) carry;
382
carry = carry >> GMP_LIMB_BITS;
384
dp[i + len2] = (mp_limb_t) carry;
387
while (dlen > 0 && dp[dlen - 1] == 0)
395
/* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
396
a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
398
Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
400
Return the allocated memory in case of success, NULL in case of memory
401
allocation failure. */
403
divide (mpn_t a, mpn_t b, mpn_t *q)
406
First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
407
with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
408
If m<n, then q:=0 and r:=a.
409
If m>=n=1, perform a single-precision division:
412
{Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
413
= a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
414
j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
415
Normalise [q[m-1],...,q[0]], yields q.
416
If m>=n>1, perform a multiple-precision division:
417
We have a/b < beta^(m-n+1).
418
s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
419
Shift a and b left by s bits, copying them. r:=a.
420
r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
421
For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
423
q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
424
In case of overflow (q* >= beta) set q* := beta-1.
425
Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
426
and c3 := b[n-2] * q*.
427
{We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
428
occurred. Furthermore 0 <= c3 < beta^2.
429
If there was overflow and
430
r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
431
the next test can be skipped.}
432
While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
433
Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
435
Put r := r - b * q* * beta^j. In detail:
436
[r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
437
hence: u:=0, for i:=0 to n-1 do
439
r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
440
u:=u div beta (+ 1, if carry in subtraction)
442
{Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
444
the carry u does not overflow.}
445
If a negative carry occurs, put q* := q* - 1
446
and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
448
Normalise [q[m-n],..,q[0]]; this yields the quotient q.
449
Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
451
The room for q[j] can be allocated at the memory location of r[n+j].
452
Finally, round-to-even:
453
Shift r left by 1 bit.
454
If r > b or if r = b and q[0] is odd, q := q+1.
456
const mp_limb_t *a_ptr = a.limbs;
457
size_t a_len = a.nlimbs;
458
const mp_limb_t *b_ptr = b.limbs;
459
size_t b_len = b.nlimbs;
461
mp_limb_t *tmp_roomptr = NULL;
467
/* Allocate room for a_len+2 digits.
468
(Need a_len+1 digits for the real division and 1 more digit for the
469
final rounding of q.) */
470
roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
475
while (a_len > 0 && a_ptr[a_len - 1] == 0)
482
/* Division by zero. */
484
if (b_ptr[b_len - 1] == 0)
490
/* Here m = a_len >= 0 and n = b_len > 0. */
494
/* m<n: trivial case. q=0, r := copy of a. */
497
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
498
q_ptr = roomptr + a_len;
503
/* n=1: single precision division.
504
beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
508
mp_limb_t den = b_ptr[0];
509
mp_limb_t remainder = 0;
510
const mp_limb_t *sourceptr = a_ptr + a_len;
511
mp_limb_t *destptr = q_ptr + a_len;
513
for (count = a_len; count > 0; count--)
516
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
517
*--destptr = num / den;
518
remainder = num % den;
520
/* Normalise and store r. */
523
r_ptr[0] = remainder;
530
if (q_ptr[q_len - 1] == 0)
536
/* n>1: multiple precision division.
537
beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
538
beta^(m-n-1) <= a/b < beta^(m-n+1). */
542
mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
570
/* 0 <= s < GMP_LIMB_BITS.
571
Copy b, shifting it left by s bits. */
574
tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
575
if (tmp_roomptr == NULL)
581
const mp_limb_t *sourceptr = b_ptr;
582
mp_limb_t *destptr = tmp_roomptr;
583
mp_twolimb_t accu = 0;
585
for (count = b_len; count > 0; count--)
587
accu += (mp_twolimb_t) *sourceptr++ << s;
588
*destptr++ = (mp_limb_t) accu;
589
accu = accu >> GMP_LIMB_BITS;
591
/* accu must be zero, since that was how s was determined. */
597
/* Copy a, shifting it left by s bits, yields r.
599
At the beginning: r = roomptr[0..a_len],
600
at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
604
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
609
const mp_limb_t *sourceptr = a_ptr;
610
mp_limb_t *destptr = r_ptr;
611
mp_twolimb_t accu = 0;
613
for (count = a_len; count > 0; count--)
615
accu += (mp_twolimb_t) *sourceptr++ << s;
616
*destptr++ = (mp_limb_t) accu;
617
accu = accu >> GMP_LIMB_BITS;
619
*destptr++ = (mp_limb_t) accu;
621
q_ptr = roomptr + b_len;
622
q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
624
size_t j = a_len - b_len; /* m-n */
625
mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
626
mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
627
mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
628
((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
629
/* Division loop, traversed m-n+1 times.
630
j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
635
if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
637
/* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
639
((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
640
| r_ptr[j + b_len - 1];
641
q_star = num / b_msd;
646
/* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
647
q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
648
/* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
649
<==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
650
<==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
652
If yes, jump directly to the subtraction loop.
653
(Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
654
<==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
655
if (r_ptr[j + b_len] > b_msd
656
|| (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
657
/* r[j+n] >= b[n-1]+1 or
658
r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
663
c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
665
mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
666
((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
667
mp_twolimb_t c3 = /* b[n-2] * q* */
668
(mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
669
/* While c2 < c3, increase c2 and decrease c3.
670
Consider c3-c2. While it is > 0, decrease it by
671
b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
672
this can happen only twice. */
675
q_star = q_star - 1; /* q* := q* - 1 */
676
if (c3 - c2 > b_msdd)
677
q_star = q_star - 1; /* q* := q* - 1 */
683
/* Subtract r := r - b * q* * beta^j. */
686
const mp_limb_t *sourceptr = b_ptr;
687
mp_limb_t *destptr = r_ptr + j;
688
mp_twolimb_t carry = 0;
690
for (count = b_len; count > 0; count--)
692
/* Here 0 <= carry <= q*. */
695
+ (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
696
+ (mp_limb_t) ~(*destptr);
697
/* Here 0 <= carry <= beta*q* + beta-1. */
698
*destptr++ = ~(mp_limb_t) carry;
699
carry = carry >> GMP_LIMB_BITS; /* <= q* */
701
cr = (mp_limb_t) carry;
703
/* Subtract cr from r_ptr[j + b_len], then forget about
705
if (cr > r_ptr[j + b_len])
707
/* Subtraction gave a carry. */
708
q_star = q_star - 1; /* q* := q* - 1 */
711
const mp_limb_t *sourceptr = b_ptr;
712
mp_limb_t *destptr = r_ptr + j;
715
for (count = b_len; count > 0; count--)
717
mp_limb_t source1 = *sourceptr++;
718
mp_limb_t source2 = *destptr;
719
*destptr++ = source1 + source2 + carry;
722
? source1 >= (mp_limb_t) ~source2
723
: source1 > (mp_limb_t) ~source2);
726
/* Forget about the carry and about r[j+n]. */
729
/* q* is determined. Store it as q[j]. */
738
if (q_ptr[q_len - 1] == 0)
740
# if 0 /* Not needed here, since we need r only to compare it with b/2, and
741
b is shifted left by s bits. */
742
/* Shift r right by s bits. */
745
mp_limb_t ptr = r_ptr + r_len;
746
mp_twolimb_t accu = 0;
748
for (count = r_len; count > 0; count--)
750
accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
751
accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
752
*ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
757
while (r_len > 0 && r_ptr[r_len - 1] == 0)
760
/* Compare r << 1 with b. */
768
(i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
769
| (i < r_len ? r_ptr[i] << 1 : 0);
770
mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
780
if (q_len > 0 && ((q_ptr[0] & 1) != 0))
785
for (i = 0; i < q_len; i++)
786
if (++(q_ptr[i]) != 0)
791
if (tmp_roomptr != NULL)
798
/* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
800
Destroys the contents of a.
801
Return the allocated memory - containing the decimal digits in low-to-high
802
order, terminated with a NUL character - in case of success, NULL in case
803
of memory allocation failure. */
805
convert_to_decimal (mpn_t a, size_t extra_zeroes)
807
mp_limb_t *a_ptr = a.limbs;
808
size_t a_len = a.nlimbs;
809
/* 0.03345 is slightly larger than log(2)/(9*log(10)). */
810
size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
811
char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
815
for (; extra_zeroes > 0; extra_zeroes--)
819
/* Divide a by 10^9, in-place. */
820
mp_limb_t remainder = 0;
821
mp_limb_t *ptr = a_ptr + a_len;
823
for (count = a_len; count > 0; count--)
826
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
827
*ptr = num / 1000000000;
828
remainder = num % 1000000000;
830
/* Store the remainder as 9 decimal digits. */
831
for (count = 9; count > 0; count--)
833
*d_ptr++ = '0' + (remainder % 10);
834
remainder = remainder / 10;
837
if (a_ptr[a_len - 1] == 0)
840
/* Remove leading zeroes. */
841
while (d_ptr > c_ptr && d_ptr[-1] == '0')
843
/* But keep at least one zero. */
846
/* Terminate the string. */
852
# if NEED_PRINTF_LONG_DOUBLE
854
/* Assuming x is finite and >= 0:
855
write x as x = 2^e * m, where m is a bignum.
856
Return the allocated memory in case of success, NULL in case of memory
857
allocation failure. */
859
decode_long_double (long double x, int *ep, mpn_t *mp)
866
/* Allocate memory for result. */
867
m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
868
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
871
/* Split into exponential part and mantissa. */
872
y = frexpl (x, &exp);
873
if (!(y >= 0.0L && y < 1.0L))
875
/* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
876
latter is an integer. */
877
/* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
878
I'm not sure whether it's safe to cast a 'long double' value between
879
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
880
'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
882
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
883
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
886
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
889
if (!(y >= 0.0L && y < 1.0L))
891
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
894
if (!(y >= 0.0L && y < 1.0L))
896
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
901
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
904
if (!(y >= 0.0L && y < 1.0L))
906
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
910
for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
913
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
916
if (!(y >= 0.0L && y < 1.0L))
918
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
921
if (!(y >= 0.0L && y < 1.0L))
923
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
925
#if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
931
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
934
*ep = exp - LDBL_MANT_BIT;
940
# if NEED_PRINTF_DOUBLE
942
/* Assuming x is finite and >= 0:
943
write x as x = 2^e * m, where m is a bignum.
944
Return the allocated memory in case of success, NULL in case of memory
945
allocation failure. */
947
decode_double (double x, int *ep, mpn_t *mp)
954
/* Allocate memory for result. */
955
m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
956
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
959
/* Split into exponential part and mantissa. */
961
if (!(y >= 0.0 && y < 1.0))
963
/* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
964
latter is an integer. */
965
/* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
966
I'm not sure whether it's safe to cast a 'double' value between
967
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
968
'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
970
# if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
971
# if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
974
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
977
if (!(y >= 0.0 && y < 1.0))
979
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
982
if (!(y >= 0.0 && y < 1.0))
984
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
989
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
992
if (!(y >= 0.0 && y < 1.0))
994
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
998
for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1001
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1004
if (!(y >= 0.0 && y < 1.0))
1006
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1009
if (!(y >= 0.0 && y < 1.0))
1011
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1016
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
1019
*ep = exp - DBL_MANT_BIT;
1025
/* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
1026
Returns the decimal representation of round (x * 10^n).
1027
Return the allocated memory - containing the decimal digits in low-to-high
1028
order, terminated with a NUL character - in case of success, NULL in case
1029
of memory allocation failure. */
1031
scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1034
size_t extra_zeroes;
1037
mp_limb_t *pow5_ptr;
1039
unsigned int s_limbs;
1040
unsigned int s_bits;
1048
/* x = 2^e * m, hence
1049
y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1050
= round (2^s * 5^n * m). */
1053
/* Factor out a common power of 10 if possible. */
1056
extra_zeroes = (s < n ? s : n);
1060
/* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1061
Before converting to decimal, we need to compute
1062
z = round (2^s * 5^n * m). */
1063
/* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1064
sign. 2.322 is slightly larger than log(5)/log(2). */
1065
abs_n = (n >= 0 ? n : -n);
1066
abs_s = (s >= 0 ? s : -s);
1067
pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1068
+ abs_s / GMP_LIMB_BITS + 1)
1069
* sizeof (mp_limb_t));
1070
if (pow5_ptr == NULL)
1075
/* Initialize with 1. */
1078
/* Multiply with 5^|n|. */
1081
static mp_limb_t const small_pow5[13 + 1] =
1083
1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1084
48828125, 244140625, 1220703125
1087
for (n13 = 0; n13 <= abs_n; n13 += 13)
1089
mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1091
mp_twolimb_t carry = 0;
1092
for (j = 0; j < pow5_len; j++)
1094
mp_limb_t digit2 = pow5_ptr[j];
1095
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1096
pow5_ptr[j] = (mp_limb_t) carry;
1097
carry = carry >> GMP_LIMB_BITS;
1100
pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1103
s_limbs = abs_s / GMP_LIMB_BITS;
1104
s_bits = abs_s % GMP_LIMB_BITS;
1105
if (n >= 0 ? s >= 0 : s <= 0)
1107
/* Multiply with 2^|s|. */
1110
mp_limb_t *ptr = pow5_ptr;
1111
mp_twolimb_t accu = 0;
1113
for (count = pow5_len; count > 0; count--)
1115
accu += (mp_twolimb_t) *ptr << s_bits;
1116
*ptr++ = (mp_limb_t) accu;
1117
accu = accu >> GMP_LIMB_BITS;
1121
*ptr = (mp_limb_t) accu;
1128
for (count = pow5_len; count > 0;)
1131
pow5_ptr[s_limbs + count] = pow5_ptr[count];
1133
for (count = s_limbs; count > 0;)
1136
pow5_ptr[count] = 0;
1138
pow5_len += s_limbs;
1140
pow5.limbs = pow5_ptr;
1141
pow5.nlimbs = pow5_len;
1144
/* Multiply m with pow5. No division needed. */
1145
z_memory = multiply (m, pow5, &z);
1149
/* Divide m by pow5 and round. */
1150
z_memory = divide (m, pow5, &z);
1155
pow5.limbs = pow5_ptr;
1156
pow5.nlimbs = pow5_len;
1160
Multiply m with pow5, then divide by 2^|s|. */
1164
tmp_memory = multiply (m, pow5, &numerator);
1165
if (tmp_memory == NULL)
1171
/* Construct 2^|s|. */
1173
mp_limb_t *ptr = pow5_ptr + pow5_len;
1175
for (i = 0; i < s_limbs; i++)
1177
ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1178
denominator.limbs = ptr;
1179
denominator.nlimbs = s_limbs + 1;
1181
z_memory = divide (numerator, denominator, &z);
1187
Multiply m with 2^s, then divide by pow5. */
1190
num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1191
* sizeof (mp_limb_t));
1192
if (num_ptr == NULL)
1199
mp_limb_t *destptr = num_ptr;
1202
for (i = 0; i < s_limbs; i++)
1207
const mp_limb_t *sourceptr = m.limbs;
1208
mp_twolimb_t accu = 0;
1210
for (count = m.nlimbs; count > 0; count--)
1212
accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1213
*destptr++ = (mp_limb_t) accu;
1214
accu = accu >> GMP_LIMB_BITS;
1217
*destptr++ = (mp_limb_t) accu;
1221
const mp_limb_t *sourceptr = m.limbs;
1223
for (count = m.nlimbs; count > 0; count--)
1224
*destptr++ = *sourceptr++;
1226
numerator.limbs = num_ptr;
1227
numerator.nlimbs = destptr - num_ptr;
1229
z_memory = divide (numerator, pow5, &z);
1236
/* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1238
if (z_memory == NULL)
1240
digits = convert_to_decimal (z, extra_zeroes);
1245
# if NEED_PRINTF_LONG_DOUBLE
1247
/* Assuming x is finite and >= 0, and n is an integer:
1248
Returns the decimal representation of round (x * 10^n).
1249
Return the allocated memory - containing the decimal digits in low-to-high
1250
order, terminated with a NUL character - in case of success, NULL in case
1251
of memory allocation failure. */
1253
scale10_round_decimal_long_double (long double x, int n)
1257
void *memory = decode_long_double (x, &e, &m);
1258
return scale10_round_decimal_decoded (e, m, memory, n);
1263
# if NEED_PRINTF_DOUBLE
1265
/* Assuming x is finite and >= 0, and n is an integer:
1266
Returns the decimal representation of round (x * 10^n).
1267
Return the allocated memory - containing the decimal digits in low-to-high
1268
order, terminated with a NUL character - in case of success, NULL in case
1269
of memory allocation failure. */
1271
scale10_round_decimal_double (double x, int n)
1275
void *memory = decode_double (x, &e, &m);
1276
return scale10_round_decimal_decoded (e, m, memory, n);
1281
# if NEED_PRINTF_LONG_DOUBLE
1283
/* Assuming x is finite and > 0:
1284
Return an approximation for n with 10^n <= x < 10^(n+1).
1285
The approximation is usually the right n, but may be off by 1 sometimes. */
1287
floorlog10l (long double x)
1294
/* Split into exponential part and mantissa. */
1295
y = frexpl (x, &exp);
1296
if (!(y >= 0.0L && y < 1.0L))
1302
while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1304
y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1305
exp -= GMP_LIMB_BITS;
1307
if (y < (1.0L / (1 << 16)))
1309
y *= 1.0L * (1 << 16);
1312
if (y < (1.0L / (1 << 8)))
1314
y *= 1.0L * (1 << 8);
1317
if (y < (1.0L / (1 << 4)))
1319
y *= 1.0L * (1 << 4);
1322
if (y < (1.0L / (1 << 2)))
1324
y *= 1.0L * (1 << 2);
1327
if (y < (1.0L / (1 << 1)))
1329
y *= 1.0L * (1 << 1);
1333
if (!(y >= 0.5L && y < 1.0L))
1335
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1338
if (z < 0.70710678118654752444)
1340
z *= 1.4142135623730950488;
1343
if (z < 0.8408964152537145431)
1345
z *= 1.1892071150027210667;
1348
if (z < 0.91700404320467123175)
1350
z *= 1.0905077326652576592;
1353
if (z < 0.9576032806985736469)
1355
z *= 1.0442737824274138403;
1358
/* Now 0.95 <= z <= 1.01. */
1360
/* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1361
Four terms are enough to get an approximation with error < 10^-7. */
1362
l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1363
/* Finally multiply with log(2)/log(10), yields an approximation for
1365
l *= 0.30102999566398119523;
1366
/* Round down to the next integer. */
1367
return (int) l + (l < 0 ? -1 : 0);
1372
# if NEED_PRINTF_DOUBLE
1374
/* Assuming x is finite and > 0:
1375
Return an approximation for n with 10^n <= x < 10^(n+1).
1376
The approximation is usually the right n, but may be off by 1 sometimes. */
1378
floorlog10 (double x)
1385
/* Split into exponential part and mantissa. */
1386
y = frexp (x, &exp);
1387
if (!(y >= 0.0 && y < 1.0))
1393
while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1395
y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1396
exp -= GMP_LIMB_BITS;
1398
if (y < (1.0 / (1 << 16)))
1400
y *= 1.0 * (1 << 16);
1403
if (y < (1.0 / (1 << 8)))
1405
y *= 1.0 * (1 << 8);
1408
if (y < (1.0 / (1 << 4)))
1410
y *= 1.0 * (1 << 4);
1413
if (y < (1.0 / (1 << 2)))
1415
y *= 1.0 * (1 << 2);
1418
if (y < (1.0 / (1 << 1)))
1420
y *= 1.0 * (1 << 1);
1424
if (!(y >= 0.5 && y < 1.0))
1426
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1429
if (z < 0.70710678118654752444)
1431
z *= 1.4142135623730950488;
1434
if (z < 0.8408964152537145431)
1436
z *= 1.1892071150027210667;
1439
if (z < 0.91700404320467123175)
1441
z *= 1.0905077326652576592;
1444
if (z < 0.9576032806985736469)
1446
z *= 1.0442737824274138403;
1449
/* Now 0.95 <= z <= 1.01. */
1451
/* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
1452
Four terms are enough to get an approximation with error < 10^-7. */
1453
l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1454
/* Finally multiply with log(2)/log(10), yields an approximation for
1456
l *= 0.30102999566398119523;
1457
/* Round down to the next integer. */
1458
return (int) l + (l < 0 ? -1 : 0);
1463
/* Tests whether a string of digits consists of exactly PRECISION zeroes and
1464
a single '1' digit. */
1466
is_borderline (const char *digits, size_t precision)
1468
for (; precision > 0; precision--, digits++)
1474
return *digits == '\0';
1480
VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1481
const FCHAR_T *format, va_list args)
1486
if (PRINTF_PARSE (format, &d, &a) < 0)
1487
/* errno is already set. */
1495
if (PRINTF_FETCHARGS (args, &a) < 0)
1503
size_t buf_neededlength;
1505
TCHAR_T *buf_malloced;
1509
/* Output string accumulator. */
1514
/* Allocate a small buffer that will hold a directive passed to
1515
sprintf or snprintf. */
1517
xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1519
if (buf_neededlength < 4000 / sizeof (TCHAR_T))
1521
buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
1522
buf_malloced = NULL;
1527
size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
1528
if (size_overflow_p (buf_memsize))
1529
goto out_of_memory_1;
1530
buf = (TCHAR_T *) malloc (buf_memsize);
1532
goto out_of_memory_1;
1536
if (resultbuf != NULL)
1539
allocated = *lengthp;
1548
result is either == resultbuf or == NULL or malloc-allocated.
1549
If length > 0, then result != NULL. */
1551
/* Ensures that allocated >= needed. Aborts through a jump to
1552
out_of_memory if needed is SIZE_MAX or otherwise too big. */
1553
#define ENSURE_ALLOCATION(needed) \
1554
if ((needed) > allocated) \
1556
size_t memory_size; \
1559
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1560
if ((needed) > allocated) \
1561
allocated = (needed); \
1562
memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1563
if (size_overflow_p (memory_size)) \
1564
goto out_of_memory; \
1565
if (result == resultbuf || result == NULL) \
1566
memory = (DCHAR_T *) malloc (memory_size); \
1568
memory = (DCHAR_T *) realloc (result, memory_size); \
1569
if (memory == NULL) \
1570
goto out_of_memory; \
1571
if (result == resultbuf && length > 0) \
1572
DCHAR_CPY (memory, result, length); \
1576
for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1578
if (cp != dp->dir_start)
1580
size_t n = dp->dir_start - cp;
1581
size_t augmented_length = xsum (length, n);
1583
ENSURE_ALLOCATION (augmented_length);
1584
/* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1585
need that the format string contains only ASCII characters
1586
if FCHAR_T and DCHAR_T are not the same type. */
1587
if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1589
DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1590
length = augmented_length;
1595
result[length++] = (unsigned char) *cp++;
1602
/* Execute a single directive. */
1603
if (dp->conversion == '%')
1605
size_t augmented_length;
1607
if (!(dp->arg_index == ARG_NONE))
1609
augmented_length = xsum (length, 1);
1610
ENSURE_ALLOCATION (augmented_length);
1611
result[length] = '%';
1612
length = augmented_length;
1616
if (!(dp->arg_index != ARG_NONE))
1619
if (dp->conversion == 'n')
1621
switch (a.arg[dp->arg_index].type)
1623
case TYPE_COUNT_SCHAR_POINTER:
1624
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1626
case TYPE_COUNT_SHORT_POINTER:
1627
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
1629
case TYPE_COUNT_INT_POINTER:
1630
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
1632
case TYPE_COUNT_LONGINT_POINTER:
1633
*a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1635
#if HAVE_LONG_LONG_INT
1636
case TYPE_COUNT_LONGLONGINT_POINTER:
1637
*a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1645
/* The unistdio extensions. */
1646
else if (dp->conversion == 'U')
1648
arg_type type = a.arg[dp->arg_index].type;
1649
int flags = dp->flags;
1657
if (dp->width_start != dp->width_end)
1659
if (dp->width_arg_index != ARG_NONE)
1663
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1665
arg = a.arg[dp->width_arg_index].a.a_int;
1668
/* "A negative field width is taken as a '-' flag
1669
followed by a positive field width." */
1671
width = (unsigned int) (-arg);
1678
const FCHAR_T *digitp = dp->width_start;
1681
width = xsum (xtimes (width, 10), *digitp++ - '0');
1682
while (digitp != dp->width_end);
1689
if (dp->precision_start != dp->precision_end)
1691
if (dp->precision_arg_index != ARG_NONE)
1695
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1697
arg = a.arg[dp->precision_arg_index].a.a_int;
1698
/* "A negative precision is taken as if the precision
1708
const FCHAR_T *digitp = dp->precision_start + 1;
1711
while (digitp != dp->precision_end)
1712
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1719
case TYPE_U8_STRING:
1721
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1722
const uint8_t *arg_end;
1727
/* Use only PRECISION characters, from the left. */
1730
for (; precision > 0; precision--)
1732
int count = u8_strmblen (arg_end);
1737
if (!(result == resultbuf || result == NULL))
1739
if (buf_malloced != NULL)
1740
free (buf_malloced);
1751
/* Use the entire string, and count the number of
1757
int count = u8_strmblen (arg_end);
1762
if (!(result == resultbuf || result == NULL))
1764
if (buf_malloced != NULL)
1765
free (buf_malloced);
1776
/* Use the entire string. */
1777
arg_end = arg + u8_strlen (arg);
1778
/* The number of characters doesn't matter. */
1782
if (has_width && width > characters
1783
&& !(dp->flags & FLAG_LEFT))
1785
size_t n = width - characters;
1786
ENSURE_ALLOCATION (xsum (length, n));
1787
DCHAR_SET (result + length, ' ', n);
1791
# if DCHAR_IS_UINT8_T
1793
size_t n = arg_end - arg;
1794
ENSURE_ALLOCATION (xsum (length, n));
1795
DCHAR_CPY (result + length, arg, n);
1800
DCHAR_T *converted = result + length;
1801
size_t converted_len = allocated - length;
1803
/* Convert from UTF-8 to locale encoding. */
1805
u8_conv_to_encoding (locale_charset (),
1806
iconveh_question_mark,
1807
arg, arg_end - arg, NULL,
1808
converted, &converted_len);
1810
/* Convert from UTF-8 to UTF-16/UTF-32. */
1812
U8_TO_DCHAR (arg, arg_end - arg,
1813
converted, &converted_len);
1815
if (converted == NULL)
1817
int saved_errno = errno;
1818
if (!(result == resultbuf || result == NULL))
1820
if (buf_malloced != NULL)
1821
free (buf_malloced);
1823
errno = saved_errno;
1826
if (converted != result + length)
1828
ENSURE_ALLOCATION (xsum (length, converted_len));
1829
DCHAR_CPY (result + length, converted, converted_len);
1832
length += converted_len;
1836
if (has_width && width > characters
1837
&& (dp->flags & FLAG_LEFT))
1839
size_t n = width - characters;
1840
ENSURE_ALLOCATION (xsum (length, n));
1841
DCHAR_SET (result + length, ' ', n);
1847
case TYPE_U16_STRING:
1849
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
1850
const uint16_t *arg_end;
1855
/* Use only PRECISION characters, from the left. */
1858
for (; precision > 0; precision--)
1860
int count = u16_strmblen (arg_end);
1865
if (!(result == resultbuf || result == NULL))
1867
if (buf_malloced != NULL)
1868
free (buf_malloced);
1879
/* Use the entire string, and count the number of
1885
int count = u16_strmblen (arg_end);
1890
if (!(result == resultbuf || result == NULL))
1892
if (buf_malloced != NULL)
1893
free (buf_malloced);
1904
/* Use the entire string. */
1905
arg_end = arg + u16_strlen (arg);
1906
/* The number of characters doesn't matter. */
1910
if (has_width && width > characters
1911
&& !(dp->flags & FLAG_LEFT))
1913
size_t n = width - characters;
1914
ENSURE_ALLOCATION (xsum (length, n));
1915
DCHAR_SET (result + length, ' ', n);
1919
# if DCHAR_IS_UINT16_T
1921
size_t n = arg_end - arg;
1922
ENSURE_ALLOCATION (xsum (length, n));
1923
DCHAR_CPY (result + length, arg, n);
1928
DCHAR_T *converted = result + length;
1929
size_t converted_len = allocated - length;
1931
/* Convert from UTF-16 to locale encoding. */
1933
u16_conv_to_encoding (locale_charset (),
1934
iconveh_question_mark,
1935
arg, arg_end - arg, NULL,
1936
converted, &converted_len);
1938
/* Convert from UTF-16 to UTF-8/UTF-32. */
1940
U16_TO_DCHAR (arg, arg_end - arg,
1941
converted, &converted_len);
1943
if (converted == NULL)
1945
int saved_errno = errno;
1946
if (!(result == resultbuf || result == NULL))
1948
if (buf_malloced != NULL)
1949
free (buf_malloced);
1951
errno = saved_errno;
1954
if (converted != result + length)
1956
ENSURE_ALLOCATION (xsum (length, converted_len));
1957
DCHAR_CPY (result + length, converted, converted_len);
1960
length += converted_len;
1964
if (has_width && width > characters
1965
&& (dp->flags & FLAG_LEFT))
1967
size_t n = width - characters;
1968
ENSURE_ALLOCATION (xsum (length, n));
1969
DCHAR_SET (result + length, ' ', n);
1975
case TYPE_U32_STRING:
1977
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
1978
const uint32_t *arg_end;
1983
/* Use only PRECISION characters, from the left. */
1986
for (; precision > 0; precision--)
1988
int count = u32_strmblen (arg_end);
1993
if (!(result == resultbuf || result == NULL))
1995
if (buf_malloced != NULL)
1996
free (buf_malloced);
2007
/* Use the entire string, and count the number of
2013
int count = u32_strmblen (arg_end);
2018
if (!(result == resultbuf || result == NULL))
2020
if (buf_malloced != NULL)
2021
free (buf_malloced);
2032
/* Use the entire string. */
2033
arg_end = arg + u32_strlen (arg);
2034
/* The number of characters doesn't matter. */
2038
if (has_width && width > characters
2039
&& !(dp->flags & FLAG_LEFT))
2041
size_t n = width - characters;
2042
ENSURE_ALLOCATION (xsum (length, n));
2043
DCHAR_SET (result + length, ' ', n);
2047
# if DCHAR_IS_UINT32_T
2049
size_t n = arg_end - arg;
2050
ENSURE_ALLOCATION (xsum (length, n));
2051
DCHAR_CPY (result + length, arg, n);
2056
DCHAR_T *converted = result + length;
2057
size_t converted_len = allocated - length;
2059
/* Convert from UTF-32 to locale encoding. */
2061
u32_conv_to_encoding (locale_charset (),
2062
iconveh_question_mark,
2063
arg, arg_end - arg, NULL,
2064
converted, &converted_len);
2066
/* Convert from UTF-32 to UTF-8/UTF-16. */
2068
U32_TO_DCHAR (arg, arg_end - arg,
2069
converted, &converted_len);
2071
if (converted == NULL)
2073
int saved_errno = errno;
2074
if (!(result == resultbuf || result == NULL))
2076
if (buf_malloced != NULL)
2077
free (buf_malloced);
2079
errno = saved_errno;
2082
if (converted != result + length)
2084
ENSURE_ALLOCATION (xsum (length, converted_len));
2085
DCHAR_CPY (result + length, converted, converted_len);
2088
length += converted_len;
2092
if (has_width && width > characters
2093
&& (dp->flags & FLAG_LEFT))
2095
size_t n = width - characters;
2096
ENSURE_ALLOCATION (xsum (length, n));
2097
DCHAR_SET (result + length, ' ', n);
2108
#if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2109
else if (dp->conversion == 's'
2110
# if WIDE_CHAR_VERSION
2111
&& a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2113
&& a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2117
/* The normal handling of the 's' directive below requires
2118
allocating a temporary buffer. The determination of its
2119
length (tmp_length), in the case when a precision is
2120
specified, below requires a conversion between a char[]
2121
string and a wchar_t[] wide string. It could be done, but
2122
we have no guarantee that the implementation of sprintf will
2123
use the exactly same algorithm. Without this guarantee, it
2124
is possible to have buffer overrun bugs. In order to avoid
2125
such bugs, we implement the entire processing of the 's'
2126
directive ourselves. */
2127
int flags = dp->flags;
2135
if (dp->width_start != dp->width_end)
2137
if (dp->width_arg_index != ARG_NONE)
2141
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2143
arg = a.arg[dp->width_arg_index].a.a_int;
2146
/* "A negative field width is taken as a '-' flag
2147
followed by a positive field width." */
2149
width = (unsigned int) (-arg);
2156
const FCHAR_T *digitp = dp->width_start;
2159
width = xsum (xtimes (width, 10), *digitp++ - '0');
2160
while (digitp != dp->width_end);
2167
if (dp->precision_start != dp->precision_end)
2169
if (dp->precision_arg_index != ARG_NONE)
2173
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2175
arg = a.arg[dp->precision_arg_index].a.a_int;
2176
/* "A negative precision is taken as if the precision
2186
const FCHAR_T *digitp = dp->precision_start + 1;
2189
while (digitp != dp->precision_end)
2190
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2195
# if WIDE_CHAR_VERSION
2196
/* %s in vasnwprintf. See the specification of fwprintf. */
2198
const char *arg = a.arg[dp->arg_index].a.a_string;
2199
const char *arg_end;
2204
/* Use only as many bytes as needed to produce PRECISION
2205
wide characters, from the left. */
2208
memset (&state, '\0', sizeof (mbstate_t));
2212
for (; precision > 0; precision--)
2216
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2218
count = mblen (arg_end, MB_CUR_MAX);
2221
/* Found the terminating NUL. */
2225
/* Invalid or incomplete multibyte character. */
2226
if (!(result == resultbuf || result == NULL))
2228
if (buf_malloced != NULL)
2229
free (buf_malloced);
2240
/* Use the entire string, and count the number of wide
2244
memset (&state, '\0', sizeof (mbstate_t));
2252
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2254
count = mblen (arg_end, MB_CUR_MAX);
2257
/* Found the terminating NUL. */
2261
/* Invalid or incomplete multibyte character. */
2262
if (!(result == resultbuf || result == NULL))
2264
if (buf_malloced != NULL)
2265
free (buf_malloced);
2276
/* Use the entire string. */
2277
arg_end = arg + strlen (arg);
2278
/* The number of characters doesn't matter. */
2282
if (has_width && width > characters
2283
&& !(dp->flags & FLAG_LEFT))
2285
size_t n = width - characters;
2286
ENSURE_ALLOCATION (xsum (length, n));
2287
DCHAR_SET (result + length, ' ', n);
2291
if (has_precision || has_width)
2293
/* We know the number of wide characters in advance. */
2297
memset (&state, '\0', sizeof (mbstate_t));
2299
ENSURE_ALLOCATION (xsum (length, characters));
2300
for (remaining = characters; remaining > 0; remaining--)
2305
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2307
count = mbtowc (&wc, arg, arg_end - arg);
2310
/* mbrtowc not consistent with mbrlen, or mbtowc
2311
not consistent with mblen. */
2313
result[length++] = wc;
2316
if (!(arg == arg_end))
2323
memset (&state, '\0', sizeof (mbstate_t));
2325
while (arg < arg_end)
2330
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2332
count = mbtowc (&wc, arg, arg_end - arg);
2335
/* mbrtowc not consistent with mbrlen, or mbtowc
2336
not consistent with mblen. */
2338
ENSURE_ALLOCATION (xsum (length, 1));
2339
result[length++] = wc;
2344
if (has_width && width > characters
2345
&& (dp->flags & FLAG_LEFT))
2347
size_t n = width - characters;
2348
ENSURE_ALLOCATION (xsum (length, n));
2349
DCHAR_SET (result + length, ' ', n);
2354
/* %ls in vasnprintf. See the specification of fprintf. */
2356
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
2357
const wchar_t *arg_end;
2359
# if !DCHAR_IS_TCHAR
2360
/* This code assumes that TCHAR_T is 'char'. */
2361
typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
2370
/* Use only as many wide characters as needed to produce
2371
at most PRECISION bytes, from the left. */
2372
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2374
memset (&state, '\0', sizeof (mbstate_t));
2378
while (precision > 0)
2380
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2384
/* Found the terminating null wide character. */
2386
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2387
count = wcrtomb (cbuf, *arg_end, &state);
2389
count = wctomb (cbuf, *arg_end);
2393
/* Cannot convert. */
2394
if (!(result == resultbuf || result == NULL))
2396
if (buf_malloced != NULL)
2397
free (buf_malloced);
2402
if (precision < count)
2405
characters += count;
2415
/* Use the entire string, and count the number of
2417
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2419
memset (&state, '\0', sizeof (mbstate_t));
2425
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2429
/* Found the terminating null wide character. */
2431
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2432
count = wcrtomb (cbuf, *arg_end, &state);
2434
count = wctomb (cbuf, *arg_end);
2438
/* Cannot convert. */
2439
if (!(result == resultbuf || result == NULL))
2441
if (buf_malloced != NULL)
2442
free (buf_malloced);
2448
characters += count;
2454
/* Use the entire string. */
2455
arg_end = arg + local_wcslen (arg);
2456
/* The number of bytes doesn't matter. */
2461
# if !DCHAR_IS_TCHAR
2462
/* Convert the string into a piece of temporary memory. */
2463
tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
2467
TCHAR_T *tmpptr = tmpsrc;
2469
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2471
memset (&state, '\0', sizeof (mbstate_t));
2473
for (remaining = characters; remaining > 0; )
2475
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2480
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2481
count = wcrtomb (cbuf, *arg, &state);
2483
count = wctomb (cbuf, *arg);
2486
/* Inconsistency. */
2488
memcpy (tmpptr, cbuf, count);
2493
if (!(arg == arg_end))
2497
/* Convert from TCHAR_T[] to DCHAR_T[]. */
2499
DCHAR_CONV_FROM_ENCODING (locale_charset (),
2500
iconveh_question_mark,
2506
int saved_errno = errno;
2508
if (!(result == resultbuf || result == NULL))
2510
if (buf_malloced != NULL)
2511
free (buf_malloced);
2513
errno = saved_errno;
2521
# if ENABLE_UNISTDIO
2522
/* Outside POSIX, it's preferrable to compare the width
2523
against the number of _characters_ of the converted
2525
w = DCHAR_MBSNLEN (result + length, characters);
2527
/* The width is compared against the number of _bytes_
2528
of the converted value, says POSIX. */
2533
/* w doesn't matter. */
2536
if (has_width && width > w
2537
&& !(dp->flags & FLAG_LEFT))
2539
size_t n = width - w;
2540
ENSURE_ALLOCATION (xsum (length, n));
2541
DCHAR_SET (result + length, ' ', n);
2546
if (has_precision || has_width)
2548
/* We know the number of bytes in advance. */
2550
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2552
memset (&state, '\0', sizeof (mbstate_t));
2554
ENSURE_ALLOCATION (xsum (length, characters));
2555
for (remaining = characters; remaining > 0; )
2557
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2562
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2563
count = wcrtomb (cbuf, *arg, &state);
2565
count = wctomb (cbuf, *arg);
2568
/* Inconsistency. */
2570
memcpy (result + length, cbuf, count);
2575
if (!(arg == arg_end))
2580
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2582
memset (&state, '\0', sizeof (mbstate_t));
2584
while (arg < arg_end)
2586
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2591
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2592
count = wcrtomb (cbuf, *arg, &state);
2594
count = wctomb (cbuf, *arg);
2597
/* Inconsistency. */
2599
ENSURE_ALLOCATION (xsum (length, count));
2600
memcpy (result + length, cbuf, count);
2606
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2607
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2609
length += tmpdst_len;
2612
if (has_width && width > w
2613
&& (dp->flags & FLAG_LEFT))
2615
size_t n = width - w;
2616
ENSURE_ALLOCATION (xsum (length, n));
2617
DCHAR_SET (result + length, ' ', n);
2624
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2625
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2626
# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2628
# if NEED_PRINTF_DOUBLE
2629
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2631
# if NEED_PRINTF_LONG_DOUBLE
2632
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2638
arg_type type = a.arg[dp->arg_index].type;
2639
int flags = dp->flags;
2645
DCHAR_T tmpbuf[700];
2652
if (dp->width_start != dp->width_end)
2654
if (dp->width_arg_index != ARG_NONE)
2658
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2660
arg = a.arg[dp->width_arg_index].a.a_int;
2663
/* "A negative field width is taken as a '-' flag
2664
followed by a positive field width." */
2666
width = (unsigned int) (-arg);
2673
const FCHAR_T *digitp = dp->width_start;
2676
width = xsum (xtimes (width, 10), *digitp++ - '0');
2677
while (digitp != dp->width_end);
2684
if (dp->precision_start != dp->precision_end)
2686
if (dp->precision_arg_index != ARG_NONE)
2690
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2692
arg = a.arg[dp->precision_arg_index].a.a_int;
2693
/* "A negative precision is taken as if the precision
2703
const FCHAR_T *digitp = dp->precision_start + 1;
2706
while (digitp != dp->precision_end)
2707
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2712
/* Allocate a temporary buffer of sufficient size. */
2713
if (type == TYPE_LONGDOUBLE)
2715
(unsigned int) ((LDBL_DIG + 1)
2716
* 0.831 /* decimal -> hexadecimal */
2718
+ 1; /* turn floor into ceil */
2721
(unsigned int) ((DBL_DIG + 1)
2722
* 0.831 /* decimal -> hexadecimal */
2724
+ 1; /* turn floor into ceil */
2725
if (tmp_length < precision)
2726
tmp_length = precision;
2727
/* Account for sign, decimal point etc. */
2728
tmp_length = xsum (tmp_length, 12);
2730
if (tmp_length < width)
2733
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2735
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2739
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2741
if (size_overflow_p (tmp_memsize))
2742
/* Overflow, would lead to out of memory. */
2744
tmp = (DCHAR_T *) malloc (tmp_memsize);
2746
/* Out of memory. */
2752
if (type == TYPE_LONGDOUBLE)
2754
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2755
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2759
if (dp->conversion == 'A')
2761
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2765
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2771
DECL_LONG_DOUBLE_ROUNDING
2773
BEGIN_LONG_DOUBLE_ROUNDING ();
2775
if (signbit (arg)) /* arg < 0.0L or negative zero */
2783
else if (flags & FLAG_SHOWSIGN)
2785
else if (flags & FLAG_SPACE)
2788
if (arg > 0.0L && arg + arg == arg)
2790
if (dp->conversion == 'A')
2792
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2796
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2802
long double mantissa;
2805
mantissa = printf_frexpl (arg, &exponent);
2813
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2815
/* Round the mantissa. */
2816
long double tail = mantissa;
2819
for (q = precision; ; q--)
2821
int digit = (int) tail;
2825
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
2834
for (q = precision; q > 0; q--)
2840
*p++ = dp->conversion - 'A' + 'X';
2845
digit = (int) mantissa;
2848
if ((flags & FLAG_ALT)
2849
|| mantissa > 0.0L || precision > 0)
2851
*p++ = decimal_point_char ();
2852
/* This loop terminates because we assume
2853
that FLT_RADIX is a power of 2. */
2854
while (mantissa > 0.0L)
2857
digit = (int) mantissa;
2862
: dp->conversion - 10);
2866
while (precision > 0)
2873
*p++ = dp->conversion - 'A' + 'P';
2874
# if WIDE_CHAR_VERSION
2876
static const wchar_t decimal_format[] =
2877
{ '%', '+', 'd', '\0' };
2878
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2883
if (sizeof (DCHAR_T) == 1)
2885
sprintf ((char *) p, "%+d", exponent);
2893
sprintf (expbuf, "%+d", exponent);
2894
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2900
END_LONG_DOUBLE_ROUNDING ();
2908
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2909
double arg = a.arg[dp->arg_index].a.a_double;
2913
if (dp->conversion == 'A')
2915
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2919
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2926
if (signbit (arg)) /* arg < 0.0 or negative zero */
2934
else if (flags & FLAG_SHOWSIGN)
2936
else if (flags & FLAG_SPACE)
2939
if (arg > 0.0 && arg + arg == arg)
2941
if (dp->conversion == 'A')
2943
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2947
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2956
mantissa = printf_frexp (arg, &exponent);
2964
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2966
/* Round the mantissa. */
2967
double tail = mantissa;
2970
for (q = precision; ; q--)
2972
int digit = (int) tail;
2976
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
2985
for (q = precision; q > 0; q--)
2991
*p++ = dp->conversion - 'A' + 'X';
2996
digit = (int) mantissa;
2999
if ((flags & FLAG_ALT)
3000
|| mantissa > 0.0 || precision > 0)
3002
*p++ = decimal_point_char ();
3003
/* This loop terminates because we assume
3004
that FLT_RADIX is a power of 2. */
3005
while (mantissa > 0.0)
3008
digit = (int) mantissa;
3013
: dp->conversion - 10);
3017
while (precision > 0)
3024
*p++ = dp->conversion - 'A' + 'P';
3025
# if WIDE_CHAR_VERSION
3027
static const wchar_t decimal_format[] =
3028
{ '%', '+', 'd', '\0' };
3029
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3034
if (sizeof (DCHAR_T) == 1)
3036
sprintf ((char *) p, "%+d", exponent);
3044
sprintf (expbuf, "%+d", exponent);
3045
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3055
/* The generated string now extends from tmp to p, with the
3056
zero padding insertion point being at pad_ptr. */
3057
if (has_width && p - tmp < width)
3059
size_t pad = width - (p - tmp);
3060
DCHAR_T *end = p + pad;
3062
if (flags & FLAG_LEFT)
3064
/* Pad with spaces on the right. */
3065
for (; pad > 0; pad--)
3068
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3070
/* Pad with zeroes. */
3075
for (; pad > 0; pad--)
3080
/* Pad with spaces on the left. */
3085
for (; pad > 0; pad--)
3093
size_t count = p - tmp;
3095
if (count >= tmp_length)
3096
/* tmp_length was incorrectly calculated - fix the
3100
/* Make room for the result. */
3101
if (count >= allocated - length)
3103
size_t n = xsum (length, count);
3105
ENSURE_ALLOCATION (n);
3108
/* Append the result. */
3109
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3116
#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3117
else if ((dp->conversion == 'f' || dp->conversion == 'F'
3118
|| dp->conversion == 'e' || dp->conversion == 'E'
3119
|| dp->conversion == 'g' || dp->conversion == 'G'
3120
|| dp->conversion == 'a' || dp->conversion == 'A')
3122
# if NEED_PRINTF_DOUBLE
3123
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
3124
# elif NEED_PRINTF_INFINITE_DOUBLE
3125
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
3126
/* The systems (mingw) which produce wrong output
3127
for Inf, -Inf, and NaN also do so for -0.0.
3128
Therefore we treat this case here as well. */
3129
&& is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
3131
# if NEED_PRINTF_LONG_DOUBLE
3132
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3133
# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3134
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3135
/* Some systems produce wrong output for Inf,
3136
-Inf, and NaN. Some systems in this category
3137
(IRIX 5.3) also do so for -0.0. Therefore we
3138
treat this case here as well. */
3139
&& is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
3143
# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3144
arg_type type = a.arg[dp->arg_index].type;
3146
int flags = dp->flags;
3152
DCHAR_T tmpbuf[700];
3159
if (dp->width_start != dp->width_end)
3161
if (dp->width_arg_index != ARG_NONE)
3165
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3167
arg = a.arg[dp->width_arg_index].a.a_int;
3170
/* "A negative field width is taken as a '-' flag
3171
followed by a positive field width." */
3173
width = (unsigned int) (-arg);
3180
const FCHAR_T *digitp = dp->width_start;
3183
width = xsum (xtimes (width, 10), *digitp++ - '0');
3184
while (digitp != dp->width_end);
3191
if (dp->precision_start != dp->precision_end)
3193
if (dp->precision_arg_index != ARG_NONE)
3197
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3199
arg = a.arg[dp->precision_arg_index].a.a_int;
3200
/* "A negative precision is taken as if the precision
3210
const FCHAR_T *digitp = dp->precision_start + 1;
3213
while (digitp != dp->precision_end)
3214
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3219
/* POSIX specifies the default precision to be 6 for %f, %F,
3220
%e, %E, but not for %g, %G. Implementations appear to use
3221
the same default precision also for %g, %G. But for %a, %A,
3222
the default precision is 0. */
3224
if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3227
/* Allocate a temporary buffer of sufficient size. */
3228
# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3229
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3230
# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3231
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3232
# elif NEED_PRINTF_LONG_DOUBLE
3233
tmp_length = LDBL_DIG + 1;
3234
# elif NEED_PRINTF_DOUBLE
3235
tmp_length = DBL_DIG + 1;
3239
if (tmp_length < precision)
3240
tmp_length = precision;
3241
# if NEED_PRINTF_LONG_DOUBLE
3242
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3243
if (type == TYPE_LONGDOUBLE)
3245
if (dp->conversion == 'f' || dp->conversion == 'F')
3247
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3248
if (!(isnanl (arg) || arg + arg == arg))
3250
/* arg is finite and nonzero. */
3251
int exponent = floorlog10l (arg < 0 ? -arg : arg);
3252
if (exponent >= 0 && tmp_length < exponent + precision)
3253
tmp_length = exponent + precision;
3257
# if NEED_PRINTF_DOUBLE
3258
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3259
if (type == TYPE_DOUBLE)
3261
if (dp->conversion == 'f' || dp->conversion == 'F')
3263
double arg = a.arg[dp->arg_index].a.a_double;
3264
if (!(isnand (arg) || arg + arg == arg))
3266
/* arg is finite and nonzero. */
3267
int exponent = floorlog10 (arg < 0 ? -arg : arg);
3268
if (exponent >= 0 && tmp_length < exponent + precision)
3269
tmp_length = exponent + precision;
3273
/* Account for sign, decimal point etc. */
3274
tmp_length = xsum (tmp_length, 12);
3276
if (tmp_length < width)
3279
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3281
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3285
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3287
if (size_overflow_p (tmp_memsize))
3288
/* Overflow, would lead to out of memory. */
3290
tmp = (DCHAR_T *) malloc (tmp_memsize);
3292
/* Out of memory. */
3299
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3300
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3301
if (type == TYPE_LONGDOUBLE)
3304
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3308
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3310
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3314
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3320
DECL_LONG_DOUBLE_ROUNDING
3322
BEGIN_LONG_DOUBLE_ROUNDING ();
3324
if (signbit (arg)) /* arg < 0.0L or negative zero */
3332
else if (flags & FLAG_SHOWSIGN)
3334
else if (flags & FLAG_SPACE)
3337
if (arg > 0.0L && arg + arg == arg)
3339
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3341
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3345
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3350
# if NEED_PRINTF_LONG_DOUBLE
3353
if (dp->conversion == 'f' || dp->conversion == 'F')
3359
scale10_round_decimal_long_double (arg, precision);
3362
END_LONG_DOUBLE_ROUNDING ();
3365
ndigits = strlen (digits);
3367
if (ndigits > precision)
3371
*p++ = digits[ndigits];
3373
while (ndigits > precision);
3376
/* Here ndigits <= precision. */
3377
if ((flags & FLAG_ALT) || precision > 0)
3379
*p++ = decimal_point_char ();
3380
for (; precision > ndigits; precision--)
3385
*p++ = digits[ndigits];
3391
else if (dp->conversion == 'e' || dp->conversion == 'E')
3399
if ((flags & FLAG_ALT) || precision > 0)
3401
*p++ = decimal_point_char ();
3402
for (; precision > 0; precision--)
3413
exponent = floorlog10l (arg);
3418
scale10_round_decimal_long_double (arg,
3419
(int)precision - exponent);
3422
END_LONG_DOUBLE_ROUNDING ();
3425
ndigits = strlen (digits);
3427
if (ndigits == precision + 1)
3429
if (ndigits < precision
3430
|| ndigits > precision + 2)
3431
/* The exponent was not guessed
3432
precisely enough. */
3435
/* None of two values of exponent is
3436
the right one. Prevent an endless
3440
if (ndigits == precision)
3446
/* Here ndigits = precision+1. */
3447
if (is_borderline (digits, precision))
3449
/* Maybe the exponent guess was too high
3450
and a smaller exponent can be reached
3451
by turning a 10...0 into 9...9x. */
3453
scale10_round_decimal_long_double (arg,
3454
(int)precision - exponent + 1);
3455
if (digits2 == NULL)
3458
END_LONG_DOUBLE_ROUNDING ();
3461
if (strlen (digits2) == precision + 1)
3470
/* Here ndigits = precision+1. */
3472
*p++ = digits[--ndigits];
3473
if ((flags & FLAG_ALT) || precision > 0)
3475
*p++ = decimal_point_char ();
3479
*p++ = digits[ndigits];
3486
*p++ = dp->conversion; /* 'e' or 'E' */
3487
# if WIDE_CHAR_VERSION
3489
static const wchar_t decimal_format[] =
3490
{ '%', '+', '.', '2', 'd', '\0' };
3491
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3496
if (sizeof (DCHAR_T) == 1)
3498
sprintf ((char *) p, "%+.2d", exponent);
3506
sprintf (expbuf, "%+.2d", exponent);
3507
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3512
else if (dp->conversion == 'g' || dp->conversion == 'G')
3516
/* precision >= 1. */
3519
/* The exponent is 0, >= -4, < precision.
3520
Use fixed-point notation. */
3522
size_t ndigits = precision;
3523
/* Number of trailing zeroes that have to be
3526
(flags & FLAG_ALT ? 0 : precision - 1);
3530
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3532
*p++ = decimal_point_char ();
3533
while (ndigits > nzeroes)
3549
exponent = floorlog10l (arg);
3554
scale10_round_decimal_long_double (arg,
3555
(int)(precision - 1) - exponent);
3558
END_LONG_DOUBLE_ROUNDING ();
3561
ndigits = strlen (digits);
3563
if (ndigits == precision)
3565
if (ndigits < precision - 1
3566
|| ndigits > precision + 1)
3567
/* The exponent was not guessed
3568
precisely enough. */
3571
/* None of two values of exponent is
3572
the right one. Prevent an endless
3576
if (ndigits < precision)
3582
/* Here ndigits = precision. */
3583
if (is_borderline (digits, precision - 1))
3585
/* Maybe the exponent guess was too high
3586
and a smaller exponent can be reached
3587
by turning a 10...0 into 9...9x. */
3589
scale10_round_decimal_long_double (arg,
3590
(int)(precision - 1) - exponent + 1);
3591
if (digits2 == NULL)
3594
END_LONG_DOUBLE_ROUNDING ();
3597
if (strlen (digits2) == precision)
3606
/* Here ndigits = precision. */
3608
/* Determine the number of trailing zeroes
3609
that have to be dropped. */
3611
if ((flags & FLAG_ALT) == 0)
3612
while (nzeroes < ndigits
3613
&& digits[nzeroes] == '0')
3616
/* The exponent is now determined. */
3618
&& exponent < (long)precision)
3620
/* Fixed-point notation:
3621
max(exponent,0)+1 digits, then the
3622
decimal point, then the remaining
3623
digits without trailing zeroes. */
3626
size_t count = exponent + 1;
3627
/* Note: count <= precision = ndigits. */
3628
for (; count > 0; count--)
3629
*p++ = digits[--ndigits];
3630
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3632
*p++ = decimal_point_char ();
3633
while (ndigits > nzeroes)
3636
*p++ = digits[ndigits];
3642
size_t count = -exponent - 1;
3644
*p++ = decimal_point_char ();
3645
for (; count > 0; count--)
3647
while (ndigits > nzeroes)
3650
*p++ = digits[ndigits];
3656
/* Exponential notation. */
3657
*p++ = digits[--ndigits];
3658
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3660
*p++ = decimal_point_char ();
3661
while (ndigits > nzeroes)
3664
*p++ = digits[ndigits];
3667
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3668
# if WIDE_CHAR_VERSION
3670
static const wchar_t decimal_format[] =
3671
{ '%', '+', '.', '2', 'd', '\0' };
3672
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3677
if (sizeof (DCHAR_T) == 1)
3679
sprintf ((char *) p, "%+.2d", exponent);
3687
sprintf (expbuf, "%+.2d", exponent);
3688
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3700
/* arg is finite. */
3706
if (dp->conversion == 'f' || dp->conversion == 'F')
3709
if ((flags & FLAG_ALT) || precision > 0)
3711
*p++ = decimal_point_char ();
3712
for (; precision > 0; precision--)
3716
else if (dp->conversion == 'e' || dp->conversion == 'E')
3719
if ((flags & FLAG_ALT) || precision > 0)
3721
*p++ = decimal_point_char ();
3722
for (; precision > 0; precision--)
3725
*p++ = dp->conversion; /* 'e' or 'E' */
3730
else if (dp->conversion == 'g' || dp->conversion == 'G')
3733
if (flags & FLAG_ALT)
3736
(precision > 0 ? precision - 1 : 0);
3737
*p++ = decimal_point_char ();
3738
for (; ndigits > 0; --ndigits)
3742
else if (dp->conversion == 'a' || dp->conversion == 'A')
3745
*p++ = dp->conversion - 'A' + 'X';
3748
if ((flags & FLAG_ALT) || precision > 0)
3750
*p++ = decimal_point_char ();
3751
for (; precision > 0; precision--)
3754
*p++ = dp->conversion - 'A' + 'P';
3763
END_LONG_DOUBLE_ROUNDING ();
3766
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3770
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3772
double arg = a.arg[dp->arg_index].a.a_double;
3776
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3778
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3782
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3789
if (signbit (arg)) /* arg < 0.0 or negative zero */
3797
else if (flags & FLAG_SHOWSIGN)
3799
else if (flags & FLAG_SPACE)
3802
if (arg > 0.0 && arg + arg == arg)
3804
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3806
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3810
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3815
# if NEED_PRINTF_DOUBLE
3818
if (dp->conversion == 'f' || dp->conversion == 'F')
3824
scale10_round_decimal_double (arg, precision);
3827
ndigits = strlen (digits);
3829
if (ndigits > precision)
3833
*p++ = digits[ndigits];
3835
while (ndigits > precision);
3838
/* Here ndigits <= precision. */
3839
if ((flags & FLAG_ALT) || precision > 0)
3841
*p++ = decimal_point_char ();
3842
for (; precision > ndigits; precision--)
3847
*p++ = digits[ndigits];
3853
else if (dp->conversion == 'e' || dp->conversion == 'E')
3861
if ((flags & FLAG_ALT) || precision > 0)
3863
*p++ = decimal_point_char ();
3864
for (; precision > 0; precision--)
3875
exponent = floorlog10 (arg);
3880
scale10_round_decimal_double (arg,
3881
(int)precision - exponent);
3884
ndigits = strlen (digits);
3886
if (ndigits == precision + 1)
3888
if (ndigits < precision
3889
|| ndigits > precision + 2)
3890
/* The exponent was not guessed
3891
precisely enough. */
3894
/* None of two values of exponent is
3895
the right one. Prevent an endless
3899
if (ndigits == precision)
3905
/* Here ndigits = precision+1. */
3906
if (is_borderline (digits, precision))
3908
/* Maybe the exponent guess was too high
3909
and a smaller exponent can be reached
3910
by turning a 10...0 into 9...9x. */
3912
scale10_round_decimal_double (arg,
3913
(int)precision - exponent + 1);
3914
if (digits2 == NULL)
3919
if (strlen (digits2) == precision + 1)
3928
/* Here ndigits = precision+1. */
3930
*p++ = digits[--ndigits];
3931
if ((flags & FLAG_ALT) || precision > 0)
3933
*p++ = decimal_point_char ();
3937
*p++ = digits[ndigits];
3944
*p++ = dp->conversion; /* 'e' or 'E' */
3945
# if WIDE_CHAR_VERSION
3947
static const wchar_t decimal_format[] =
3948
/* Produce the same number of exponent digits
3949
as the native printf implementation. */
3950
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3951
{ '%', '+', '.', '3', 'd', '\0' };
3953
{ '%', '+', '.', '2', 'd', '\0' };
3955
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3961
static const char decimal_format[] =
3962
/* Produce the same number of exponent digits
3963
as the native printf implementation. */
3964
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3969
if (sizeof (DCHAR_T) == 1)
3971
sprintf ((char *) p, decimal_format, exponent);
3979
sprintf (expbuf, decimal_format, exponent);
3980
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3986
else if (dp->conversion == 'g' || dp->conversion == 'G')
3990
/* precision >= 1. */
3993
/* The exponent is 0, >= -4, < precision.
3994
Use fixed-point notation. */
3996
size_t ndigits = precision;
3997
/* Number of trailing zeroes that have to be
4000
(flags & FLAG_ALT ? 0 : precision - 1);
4004
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4006
*p++ = decimal_point_char ();
4007
while (ndigits > nzeroes)
4023
exponent = floorlog10 (arg);
4028
scale10_round_decimal_double (arg,
4029
(int)(precision - 1) - exponent);
4032
ndigits = strlen (digits);
4034
if (ndigits == precision)
4036
if (ndigits < precision - 1
4037
|| ndigits > precision + 1)
4038
/* The exponent was not guessed
4039
precisely enough. */
4042
/* None of two values of exponent is
4043
the right one. Prevent an endless
4047
if (ndigits < precision)
4053
/* Here ndigits = precision. */
4054
if (is_borderline (digits, precision - 1))
4056
/* Maybe the exponent guess was too high
4057
and a smaller exponent can be reached
4058
by turning a 10...0 into 9...9x. */
4060
scale10_round_decimal_double (arg,
4061
(int)(precision - 1) - exponent + 1);
4062
if (digits2 == NULL)
4067
if (strlen (digits2) == precision)
4076
/* Here ndigits = precision. */
4078
/* Determine the number of trailing zeroes
4079
that have to be dropped. */
4081
if ((flags & FLAG_ALT) == 0)
4082
while (nzeroes < ndigits
4083
&& digits[nzeroes] == '0')
4086
/* The exponent is now determined. */
4088
&& exponent < (long)precision)
4090
/* Fixed-point notation:
4091
max(exponent,0)+1 digits, then the
4092
decimal point, then the remaining
4093
digits without trailing zeroes. */
4096
size_t count = exponent + 1;
4097
/* Note: count <= precision = ndigits. */
4098
for (; count > 0; count--)
4099
*p++ = digits[--ndigits];
4100
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4102
*p++ = decimal_point_char ();
4103
while (ndigits > nzeroes)
4106
*p++ = digits[ndigits];
4112
size_t count = -exponent - 1;
4114
*p++ = decimal_point_char ();
4115
for (; count > 0; count--)
4117
while (ndigits > nzeroes)
4120
*p++ = digits[ndigits];
4126
/* Exponential notation. */
4127
*p++ = digits[--ndigits];
4128
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4130
*p++ = decimal_point_char ();
4131
while (ndigits > nzeroes)
4134
*p++ = digits[ndigits];
4137
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
4138
# if WIDE_CHAR_VERSION
4140
static const wchar_t decimal_format[] =
4141
/* Produce the same number of exponent digits
4142
as the native printf implementation. */
4143
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4144
{ '%', '+', '.', '3', 'd', '\0' };
4146
{ '%', '+', '.', '2', 'd', '\0' };
4148
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4154
static const char decimal_format[] =
4155
/* Produce the same number of exponent digits
4156
as the native printf implementation. */
4157
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4162
if (sizeof (DCHAR_T) == 1)
4164
sprintf ((char *) p, decimal_format, exponent);
4172
sprintf (expbuf, decimal_format, exponent);
4173
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4186
/* arg is finite. */
4192
if (dp->conversion == 'f' || dp->conversion == 'F')
4195
if ((flags & FLAG_ALT) || precision > 0)
4197
*p++ = decimal_point_char ();
4198
for (; precision > 0; precision--)
4202
else if (dp->conversion == 'e' || dp->conversion == 'E')
4205
if ((flags & FLAG_ALT) || precision > 0)
4207
*p++ = decimal_point_char ();
4208
for (; precision > 0; precision--)
4211
*p++ = dp->conversion; /* 'e' or 'E' */
4213
/* Produce the same number of exponent digits as
4214
the native printf implementation. */
4215
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4221
else if (dp->conversion == 'g' || dp->conversion == 'G')
4224
if (flags & FLAG_ALT)
4227
(precision > 0 ? precision - 1 : 0);
4228
*p++ = decimal_point_char ();
4229
for (; ndigits > 0; --ndigits)
4241
/* The generated string now extends from tmp to p, with the
4242
zero padding insertion point being at pad_ptr. */
4243
if (has_width && p - tmp < width)
4245
size_t pad = width - (p - tmp);
4246
DCHAR_T *end = p + pad;
4248
if (flags & FLAG_LEFT)
4250
/* Pad with spaces on the right. */
4251
for (; pad > 0; pad--)
4254
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4256
/* Pad with zeroes. */
4261
for (; pad > 0; pad--)
4266
/* Pad with spaces on the left. */
4271
for (; pad > 0; pad--)
4279
size_t count = p - tmp;
4281
if (count >= tmp_length)
4282
/* tmp_length was incorrectly calculated - fix the
4286
/* Make room for the result. */
4287
if (count >= allocated - length)
4289
size_t n = xsum (length, count);
4291
ENSURE_ALLOCATION (n);
4294
/* Append the result. */
4295
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4304
arg_type type = a.arg[dp->arg_index].type;
4305
int flags = dp->flags;
4306
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4310
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4314
#if NEED_PRINTF_UNBOUNDED_PRECISION
4317
# define prec_ourselves 0
4319
#if NEED_PRINTF_FLAG_LEFTADJUST
4320
# define pad_ourselves 1
4321
#elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4324
# define pad_ourselves 0
4327
unsigned int prefix_count;
4328
int prefixes[2] IF_LINT (= { 0 });
4331
TCHAR_T tmpbuf[700];
4335
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4338
if (dp->width_start != dp->width_end)
4340
if (dp->width_arg_index != ARG_NONE)
4344
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4346
arg = a.arg[dp->width_arg_index].a.a_int;
4349
/* "A negative field width is taken as a '-' flag
4350
followed by a positive field width." */
4352
width = (unsigned int) (-arg);
4359
const FCHAR_T *digitp = dp->width_start;
4362
width = xsum (xtimes (width, 10), *digitp++ - '0');
4363
while (digitp != dp->width_end);
4369
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4372
if (dp->precision_start != dp->precision_end)
4374
if (dp->precision_arg_index != ARG_NONE)
4378
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4380
arg = a.arg[dp->precision_arg_index].a.a_int;
4381
/* "A negative precision is taken as if the precision
4391
const FCHAR_T *digitp = dp->precision_start + 1;
4394
while (digitp != dp->precision_end)
4395
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
4401
/* Decide whether to handle the precision ourselves. */
4402
#if NEED_PRINTF_UNBOUNDED_PRECISION
4403
switch (dp->conversion)
4405
case 'd': case 'i': case 'u':
4407
case 'x': case 'X': case 'p':
4408
prec_ourselves = has_precision && (precision > 0);
4416
/* Decide whether to perform the padding ourselves. */
4417
#if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4418
switch (dp->conversion)
4420
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4421
/* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4422
to perform the padding after this conversion. Functions
4423
with unistdio extensions perform the padding based on
4424
character count rather than element count. */
4427
# if NEED_PRINTF_FLAG_ZERO
4428
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4434
pad_ourselves = prec_ourselves;
4440
/* Allocate a temporary buffer of sufficient size for calling
4443
switch (dp->conversion)
4446
case 'd': case 'i': case 'u':
4447
# if HAVE_LONG_LONG_INT
4448
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4450
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4451
* 0.30103 /* binary -> decimal */
4453
+ 1; /* turn floor into ceil */
4456
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4458
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4459
* 0.30103 /* binary -> decimal */
4461
+ 1; /* turn floor into ceil */
4464
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4465
* 0.30103 /* binary -> decimal */
4467
+ 1; /* turn floor into ceil */
4468
if (tmp_length < precision)
4469
tmp_length = precision;
4470
/* Multiply by 2, as an estimate for FLAG_GROUP. */
4471
tmp_length = xsum (tmp_length, tmp_length);
4472
/* Add 1, to account for a leading sign. */
4473
tmp_length = xsum (tmp_length, 1);
4477
# if HAVE_LONG_LONG_INT
4478
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4480
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4481
* 0.333334 /* binary -> octal */
4483
+ 1; /* turn floor into ceil */
4486
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4488
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4489
* 0.333334 /* binary -> octal */
4491
+ 1; /* turn floor into ceil */
4494
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4495
* 0.333334 /* binary -> octal */
4497
+ 1; /* turn floor into ceil */
4498
if (tmp_length < precision)
4499
tmp_length = precision;
4500
/* Add 1, to account for a leading sign. */
4501
tmp_length = xsum (tmp_length, 1);
4505
# if HAVE_LONG_LONG_INT
4506
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4508
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4509
* 0.25 /* binary -> hexadecimal */
4511
+ 1; /* turn floor into ceil */
4514
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4516
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4517
* 0.25 /* binary -> hexadecimal */
4519
+ 1; /* turn floor into ceil */
4522
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4523
* 0.25 /* binary -> hexadecimal */
4525
+ 1; /* turn floor into ceil */
4526
if (tmp_length < precision)
4527
tmp_length = precision;
4528
/* Add 2, to account for a leading sign or alternate form. */
4529
tmp_length = xsum (tmp_length, 2);
4533
if (type == TYPE_LONGDOUBLE)
4535
(unsigned int) (LDBL_MAX_EXP
4536
* 0.30103 /* binary -> decimal */
4537
* 2 /* estimate for FLAG_GROUP */
4539
+ 1 /* turn floor into ceil */
4540
+ 10; /* sign, decimal point etc. */
4543
(unsigned int) (DBL_MAX_EXP
4544
* 0.30103 /* binary -> decimal */
4545
* 2 /* estimate for FLAG_GROUP */
4547
+ 1 /* turn floor into ceil */
4548
+ 10; /* sign, decimal point etc. */
4549
tmp_length = xsum (tmp_length, precision);
4552
case 'e': case 'E': case 'g': case 'G':
4554
12; /* sign, decimal point, exponent etc. */
4555
tmp_length = xsum (tmp_length, precision);
4559
if (type == TYPE_LONGDOUBLE)
4561
(unsigned int) (LDBL_DIG
4562
* 0.831 /* decimal -> hexadecimal */
4564
+ 1; /* turn floor into ceil */
4567
(unsigned int) (DBL_DIG
4568
* 0.831 /* decimal -> hexadecimal */
4570
+ 1; /* turn floor into ceil */
4571
if (tmp_length < precision)
4572
tmp_length = precision;
4573
/* Account for sign, decimal point etc. */
4574
tmp_length = xsum (tmp_length, 12);
4578
# if HAVE_WINT_T && !WIDE_CHAR_VERSION
4579
if (type == TYPE_WIDE_CHAR)
4580
tmp_length = MB_CUR_MAX;
4588
if (type == TYPE_WIDE_STRING)
4590
# if WIDE_CHAR_VERSION
4591
/* ISO C says about %ls in fwprintf:
4592
"If the precision is not specified or is greater
4593
than the size of the array, the array shall
4594
contain a null wide character."
4595
So if there is a precision, we must not use
4597
const wchar_t *arg =
4598
a.arg[dp->arg_index].a.a_wide_string;
4601
tmp_length = local_wcsnlen (arg, precision);
4603
tmp_length = local_wcslen (arg);
4605
/* ISO C says about %ls in fprintf:
4606
"If a precision is specified, no more than that
4607
many bytes are written (including shift
4608
sequences, if any), and the array shall contain
4609
a null wide character if, to equal the
4610
multibyte character sequence length given by
4611
the precision, the function would need to
4612
access a wide character one past the end of the
4614
So if there is a precision, we must not use
4616
/* This case has already been handled above. */
4623
# if WIDE_CHAR_VERSION
4624
/* ISO C says about %s in fwprintf:
4625
"If the precision is not specified or is greater
4626
than the size of the converted array, the
4627
converted array shall contain a null wide
4629
So if there is a precision, we must not use
4631
/* This case has already been handled above. */
4634
/* ISO C says about %s in fprintf:
4635
"If the precision is not specified or greater
4636
than the size of the array, the array shall
4637
contain a null character."
4638
So if there is a precision, we must not use
4640
const char *arg = a.arg[dp->arg_index].a.a_string;
4643
tmp_length = local_strnlen (arg, precision);
4645
tmp_length = strlen (arg);
4652
(unsigned int) (sizeof (void *) * CHAR_BIT
4653
* 0.25 /* binary -> hexadecimal */
4655
+ 1 /* turn floor into ceil */
4656
+ 2; /* account for leading 0x */
4665
# if ENABLE_UNISTDIO
4666
/* Padding considers the number of characters, therefore
4667
the number of elements after padding may be
4668
> max (tmp_length, width)
4670
<= tmp_length + width. */
4671
tmp_length = xsum (tmp_length, width);
4673
/* Padding considers the number of elements,
4675
if (tmp_length < width)
4680
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
4683
if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4687
size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4689
if (size_overflow_p (tmp_memsize))
4690
/* Overflow, would lead to out of memory. */
4692
tmp = (TCHAR_T *) malloc (tmp_memsize);
4694
/* Out of memory. */
4699
/* Construct the format string for calling snprintf or
4703
#if NEED_PRINTF_FLAG_GROUPING
4704
/* The underlying implementation doesn't support the ' flag.
4705
Produce no grouping characters in this case; this is
4706
acceptable because the grouping is locale dependent. */
4708
if (flags & FLAG_GROUP)
4711
if (flags & FLAG_LEFT)
4713
if (flags & FLAG_SHOWSIGN)
4715
if (flags & FLAG_SPACE)
4717
if (flags & FLAG_ALT)
4721
if (flags & FLAG_ZERO)
4723
if (dp->width_start != dp->width_end)
4725
size_t n = dp->width_end - dp->width_start;
4726
/* The width specification is known to consist only
4727
of standard ASCII characters. */
4728
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4730
memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4735
const FCHAR_T *mp = dp->width_start;
4737
*fbp++ = (unsigned char) *mp++;
4742
if (!prec_ourselves)
4744
if (dp->precision_start != dp->precision_end)
4746
size_t n = dp->precision_end - dp->precision_start;
4747
/* The precision specification is known to consist only
4748
of standard ASCII characters. */
4749
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4751
memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4756
const FCHAR_T *mp = dp->precision_start;
4758
*fbp++ = (unsigned char) *mp++;
4766
#if HAVE_LONG_LONG_INT
4767
case TYPE_LONGLONGINT:
4768
case TYPE_ULONGLONGINT:
4769
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4782
case TYPE_WIDE_CHAR:
4785
case TYPE_WIDE_STRING:
4789
case TYPE_LONGDOUBLE:
4795
#if NEED_PRINTF_DIRECTIVE_F
4796
if (dp->conversion == 'F')
4800
*fbp = dp->conversion;
4802
# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4807
/* On glibc2 systems from glibc >= 2.3 - probably also older
4808
ones - we know that snprintf's returns value conforms to
4809
ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4810
Therefore we can avoid using %n in this situation.
4811
On glibc2 systems from 2004-10-18 or newer, the use of %n
4812
in format strings in writable memory may crash the program
4813
(if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4814
in this situation. */
4815
/* On native Win32 systems (such as mingw), we can avoid using
4817
- Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4818
snprintf does not write more than the specified number
4819
of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4820
'4', '5', '6' into buf, not '4', '5', '\0'.)
4821
- Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4822
allows us to recognize the case of an insufficient
4823
buffer size: it returns -1 in this case.
4824
On native Win32 systems (such as mingw) where the OS is
4825
Windows Vista, the use of %n in format strings by default
4826
crashes the program. See
4827
<http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4828
<http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4829
So we should avoid %n in this situation. */
4836
/* Construct the arguments for calling snprintf or sprintf. */
4838
if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4840
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4842
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4844
if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4846
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4848
prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4852
/* The SNPRINTF result is appended after result[0..length].
4853
The latter is an array of DCHAR_T; SNPRINTF appends an
4854
array of TCHAR_T to it. This is possible because
4855
sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4856
alignof (TCHAR_T) <= alignof (DCHAR_T). */
4857
# define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4858
/* Ensure that maxlen below will be >= 2. Needed on BeOS,
4859
where an snprintf() with maxlen==1 acts like sprintf(). */
4860
ENSURE_ALLOCATION (xsum (length,
4861
(2 + TCHARS_PER_DCHAR - 1)
4862
/ TCHARS_PER_DCHAR));
4863
/* Prepare checking whether snprintf returns the count
4865
*(TCHAR_T *) (result + length) = '\0';
4874
size_t maxlen = allocated - length;
4875
/* SNPRINTF can fail if its second argument is
4877
if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4878
maxlen = INT_MAX / TCHARS_PER_DCHAR;
4879
maxlen = maxlen * TCHARS_PER_DCHAR;
4880
# define SNPRINTF_BUF(arg) \
4881
switch (prefix_count) \
4884
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4889
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4891
prefixes[0], arg, &count); \
4894
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4896
prefixes[0], prefixes[1], arg, \
4903
# define SNPRINTF_BUF(arg) \
4904
switch (prefix_count) \
4907
count = sprintf (tmp, buf, arg); \
4910
count = sprintf (tmp, buf, prefixes[0], arg); \
4913
count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4925
int arg = a.arg[dp->arg_index].a.a_schar;
4931
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4937
int arg = a.arg[dp->arg_index].a.a_short;
4943
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4949
int arg = a.arg[dp->arg_index].a.a_int;
4955
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4961
long int arg = a.arg[dp->arg_index].a.a_longint;
4967
unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
4971
#if HAVE_LONG_LONG_INT
4972
case TYPE_LONGLONGINT:
4974
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
4978
case TYPE_ULONGLONGINT:
4980
unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
4987
double arg = a.arg[dp->arg_index].a.a_double;
4991
case TYPE_LONGDOUBLE:
4993
long double arg = a.arg[dp->arg_index].a.a_longdouble;
4999
int arg = a.arg[dp->arg_index].a.a_char;
5004
case TYPE_WIDE_CHAR:
5006
wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
5013
const char *arg = a.arg[dp->arg_index].a.a_string;
5018
case TYPE_WIDE_STRING:
5020
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
5027
void *arg = a.arg[dp->arg_index].a.a_pointer;
5036
/* Portability: Not all implementations of snprintf()
5037
are ISO C 99 compliant. Determine the number of
5038
bytes that snprintf() has produced or would have
5042
/* Verify that snprintf() has NUL-terminated its
5045
&& ((TCHAR_T *) (result + length)) [count] != '\0')
5047
/* Portability hack. */
5048
if (retcount > count)
5053
/* snprintf() doesn't understand the '%n'
5057
/* Don't use the '%n' directive; instead, look
5058
at the snprintf() return value. */
5064
/* Look at the snprintf() return value. */
5067
/* HP-UX 10.20 snprintf() is doubly deficient:
5068
It doesn't understand the '%n' directive,
5069
*and* it returns -1 (rather than the length
5070
that would have been required) when the
5071
buffer is too small. */
5072
size_t bigger_need =
5073
xsum (xtimes (allocated, 2), 12);
5074
ENSURE_ALLOCATION (bigger_need);
5083
/* Attempt to handle failure. */
5086
if (!(result == resultbuf || result == NULL))
5088
if (buf_malloced != NULL)
5089
free (buf_malloced);
5096
/* Handle overflow of the allocated buffer.
5097
If such an overflow occurs, a C99 compliant snprintf()
5098
returns a count >= maxlen. However, a non-compliant
5099
snprintf() function returns only count = maxlen - 1. To
5100
cover both cases, test whether count >= maxlen - 1. */
5101
if ((unsigned int) count + 1 >= maxlen)
5103
/* If maxlen already has attained its allowed maximum,
5104
allocating more memory will not increase maxlen.
5105
Instead of looping, bail out. */
5106
if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5110
/* Need at least (count + 1) * sizeof (TCHAR_T)
5111
bytes. (The +1 is for the trailing NUL.)
5112
But ask for (count + 2) * sizeof (TCHAR_T)
5113
bytes, so that in the next round, we likely get
5114
maxlen > (unsigned int) count + 1
5115
and so we don't get here again.
5116
And allocate proportionally, to avoid looping
5117
eternally if snprintf() reports a too small
5121
((unsigned int) count + 2
5122
+ TCHARS_PER_DCHAR - 1)
5123
/ TCHARS_PER_DCHAR),
5124
xtimes (allocated, 2));
5126
ENSURE_ALLOCATION (n);
5132
#if NEED_PRINTF_UNBOUNDED_PRECISION
5135
/* Handle the precision. */
5138
(TCHAR_T *) (result + length);
5142
size_t prefix_count;
5146
/* Put the additional zeroes after the sign. */
5148
&& (*prec_ptr == '-' || *prec_ptr == '+'
5149
|| *prec_ptr == ' '))
5151
/* Put the additional zeroes after the 0x prefix if
5152
(flags & FLAG_ALT) || (dp->conversion == 'p'). */
5154
&& prec_ptr[0] == '0'
5155
&& (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
5158
move = count - prefix_count;
5159
if (precision > move)
5161
/* Insert zeroes. */
5162
size_t insert = precision - move;
5168
(count + insert + TCHARS_PER_DCHAR - 1)
5169
/ TCHARS_PER_DCHAR);
5170
length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5171
ENSURE_ALLOCATION (n);
5172
length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5173
prec_ptr = (TCHAR_T *) (result + length);
5176
prec_end = prec_ptr + count;
5177
prec_ptr += prefix_count;
5179
while (prec_end > prec_ptr)
5182
prec_end[insert] = prec_end[0];
5188
while (prec_end > prec_ptr);
5196
if (count >= tmp_length)
5197
/* tmp_length was incorrectly calculated - fix the
5203
/* Convert from TCHAR_T[] to DCHAR_T[]. */
5204
if (dp->conversion == 'c' || dp->conversion == 's')
5206
/* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5208
The result string is not certainly ASCII. */
5209
const TCHAR_T *tmpsrc;
5212
/* This code assumes that TCHAR_T is 'char'. */
5213
typedef int TCHAR_T_verify
5214
[2 * (sizeof (TCHAR_T) == 1) - 1];
5216
tmpsrc = (TCHAR_T *) (result + length);
5221
DCHAR_CONV_FROM_ENCODING (locale_charset (),
5222
iconveh_question_mark,
5228
int saved_errno = errno;
5229
if (!(result == resultbuf || result == NULL))
5231
if (buf_malloced != NULL)
5232
free (buf_malloced);
5234
errno = saved_errno;
5237
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
5238
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5244
/* The result string is ASCII.
5245
Simple 1:1 conversion. */
5247
/* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5248
no-op conversion, in-place on the array starting
5249
at (result + length). */
5250
if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
5253
const TCHAR_T *tmpsrc;
5258
if (result == resultbuf)
5260
tmpsrc = (TCHAR_T *) (result + length);
5261
/* ENSURE_ALLOCATION will not move tmpsrc
5262
(because it's part of resultbuf). */
5263
ENSURE_ALLOCATION (xsum (length, count));
5267
/* ENSURE_ALLOCATION will move the array
5268
(because it uses realloc(). */
5269
ENSURE_ALLOCATION (xsum (length, count));
5270
tmpsrc = (TCHAR_T *) (result + length);
5274
ENSURE_ALLOCATION (xsum (length, count));
5276
tmpdst = result + length;
5277
/* Copy backwards, because of overlapping. */
5280
for (n = count; n > 0; n--)
5281
*--tmpdst = (unsigned char) *--tmpsrc;
5286
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
5287
/* Make room for the result. */
5288
if (count > allocated - length)
5290
/* Need at least count elements. But allocate
5293
xmax (xsum (length, count), xtimes (allocated, 2));
5295
ENSURE_ALLOCATION (n);
5299
/* Here count <= allocated - length. */
5301
/* Perform padding. */
5302
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5303
if (pad_ourselves && has_width)
5306
# if ENABLE_UNISTDIO
5307
/* Outside POSIX, it's preferrable to compare the width
5308
against the number of _characters_ of the converted
5310
w = DCHAR_MBSNLEN (result + length, count);
5312
/* The width is compared against the number of _bytes_
5313
of the converted value, says POSIX. */
5318
size_t pad = width - w;
5320
/* Make room for the result. */
5321
if (xsum (count, pad) > allocated - length)
5323
/* Need at least count + pad elements. But
5324
allocate proportionally. */
5326
xmax (xsum3 (length, count, pad),
5327
xtimes (allocated, 2));
5331
ENSURE_ALLOCATION (n);
5334
ENSURE_ALLOCATION (n);
5337
/* Here count + pad <= allocated - length. */
5340
# if !DCHAR_IS_TCHAR || USE_SNPRINTF
5341
DCHAR_T * const rp = result + length;
5343
DCHAR_T * const rp = tmp;
5345
DCHAR_T *p = rp + count;
5346
DCHAR_T *end = p + pad;
5348
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5349
if (dp->conversion == 'c'
5350
|| dp->conversion == 's')
5351
/* No zero-padding for string directives. */
5356
pad_ptr = (*rp == '-' ? rp + 1 : rp);
5357
/* No zero-padding of "inf" and "nan". */
5358
if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
5359
|| (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
5362
/* The generated string now extends from rp to p,
5363
with the zero padding insertion point being at
5366
count = count + pad; /* = end - rp */
5368
if (flags & FLAG_LEFT)
5370
/* Pad with spaces on the right. */
5371
for (; pad > 0; pad--)
5374
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
5376
/* Pad with zeroes. */
5381
for (; pad > 0; pad--)
5386
/* Pad with spaces on the left. */
5391
for (; pad > 0; pad--)
5399
/* Here still count <= allocated - length. */
5401
#if !DCHAR_IS_TCHAR || USE_SNPRINTF
5402
/* The snprintf() result did fit. */
5404
/* Append the sprintf() result. */
5405
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5412
#if NEED_PRINTF_DIRECTIVE_F
5413
if (dp->conversion == 'F')
5415
/* Convert the %f result to upper case for %F. */
5416
DCHAR_T *rp = result + length;
5418
for (rc = count; rc > 0; rc--, rp++)
5419
if (*rp >= 'a' && *rp <= 'z')
5420
*rp = *rp - 'a' + 'A';
5431
/* Add the final NUL. */
5432
ENSURE_ALLOCATION (xsum (length, 1));
5433
result[length] = '\0';
5435
if (result != resultbuf && length + 1 < allocated)
5437
/* Shrink the allocated memory if possible. */
5440
memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
5445
if (buf_malloced != NULL)
5446
free (buf_malloced);
5449
/* Note that we can produce a big string of a length > INT_MAX. POSIX
5450
says that snprintf() fails with errno = EOVERFLOW in this case, but
5451
that's only because snprintf() returns an 'int'. This function does
5452
not have this limitation. */
5457
if (!(result == resultbuf || result == NULL))
5459
if (buf_malloced != NULL)
5460
free (buf_malloced);
5467
if (!(result == resultbuf || result == NULL))
5469
if (buf_malloced != NULL)
5470
free (buf_malloced);
5478
#undef TCHARS_PER_DCHAR
5486
#undef DCHAR_IS_TCHAR