diff options
| author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-02-12 11:07:18 +0000 |
|---|---|---|
| committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-02-12 11:07:18 +0000 |
| commit | bd7029a99b0c2974265c6665638ef14a052f42ab (patch) | |
| tree | f5661ba73366d81ef6e91f889ea7fec5ebe07b6b /gl/vasnprintf.c | |
| parent | f99612320d6eda67644c07be04bb21aa4d7789db (diff) | |
| download | monitoring-plugins-bd7029a99b0c2974265c6665638ef14a052f42ab.tar.gz | |
Sync to latest Gnulib
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1925 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'gl/vasnprintf.c')
| -rw-r--r-- | gl/vasnprintf.c | 4143 |
1 files changed, 3979 insertions, 164 deletions
diff --git a/gl/vasnprintf.c b/gl/vasnprintf.c index 0fe2aada..d5b40286 100644 --- a/gl/vasnprintf.c +++ b/gl/vasnprintf.c | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | /* vsprintf with automatic memory allocation. | 1 | /* vsprintf with automatic memory allocation. |
| 2 | Copyright (C) 1999, 2002-2006 Free Software Foundation, Inc. | 2 | Copyright (C) 1999, 2002-2008 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This program is free software; you can redistribute it and/or modify | 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by | 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2, or (at your option) | 6 | the Free Software Foundation; either version 3, or (at your option) |
| 7 | any later version. | 7 | any later version. |
| 8 | 8 | ||
| 9 | This program is distributed in the hope that it will be useful, | 9 | This program is distributed in the hope that it will be useful, |
| @@ -15,6 +15,35 @@ | |||
| 15 | with this program; if not, write to the Free Software Foundation, | 15 | with this program; if not, write to the Free Software Foundation, |
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 17 | 17 | ||
| 18 | /* This file can be parametrized with the following macros: | ||
| 19 | VASNPRINTF The name of the function being defined. | ||
| 20 | FCHAR_T The element type of the format string. | ||
| 21 | DCHAR_T The element type of the destination (result) string. | ||
| 22 | FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters | ||
| 23 | in the format string are ASCII. MUST be set if | ||
| 24 | FCHAR_T and DCHAR_T are not the same type. | ||
| 25 | DIRECTIVE Structure denoting a format directive. | ||
| 26 | Depends on FCHAR_T. | ||
| 27 | DIRECTIVES Structure denoting the set of format directives of a | ||
| 28 | format string. Depends on FCHAR_T. | ||
| 29 | PRINTF_PARSE Function that parses a format string. | ||
| 30 | Depends on FCHAR_T. | ||
| 31 | DCHAR_CPY memcpy like function for DCHAR_T[] arrays. | ||
| 32 | DCHAR_SET memset like function for DCHAR_T[] arrays. | ||
| 33 | DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays. | ||
| 34 | SNPRINTF The system's snprintf (or similar) function. | ||
| 35 | This may be either snprintf or swprintf. | ||
| 36 | TCHAR_T The element type of the argument and result string | ||
| 37 | of the said SNPRINTF function. This may be either | ||
| 38 | char or wchar_t. The code exploits that | ||
| 39 | sizeof (TCHAR_T) | sizeof (DCHAR_T) and | ||
| 40 | alignof (TCHAR_T) <= alignof (DCHAR_T). | ||
| 41 | DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type. | ||
| 42 | DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[]. | ||
| 43 | DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t. | ||
| 44 | DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t. | ||
| 45 | DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */ | ||
| 46 | |||
| 18 | /* Tell glibc's <stdio.h> to provide a prototype for snprintf(). | 47 | /* Tell glibc's <stdio.h> to provide a prototype for snprintf(). |
| 19 | This must come before <config.h> because <config.h> may include | 48 | This must come before <config.h> because <config.h> may include |
| 20 | <features.h>, and once <features.h> has been included, it's too late. */ | 49 | <features.h>, and once <features.h> has been included, it's too late. */ |
| @@ -22,35 +51,79 @@ | |||
| 22 | # define _GNU_SOURCE 1 | 51 | # define _GNU_SOURCE 1 |
| 23 | #endif | 52 | #endif |
| 24 | 53 | ||
| 25 | #include <config.h> | 54 | #ifndef VASNPRINTF |
| 55 | # include <config.h> | ||
| 56 | #endif | ||
| 26 | #ifndef IN_LIBINTL | 57 | #ifndef IN_LIBINTL |
| 27 | # include <alloca.h> | 58 | # include <alloca.h> |
| 28 | #endif | 59 | #endif |
| 29 | 60 | ||
| 30 | /* Specification. */ | 61 | /* Specification. */ |
| 31 | #if WIDE_CHAR_VERSION | 62 | #ifndef VASNPRINTF |
| 32 | # include "vasnwprintf.h" | 63 | # if WIDE_CHAR_VERSION |
| 33 | #else | 64 | # include "vasnwprintf.h" |
| 34 | # include "vasnprintf.h" | 65 | # else |
| 66 | # include "vasnprintf.h" | ||
| 67 | # endif | ||
| 35 | #endif | 68 | #endif |
| 36 | 69 | ||
| 70 | #include <locale.h> /* localeconv() */ | ||
| 37 | #include <stdio.h> /* snprintf(), sprintf() */ | 71 | #include <stdio.h> /* snprintf(), sprintf() */ |
| 38 | #include <stdlib.h> /* abort(), malloc(), realloc(), free() */ | 72 | #include <stdlib.h> /* abort(), malloc(), realloc(), free() */ |
| 39 | #include <string.h> /* memcpy(), strlen() */ | 73 | #include <string.h> /* memcpy(), strlen() */ |
| 40 | #include <errno.h> /* errno */ | 74 | #include <errno.h> /* errno */ |
| 41 | #include <limits.h> /* CHAR_BIT */ | 75 | #include <limits.h> /* CHAR_BIT */ |
| 42 | #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */ | 76 | #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */ |
| 43 | #if WIDE_CHAR_VERSION | 77 | #if HAVE_NL_LANGINFO |
| 44 | # include "wprintf-parse.h" | 78 | # include <langinfo.h> |
| 45 | #else | 79 | #endif |
| 46 | # include "printf-parse.h" | 80 | #ifndef VASNPRINTF |
| 81 | # if WIDE_CHAR_VERSION | ||
| 82 | # include "wprintf-parse.h" | ||
| 83 | # else | ||
| 84 | # include "printf-parse.h" | ||
| 85 | # endif | ||
| 47 | #endif | 86 | #endif |
| 48 | 87 | ||
| 49 | /* Checked size_t computations. */ | 88 | /* Checked size_t computations. */ |
| 50 | #include "xsize.h" | 89 | #include "xsize.h" |
| 51 | 90 | ||
| 52 | #ifdef HAVE_WCHAR_T | 91 | #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL |
| 53 | # ifdef HAVE_WCSLEN | 92 | # include <math.h> |
| 93 | # include "float+.h" | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL | ||
| 97 | # include <math.h> | ||
| 98 | # include "isnand.h" | ||
| 99 | #endif | ||
| 100 | |||
| 101 | #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL | ||
| 102 | # include <math.h> | ||
| 103 | # include "isnanl-nolibm.h" | ||
| 104 | # include "fpucw.h" | ||
| 105 | #endif | ||
| 106 | |||
| 107 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
| 108 | # include <math.h> | ||
| 109 | # include "isnand.h" | ||
| 110 | # include "printf-frexp.h" | ||
| 111 | #endif | ||
| 112 | |||
| 113 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL | ||
| 114 | # include <math.h> | ||
| 115 | # include "isnanl-nolibm.h" | ||
| 116 | # include "printf-frexpl.h" | ||
| 117 | # include "fpucw.h" | ||
| 118 | #endif | ||
| 119 | |||
| 120 | /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */ | ||
| 121 | #ifndef EOVERFLOW | ||
| 122 | # define EOVERFLOW E2BIG | ||
| 123 | #endif | ||
| 124 | |||
| 125 | #if HAVE_WCHAR_T | ||
| 126 | # if HAVE_WCSLEN | ||
| 54 | # define local_wcslen wcslen | 127 | # define local_wcslen wcslen |
| 55 | # else | 128 | # else |
| 56 | /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid | 129 | /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid |
| @@ -72,12 +145,32 @@ local_wcslen (const wchar_t *s) | |||
| 72 | # endif | 145 | # endif |
| 73 | #endif | 146 | #endif |
| 74 | 147 | ||
| 148 | /* Default parameters. */ | ||
| 149 | #ifndef VASNPRINTF | ||
| 150 | # if WIDE_CHAR_VERSION | ||
| 151 | # define VASNPRINTF vasnwprintf | ||
| 152 | # define FCHAR_T wchar_t | ||
| 153 | # define DCHAR_T wchar_t | ||
| 154 | # define TCHAR_T wchar_t | ||
| 155 | # define DCHAR_IS_TCHAR 1 | ||
| 156 | # define DIRECTIVE wchar_t_directive | ||
| 157 | # define DIRECTIVES wchar_t_directives | ||
| 158 | # define PRINTF_PARSE wprintf_parse | ||
| 159 | # define DCHAR_CPY wmemcpy | ||
| 160 | # else | ||
| 161 | # define VASNPRINTF vasnprintf | ||
| 162 | # define FCHAR_T char | ||
| 163 | # define DCHAR_T char | ||
| 164 | # define TCHAR_T char | ||
| 165 | # define DCHAR_IS_TCHAR 1 | ||
| 166 | # define DIRECTIVE char_directive | ||
| 167 | # define DIRECTIVES char_directives | ||
| 168 | # define PRINTF_PARSE printf_parse | ||
| 169 | # define DCHAR_CPY memcpy | ||
| 170 | # endif | ||
| 171 | #endif | ||
| 75 | #if WIDE_CHAR_VERSION | 172 | #if WIDE_CHAR_VERSION |
| 76 | # define VASNPRINTF vasnwprintf | 173 | /* TCHAR_T is wchar_t. */ |
| 77 | # define CHAR_T wchar_t | ||
| 78 | # define DIRECTIVE wchar_t_directive | ||
| 79 | # define DIRECTIVES wchar_t_directives | ||
| 80 | # define PRINTF_PARSE wprintf_parse | ||
| 81 | # define USE_SNPRINTF 1 | 174 | # define USE_SNPRINTF 1 |
| 82 | # if HAVE_DECL__SNWPRINTF | 175 | # if HAVE_DECL__SNWPRINTF |
| 83 | /* On Windows, the function swprintf() has a different signature than | 176 | /* On Windows, the function swprintf() has a different signature than |
| @@ -88,39 +181,1253 @@ local_wcslen (const wchar_t *s) | |||
| 88 | # define SNPRINTF swprintf | 181 | # define SNPRINTF swprintf |
| 89 | # endif | 182 | # endif |
| 90 | #else | 183 | #else |
| 91 | # define VASNPRINTF vasnprintf | 184 | /* TCHAR_T is char. */ |
| 92 | # define CHAR_T char | 185 | # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'. |
| 93 | # define DIRECTIVE char_directive | 186 | But don't use it on BeOS, since BeOS snprintf produces no output if the |
| 94 | # define DIRECTIVES char_directives | 187 | size argument is >= 0x3000000. */ |
| 95 | # define PRINTF_PARSE printf_parse | 188 | # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ |
| 96 | # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) | 189 | # define USE_SNPRINTF 1 |
| 190 | # else | ||
| 191 | # define USE_SNPRINTF 0 | ||
| 192 | # endif | ||
| 97 | # if HAVE_DECL__SNPRINTF | 193 | # if HAVE_DECL__SNPRINTF |
| 98 | /* Windows. */ | 194 | /* Windows. */ |
| 99 | # define SNPRINTF _snprintf | 195 | # define SNPRINTF _snprintf |
| 100 | # else | 196 | # else |
| 101 | /* Unix. */ | 197 | /* Unix. */ |
| 102 | # define SNPRINTF snprintf | 198 | # define SNPRINTF snprintf |
| 199 | /* Here we need to call the native snprintf, not rpl_snprintf. */ | ||
| 200 | # undef snprintf | ||
| 103 | # endif | 201 | # endif |
| 104 | #endif | 202 | #endif |
| 203 | /* Here we need to call the native sprintf, not rpl_sprintf. */ | ||
| 204 | #undef sprintf | ||
| 105 | 205 | ||
| 106 | CHAR_T * | 206 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL |
| 107 | VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args) | 207 | /* Determine the decimal-point character according to the current locale. */ |
| 208 | # ifndef decimal_point_char_defined | ||
| 209 | # define decimal_point_char_defined 1 | ||
| 210 | static char | ||
| 211 | decimal_point_char () | ||
| 108 | { | 212 | { |
| 109 | DIRECTIVES d; | 213 | const char *point; |
| 110 | arguments a; | 214 | /* Determine it in a multithread-safe way. We know nl_langinfo is |
| 215 | multithread-safe on glibc systems, but is not required to be multithread- | ||
| 216 | safe by POSIX. sprintf(), however, is multithread-safe. localeconv() | ||
| 217 | is rarely multithread-safe. */ | ||
| 218 | # if HAVE_NL_LANGINFO && __GLIBC__ | ||
| 219 | point = nl_langinfo (RADIXCHAR); | ||
| 220 | # elif 1 | ||
| 221 | char pointbuf[5]; | ||
| 222 | sprintf (pointbuf, "%#.0f", 1.0); | ||
| 223 | point = &pointbuf[1]; | ||
| 224 | # else | ||
| 225 | point = localeconv () -> decimal_point; | ||
| 226 | # endif | ||
| 227 | /* The decimal point is always a single byte: either '.' or ','. */ | ||
| 228 | return (point[0] != '\0' ? point[0] : '.'); | ||
| 229 | } | ||
| 230 | # endif | ||
| 231 | #endif | ||
| 111 | 232 | ||
| 112 | if (PRINTF_PARSE (format, &d, &a) < 0) | 233 | #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL |
| 234 | |||
| 235 | /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */ | ||
| 236 | static int | ||
| 237 | is_infinite_or_zero (double x) | ||
| 238 | { | ||
| 239 | return isnand (x) || x + x == x; | ||
| 240 | } | ||
| 241 | |||
| 242 | #endif | ||
| 243 | |||
| 244 | #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL | ||
| 245 | |||
| 246 | /* Equivalent to !isfinite(x), but does not require libm. */ | ||
| 247 | static int | ||
| 248 | is_infinitel (long double x) | ||
| 249 | { | ||
| 250 | return isnanl (x) || (x + x == x && x != 0.0L); | ||
| 251 | } | ||
| 252 | |||
| 253 | #endif | ||
| 254 | |||
| 255 | #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
| 256 | |||
| 257 | /* Converting 'long double' to decimal without rare rounding bugs requires | ||
| 258 | real bignums. We use the naming conventions of GNU gmp, but vastly simpler | ||
| 259 | (and slower) algorithms. */ | ||
| 260 | |||
| 261 | typedef unsigned int mp_limb_t; | ||
| 262 | # define GMP_LIMB_BITS 32 | ||
| 263 | typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1]; | ||
| 264 | |||
| 265 | typedef unsigned long long mp_twolimb_t; | ||
| 266 | # define GMP_TWOLIMB_BITS 64 | ||
| 267 | typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1]; | ||
| 268 | |||
| 269 | /* Representation of a bignum >= 0. */ | ||
| 270 | typedef struct | ||
| 271 | { | ||
| 272 | size_t nlimbs; | ||
| 273 | mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */ | ||
| 274 | } mpn_t; | ||
| 275 | |||
| 276 | /* Compute the product of two bignums >= 0. | ||
| 277 | Return the allocated memory in case of success, NULL in case of memory | ||
| 278 | allocation failure. */ | ||
| 279 | static void * | ||
| 280 | multiply (mpn_t src1, mpn_t src2, mpn_t *dest) | ||
| 281 | { | ||
| 282 | const mp_limb_t *p1; | ||
| 283 | const mp_limb_t *p2; | ||
| 284 | size_t len1; | ||
| 285 | size_t len2; | ||
| 286 | |||
| 287 | if (src1.nlimbs <= src2.nlimbs) | ||
| 113 | { | 288 | { |
| 114 | errno = EINVAL; | 289 | len1 = src1.nlimbs; |
| 290 | p1 = src1.limbs; | ||
| 291 | len2 = src2.nlimbs; | ||
| 292 | p2 = src2.limbs; | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | len1 = src2.nlimbs; | ||
| 297 | p1 = src2.limbs; | ||
| 298 | len2 = src1.nlimbs; | ||
| 299 | p2 = src1.limbs; | ||
| 300 | } | ||
| 301 | /* Now 0 <= len1 <= len2. */ | ||
| 302 | if (len1 == 0) | ||
| 303 | { | ||
| 304 | /* src1 or src2 is zero. */ | ||
| 305 | dest->nlimbs = 0; | ||
| 306 | dest->limbs = (mp_limb_t *) malloc (1); | ||
| 307 | } | ||
| 308 | else | ||
| 309 | { | ||
| 310 | /* Here 1 <= len1 <= len2. */ | ||
| 311 | size_t dlen; | ||
| 312 | mp_limb_t *dp; | ||
| 313 | size_t k, i, j; | ||
| 314 | |||
| 315 | dlen = len1 + len2; | ||
| 316 | dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t)); | ||
| 317 | if (dp == NULL) | ||
| 318 | return NULL; | ||
| 319 | for (k = len2; k > 0; ) | ||
| 320 | dp[--k] = 0; | ||
| 321 | for (i = 0; i < len1; i++) | ||
| 322 | { | ||
| 323 | mp_limb_t digit1 = p1[i]; | ||
| 324 | mp_twolimb_t carry = 0; | ||
| 325 | for (j = 0; j < len2; j++) | ||
| 326 | { | ||
| 327 | mp_limb_t digit2 = p2[j]; | ||
| 328 | carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; | ||
| 329 | carry += dp[i + j]; | ||
| 330 | dp[i + j] = (mp_limb_t) carry; | ||
| 331 | carry = carry >> GMP_LIMB_BITS; | ||
| 332 | } | ||
| 333 | dp[i + len2] = (mp_limb_t) carry; | ||
| 334 | } | ||
| 335 | /* Normalise. */ | ||
| 336 | while (dlen > 0 && dp[dlen - 1] == 0) | ||
| 337 | dlen--; | ||
| 338 | dest->nlimbs = dlen; | ||
| 339 | dest->limbs = dp; | ||
| 340 | } | ||
| 341 | return dest->limbs; | ||
| 342 | } | ||
| 343 | |||
| 344 | /* Compute the quotient of a bignum a >= 0 and a bignum b > 0. | ||
| 345 | a is written as a = q * b + r with 0 <= r < b. q is the quotient, r | ||
| 346 | the remainder. | ||
| 347 | Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd, | ||
| 348 | q is incremented. | ||
| 349 | Return the allocated memory in case of success, NULL in case of memory | ||
| 350 | allocation failure. */ | ||
| 351 | static void * | ||
| 352 | divide (mpn_t a, mpn_t b, mpn_t *q) | ||
| 353 | { | ||
| 354 | /* Algorithm: | ||
| 355 | First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]] | ||
| 356 | with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS). | ||
| 357 | If m<n, then q:=0 and r:=a. | ||
| 358 | If m>=n=1, perform a single-precision division: | ||
| 359 | r:=0, j:=m, | ||
| 360 | while j>0 do | ||
| 361 | {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j = | ||
| 362 | = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta} | ||
| 363 | j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j]. | ||
| 364 | Normalise [q[m-1],...,q[0]], yields q. | ||
| 365 | If m>=n>1, perform a multiple-precision division: | ||
| 366 | We have a/b < beta^(m-n+1). | ||
| 367 | s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize. | ||
| 368 | Shift a and b left by s bits, copying them. r:=a. | ||
| 369 | r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2. | ||
| 370 | For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).} | ||
| 371 | Compute q* : | ||
| 372 | q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]). | ||
| 373 | In case of overflow (q* >= beta) set q* := beta-1. | ||
| 374 | Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2] | ||
| 375 | and c3 := b[n-2] * q*. | ||
| 376 | {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow | ||
| 377 | occurred. Furthermore 0 <= c3 < beta^2. | ||
| 378 | If there was overflow and | ||
| 379 | r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2, | ||
| 380 | the next test can be skipped.} | ||
| 381 | While c3 > c2, {Here 0 <= c2 < c3 < beta^2} | ||
| 382 | Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2]. | ||
| 383 | If q* > 0: | ||
| 384 | Put r := r - b * q* * beta^j. In detail: | ||
| 385 | [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]]. | ||
| 386 | hence: u:=0, for i:=0 to n-1 do | ||
| 387 | u := u + q* * b[i], | ||
| 388 | r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry), | ||
| 389 | u:=u div beta (+ 1, if carry in subtraction) | ||
| 390 | r[n+j]:=r[n+j]-u. | ||
| 391 | {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1 | ||
| 392 | < q* + 1 <= beta, | ||
| 393 | the carry u does not overflow.} | ||
| 394 | If a negative carry occurs, put q* := q* - 1 | ||
| 395 | and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]]. | ||
| 396 | Set q[j] := q*. | ||
| 397 | Normalise [q[m-n],..,q[0]]; this yields the quotient q. | ||
| 398 | Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the | ||
| 399 | rest r. | ||
| 400 | The room for q[j] can be allocated at the memory location of r[n+j]. | ||
| 401 | Finally, round-to-even: | ||
| 402 | Shift r left by 1 bit. | ||
| 403 | If r > b or if r = b and q[0] is odd, q := q+1. | ||
| 404 | */ | ||
| 405 | const mp_limb_t *a_ptr = a.limbs; | ||
| 406 | size_t a_len = a.nlimbs; | ||
| 407 | const mp_limb_t *b_ptr = b.limbs; | ||
| 408 | size_t b_len = b.nlimbs; | ||
| 409 | mp_limb_t *roomptr; | ||
| 410 | mp_limb_t *tmp_roomptr = NULL; | ||
| 411 | mp_limb_t *q_ptr; | ||
| 412 | size_t q_len; | ||
| 413 | mp_limb_t *r_ptr; | ||
| 414 | size_t r_len; | ||
| 415 | |||
| 416 | /* Allocate room for a_len+2 digits. | ||
| 417 | (Need a_len+1 digits for the real division and 1 more digit for the | ||
| 418 | final rounding of q.) */ | ||
| 419 | roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t)); | ||
| 420 | if (roomptr == NULL) | ||
| 421 | return NULL; | ||
| 422 | |||
| 423 | /* Normalise a. */ | ||
| 424 | while (a_len > 0 && a_ptr[a_len - 1] == 0) | ||
| 425 | a_len--; | ||
| 426 | |||
| 427 | /* Normalise b. */ | ||
| 428 | for (;;) | ||
| 429 | { | ||
| 430 | if (b_len == 0) | ||
| 431 | /* Division by zero. */ | ||
| 432 | abort (); | ||
| 433 | if (b_ptr[b_len - 1] == 0) | ||
| 434 | b_len--; | ||
| 435 | else | ||
| 436 | break; | ||
| 437 | } | ||
| 438 | |||
| 439 | /* Here m = a_len >= 0 and n = b_len > 0. */ | ||
| 440 | |||
| 441 | if (a_len < b_len) | ||
| 442 | { | ||
| 443 | /* m<n: trivial case. q=0, r := copy of a. */ | ||
| 444 | r_ptr = roomptr; | ||
| 445 | r_len = a_len; | ||
| 446 | memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t)); | ||
| 447 | q_ptr = roomptr + a_len; | ||
| 448 | q_len = 0; | ||
| 449 | } | ||
| 450 | else if (b_len == 1) | ||
| 451 | { | ||
| 452 | /* n=1: single precision division. | ||
| 453 | beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */ | ||
| 454 | r_ptr = roomptr; | ||
| 455 | q_ptr = roomptr + 1; | ||
| 456 | { | ||
| 457 | mp_limb_t den = b_ptr[0]; | ||
| 458 | mp_limb_t remainder = 0; | ||
| 459 | const mp_limb_t *sourceptr = a_ptr + a_len; | ||
| 460 | mp_limb_t *destptr = q_ptr + a_len; | ||
| 461 | size_t count; | ||
| 462 | for (count = a_len; count > 0; count--) | ||
| 463 | { | ||
| 464 | mp_twolimb_t num = | ||
| 465 | ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr; | ||
| 466 | *--destptr = num / den; | ||
| 467 | remainder = num % den; | ||
| 468 | } | ||
| 469 | /* Normalise and store r. */ | ||
| 470 | if (remainder > 0) | ||
| 471 | { | ||
| 472 | r_ptr[0] = remainder; | ||
| 473 | r_len = 1; | ||
| 474 | } | ||
| 475 | else | ||
| 476 | r_len = 0; | ||
| 477 | /* Normalise q. */ | ||
| 478 | q_len = a_len; | ||
| 479 | if (q_ptr[q_len - 1] == 0) | ||
| 480 | q_len--; | ||
| 481 | } | ||
| 482 | } | ||
| 483 | else | ||
| 484 | { | ||
| 485 | /* n>1: multiple precision division. | ||
| 486 | beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==> | ||
| 487 | beta^(m-n-1) <= a/b < beta^(m-n+1). */ | ||
| 488 | /* Determine s. */ | ||
| 489 | size_t s; | ||
| 490 | { | ||
| 491 | mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */ | ||
| 492 | s = 31; | ||
| 493 | if (msd >= 0x10000) | ||
| 494 | { | ||
| 495 | msd = msd >> 16; | ||
| 496 | s -= 16; | ||
| 497 | } | ||
| 498 | if (msd >= 0x100) | ||
| 499 | { | ||
| 500 | msd = msd >> 8; | ||
| 501 | s -= 8; | ||
| 502 | } | ||
| 503 | if (msd >= 0x10) | ||
| 504 | { | ||
| 505 | msd = msd >> 4; | ||
| 506 | s -= 4; | ||
| 507 | } | ||
| 508 | if (msd >= 0x4) | ||
| 509 | { | ||
| 510 | msd = msd >> 2; | ||
| 511 | s -= 2; | ||
| 512 | } | ||
| 513 | if (msd >= 0x2) | ||
| 514 | { | ||
| 515 | msd = msd >> 1; | ||
| 516 | s -= 1; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | /* 0 <= s < GMP_LIMB_BITS. | ||
| 520 | Copy b, shifting it left by s bits. */ | ||
| 521 | if (s > 0) | ||
| 522 | { | ||
| 523 | tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t)); | ||
| 524 | if (tmp_roomptr == NULL) | ||
| 525 | { | ||
| 526 | free (roomptr); | ||
| 527 | return NULL; | ||
| 528 | } | ||
| 529 | { | ||
| 530 | const mp_limb_t *sourceptr = b_ptr; | ||
| 531 | mp_limb_t *destptr = tmp_roomptr; | ||
| 532 | mp_twolimb_t accu = 0; | ||
| 533 | size_t count; | ||
| 534 | for (count = b_len; count > 0; count--) | ||
| 535 | { | ||
| 536 | accu += (mp_twolimb_t) *sourceptr++ << s; | ||
| 537 | *destptr++ = (mp_limb_t) accu; | ||
| 538 | accu = accu >> GMP_LIMB_BITS; | ||
| 539 | } | ||
| 540 | /* accu must be zero, since that was how s was determined. */ | ||
| 541 | if (accu != 0) | ||
| 542 | abort (); | ||
| 543 | } | ||
| 544 | b_ptr = tmp_roomptr; | ||
| 545 | } | ||
| 546 | /* Copy a, shifting it left by s bits, yields r. | ||
| 547 | Memory layout: | ||
| 548 | At the beginning: r = roomptr[0..a_len], | ||
| 549 | at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */ | ||
| 550 | r_ptr = roomptr; | ||
| 551 | if (s == 0) | ||
| 552 | { | ||
| 553 | memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t)); | ||
| 554 | r_ptr[a_len] = 0; | ||
| 555 | } | ||
| 556 | else | ||
| 557 | { | ||
| 558 | const mp_limb_t *sourceptr = a_ptr; | ||
| 559 | mp_limb_t *destptr = r_ptr; | ||
| 560 | mp_twolimb_t accu = 0; | ||
| 561 | size_t count; | ||
| 562 | for (count = a_len; count > 0; count--) | ||
| 563 | { | ||
| 564 | accu += (mp_twolimb_t) *sourceptr++ << s; | ||
| 565 | *destptr++ = (mp_limb_t) accu; | ||
| 566 | accu = accu >> GMP_LIMB_BITS; | ||
| 567 | } | ||
| 568 | *destptr++ = (mp_limb_t) accu; | ||
| 569 | } | ||
| 570 | q_ptr = roomptr + b_len; | ||
| 571 | q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */ | ||
| 572 | { | ||
| 573 | size_t j = a_len - b_len; /* m-n */ | ||
| 574 | mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */ | ||
| 575 | mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */ | ||
| 576 | mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */ | ||
| 577 | ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd; | ||
| 578 | /* Division loop, traversed m-n+1 times. | ||
| 579 | j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */ | ||
| 580 | for (;;) | ||
| 581 | { | ||
| 582 | mp_limb_t q_star; | ||
| 583 | mp_limb_t c1; | ||
| 584 | if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */ | ||
| 585 | { | ||
| 586 | /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */ | ||
| 587 | mp_twolimb_t num = | ||
| 588 | ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS) | ||
| 589 | | r_ptr[j + b_len - 1]; | ||
| 590 | q_star = num / b_msd; | ||
| 591 | c1 = num % b_msd; | ||
| 592 | } | ||
| 593 | else | ||
| 594 | { | ||
| 595 | /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */ | ||
| 596 | q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */ | ||
| 597 | /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta | ||
| 598 | <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta | ||
| 599 | <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) | ||
| 600 | {<= beta !}. | ||
| 601 | If yes, jump directly to the subtraction loop. | ||
| 602 | (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta | ||
| 603 | <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */ | ||
| 604 | if (r_ptr[j + b_len] > b_msd | ||
| 605 | || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd) | ||
| 606 | /* r[j+n] >= b[n-1]+1 or | ||
| 607 | r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a | ||
| 608 | carry. */ | ||
| 609 | goto subtract; | ||
| 610 | } | ||
| 611 | /* q_star = q*, | ||
| 612 | c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */ | ||
| 613 | { | ||
| 614 | mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */ | ||
| 615 | ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2]; | ||
| 616 | mp_twolimb_t c3 = /* b[n-2] * q* */ | ||
| 617 | (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star; | ||
| 618 | /* While c2 < c3, increase c2 and decrease c3. | ||
| 619 | Consider c3-c2. While it is > 0, decrease it by | ||
| 620 | b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2 | ||
| 621 | this can happen only twice. */ | ||
| 622 | if (c3 > c2) | ||
| 623 | { | ||
| 624 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
| 625 | if (c3 - c2 > b_msdd) | ||
| 626 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
| 627 | } | ||
| 628 | } | ||
| 629 | if (q_star > 0) | ||
| 630 | subtract: | ||
| 631 | { | ||
| 632 | /* Subtract r := r - b * q* * beta^j. */ | ||
| 633 | mp_limb_t cr; | ||
| 634 | { | ||
| 635 | const mp_limb_t *sourceptr = b_ptr; | ||
| 636 | mp_limb_t *destptr = r_ptr + j; | ||
| 637 | mp_twolimb_t carry = 0; | ||
| 638 | size_t count; | ||
| 639 | for (count = b_len; count > 0; count--) | ||
| 640 | { | ||
| 641 | /* Here 0 <= carry <= q*. */ | ||
| 642 | carry = | ||
| 643 | carry | ||
| 644 | + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++ | ||
| 645 | + (mp_limb_t) ~(*destptr); | ||
| 646 | /* Here 0 <= carry <= beta*q* + beta-1. */ | ||
| 647 | *destptr++ = ~(mp_limb_t) carry; | ||
| 648 | carry = carry >> GMP_LIMB_BITS; /* <= q* */ | ||
| 649 | } | ||
| 650 | cr = (mp_limb_t) carry; | ||
| 651 | } | ||
| 652 | /* Subtract cr from r_ptr[j + b_len], then forget about | ||
| 653 | r_ptr[j + b_len]. */ | ||
| 654 | if (cr > r_ptr[j + b_len]) | ||
| 655 | { | ||
| 656 | /* Subtraction gave a carry. */ | ||
| 657 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
| 658 | /* Add b back. */ | ||
| 659 | { | ||
| 660 | const mp_limb_t *sourceptr = b_ptr; | ||
| 661 | mp_limb_t *destptr = r_ptr + j; | ||
| 662 | mp_limb_t carry = 0; | ||
| 663 | size_t count; | ||
| 664 | for (count = b_len; count > 0; count--) | ||
| 665 | { | ||
| 666 | mp_limb_t source1 = *sourceptr++; | ||
| 667 | mp_limb_t source2 = *destptr; | ||
| 668 | *destptr++ = source1 + source2 + carry; | ||
| 669 | carry = | ||
| 670 | (carry | ||
| 671 | ? source1 >= (mp_limb_t) ~source2 | ||
| 672 | : source1 > (mp_limb_t) ~source2); | ||
| 673 | } | ||
| 674 | } | ||
| 675 | /* Forget about the carry and about r[j+n]. */ | ||
| 676 | } | ||
| 677 | } | ||
| 678 | /* q* is determined. Store it as q[j]. */ | ||
| 679 | q_ptr[j] = q_star; | ||
| 680 | if (j == 0) | ||
| 681 | break; | ||
| 682 | j--; | ||
| 683 | } | ||
| 684 | } | ||
| 685 | r_len = b_len; | ||
| 686 | /* Normalise q. */ | ||
| 687 | if (q_ptr[q_len - 1] == 0) | ||
| 688 | q_len--; | ||
| 689 | # if 0 /* Not needed here, since we need r only to compare it with b/2, and | ||
| 690 | b is shifted left by s bits. */ | ||
| 691 | /* Shift r right by s bits. */ | ||
| 692 | if (s > 0) | ||
| 693 | { | ||
| 694 | mp_limb_t ptr = r_ptr + r_len; | ||
| 695 | mp_twolimb_t accu = 0; | ||
| 696 | size_t count; | ||
| 697 | for (count = r_len; count > 0; count--) | ||
| 698 | { | ||
| 699 | accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS; | ||
| 700 | accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s); | ||
| 701 | *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS); | ||
| 702 | } | ||
| 703 | } | ||
| 704 | # endif | ||
| 705 | /* Normalise r. */ | ||
| 706 | while (r_len > 0 && r_ptr[r_len - 1] == 0) | ||
| 707 | r_len--; | ||
| 708 | } | ||
| 709 | /* Compare r << 1 with b. */ | ||
| 710 | if (r_len > b_len) | ||
| 711 | goto increment_q; | ||
| 712 | { | ||
| 713 | size_t i; | ||
| 714 | for (i = b_len;;) | ||
| 715 | { | ||
| 716 | mp_limb_t r_i = | ||
| 717 | (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0) | ||
| 718 | | (i < r_len ? r_ptr[i] << 1 : 0); | ||
| 719 | mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0); | ||
| 720 | if (r_i > b_i) | ||
| 721 | goto increment_q; | ||
| 722 | if (r_i < b_i) | ||
| 723 | goto keep_q; | ||
| 724 | if (i == 0) | ||
| 725 | break; | ||
| 726 | i--; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | if (q_len > 0 && ((q_ptr[0] & 1) != 0)) | ||
| 730 | /* q is odd. */ | ||
| 731 | increment_q: | ||
| 732 | { | ||
| 733 | size_t i; | ||
| 734 | for (i = 0; i < q_len; i++) | ||
| 735 | if (++(q_ptr[i]) != 0) | ||
| 736 | goto keep_q; | ||
| 737 | q_ptr[q_len++] = 1; | ||
| 738 | } | ||
| 739 | keep_q: | ||
| 740 | if (tmp_roomptr != NULL) | ||
| 741 | free (tmp_roomptr); | ||
| 742 | q->limbs = q_ptr; | ||
| 743 | q->nlimbs = q_len; | ||
| 744 | return roomptr; | ||
| 745 | } | ||
| 746 | |||
| 747 | /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal | ||
| 748 | representation. | ||
| 749 | Destroys the contents of a. | ||
| 750 | Return the allocated memory - containing the decimal digits in low-to-high | ||
| 751 | order, terminated with a NUL character - in case of success, NULL in case | ||
| 752 | of memory allocation failure. */ | ||
| 753 | static char * | ||
| 754 | convert_to_decimal (mpn_t a, size_t extra_zeroes) | ||
| 755 | { | ||
| 756 | mp_limb_t *a_ptr = a.limbs; | ||
| 757 | size_t a_len = a.nlimbs; | ||
| 758 | /* 0.03345 is slightly larger than log(2)/(9*log(10)). */ | ||
| 759 | size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1); | ||
| 760 | char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes)); | ||
| 761 | if (c_ptr != NULL) | ||
| 762 | { | ||
| 763 | char *d_ptr = c_ptr; | ||
| 764 | for (; extra_zeroes > 0; extra_zeroes--) | ||
| 765 | *d_ptr++ = '0'; | ||
| 766 | while (a_len > 0) | ||
| 767 | { | ||
| 768 | /* Divide a by 10^9, in-place. */ | ||
| 769 | mp_limb_t remainder = 0; | ||
| 770 | mp_limb_t *ptr = a_ptr + a_len; | ||
| 771 | size_t count; | ||
| 772 | for (count = a_len; count > 0; count--) | ||
| 773 | { | ||
| 774 | mp_twolimb_t num = | ||
| 775 | ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr; | ||
| 776 | *ptr = num / 1000000000; | ||
| 777 | remainder = num % 1000000000; | ||
| 778 | } | ||
| 779 | /* Store the remainder as 9 decimal digits. */ | ||
| 780 | for (count = 9; count > 0; count--) | ||
| 781 | { | ||
| 782 | *d_ptr++ = '0' + (remainder % 10); | ||
| 783 | remainder = remainder / 10; | ||
| 784 | } | ||
| 785 | /* Normalize a. */ | ||
| 786 | if (a_ptr[a_len - 1] == 0) | ||
| 787 | a_len--; | ||
| 788 | } | ||
| 789 | /* Remove leading zeroes. */ | ||
| 790 | while (d_ptr > c_ptr && d_ptr[-1] == '0') | ||
| 791 | d_ptr--; | ||
| 792 | /* But keep at least one zero. */ | ||
| 793 | if (d_ptr == c_ptr) | ||
| 794 | *d_ptr++ = '0'; | ||
| 795 | /* Terminate the string. */ | ||
| 796 | *d_ptr = '\0'; | ||
| 797 | } | ||
| 798 | return c_ptr; | ||
| 799 | } | ||
| 800 | |||
| 801 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 802 | |||
| 803 | /* Assuming x is finite and >= 0: | ||
| 804 | write x as x = 2^e * m, where m is a bignum. | ||
| 805 | Return the allocated memory in case of success, NULL in case of memory | ||
| 806 | allocation failure. */ | ||
| 807 | static void * | ||
| 808 | decode_long_double (long double x, int *ep, mpn_t *mp) | ||
| 809 | { | ||
| 810 | mpn_t m; | ||
| 811 | int exp; | ||
| 812 | long double y; | ||
| 813 | size_t i; | ||
| 814 | |||
| 815 | /* Allocate memory for result. */ | ||
| 816 | m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; | ||
| 817 | m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); | ||
| 818 | if (m.limbs == NULL) | ||
| 819 | return NULL; | ||
| 820 | /* Split into exponential part and mantissa. */ | ||
| 821 | y = frexpl (x, &exp); | ||
| 822 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 823 | abort (); | ||
| 824 | /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the | ||
| 825 | latter is an integer. */ | ||
| 826 | /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs. | ||
| 827 | I'm not sure whether it's safe to cast a 'long double' value between | ||
| 828 | 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only | ||
| 829 | 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int', | ||
| 830 | doesn't matter). */ | ||
| 831 | # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0 | ||
| 832 | # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2 | ||
| 833 | { | ||
| 834 | mp_limb_t hi, lo; | ||
| 835 | y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2)); | ||
| 836 | hi = (int) y; | ||
| 837 | y -= hi; | ||
| 838 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 839 | abort (); | ||
| 840 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 841 | lo = (int) y; | ||
| 842 | y -= lo; | ||
| 843 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 844 | abort (); | ||
| 845 | m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
| 846 | } | ||
| 847 | # else | ||
| 848 | { | ||
| 849 | mp_limb_t d; | ||
| 850 | y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS); | ||
| 851 | d = (int) y; | ||
| 852 | y -= d; | ||
| 853 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 854 | abort (); | ||
| 855 | m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d; | ||
| 856 | } | ||
| 857 | # endif | ||
| 858 | # endif | ||
| 859 | for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) | ||
| 860 | { | ||
| 861 | mp_limb_t hi, lo; | ||
| 862 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 863 | hi = (int) y; | ||
| 864 | y -= hi; | ||
| 865 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 866 | abort (); | ||
| 867 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 868 | lo = (int) y; | ||
| 869 | y -= lo; | ||
| 870 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 871 | abort (); | ||
| 872 | m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
| 873 | } | ||
| 874 | #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess | ||
| 875 | precision. */ | ||
| 876 | if (!(y == 0.0L)) | ||
| 877 | abort (); | ||
| 878 | #endif | ||
| 879 | /* Normalise. */ | ||
| 880 | while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0) | ||
| 881 | m.nlimbs--; | ||
| 882 | *mp = m; | ||
| 883 | *ep = exp - LDBL_MANT_BIT; | ||
| 884 | return m.limbs; | ||
| 885 | } | ||
| 886 | |||
| 887 | # endif | ||
| 888 | |||
| 889 | # if NEED_PRINTF_DOUBLE | ||
| 890 | |||
| 891 | /* Assuming x is finite and >= 0: | ||
| 892 | write x as x = 2^e * m, where m is a bignum. | ||
| 893 | Return the allocated memory in case of success, NULL in case of memory | ||
| 894 | allocation failure. */ | ||
| 895 | static void * | ||
| 896 | decode_double (double x, int *ep, mpn_t *mp) | ||
| 897 | { | ||
| 898 | mpn_t m; | ||
| 899 | int exp; | ||
| 900 | double y; | ||
| 901 | size_t i; | ||
| 902 | |||
| 903 | /* Allocate memory for result. */ | ||
| 904 | m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; | ||
| 905 | m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); | ||
| 906 | if (m.limbs == NULL) | ||
| 907 | return NULL; | ||
| 908 | /* Split into exponential part and mantissa. */ | ||
| 909 | y = frexp (x, &exp); | ||
| 910 | if (!(y >= 0.0 && y < 1.0)) | ||
| 911 | abort (); | ||
| 912 | /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the | ||
| 913 | latter is an integer. */ | ||
| 914 | /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs. | ||
| 915 | I'm not sure whether it's safe to cast a 'double' value between | ||
| 916 | 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only | ||
| 917 | 'double' values between 0 and 2^16 (to 'unsigned int' or 'int', | ||
| 918 | doesn't matter). */ | ||
| 919 | # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0 | ||
| 920 | # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2 | ||
| 921 | { | ||
| 922 | mp_limb_t hi, lo; | ||
| 923 | y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2)); | ||
| 924 | hi = (int) y; | ||
| 925 | y -= hi; | ||
| 926 | if (!(y >= 0.0 && y < 1.0)) | ||
| 927 | abort (); | ||
| 928 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 929 | lo = (int) y; | ||
| 930 | y -= lo; | ||
| 931 | if (!(y >= 0.0 && y < 1.0)) | ||
| 932 | abort (); | ||
| 933 | m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
| 934 | } | ||
| 935 | # else | ||
| 936 | { | ||
| 937 | mp_limb_t d; | ||
| 938 | y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS); | ||
| 939 | d = (int) y; | ||
| 940 | y -= d; | ||
| 941 | if (!(y >= 0.0 && y < 1.0)) | ||
| 942 | abort (); | ||
| 943 | m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d; | ||
| 944 | } | ||
| 945 | # endif | ||
| 946 | # endif | ||
| 947 | for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) | ||
| 948 | { | ||
| 949 | mp_limb_t hi, lo; | ||
| 950 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 951 | hi = (int) y; | ||
| 952 | y -= hi; | ||
| 953 | if (!(y >= 0.0 && y < 1.0)) | ||
| 954 | abort (); | ||
| 955 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
| 956 | lo = (int) y; | ||
| 957 | y -= lo; | ||
| 958 | if (!(y >= 0.0 && y < 1.0)) | ||
| 959 | abort (); | ||
| 960 | m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
| 961 | } | ||
| 962 | if (!(y == 0.0)) | ||
| 963 | abort (); | ||
| 964 | /* Normalise. */ | ||
| 965 | while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0) | ||
| 966 | m.nlimbs--; | ||
| 967 | *mp = m; | ||
| 968 | *ep = exp - DBL_MANT_BIT; | ||
| 969 | return m.limbs; | ||
| 970 | } | ||
| 971 | |||
| 972 | # endif | ||
| 973 | |||
| 974 | /* Assuming x = 2^e * m is finite and >= 0, and n is an integer: | ||
| 975 | Returns the decimal representation of round (x * 10^n). | ||
| 976 | Return the allocated memory - containing the decimal digits in low-to-high | ||
| 977 | order, terminated with a NUL character - in case of success, NULL in case | ||
| 978 | of memory allocation failure. */ | ||
| 979 | static char * | ||
| 980 | scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) | ||
| 981 | { | ||
| 982 | int s; | ||
| 983 | size_t extra_zeroes; | ||
| 984 | unsigned int abs_n; | ||
| 985 | unsigned int abs_s; | ||
| 986 | mp_limb_t *pow5_ptr; | ||
| 987 | size_t pow5_len; | ||
| 988 | unsigned int s_limbs; | ||
| 989 | unsigned int s_bits; | ||
| 990 | mpn_t pow5; | ||
| 991 | mpn_t z; | ||
| 992 | void *z_memory; | ||
| 993 | char *digits; | ||
| 994 | |||
| 995 | if (memory == NULL) | ||
| 996 | return NULL; | ||
| 997 | /* x = 2^e * m, hence | ||
| 998 | y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m) | ||
| 999 | = round (2^s * 5^n * m). */ | ||
| 1000 | s = e + n; | ||
| 1001 | extra_zeroes = 0; | ||
| 1002 | /* Factor out a common power of 10 if possible. */ | ||
| 1003 | if (s > 0 && n > 0) | ||
| 1004 | { | ||
| 1005 | extra_zeroes = (s < n ? s : n); | ||
| 1006 | s -= extra_zeroes; | ||
| 1007 | n -= extra_zeroes; | ||
| 1008 | } | ||
| 1009 | /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes. | ||
| 1010 | Before converting to decimal, we need to compute | ||
| 1011 | z = round (2^s * 5^n * m). */ | ||
| 1012 | /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same | ||
| 1013 | sign. 2.322 is slightly larger than log(5)/log(2). */ | ||
| 1014 | abs_n = (n >= 0 ? n : -n); | ||
| 1015 | abs_s = (s >= 0 ? s : -s); | ||
| 1016 | pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1 | ||
| 1017 | + abs_s / GMP_LIMB_BITS + 1) | ||
| 1018 | * sizeof (mp_limb_t)); | ||
| 1019 | if (pow5_ptr == NULL) | ||
| 1020 | { | ||
| 1021 | free (memory); | ||
| 115 | return NULL; | 1022 | return NULL; |
| 116 | } | 1023 | } |
| 1024 | /* Initialize with 1. */ | ||
| 1025 | pow5_ptr[0] = 1; | ||
| 1026 | pow5_len = 1; | ||
| 1027 | /* Multiply with 5^|n|. */ | ||
| 1028 | if (abs_n > 0) | ||
| 1029 | { | ||
| 1030 | static mp_limb_t const small_pow5[13 + 1] = | ||
| 1031 | { | ||
| 1032 | 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, | ||
| 1033 | 48828125, 244140625, 1220703125 | ||
| 1034 | }; | ||
| 1035 | unsigned int n13; | ||
| 1036 | for (n13 = 0; n13 <= abs_n; n13 += 13) | ||
| 1037 | { | ||
| 1038 | mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13]; | ||
| 1039 | size_t j; | ||
| 1040 | mp_twolimb_t carry = 0; | ||
| 1041 | for (j = 0; j < pow5_len; j++) | ||
| 1042 | { | ||
| 1043 | mp_limb_t digit2 = pow5_ptr[j]; | ||
| 1044 | carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; | ||
| 1045 | pow5_ptr[j] = (mp_limb_t) carry; | ||
| 1046 | carry = carry >> GMP_LIMB_BITS; | ||
| 1047 | } | ||
| 1048 | if (carry > 0) | ||
| 1049 | pow5_ptr[pow5_len++] = (mp_limb_t) carry; | ||
| 1050 | } | ||
| 1051 | } | ||
| 1052 | s_limbs = abs_s / GMP_LIMB_BITS; | ||
| 1053 | s_bits = abs_s % GMP_LIMB_BITS; | ||
| 1054 | if (n >= 0 ? s >= 0 : s <= 0) | ||
| 1055 | { | ||
| 1056 | /* Multiply with 2^|s|. */ | ||
| 1057 | if (s_bits > 0) | ||
| 1058 | { | ||
| 1059 | mp_limb_t *ptr = pow5_ptr; | ||
| 1060 | mp_twolimb_t accu = 0; | ||
| 1061 | size_t count; | ||
| 1062 | for (count = pow5_len; count > 0; count--) | ||
| 1063 | { | ||
| 1064 | accu += (mp_twolimb_t) *ptr << s_bits; | ||
| 1065 | *ptr++ = (mp_limb_t) accu; | ||
| 1066 | accu = accu >> GMP_LIMB_BITS; | ||
| 1067 | } | ||
| 1068 | if (accu > 0) | ||
| 1069 | { | ||
| 1070 | *ptr = (mp_limb_t) accu; | ||
| 1071 | pow5_len++; | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | if (s_limbs > 0) | ||
| 1075 | { | ||
| 1076 | size_t count; | ||
| 1077 | for (count = pow5_len; count > 0;) | ||
| 1078 | { | ||
| 1079 | count--; | ||
| 1080 | pow5_ptr[s_limbs + count] = pow5_ptr[count]; | ||
| 1081 | } | ||
| 1082 | for (count = s_limbs; count > 0;) | ||
| 1083 | { | ||
| 1084 | count--; | ||
| 1085 | pow5_ptr[count] = 0; | ||
| 1086 | } | ||
| 1087 | pow5_len += s_limbs; | ||
| 1088 | } | ||
| 1089 | pow5.limbs = pow5_ptr; | ||
| 1090 | pow5.nlimbs = pow5_len; | ||
| 1091 | if (n >= 0) | ||
| 1092 | { | ||
| 1093 | /* Multiply m with pow5. No division needed. */ | ||
| 1094 | z_memory = multiply (m, pow5, &z); | ||
| 1095 | } | ||
| 1096 | else | ||
| 1097 | { | ||
| 1098 | /* Divide m by pow5 and round. */ | ||
| 1099 | z_memory = divide (m, pow5, &z); | ||
| 1100 | } | ||
| 1101 | } | ||
| 1102 | else | ||
| 1103 | { | ||
| 1104 | pow5.limbs = pow5_ptr; | ||
| 1105 | pow5.nlimbs = pow5_len; | ||
| 1106 | if (n >= 0) | ||
| 1107 | { | ||
| 1108 | /* n >= 0, s < 0. | ||
| 1109 | Multiply m with pow5, then divide by 2^|s|. */ | ||
| 1110 | mpn_t numerator; | ||
| 1111 | mpn_t denominator; | ||
| 1112 | void *tmp_memory; | ||
| 1113 | tmp_memory = multiply (m, pow5, &numerator); | ||
| 1114 | if (tmp_memory == NULL) | ||
| 1115 | { | ||
| 1116 | free (pow5_ptr); | ||
| 1117 | free (memory); | ||
| 1118 | return NULL; | ||
| 1119 | } | ||
| 1120 | /* Construct 2^|s|. */ | ||
| 1121 | { | ||
| 1122 | mp_limb_t *ptr = pow5_ptr + pow5_len; | ||
| 1123 | size_t i; | ||
| 1124 | for (i = 0; i < s_limbs; i++) | ||
| 1125 | ptr[i] = 0; | ||
| 1126 | ptr[s_limbs] = (mp_limb_t) 1 << s_bits; | ||
| 1127 | denominator.limbs = ptr; | ||
| 1128 | denominator.nlimbs = s_limbs + 1; | ||
| 1129 | } | ||
| 1130 | z_memory = divide (numerator, denominator, &z); | ||
| 1131 | free (tmp_memory); | ||
| 1132 | } | ||
| 1133 | else | ||
| 1134 | { | ||
| 1135 | /* n < 0, s > 0. | ||
| 1136 | Multiply m with 2^s, then divide by pow5. */ | ||
| 1137 | mpn_t numerator; | ||
| 1138 | mp_limb_t *num_ptr; | ||
| 1139 | num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1) | ||
| 1140 | * sizeof (mp_limb_t)); | ||
| 1141 | if (num_ptr == NULL) | ||
| 1142 | { | ||
| 1143 | free (pow5_ptr); | ||
| 1144 | free (memory); | ||
| 1145 | return NULL; | ||
| 1146 | } | ||
| 1147 | { | ||
| 1148 | mp_limb_t *destptr = num_ptr; | ||
| 1149 | { | ||
| 1150 | size_t i; | ||
| 1151 | for (i = 0; i < s_limbs; i++) | ||
| 1152 | *destptr++ = 0; | ||
| 1153 | } | ||
| 1154 | if (s_bits > 0) | ||
| 1155 | { | ||
| 1156 | const mp_limb_t *sourceptr = m.limbs; | ||
| 1157 | mp_twolimb_t accu = 0; | ||
| 1158 | size_t count; | ||
| 1159 | for (count = m.nlimbs; count > 0; count--) | ||
| 1160 | { | ||
| 1161 | accu += (mp_twolimb_t) *sourceptr++ << s_bits; | ||
| 1162 | *destptr++ = (mp_limb_t) accu; | ||
| 1163 | accu = accu >> GMP_LIMB_BITS; | ||
| 1164 | } | ||
| 1165 | if (accu > 0) | ||
| 1166 | *destptr++ = (mp_limb_t) accu; | ||
| 1167 | } | ||
| 1168 | else | ||
| 1169 | { | ||
| 1170 | const mp_limb_t *sourceptr = m.limbs; | ||
| 1171 | size_t count; | ||
| 1172 | for (count = m.nlimbs; count > 0; count--) | ||
| 1173 | *destptr++ = *sourceptr++; | ||
| 1174 | } | ||
| 1175 | numerator.limbs = num_ptr; | ||
| 1176 | numerator.nlimbs = destptr - num_ptr; | ||
| 1177 | } | ||
| 1178 | z_memory = divide (numerator, pow5, &z); | ||
| 1179 | free (num_ptr); | ||
| 1180 | } | ||
| 1181 | } | ||
| 1182 | free (pow5_ptr); | ||
| 1183 | free (memory); | ||
| 1184 | |||
| 1185 | /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */ | ||
| 1186 | |||
| 1187 | if (z_memory == NULL) | ||
| 1188 | return NULL; | ||
| 1189 | digits = convert_to_decimal (z, extra_zeroes); | ||
| 1190 | free (z_memory); | ||
| 1191 | return digits; | ||
| 1192 | } | ||
| 1193 | |||
| 1194 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 1195 | |||
| 1196 | /* Assuming x is finite and >= 0, and n is an integer: | ||
| 1197 | Returns the decimal representation of round (x * 10^n). | ||
| 1198 | Return the allocated memory - containing the decimal digits in low-to-high | ||
| 1199 | order, terminated with a NUL character - in case of success, NULL in case | ||
| 1200 | of memory allocation failure. */ | ||
| 1201 | static char * | ||
| 1202 | scale10_round_decimal_long_double (long double x, int n) | ||
| 1203 | { | ||
| 1204 | int e; | ||
| 1205 | mpn_t m; | ||
| 1206 | void *memory = decode_long_double (x, &e, &m); | ||
| 1207 | return scale10_round_decimal_decoded (e, m, memory, n); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | # endif | ||
| 1211 | |||
| 1212 | # if NEED_PRINTF_DOUBLE | ||
| 1213 | |||
| 1214 | /* Assuming x is finite and >= 0, and n is an integer: | ||
| 1215 | Returns the decimal representation of round (x * 10^n). | ||
| 1216 | Return the allocated memory - containing the decimal digits in low-to-high | ||
| 1217 | order, terminated with a NUL character - in case of success, NULL in case | ||
| 1218 | of memory allocation failure. */ | ||
| 1219 | static char * | ||
| 1220 | scale10_round_decimal_double (double x, int n) | ||
| 1221 | { | ||
| 1222 | int e; | ||
| 1223 | mpn_t m; | ||
| 1224 | void *memory = decode_double (x, &e, &m); | ||
| 1225 | return scale10_round_decimal_decoded (e, m, memory, n); | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | # endif | ||
| 1229 | |||
| 1230 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 1231 | |||
| 1232 | /* Assuming x is finite and > 0: | ||
| 1233 | Return an approximation for n with 10^n <= x < 10^(n+1). | ||
| 1234 | The approximation is usually the right n, but may be off by 1 sometimes. */ | ||
| 1235 | static int | ||
| 1236 | floorlog10l (long double x) | ||
| 1237 | { | ||
| 1238 | int exp; | ||
| 1239 | long double y; | ||
| 1240 | double z; | ||
| 1241 | double l; | ||
| 1242 | |||
| 1243 | /* Split into exponential part and mantissa. */ | ||
| 1244 | y = frexpl (x, &exp); | ||
| 1245 | if (!(y >= 0.0L && y < 1.0L)) | ||
| 1246 | abort (); | ||
| 1247 | if (y == 0.0L) | ||
| 1248 | return INT_MIN; | ||
| 1249 | if (y < 0.5L) | ||
| 1250 | { | ||
| 1251 | while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2)))) | ||
| 1252 | { | ||
| 1253 | y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2)); | ||
| 1254 | exp -= GMP_LIMB_BITS; | ||
| 1255 | } | ||
| 1256 | if (y < (1.0L / (1 << 16))) | ||
| 1257 | { | ||
| 1258 | y *= 1.0L * (1 << 16); | ||
| 1259 | exp -= 16; | ||
| 1260 | } | ||
| 1261 | if (y < (1.0L / (1 << 8))) | ||
| 1262 | { | ||
| 1263 | y *= 1.0L * (1 << 8); | ||
| 1264 | exp -= 8; | ||
| 1265 | } | ||
| 1266 | if (y < (1.0L / (1 << 4))) | ||
| 1267 | { | ||
| 1268 | y *= 1.0L * (1 << 4); | ||
| 1269 | exp -= 4; | ||
| 1270 | } | ||
| 1271 | if (y < (1.0L / (1 << 2))) | ||
| 1272 | { | ||
| 1273 | y *= 1.0L * (1 << 2); | ||
| 1274 | exp -= 2; | ||
| 1275 | } | ||
| 1276 | if (y < (1.0L / (1 << 1))) | ||
| 1277 | { | ||
| 1278 | y *= 1.0L * (1 << 1); | ||
| 1279 | exp -= 1; | ||
| 1280 | } | ||
| 1281 | } | ||
| 1282 | if (!(y >= 0.5L && y < 1.0L)) | ||
| 1283 | abort (); | ||
| 1284 | /* Compute an approximation for l = log2(x) = exp + log2(y). */ | ||
| 1285 | l = exp; | ||
| 1286 | z = y; | ||
| 1287 | if (z < 0.70710678118654752444) | ||
| 1288 | { | ||
| 1289 | z *= 1.4142135623730950488; | ||
| 1290 | l -= 0.5; | ||
| 1291 | } | ||
| 1292 | if (z < 0.8408964152537145431) | ||
| 1293 | { | ||
| 1294 | z *= 1.1892071150027210667; | ||
| 1295 | l -= 0.25; | ||
| 1296 | } | ||
| 1297 | if (z < 0.91700404320467123175) | ||
| 1298 | { | ||
| 1299 | z *= 1.0905077326652576592; | ||
| 1300 | l -= 0.125; | ||
| 1301 | } | ||
| 1302 | if (z < 0.9576032806985736469) | ||
| 1303 | { | ||
| 1304 | z *= 1.0442737824274138403; | ||
| 1305 | l -= 0.0625; | ||
| 1306 | } | ||
| 1307 | /* Now 0.95 <= z <= 1.01. */ | ||
| 1308 | z = 1 - z; | ||
| 1309 | /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... | ||
| 1310 | Four terms are enough to get an approximation with error < 10^-7. */ | ||
| 1311 | l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); | ||
| 1312 | /* Finally multiply with log(2)/log(10), yields an approximation for | ||
| 1313 | log10(x). */ | ||
| 1314 | l *= 0.30102999566398119523; | ||
| 1315 | /* Round down to the next integer. */ | ||
| 1316 | return (int) l + (l < 0 ? -1 : 0); | ||
| 1317 | } | ||
| 1318 | |||
| 1319 | # endif | ||
| 1320 | |||
| 1321 | # if NEED_PRINTF_DOUBLE | ||
| 1322 | |||
| 1323 | /* Assuming x is finite and > 0: | ||
| 1324 | Return an approximation for n with 10^n <= x < 10^(n+1). | ||
| 1325 | The approximation is usually the right n, but may be off by 1 sometimes. */ | ||
| 1326 | static int | ||
| 1327 | floorlog10 (double x) | ||
| 1328 | { | ||
| 1329 | int exp; | ||
| 1330 | double y; | ||
| 1331 | double z; | ||
| 1332 | double l; | ||
| 1333 | |||
| 1334 | /* Split into exponential part and mantissa. */ | ||
| 1335 | y = frexp (x, &exp); | ||
| 1336 | if (!(y >= 0.0 && y < 1.0)) | ||
| 1337 | abort (); | ||
| 1338 | if (y == 0.0) | ||
| 1339 | return INT_MIN; | ||
| 1340 | if (y < 0.5) | ||
| 1341 | { | ||
| 1342 | while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2)))) | ||
| 1343 | { | ||
| 1344 | y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2)); | ||
| 1345 | exp -= GMP_LIMB_BITS; | ||
| 1346 | } | ||
| 1347 | if (y < (1.0 / (1 << 16))) | ||
| 1348 | { | ||
| 1349 | y *= 1.0 * (1 << 16); | ||
| 1350 | exp -= 16; | ||
| 1351 | } | ||
| 1352 | if (y < (1.0 / (1 << 8))) | ||
| 1353 | { | ||
| 1354 | y *= 1.0 * (1 << 8); | ||
| 1355 | exp -= 8; | ||
| 1356 | } | ||
| 1357 | if (y < (1.0 / (1 << 4))) | ||
| 1358 | { | ||
| 1359 | y *= 1.0 * (1 << 4); | ||
| 1360 | exp -= 4; | ||
| 1361 | } | ||
| 1362 | if (y < (1.0 / (1 << 2))) | ||
| 1363 | { | ||
| 1364 | y *= 1.0 * (1 << 2); | ||
| 1365 | exp -= 2; | ||
| 1366 | } | ||
| 1367 | if (y < (1.0 / (1 << 1))) | ||
| 1368 | { | ||
| 1369 | y *= 1.0 * (1 << 1); | ||
| 1370 | exp -= 1; | ||
| 1371 | } | ||
| 1372 | } | ||
| 1373 | if (!(y >= 0.5 && y < 1.0)) | ||
| 1374 | abort (); | ||
| 1375 | /* Compute an approximation for l = log2(x) = exp + log2(y). */ | ||
| 1376 | l = exp; | ||
| 1377 | z = y; | ||
| 1378 | if (z < 0.70710678118654752444) | ||
| 1379 | { | ||
| 1380 | z *= 1.4142135623730950488; | ||
| 1381 | l -= 0.5; | ||
| 1382 | } | ||
| 1383 | if (z < 0.8408964152537145431) | ||
| 1384 | { | ||
| 1385 | z *= 1.1892071150027210667; | ||
| 1386 | l -= 0.25; | ||
| 1387 | } | ||
| 1388 | if (z < 0.91700404320467123175) | ||
| 1389 | { | ||
| 1390 | z *= 1.0905077326652576592; | ||
| 1391 | l -= 0.125; | ||
| 1392 | } | ||
| 1393 | if (z < 0.9576032806985736469) | ||
| 1394 | { | ||
| 1395 | z *= 1.0442737824274138403; | ||
| 1396 | l -= 0.0625; | ||
| 1397 | } | ||
| 1398 | /* Now 0.95 <= z <= 1.01. */ | ||
| 1399 | z = 1 - z; | ||
| 1400 | /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... | ||
| 1401 | Four terms are enough to get an approximation with error < 10^-7. */ | ||
| 1402 | l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); | ||
| 1403 | /* Finally multiply with log(2)/log(10), yields an approximation for | ||
| 1404 | log10(x). */ | ||
| 1405 | l *= 0.30102999566398119523; | ||
| 1406 | /* Round down to the next integer. */ | ||
| 1407 | return (int) l + (l < 0 ? -1 : 0); | ||
| 1408 | } | ||
| 1409 | |||
| 1410 | # endif | ||
| 1411 | |||
| 1412 | #endif | ||
| 1413 | |||
| 1414 | DCHAR_T * | ||
| 1415 | VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, | ||
| 1416 | const FCHAR_T *format, va_list args) | ||
| 1417 | { | ||
| 1418 | DIRECTIVES d; | ||
| 1419 | arguments a; | ||
| 1420 | |||
| 1421 | if (PRINTF_PARSE (format, &d, &a) < 0) | ||
| 1422 | /* errno is already set. */ | ||
| 1423 | return NULL; | ||
| 117 | 1424 | ||
| 118 | #define CLEANUP() \ | 1425 | #define CLEANUP() \ |
| 119 | free (d.dir); \ | 1426 | free (d.dir); \ |
| 120 | if (a.arg) \ | 1427 | if (a.arg) \ |
| 121 | free (a.arg); | 1428 | free (a.arg); |
| 122 | 1429 | ||
| 123 | if (printf_fetchargs (args, &a) < 0) | 1430 | if (PRINTF_FETCHARGS (args, &a) < 0) |
| 124 | { | 1431 | { |
| 125 | CLEANUP (); | 1432 | CLEANUP (); |
| 126 | errno = EINVAL; | 1433 | errno = EINVAL; |
| @@ -129,13 +1436,13 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 129 | 1436 | ||
| 130 | { | 1437 | { |
| 131 | size_t buf_neededlength; | 1438 | size_t buf_neededlength; |
| 132 | CHAR_T *buf; | 1439 | TCHAR_T *buf; |
| 133 | CHAR_T *buf_malloced; | 1440 | TCHAR_T *buf_malloced; |
| 134 | const CHAR_T *cp; | 1441 | const FCHAR_T *cp; |
| 135 | size_t i; | 1442 | size_t i; |
| 136 | DIRECTIVE *dp; | 1443 | DIRECTIVE *dp; |
| 137 | /* Output string accumulator. */ | 1444 | /* Output string accumulator. */ |
| 138 | CHAR_T *result; | 1445 | DCHAR_T *result; |
| 139 | size_t allocated; | 1446 | size_t allocated; |
| 140 | size_t length; | 1447 | size_t length; |
| 141 | 1448 | ||
| @@ -144,18 +1451,18 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 144 | buf_neededlength = | 1451 | buf_neededlength = |
| 145 | xsum4 (7, d.max_width_length, d.max_precision_length, 6); | 1452 | xsum4 (7, d.max_width_length, d.max_precision_length, 6); |
| 146 | #if HAVE_ALLOCA | 1453 | #if HAVE_ALLOCA |
| 147 | if (buf_neededlength < 4000 / sizeof (CHAR_T)) | 1454 | if (buf_neededlength < 4000 / sizeof (TCHAR_T)) |
| 148 | { | 1455 | { |
| 149 | buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T)); | 1456 | buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T)); |
| 150 | buf_malloced = NULL; | 1457 | buf_malloced = NULL; |
| 151 | } | 1458 | } |
| 152 | else | 1459 | else |
| 153 | #endif | 1460 | #endif |
| 154 | { | 1461 | { |
| 155 | size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T)); | 1462 | size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T)); |
| 156 | if (size_overflow_p (buf_memsize)) | 1463 | if (size_overflow_p (buf_memsize)) |
| 157 | goto out_of_memory_1; | 1464 | goto out_of_memory_1; |
| 158 | buf = (CHAR_T *) malloc (buf_memsize); | 1465 | buf = (TCHAR_T *) malloc (buf_memsize); |
| 159 | if (buf == NULL) | 1466 | if (buf == NULL) |
| 160 | goto out_of_memory_1; | 1467 | goto out_of_memory_1; |
| 161 | buf_malloced = buf; | 1468 | buf_malloced = buf; |
| @@ -182,22 +1489,22 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 182 | if ((needed) > allocated) \ | 1489 | if ((needed) > allocated) \ |
| 183 | { \ | 1490 | { \ |
| 184 | size_t memory_size; \ | 1491 | size_t memory_size; \ |
| 185 | CHAR_T *memory; \ | 1492 | DCHAR_T *memory; \ |
| 186 | \ | 1493 | \ |
| 187 | allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ | 1494 | allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ |
| 188 | if ((needed) > allocated) \ | 1495 | if ((needed) > allocated) \ |
| 189 | allocated = (needed); \ | 1496 | allocated = (needed); \ |
| 190 | memory_size = xtimes (allocated, sizeof (CHAR_T)); \ | 1497 | memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ |
| 191 | if (size_overflow_p (memory_size)) \ | 1498 | if (size_overflow_p (memory_size)) \ |
| 192 | goto out_of_memory; \ | 1499 | goto out_of_memory; \ |
| 193 | if (result == resultbuf || result == NULL) \ | 1500 | if (result == resultbuf || result == NULL) \ |
| 194 | memory = (CHAR_T *) malloc (memory_size); \ | 1501 | memory = (DCHAR_T *) malloc (memory_size); \ |
| 195 | else \ | 1502 | else \ |
| 196 | memory = (CHAR_T *) realloc (result, memory_size); \ | 1503 | memory = (DCHAR_T *) realloc (result, memory_size); \ |
| 197 | if (memory == NULL) \ | 1504 | if (memory == NULL) \ |
| 198 | goto out_of_memory; \ | 1505 | goto out_of_memory; \ |
| 199 | if (result == resultbuf && length > 0) \ | 1506 | if (result == resultbuf && length > 0) \ |
| 200 | memcpy (memory, result, length * sizeof (CHAR_T)); \ | 1507 | DCHAR_CPY (memory, result, length); \ |
| 201 | result = memory; \ | 1508 | result = memory; \ |
| 202 | } | 1509 | } |
| 203 | 1510 | ||
| @@ -209,8 +1516,20 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 209 | size_t augmented_length = xsum (length, n); | 1516 | size_t augmented_length = xsum (length, n); |
| 210 | 1517 | ||
| 211 | ENSURE_ALLOCATION (augmented_length); | 1518 | ENSURE_ALLOCATION (augmented_length); |
| 212 | memcpy (result + length, cp, n * sizeof (CHAR_T)); | 1519 | /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we |
| 213 | length = augmented_length; | 1520 | need that the format string contains only ASCII characters |
| 1521 | if FCHAR_T and DCHAR_T are not the same type. */ | ||
| 1522 | if (sizeof (FCHAR_T) == sizeof (DCHAR_T)) | ||
| 1523 | { | ||
| 1524 | DCHAR_CPY (result + length, (const DCHAR_T *) cp, n); | ||
| 1525 | length = augmented_length; | ||
| 1526 | } | ||
| 1527 | else | ||
| 1528 | { | ||
| 1529 | do | ||
| 1530 | result[length++] = (unsigned char) *cp++; | ||
| 1531 | while (--n > 0); | ||
| 1532 | } | ||
| 214 | } | 1533 | } |
| 215 | if (i == d.count) | 1534 | if (i == d.count) |
| 216 | break; | 1535 | break; |
| @@ -248,7 +1567,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 248 | case TYPE_COUNT_LONGINT_POINTER: | 1567 | case TYPE_COUNT_LONGINT_POINTER: |
| 249 | *a.arg[dp->arg_index].a.a_count_longint_pointer = length; | 1568 | *a.arg[dp->arg_index].a.a_count_longint_pointer = length; |
| 250 | break; | 1569 | break; |
| 251 | #ifdef HAVE_LONG_LONG_INT | 1570 | #if HAVE_LONG_LONG_INT |
| 252 | case TYPE_COUNT_LONGLONGINT_POINTER: | 1571 | case TYPE_COUNT_LONGLONGINT_POINTER: |
| 253 | *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length; | 1572 | *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length; |
| 254 | break; | 1573 | break; |
| @@ -257,72 +1576,2100 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 257 | abort (); | 1576 | abort (); |
| 258 | } | 1577 | } |
| 259 | } | 1578 | } |
| 260 | else | 1579 | #if ENABLE_UNISTDIO |
| 1580 | /* The unistdio extensions. */ | ||
| 1581 | else if (dp->conversion == 'U') | ||
| 261 | { | 1582 | { |
| 262 | arg_type type = a.arg[dp->arg_index].type; | 1583 | arg_type type = a.arg[dp->arg_index].type; |
| 263 | CHAR_T *p; | 1584 | int flags = dp->flags; |
| 264 | unsigned int prefix_count; | 1585 | int has_width; |
| 265 | int prefixes[2]; | 1586 | size_t width; |
| 266 | #if !USE_SNPRINTF | 1587 | int has_precision; |
| 267 | size_t tmp_length; | 1588 | size_t precision; |
| 268 | CHAR_T tmpbuf[700]; | ||
| 269 | CHAR_T *tmp; | ||
| 270 | 1589 | ||
| 271 | /* Allocate a temporary buffer of sufficient size for calling | 1590 | has_width = 0; |
| 272 | sprintf. */ | 1591 | width = 0; |
| 273 | { | 1592 | if (dp->width_start != dp->width_end) |
| 274 | size_t width; | 1593 | { |
| 275 | size_t precision; | 1594 | if (dp->width_arg_index != ARG_NONE) |
| 1595 | { | ||
| 1596 | int arg; | ||
| 276 | 1597 | ||
| 277 | width = 0; | 1598 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) |
| 278 | if (dp->width_start != dp->width_end) | 1599 | abort (); |
| 1600 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
| 1601 | if (arg < 0) | ||
| 1602 | { | ||
| 1603 | /* "A negative field width is taken as a '-' flag | ||
| 1604 | followed by a positive field width." */ | ||
| 1605 | flags |= FLAG_LEFT; | ||
| 1606 | width = (unsigned int) (-arg); | ||
| 1607 | } | ||
| 1608 | else | ||
| 1609 | width = arg; | ||
| 1610 | } | ||
| 1611 | else | ||
| 1612 | { | ||
| 1613 | const FCHAR_T *digitp = dp->width_start; | ||
| 1614 | |||
| 1615 | do | ||
| 1616 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
| 1617 | while (digitp != dp->width_end); | ||
| 1618 | } | ||
| 1619 | has_width = 1; | ||
| 1620 | } | ||
| 1621 | |||
| 1622 | has_precision = 0; | ||
| 1623 | precision = 0; | ||
| 1624 | if (dp->precision_start != dp->precision_end) | ||
| 1625 | { | ||
| 1626 | if (dp->precision_arg_index != ARG_NONE) | ||
| 1627 | { | ||
| 1628 | int arg; | ||
| 1629 | |||
| 1630 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
| 1631 | abort (); | ||
| 1632 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
| 1633 | /* "A negative precision is taken as if the precision | ||
| 1634 | were omitted." */ | ||
| 1635 | if (arg >= 0) | ||
| 1636 | { | ||
| 1637 | precision = arg; | ||
| 1638 | has_precision = 1; | ||
| 1639 | } | ||
| 1640 | } | ||
| 1641 | else | ||
| 1642 | { | ||
| 1643 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
| 1644 | |||
| 1645 | precision = 0; | ||
| 1646 | while (digitp != dp->precision_end) | ||
| 1647 | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
| 1648 | has_precision = 1; | ||
| 1649 | } | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | switch (type) | ||
| 1653 | { | ||
| 1654 | case TYPE_U8_STRING: | ||
| 279 | { | 1655 | { |
| 280 | if (dp->width_arg_index != ARG_NONE) | 1656 | const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string; |
| 281 | { | 1657 | const uint8_t *arg_end; |
| 282 | int arg; | 1658 | size_t characters; |
| 283 | 1659 | ||
| 284 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | 1660 | if (has_precision) |
| 285 | abort (); | 1661 | { |
| 286 | arg = a.arg[dp->width_arg_index].a.a_int; | 1662 | /* Use only PRECISION characters, from the left. */ |
| 287 | width = (arg < 0 ? (unsigned int) (-arg) : arg); | 1663 | arg_end = arg; |
| 1664 | characters = 0; | ||
| 1665 | for (; precision > 0; precision--) | ||
| 1666 | { | ||
| 1667 | int count = u8_strmblen (arg_end); | ||
| 1668 | if (count == 0) | ||
| 1669 | break; | ||
| 1670 | if (count < 0) | ||
| 1671 | { | ||
| 1672 | if (!(result == resultbuf || result == NULL)) | ||
| 1673 | free (result); | ||
| 1674 | if (buf_malloced != NULL) | ||
| 1675 | free (buf_malloced); | ||
| 1676 | CLEANUP (); | ||
| 1677 | errno = EILSEQ; | ||
| 1678 | return NULL; | ||
| 1679 | } | ||
| 1680 | arg_end += count; | ||
| 1681 | characters++; | ||
| 1682 | } | ||
| 1683 | } | ||
| 1684 | else if (has_width) | ||
| 1685 | { | ||
| 1686 | /* Use the entire string, and count the number of | ||
| 1687 | characters. */ | ||
| 1688 | arg_end = arg; | ||
| 1689 | characters = 0; | ||
| 1690 | for (;;) | ||
| 1691 | { | ||
| 1692 | int count = u8_strmblen (arg_end); | ||
| 1693 | if (count == 0) | ||
| 1694 | break; | ||
| 1695 | if (count < 0) | ||
| 1696 | { | ||
| 1697 | if (!(result == resultbuf || result == NULL)) | ||
| 1698 | free (result); | ||
| 1699 | if (buf_malloced != NULL) | ||
| 1700 | free (buf_malloced); | ||
| 1701 | CLEANUP (); | ||
| 1702 | errno = EILSEQ; | ||
| 1703 | return NULL; | ||
| 1704 | } | ||
| 1705 | arg_end += count; | ||
| 1706 | characters++; | ||
| 1707 | } | ||
| 288 | } | 1708 | } |
| 289 | else | 1709 | else |
| 290 | { | 1710 | { |
| 291 | const CHAR_T *digitp = dp->width_start; | 1711 | /* Use the entire string. */ |
| 1712 | arg_end = arg + u8_strlen (arg); | ||
| 1713 | /* The number of characters doesn't matter. */ | ||
| 1714 | characters = 0; | ||
| 1715 | } | ||
| 292 | 1716 | ||
| 293 | do | 1717 | if (has_width && width > characters |
| 294 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | 1718 | && !(dp->flags & FLAG_LEFT)) |
| 295 | while (digitp != dp->width_end); | 1719 | { |
| 1720 | size_t n = width - characters; | ||
| 1721 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1722 | DCHAR_SET (result + length, ' ', n); | ||
| 1723 | length += n; | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | # if DCHAR_IS_UINT8_T | ||
| 1727 | { | ||
| 1728 | size_t n = arg_end - arg; | ||
| 1729 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1730 | DCHAR_CPY (result + length, arg, n); | ||
| 1731 | length += n; | ||
| 1732 | } | ||
| 1733 | # else | ||
| 1734 | { /* Convert. */ | ||
| 1735 | DCHAR_T *converted = result + length; | ||
| 1736 | size_t converted_len = allocated - length; | ||
| 1737 | # if DCHAR_IS_TCHAR | ||
| 1738 | /* Convert from UTF-8 to locale encoding. */ | ||
| 1739 | if (u8_conv_to_encoding (locale_charset (), | ||
| 1740 | iconveh_question_mark, | ||
| 1741 | arg, arg_end - arg, NULL, | ||
| 1742 | &converted, &converted_len) | ||
| 1743 | < 0) | ||
| 1744 | # else | ||
| 1745 | /* Convert from UTF-8 to UTF-16/UTF-32. */ | ||
| 1746 | converted = | ||
| 1747 | U8_TO_DCHAR (arg, arg_end - arg, | ||
| 1748 | converted, &converted_len); | ||
| 1749 | if (converted == NULL) | ||
| 1750 | # endif | ||
| 1751 | { | ||
| 1752 | int saved_errno = errno; | ||
| 1753 | if (!(result == resultbuf || result == NULL)) | ||
| 1754 | free (result); | ||
| 1755 | if (buf_malloced != NULL) | ||
| 1756 | free (buf_malloced); | ||
| 1757 | CLEANUP (); | ||
| 1758 | errno = saved_errno; | ||
| 1759 | return NULL; | ||
| 1760 | } | ||
| 1761 | if (converted != result + length) | ||
| 1762 | { | ||
| 1763 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
| 1764 | DCHAR_CPY (result + length, converted, converted_len); | ||
| 1765 | free (converted); | ||
| 1766 | } | ||
| 1767 | length += converted_len; | ||
| 1768 | } | ||
| 1769 | # endif | ||
| 1770 | |||
| 1771 | if (has_width && width > characters | ||
| 1772 | && (dp->flags & FLAG_LEFT)) | ||
| 1773 | { | ||
| 1774 | size_t n = width - characters; | ||
| 1775 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1776 | DCHAR_SET (result + length, ' ', n); | ||
| 1777 | length += n; | ||
| 296 | } | 1778 | } |
| 297 | } | 1779 | } |
| 1780 | break; | ||
| 298 | 1781 | ||
| 299 | precision = 6; | 1782 | case TYPE_U16_STRING: |
| 300 | if (dp->precision_start != dp->precision_end) | ||
| 301 | { | 1783 | { |
| 302 | if (dp->precision_arg_index != ARG_NONE) | 1784 | const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string; |
| 1785 | const uint16_t *arg_end; | ||
| 1786 | size_t characters; | ||
| 1787 | |||
| 1788 | if (has_precision) | ||
| 1789 | { | ||
| 1790 | /* Use only PRECISION characters, from the left. */ | ||
| 1791 | arg_end = arg; | ||
| 1792 | characters = 0; | ||
| 1793 | for (; precision > 0; precision--) | ||
| 1794 | { | ||
| 1795 | int count = u16_strmblen (arg_end); | ||
| 1796 | if (count == 0) | ||
| 1797 | break; | ||
| 1798 | if (count < 0) | ||
| 1799 | { | ||
| 1800 | if (!(result == resultbuf || result == NULL)) | ||
| 1801 | free (result); | ||
| 1802 | if (buf_malloced != NULL) | ||
| 1803 | free (buf_malloced); | ||
| 1804 | CLEANUP (); | ||
| 1805 | errno = EILSEQ; | ||
| 1806 | return NULL; | ||
| 1807 | } | ||
| 1808 | arg_end += count; | ||
| 1809 | characters++; | ||
| 1810 | } | ||
| 1811 | } | ||
| 1812 | else if (has_width) | ||
| 303 | { | 1813 | { |
| 304 | int arg; | 1814 | /* Use the entire string, and count the number of |
| 1815 | characters. */ | ||
| 1816 | arg_end = arg; | ||
| 1817 | characters = 0; | ||
| 1818 | for (;;) | ||
| 1819 | { | ||
| 1820 | int count = u16_strmblen (arg_end); | ||
| 1821 | if (count == 0) | ||
| 1822 | break; | ||
| 1823 | if (count < 0) | ||
| 1824 | { | ||
| 1825 | if (!(result == resultbuf || result == NULL)) | ||
| 1826 | free (result); | ||
| 1827 | if (buf_malloced != NULL) | ||
| 1828 | free (buf_malloced); | ||
| 1829 | CLEANUP (); | ||
| 1830 | errno = EILSEQ; | ||
| 1831 | return NULL; | ||
| 1832 | } | ||
| 1833 | arg_end += count; | ||
| 1834 | characters++; | ||
| 1835 | } | ||
| 1836 | } | ||
| 1837 | else | ||
| 1838 | { | ||
| 1839 | /* Use the entire string. */ | ||
| 1840 | arg_end = arg + u16_strlen (arg); | ||
| 1841 | /* The number of characters doesn't matter. */ | ||
| 1842 | characters = 0; | ||
| 1843 | } | ||
| 305 | 1844 | ||
| 306 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | 1845 | if (has_width && width > characters |
| 307 | abort (); | 1846 | && !(dp->flags & FLAG_LEFT)) |
| 308 | arg = a.arg[dp->precision_arg_index].a.a_int; | 1847 | { |
| 309 | precision = (arg < 0 ? 0 : arg); | 1848 | size_t n = width - characters; |
| 1849 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1850 | DCHAR_SET (result + length, ' ', n); | ||
| 1851 | length += n; | ||
| 1852 | } | ||
| 1853 | |||
| 1854 | # if DCHAR_IS_UINT16_T | ||
| 1855 | { | ||
| 1856 | size_t n = arg_end - arg; | ||
| 1857 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1858 | DCHAR_CPY (result + length, arg, n); | ||
| 1859 | length += n; | ||
| 1860 | } | ||
| 1861 | # else | ||
| 1862 | { /* Convert. */ | ||
| 1863 | DCHAR_T *converted = result + length; | ||
| 1864 | size_t converted_len = allocated - length; | ||
| 1865 | # if DCHAR_IS_TCHAR | ||
| 1866 | /* Convert from UTF-16 to locale encoding. */ | ||
| 1867 | if (u16_conv_to_encoding (locale_charset (), | ||
| 1868 | iconveh_question_mark, | ||
| 1869 | arg, arg_end - arg, NULL, | ||
| 1870 | &converted, &converted_len) | ||
| 1871 | < 0) | ||
| 1872 | # else | ||
| 1873 | /* Convert from UTF-16 to UTF-8/UTF-32. */ | ||
| 1874 | converted = | ||
| 1875 | U16_TO_DCHAR (arg, arg_end - arg, | ||
| 1876 | converted, &converted_len); | ||
| 1877 | if (converted == NULL) | ||
| 1878 | # endif | ||
| 1879 | { | ||
| 1880 | int saved_errno = errno; | ||
| 1881 | if (!(result == resultbuf || result == NULL)) | ||
| 1882 | free (result); | ||
| 1883 | if (buf_malloced != NULL) | ||
| 1884 | free (buf_malloced); | ||
| 1885 | CLEANUP (); | ||
| 1886 | errno = saved_errno; | ||
| 1887 | return NULL; | ||
| 1888 | } | ||
| 1889 | if (converted != result + length) | ||
| 1890 | { | ||
| 1891 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
| 1892 | DCHAR_CPY (result + length, converted, converted_len); | ||
| 1893 | free (converted); | ||
| 1894 | } | ||
| 1895 | length += converted_len; | ||
| 1896 | } | ||
| 1897 | # endif | ||
| 1898 | |||
| 1899 | if (has_width && width > characters | ||
| 1900 | && (dp->flags & FLAG_LEFT)) | ||
| 1901 | { | ||
| 1902 | size_t n = width - characters; | ||
| 1903 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1904 | DCHAR_SET (result + length, ' ', n); | ||
| 1905 | length += n; | ||
| 1906 | } | ||
| 1907 | } | ||
| 1908 | break; | ||
| 1909 | |||
| 1910 | case TYPE_U32_STRING: | ||
| 1911 | { | ||
| 1912 | const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string; | ||
| 1913 | const uint32_t *arg_end; | ||
| 1914 | size_t characters; | ||
| 1915 | |||
| 1916 | if (has_precision) | ||
| 1917 | { | ||
| 1918 | /* Use only PRECISION characters, from the left. */ | ||
| 1919 | arg_end = arg; | ||
| 1920 | characters = 0; | ||
| 1921 | for (; precision > 0; precision--) | ||
| 1922 | { | ||
| 1923 | int count = u32_strmblen (arg_end); | ||
| 1924 | if (count == 0) | ||
| 1925 | break; | ||
| 1926 | if (count < 0) | ||
| 1927 | { | ||
| 1928 | if (!(result == resultbuf || result == NULL)) | ||
| 1929 | free (result); | ||
| 1930 | if (buf_malloced != NULL) | ||
| 1931 | free (buf_malloced); | ||
| 1932 | CLEANUP (); | ||
| 1933 | errno = EILSEQ; | ||
| 1934 | return NULL; | ||
| 1935 | } | ||
| 1936 | arg_end += count; | ||
| 1937 | characters++; | ||
| 1938 | } | ||
| 1939 | } | ||
| 1940 | else if (has_width) | ||
| 1941 | { | ||
| 1942 | /* Use the entire string, and count the number of | ||
| 1943 | characters. */ | ||
| 1944 | arg_end = arg; | ||
| 1945 | characters = 0; | ||
| 1946 | for (;;) | ||
| 1947 | { | ||
| 1948 | int count = u32_strmblen (arg_end); | ||
| 1949 | if (count == 0) | ||
| 1950 | break; | ||
| 1951 | if (count < 0) | ||
| 1952 | { | ||
| 1953 | if (!(result == resultbuf || result == NULL)) | ||
| 1954 | free (result); | ||
| 1955 | if (buf_malloced != NULL) | ||
| 1956 | free (buf_malloced); | ||
| 1957 | CLEANUP (); | ||
| 1958 | errno = EILSEQ; | ||
| 1959 | return NULL; | ||
| 1960 | } | ||
| 1961 | arg_end += count; | ||
| 1962 | characters++; | ||
| 1963 | } | ||
| 310 | } | 1964 | } |
| 311 | else | 1965 | else |
| 312 | { | 1966 | { |
| 313 | const CHAR_T *digitp = dp->precision_start + 1; | 1967 | /* Use the entire string. */ |
| 1968 | arg_end = arg + u32_strlen (arg); | ||
| 1969 | /* The number of characters doesn't matter. */ | ||
| 1970 | characters = 0; | ||
| 1971 | } | ||
| 314 | 1972 | ||
| 315 | precision = 0; | 1973 | if (has_width && width > characters |
| 316 | while (digitp != dp->precision_end) | 1974 | && !(dp->flags & FLAG_LEFT)) |
| 317 | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); | 1975 | { |
| 1976 | size_t n = width - characters; | ||
| 1977 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1978 | DCHAR_SET (result + length, ' ', n); | ||
| 1979 | length += n; | ||
| 1980 | } | ||
| 1981 | |||
| 1982 | # if DCHAR_IS_UINT32_T | ||
| 1983 | { | ||
| 1984 | size_t n = arg_end - arg; | ||
| 1985 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 1986 | DCHAR_CPY (result + length, arg, n); | ||
| 1987 | length += n; | ||
| 1988 | } | ||
| 1989 | # else | ||
| 1990 | { /* Convert. */ | ||
| 1991 | DCHAR_T *converted = result + length; | ||
| 1992 | size_t converted_len = allocated - length; | ||
| 1993 | # if DCHAR_IS_TCHAR | ||
| 1994 | /* Convert from UTF-32 to locale encoding. */ | ||
| 1995 | if (u32_conv_to_encoding (locale_charset (), | ||
| 1996 | iconveh_question_mark, | ||
| 1997 | arg, arg_end - arg, NULL, | ||
| 1998 | &converted, &converted_len) | ||
| 1999 | < 0) | ||
| 2000 | # else | ||
| 2001 | /* Convert from UTF-32 to UTF-8/UTF-16. */ | ||
| 2002 | converted = | ||
| 2003 | U32_TO_DCHAR (arg, arg_end - arg, | ||
| 2004 | converted, &converted_len); | ||
| 2005 | if (converted == NULL) | ||
| 2006 | # endif | ||
| 2007 | { | ||
| 2008 | int saved_errno = errno; | ||
| 2009 | if (!(result == resultbuf || result == NULL)) | ||
| 2010 | free (result); | ||
| 2011 | if (buf_malloced != NULL) | ||
| 2012 | free (buf_malloced); | ||
| 2013 | CLEANUP (); | ||
| 2014 | errno = saved_errno; | ||
| 2015 | return NULL; | ||
| 2016 | } | ||
| 2017 | if (converted != result + length) | ||
| 2018 | { | ||
| 2019 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
| 2020 | DCHAR_CPY (result + length, converted, converted_len); | ||
| 2021 | free (converted); | ||
| 2022 | } | ||
| 2023 | length += converted_len; | ||
| 2024 | } | ||
| 2025 | # endif | ||
| 2026 | |||
| 2027 | if (has_width && width > characters | ||
| 2028 | && (dp->flags & FLAG_LEFT)) | ||
| 2029 | { | ||
| 2030 | size_t n = width - characters; | ||
| 2031 | ENSURE_ALLOCATION (xsum (length, n)); | ||
| 2032 | DCHAR_SET (result + length, ' ', n); | ||
| 2033 | length += n; | ||
| 318 | } | 2034 | } |
| 319 | } | 2035 | } |
| 2036 | break; | ||
| 320 | 2037 | ||
| 2038 | default: | ||
| 2039 | abort (); | ||
| 2040 | } | ||
| 2041 | } | ||
| 2042 | #endif | ||
| 2043 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
| 2044 | else if ((dp->conversion == 'a' || dp->conversion == 'A') | ||
| 2045 | # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE)) | ||
| 2046 | && (0 | ||
| 2047 | # if NEED_PRINTF_DOUBLE | ||
| 2048 | || a.arg[dp->arg_index].type == TYPE_DOUBLE | ||
| 2049 | # endif | ||
| 2050 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 2051 | || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE | ||
| 2052 | # endif | ||
| 2053 | ) | ||
| 2054 | # endif | ||
| 2055 | ) | ||
| 2056 | { | ||
| 2057 | arg_type type = a.arg[dp->arg_index].type; | ||
| 2058 | int flags = dp->flags; | ||
| 2059 | int has_width; | ||
| 2060 | size_t width; | ||
| 2061 | int has_precision; | ||
| 2062 | size_t precision; | ||
| 2063 | size_t tmp_length; | ||
| 2064 | DCHAR_T tmpbuf[700]; | ||
| 2065 | DCHAR_T *tmp; | ||
| 2066 | DCHAR_T *pad_ptr; | ||
| 2067 | DCHAR_T *p; | ||
| 2068 | |||
| 2069 | has_width = 0; | ||
| 2070 | width = 0; | ||
| 2071 | if (dp->width_start != dp->width_end) | ||
| 2072 | { | ||
| 2073 | if (dp->width_arg_index != ARG_NONE) | ||
| 2074 | { | ||
| 2075 | int arg; | ||
| 2076 | |||
| 2077 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
| 2078 | abort (); | ||
| 2079 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
| 2080 | if (arg < 0) | ||
| 2081 | { | ||
| 2082 | /* "A negative field width is taken as a '-' flag | ||
| 2083 | followed by a positive field width." */ | ||
| 2084 | flags |= FLAG_LEFT; | ||
| 2085 | width = (unsigned int) (-arg); | ||
| 2086 | } | ||
| 2087 | else | ||
| 2088 | width = arg; | ||
| 2089 | } | ||
| 2090 | else | ||
| 2091 | { | ||
| 2092 | const FCHAR_T *digitp = dp->width_start; | ||
| 2093 | |||
| 2094 | do | ||
| 2095 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
| 2096 | while (digitp != dp->width_end); | ||
| 2097 | } | ||
| 2098 | has_width = 1; | ||
| 2099 | } | ||
| 2100 | |||
| 2101 | has_precision = 0; | ||
| 2102 | precision = 0; | ||
| 2103 | if (dp->precision_start != dp->precision_end) | ||
| 2104 | { | ||
| 2105 | if (dp->precision_arg_index != ARG_NONE) | ||
| 2106 | { | ||
| 2107 | int arg; | ||
| 2108 | |||
| 2109 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
| 2110 | abort (); | ||
| 2111 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
| 2112 | /* "A negative precision is taken as if the precision | ||
| 2113 | were omitted." */ | ||
| 2114 | if (arg >= 0) | ||
| 2115 | { | ||
| 2116 | precision = arg; | ||
| 2117 | has_precision = 1; | ||
| 2118 | } | ||
| 2119 | } | ||
| 2120 | else | ||
| 2121 | { | ||
| 2122 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
| 2123 | |||
| 2124 | precision = 0; | ||
| 2125 | while (digitp != dp->precision_end) | ||
| 2126 | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
| 2127 | has_precision = 1; | ||
| 2128 | } | ||
| 2129 | } | ||
| 2130 | |||
| 2131 | /* Allocate a temporary buffer of sufficient size. */ | ||
| 2132 | if (type == TYPE_LONGDOUBLE) | ||
| 2133 | tmp_length = | ||
| 2134 | (unsigned int) ((LDBL_DIG + 1) | ||
| 2135 | * 0.831 /* decimal -> hexadecimal */ | ||
| 2136 | ) | ||
| 2137 | + 1; /* turn floor into ceil */ | ||
| 2138 | else | ||
| 2139 | tmp_length = | ||
| 2140 | (unsigned int) ((DBL_DIG + 1) | ||
| 2141 | * 0.831 /* decimal -> hexadecimal */ | ||
| 2142 | ) | ||
| 2143 | + 1; /* turn floor into ceil */ | ||
| 2144 | if (tmp_length < precision) | ||
| 2145 | tmp_length = precision; | ||
| 2146 | /* Account for sign, decimal point etc. */ | ||
| 2147 | tmp_length = xsum (tmp_length, 12); | ||
| 2148 | |||
| 2149 | if (tmp_length < width) | ||
| 2150 | tmp_length = width; | ||
| 2151 | |||
| 2152 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | ||
| 2153 | |||
| 2154 | if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) | ||
| 2155 | tmp = tmpbuf; | ||
| 2156 | else | ||
| 2157 | { | ||
| 2158 | size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T)); | ||
| 2159 | |||
| 2160 | if (size_overflow_p (tmp_memsize)) | ||
| 2161 | /* Overflow, would lead to out of memory. */ | ||
| 2162 | goto out_of_memory; | ||
| 2163 | tmp = (DCHAR_T *) malloc (tmp_memsize); | ||
| 2164 | if (tmp == NULL) | ||
| 2165 | /* Out of memory. */ | ||
| 2166 | goto out_of_memory; | ||
| 2167 | } | ||
| 2168 | |||
| 2169 | pad_ptr = NULL; | ||
| 2170 | p = tmp; | ||
| 2171 | if (type == TYPE_LONGDOUBLE) | ||
| 2172 | { | ||
| 2173 | # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE | ||
| 2174 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
| 2175 | |||
| 2176 | if (isnanl (arg)) | ||
| 2177 | { | ||
| 2178 | if (dp->conversion == 'A') | ||
| 2179 | { | ||
| 2180 | *p++ = 'N'; *p++ = 'A'; *p++ = 'N'; | ||
| 2181 | } | ||
| 2182 | else | ||
| 2183 | { | ||
| 2184 | *p++ = 'n'; *p++ = 'a'; *p++ = 'n'; | ||
| 2185 | } | ||
| 2186 | } | ||
| 2187 | else | ||
| 2188 | { | ||
| 2189 | int sign = 0; | ||
| 2190 | DECL_LONG_DOUBLE_ROUNDING | ||
| 2191 | |||
| 2192 | BEGIN_LONG_DOUBLE_ROUNDING (); | ||
| 2193 | |||
| 2194 | if (signbit (arg)) /* arg < 0.0L or negative zero */ | ||
| 2195 | { | ||
| 2196 | sign = -1; | ||
| 2197 | arg = -arg; | ||
| 2198 | } | ||
| 2199 | |||
| 2200 | if (sign < 0) | ||
| 2201 | *p++ = '-'; | ||
| 2202 | else if (flags & FLAG_SHOWSIGN) | ||
| 2203 | *p++ = '+'; | ||
| 2204 | else if (flags & FLAG_SPACE) | ||
| 2205 | *p++ = ' '; | ||
| 2206 | |||
| 2207 | if (arg > 0.0L && arg + arg == arg) | ||
| 2208 | { | ||
| 2209 | if (dp->conversion == 'A') | ||
| 2210 | { | ||
| 2211 | *p++ = 'I'; *p++ = 'N'; *p++ = 'F'; | ||
| 2212 | } | ||
| 2213 | else | ||
| 2214 | { | ||
| 2215 | *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
| 2216 | } | ||
| 2217 | } | ||
| 2218 | else | ||
| 2219 | { | ||
| 2220 | int exponent; | ||
| 2221 | long double mantissa; | ||
| 2222 | |||
| 2223 | if (arg > 0.0L) | ||
| 2224 | mantissa = printf_frexpl (arg, &exponent); | ||
| 2225 | else | ||
| 2226 | { | ||
| 2227 | exponent = 0; | ||
| 2228 | mantissa = 0.0L; | ||
| 2229 | } | ||
| 2230 | |||
| 2231 | if (has_precision | ||
| 2232 | && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1) | ||
| 2233 | { | ||
| 2234 | /* Round the mantissa. */ | ||
| 2235 | long double tail = mantissa; | ||
| 2236 | size_t q; | ||
| 2237 | |||
| 2238 | for (q = precision; ; q--) | ||
| 2239 | { | ||
| 2240 | int digit = (int) tail; | ||
| 2241 | tail -= digit; | ||
| 2242 | if (q == 0) | ||
| 2243 | { | ||
| 2244 | if (digit & 1 ? tail >= 0.5L : tail > 0.5L) | ||
| 2245 | tail = 1 - tail; | ||
| 2246 | else | ||
| 2247 | tail = - tail; | ||
| 2248 | break; | ||
| 2249 | } | ||
| 2250 | tail *= 16.0L; | ||
| 2251 | } | ||
| 2252 | if (tail != 0.0L) | ||
| 2253 | for (q = precision; q > 0; q--) | ||
| 2254 | tail *= 0.0625L; | ||
| 2255 | mantissa += tail; | ||
| 2256 | } | ||
| 2257 | |||
| 2258 | *p++ = '0'; | ||
| 2259 | *p++ = dp->conversion - 'A' + 'X'; | ||
| 2260 | pad_ptr = p; | ||
| 2261 | { | ||
| 2262 | int digit; | ||
| 2263 | |||
| 2264 | digit = (int) mantissa; | ||
| 2265 | mantissa -= digit; | ||
| 2266 | *p++ = '0' + digit; | ||
| 2267 | if ((flags & FLAG_ALT) | ||
| 2268 | || mantissa > 0.0L || precision > 0) | ||
| 2269 | { | ||
| 2270 | *p++ = decimal_point_char (); | ||
| 2271 | /* This loop terminates because we assume | ||
| 2272 | that FLT_RADIX is a power of 2. */ | ||
| 2273 | while (mantissa > 0.0L) | ||
| 2274 | { | ||
| 2275 | mantissa *= 16.0L; | ||
| 2276 | digit = (int) mantissa; | ||
| 2277 | mantissa -= digit; | ||
| 2278 | *p++ = digit | ||
| 2279 | + (digit < 10 | ||
| 2280 | ? '0' | ||
| 2281 | : dp->conversion - 10); | ||
| 2282 | if (precision > 0) | ||
| 2283 | precision--; | ||
| 2284 | } | ||
| 2285 | while (precision > 0) | ||
| 2286 | { | ||
| 2287 | *p++ = '0'; | ||
| 2288 | precision--; | ||
| 2289 | } | ||
| 2290 | } | ||
| 2291 | } | ||
| 2292 | *p++ = dp->conversion - 'A' + 'P'; | ||
| 2293 | # if WIDE_CHAR_VERSION | ||
| 2294 | { | ||
| 2295 | static const wchar_t decimal_format[] = | ||
| 2296 | { '%', '+', 'd', '\0' }; | ||
| 2297 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 2298 | } | ||
| 2299 | while (*p != '\0') | ||
| 2300 | p++; | ||
| 2301 | # else | ||
| 2302 | if (sizeof (DCHAR_T) == 1) | ||
| 2303 | { | ||
| 2304 | sprintf ((char *) p, "%+d", exponent); | ||
| 2305 | while (*p != '\0') | ||
| 2306 | p++; | ||
| 2307 | } | ||
| 2308 | else | ||
| 2309 | { | ||
| 2310 | char expbuf[6 + 1]; | ||
| 2311 | const char *ep; | ||
| 2312 | sprintf (expbuf, "%+d", exponent); | ||
| 2313 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 2314 | p++; | ||
| 2315 | } | ||
| 2316 | # endif | ||
| 2317 | } | ||
| 2318 | |||
| 2319 | END_LONG_DOUBLE_ROUNDING (); | ||
| 2320 | } | ||
| 2321 | # else | ||
| 2322 | abort (); | ||
| 2323 | # endif | ||
| 2324 | } | ||
| 2325 | else | ||
| 2326 | { | ||
| 2327 | # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE | ||
| 2328 | double arg = a.arg[dp->arg_index].a.a_double; | ||
| 2329 | |||
| 2330 | if (isnand (arg)) | ||
| 2331 | { | ||
| 2332 | if (dp->conversion == 'A') | ||
| 2333 | { | ||
| 2334 | *p++ = 'N'; *p++ = 'A'; *p++ = 'N'; | ||
| 2335 | } | ||
| 2336 | else | ||
| 2337 | { | ||
| 2338 | *p++ = 'n'; *p++ = 'a'; *p++ = 'n'; | ||
| 2339 | } | ||
| 2340 | } | ||
| 2341 | else | ||
| 2342 | { | ||
| 2343 | int sign = 0; | ||
| 2344 | |||
| 2345 | if (signbit (arg)) /* arg < 0.0 or negative zero */ | ||
| 2346 | { | ||
| 2347 | sign = -1; | ||
| 2348 | arg = -arg; | ||
| 2349 | } | ||
| 2350 | |||
| 2351 | if (sign < 0) | ||
| 2352 | *p++ = '-'; | ||
| 2353 | else if (flags & FLAG_SHOWSIGN) | ||
| 2354 | *p++ = '+'; | ||
| 2355 | else if (flags & FLAG_SPACE) | ||
| 2356 | *p++ = ' '; | ||
| 2357 | |||
| 2358 | if (arg > 0.0 && arg + arg == arg) | ||
| 2359 | { | ||
| 2360 | if (dp->conversion == 'A') | ||
| 2361 | { | ||
| 2362 | *p++ = 'I'; *p++ = 'N'; *p++ = 'F'; | ||
| 2363 | } | ||
| 2364 | else | ||
| 2365 | { | ||
| 2366 | *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
| 2367 | } | ||
| 2368 | } | ||
| 2369 | else | ||
| 2370 | { | ||
| 2371 | int exponent; | ||
| 2372 | double mantissa; | ||
| 2373 | |||
| 2374 | if (arg > 0.0) | ||
| 2375 | mantissa = printf_frexp (arg, &exponent); | ||
| 2376 | else | ||
| 2377 | { | ||
| 2378 | exponent = 0; | ||
| 2379 | mantissa = 0.0; | ||
| 2380 | } | ||
| 2381 | |||
| 2382 | if (has_precision | ||
| 2383 | && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1) | ||
| 2384 | { | ||
| 2385 | /* Round the mantissa. */ | ||
| 2386 | double tail = mantissa; | ||
| 2387 | size_t q; | ||
| 2388 | |||
| 2389 | for (q = precision; ; q--) | ||
| 2390 | { | ||
| 2391 | int digit = (int) tail; | ||
| 2392 | tail -= digit; | ||
| 2393 | if (q == 0) | ||
| 2394 | { | ||
| 2395 | if (digit & 1 ? tail >= 0.5 : tail > 0.5) | ||
| 2396 | tail = 1 - tail; | ||
| 2397 | else | ||
| 2398 | tail = - tail; | ||
| 2399 | break; | ||
| 2400 | } | ||
| 2401 | tail *= 16.0; | ||
| 2402 | } | ||
| 2403 | if (tail != 0.0) | ||
| 2404 | for (q = precision; q > 0; q--) | ||
| 2405 | tail *= 0.0625; | ||
| 2406 | mantissa += tail; | ||
| 2407 | } | ||
| 2408 | |||
| 2409 | *p++ = '0'; | ||
| 2410 | *p++ = dp->conversion - 'A' + 'X'; | ||
| 2411 | pad_ptr = p; | ||
| 2412 | { | ||
| 2413 | int digit; | ||
| 2414 | |||
| 2415 | digit = (int) mantissa; | ||
| 2416 | mantissa -= digit; | ||
| 2417 | *p++ = '0' + digit; | ||
| 2418 | if ((flags & FLAG_ALT) | ||
| 2419 | || mantissa > 0.0 || precision > 0) | ||
| 2420 | { | ||
| 2421 | *p++ = decimal_point_char (); | ||
| 2422 | /* This loop terminates because we assume | ||
| 2423 | that FLT_RADIX is a power of 2. */ | ||
| 2424 | while (mantissa > 0.0) | ||
| 2425 | { | ||
| 2426 | mantissa *= 16.0; | ||
| 2427 | digit = (int) mantissa; | ||
| 2428 | mantissa -= digit; | ||
| 2429 | *p++ = digit | ||
| 2430 | + (digit < 10 | ||
| 2431 | ? '0' | ||
| 2432 | : dp->conversion - 10); | ||
| 2433 | if (precision > 0) | ||
| 2434 | precision--; | ||
| 2435 | } | ||
| 2436 | while (precision > 0) | ||
| 2437 | { | ||
| 2438 | *p++ = '0'; | ||
| 2439 | precision--; | ||
| 2440 | } | ||
| 2441 | } | ||
| 2442 | } | ||
| 2443 | *p++ = dp->conversion - 'A' + 'P'; | ||
| 2444 | # if WIDE_CHAR_VERSION | ||
| 2445 | { | ||
| 2446 | static const wchar_t decimal_format[] = | ||
| 2447 | { '%', '+', 'd', '\0' }; | ||
| 2448 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 2449 | } | ||
| 2450 | while (*p != '\0') | ||
| 2451 | p++; | ||
| 2452 | # else | ||
| 2453 | if (sizeof (DCHAR_T) == 1) | ||
| 2454 | { | ||
| 2455 | sprintf ((char *) p, "%+d", exponent); | ||
| 2456 | while (*p != '\0') | ||
| 2457 | p++; | ||
| 2458 | } | ||
| 2459 | else | ||
| 2460 | { | ||
| 2461 | char expbuf[6 + 1]; | ||
| 2462 | const char *ep; | ||
| 2463 | sprintf (expbuf, "%+d", exponent); | ||
| 2464 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 2465 | p++; | ||
| 2466 | } | ||
| 2467 | # endif | ||
| 2468 | } | ||
| 2469 | } | ||
| 2470 | # else | ||
| 2471 | abort (); | ||
| 2472 | # endif | ||
| 2473 | } | ||
| 2474 | /* The generated string now extends from tmp to p, with the | ||
| 2475 | zero padding insertion point being at pad_ptr. */ | ||
| 2476 | if (has_width && p - tmp < width) | ||
| 2477 | { | ||
| 2478 | size_t pad = width - (p - tmp); | ||
| 2479 | DCHAR_T *end = p + pad; | ||
| 2480 | |||
| 2481 | if (flags & FLAG_LEFT) | ||
| 2482 | { | ||
| 2483 | /* Pad with spaces on the right. */ | ||
| 2484 | for (; pad > 0; pad--) | ||
| 2485 | *p++ = ' '; | ||
| 2486 | } | ||
| 2487 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
| 2488 | { | ||
| 2489 | /* Pad with zeroes. */ | ||
| 2490 | DCHAR_T *q = end; | ||
| 2491 | |||
| 2492 | while (p > pad_ptr) | ||
| 2493 | *--q = *--p; | ||
| 2494 | for (; pad > 0; pad--) | ||
| 2495 | *p++ = '0'; | ||
| 2496 | } | ||
| 2497 | else | ||
| 2498 | { | ||
| 2499 | /* Pad with spaces on the left. */ | ||
| 2500 | DCHAR_T *q = end; | ||
| 2501 | |||
| 2502 | while (p > tmp) | ||
| 2503 | *--q = *--p; | ||
| 2504 | for (; pad > 0; pad--) | ||
| 2505 | *p++ = ' '; | ||
| 2506 | } | ||
| 2507 | |||
| 2508 | p = end; | ||
| 2509 | } | ||
| 2510 | |||
| 2511 | { | ||
| 2512 | size_t count = p - tmp; | ||
| 2513 | |||
| 2514 | if (count >= tmp_length) | ||
| 2515 | /* tmp_length was incorrectly calculated - fix the | ||
| 2516 | code above! */ | ||
| 2517 | abort (); | ||
| 2518 | |||
| 2519 | /* Make room for the result. */ | ||
| 2520 | if (count >= allocated - length) | ||
| 2521 | { | ||
| 2522 | size_t n = xsum (length, count); | ||
| 2523 | |||
| 2524 | ENSURE_ALLOCATION (n); | ||
| 2525 | } | ||
| 2526 | |||
| 2527 | /* Append the result. */ | ||
| 2528 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); | ||
| 2529 | if (tmp != tmpbuf) | ||
| 2530 | free (tmp); | ||
| 2531 | length += count; | ||
| 2532 | } | ||
| 2533 | } | ||
| 2534 | #endif | ||
| 2535 | #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL | ||
| 2536 | else if ((dp->conversion == 'f' || dp->conversion == 'F' | ||
| 2537 | || dp->conversion == 'e' || dp->conversion == 'E' | ||
| 2538 | || dp->conversion == 'g' || dp->conversion == 'G' | ||
| 2539 | || dp->conversion == 'a' || dp->conversion == 'A') | ||
| 2540 | && (0 | ||
| 2541 | # if NEED_PRINTF_DOUBLE | ||
| 2542 | || a.arg[dp->arg_index].type == TYPE_DOUBLE | ||
| 2543 | # elif NEED_PRINTF_INFINITE_DOUBLE | ||
| 2544 | || (a.arg[dp->arg_index].type == TYPE_DOUBLE | ||
| 2545 | /* The systems (mingw) which produce wrong output | ||
| 2546 | for Inf, -Inf, and NaN also do so for -0.0. | ||
| 2547 | Therefore we treat this case here as well. */ | ||
| 2548 | && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double)) | ||
| 2549 | # endif | ||
| 2550 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 2551 | || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE | ||
| 2552 | # elif NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
| 2553 | || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE | ||
| 2554 | /* Some systems produce wrong output for Inf, | ||
| 2555 | -Inf, and NaN. */ | ||
| 2556 | && is_infinitel (a.arg[dp->arg_index].a.a_longdouble)) | ||
| 2557 | # endif | ||
| 2558 | )) | ||
| 2559 | { | ||
| 2560 | # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) | ||
| 2561 | arg_type type = a.arg[dp->arg_index].type; | ||
| 2562 | # endif | ||
| 2563 | int flags = dp->flags; | ||
| 2564 | int has_width; | ||
| 2565 | size_t width; | ||
| 2566 | int has_precision; | ||
| 2567 | size_t precision; | ||
| 2568 | size_t tmp_length; | ||
| 2569 | DCHAR_T tmpbuf[700]; | ||
| 2570 | DCHAR_T *tmp; | ||
| 2571 | DCHAR_T *pad_ptr; | ||
| 2572 | DCHAR_T *p; | ||
| 2573 | |||
| 2574 | has_width = 0; | ||
| 2575 | width = 0; | ||
| 2576 | if (dp->width_start != dp->width_end) | ||
| 2577 | { | ||
| 2578 | if (dp->width_arg_index != ARG_NONE) | ||
| 2579 | { | ||
| 2580 | int arg; | ||
| 2581 | |||
| 2582 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
| 2583 | abort (); | ||
| 2584 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
| 2585 | if (arg < 0) | ||
| 2586 | { | ||
| 2587 | /* "A negative field width is taken as a '-' flag | ||
| 2588 | followed by a positive field width." */ | ||
| 2589 | flags |= FLAG_LEFT; | ||
| 2590 | width = (unsigned int) (-arg); | ||
| 2591 | } | ||
| 2592 | else | ||
| 2593 | width = arg; | ||
| 2594 | } | ||
| 2595 | else | ||
| 2596 | { | ||
| 2597 | const FCHAR_T *digitp = dp->width_start; | ||
| 2598 | |||
| 2599 | do | ||
| 2600 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
| 2601 | while (digitp != dp->width_end); | ||
| 2602 | } | ||
| 2603 | has_width = 1; | ||
| 2604 | } | ||
| 2605 | |||
| 2606 | has_precision = 0; | ||
| 2607 | precision = 0; | ||
| 2608 | if (dp->precision_start != dp->precision_end) | ||
| 2609 | { | ||
| 2610 | if (dp->precision_arg_index != ARG_NONE) | ||
| 2611 | { | ||
| 2612 | int arg; | ||
| 2613 | |||
| 2614 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
| 2615 | abort (); | ||
| 2616 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
| 2617 | /* "A negative precision is taken as if the precision | ||
| 2618 | were omitted." */ | ||
| 2619 | if (arg >= 0) | ||
| 2620 | { | ||
| 2621 | precision = arg; | ||
| 2622 | has_precision = 1; | ||
| 2623 | } | ||
| 2624 | } | ||
| 2625 | else | ||
| 2626 | { | ||
| 2627 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
| 2628 | |||
| 2629 | precision = 0; | ||
| 2630 | while (digitp != dp->precision_end) | ||
| 2631 | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
| 2632 | has_precision = 1; | ||
| 2633 | } | ||
| 2634 | } | ||
| 2635 | |||
| 2636 | /* POSIX specifies the default precision to be 6 for %f, %F, | ||
| 2637 | %e, %E, but not for %g, %G. Implementations appear to use | ||
| 2638 | the same default precision also for %g, %G. */ | ||
| 2639 | if (!has_precision) | ||
| 2640 | precision = 6; | ||
| 2641 | |||
| 2642 | /* Allocate a temporary buffer of sufficient size. */ | ||
| 2643 | # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE | ||
| 2644 | tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1); | ||
| 2645 | # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE | ||
| 2646 | tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0); | ||
| 2647 | # elif NEED_PRINTF_LONG_DOUBLE | ||
| 2648 | tmp_length = LDBL_DIG + 1; | ||
| 2649 | # elif NEED_PRINTF_DOUBLE | ||
| 2650 | tmp_length = DBL_DIG + 1; | ||
| 2651 | # else | ||
| 2652 | tmp_length = 0; | ||
| 2653 | # endif | ||
| 2654 | if (tmp_length < precision) | ||
| 2655 | tmp_length = precision; | ||
| 2656 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 2657 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
| 2658 | if (type == TYPE_LONGDOUBLE) | ||
| 2659 | # endif | ||
| 2660 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
| 2661 | { | ||
| 2662 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
| 2663 | if (!(isnanl (arg) || arg + arg == arg)) | ||
| 2664 | { | ||
| 2665 | /* arg is finite and nonzero. */ | ||
| 2666 | int exponent = floorlog10l (arg < 0 ? -arg : arg); | ||
| 2667 | if (exponent >= 0 && tmp_length < exponent + precision) | ||
| 2668 | tmp_length = exponent + precision; | ||
| 2669 | } | ||
| 2670 | } | ||
| 2671 | # endif | ||
| 2672 | # if NEED_PRINTF_DOUBLE | ||
| 2673 | # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
| 2674 | if (type == TYPE_DOUBLE) | ||
| 2675 | # endif | ||
| 2676 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
| 2677 | { | ||
| 2678 | double arg = a.arg[dp->arg_index].a.a_double; | ||
| 2679 | if (!(isnand (arg) || arg + arg == arg)) | ||
| 2680 | { | ||
| 2681 | /* arg is finite and nonzero. */ | ||
| 2682 | int exponent = floorlog10 (arg < 0 ? -arg : arg); | ||
| 2683 | if (exponent >= 0 && tmp_length < exponent + precision) | ||
| 2684 | tmp_length = exponent + precision; | ||
| 2685 | } | ||
| 2686 | } | ||
| 2687 | # endif | ||
| 2688 | /* Account for sign, decimal point etc. */ | ||
| 2689 | tmp_length = xsum (tmp_length, 12); | ||
| 2690 | |||
| 2691 | if (tmp_length < width) | ||
| 2692 | tmp_length = width; | ||
| 2693 | |||
| 2694 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | ||
| 2695 | |||
| 2696 | if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) | ||
| 2697 | tmp = tmpbuf; | ||
| 2698 | else | ||
| 2699 | { | ||
| 2700 | size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T)); | ||
| 2701 | |||
| 2702 | if (size_overflow_p (tmp_memsize)) | ||
| 2703 | /* Overflow, would lead to out of memory. */ | ||
| 2704 | goto out_of_memory; | ||
| 2705 | tmp = (DCHAR_T *) malloc (tmp_memsize); | ||
| 2706 | if (tmp == NULL) | ||
| 2707 | /* Out of memory. */ | ||
| 2708 | goto out_of_memory; | ||
| 2709 | } | ||
| 2710 | |||
| 2711 | pad_ptr = NULL; | ||
| 2712 | p = tmp; | ||
| 2713 | |||
| 2714 | # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
| 2715 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
| 2716 | if (type == TYPE_LONGDOUBLE) | ||
| 2717 | # endif | ||
| 2718 | { | ||
| 2719 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
| 2720 | |||
| 2721 | if (isnanl (arg)) | ||
| 2722 | { | ||
| 2723 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
| 2724 | { | ||
| 2725 | *p++ = 'N'; *p++ = 'A'; *p++ = 'N'; | ||
| 2726 | } | ||
| 2727 | else | ||
| 2728 | { | ||
| 2729 | *p++ = 'n'; *p++ = 'a'; *p++ = 'n'; | ||
| 2730 | } | ||
| 2731 | } | ||
| 2732 | else | ||
| 2733 | { | ||
| 2734 | int sign = 0; | ||
| 2735 | DECL_LONG_DOUBLE_ROUNDING | ||
| 2736 | |||
| 2737 | BEGIN_LONG_DOUBLE_ROUNDING (); | ||
| 2738 | |||
| 2739 | if (signbit (arg)) /* arg < 0.0L or negative zero */ | ||
| 2740 | { | ||
| 2741 | sign = -1; | ||
| 2742 | arg = -arg; | ||
| 2743 | } | ||
| 2744 | |||
| 2745 | if (sign < 0) | ||
| 2746 | *p++ = '-'; | ||
| 2747 | else if (flags & FLAG_SHOWSIGN) | ||
| 2748 | *p++ = '+'; | ||
| 2749 | else if (flags & FLAG_SPACE) | ||
| 2750 | *p++ = ' '; | ||
| 2751 | |||
| 2752 | if (arg > 0.0L && arg + arg == arg) | ||
| 2753 | { | ||
| 2754 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
| 2755 | { | ||
| 2756 | *p++ = 'I'; *p++ = 'N'; *p++ = 'F'; | ||
| 2757 | } | ||
| 2758 | else | ||
| 2759 | { | ||
| 2760 | *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
| 2761 | } | ||
| 2762 | } | ||
| 2763 | else | ||
| 2764 | { | ||
| 2765 | # if NEED_PRINTF_LONG_DOUBLE | ||
| 2766 | pad_ptr = p; | ||
| 2767 | |||
| 2768 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
| 2769 | { | ||
| 2770 | char *digits; | ||
| 2771 | size_t ndigits; | ||
| 2772 | |||
| 2773 | digits = | ||
| 2774 | scale10_round_decimal_long_double (arg, precision); | ||
| 2775 | if (digits == NULL) | ||
| 2776 | { | ||
| 2777 | END_LONG_DOUBLE_ROUNDING (); | ||
| 2778 | goto out_of_memory; | ||
| 2779 | } | ||
| 2780 | ndigits = strlen (digits); | ||
| 2781 | |||
| 2782 | if (ndigits > precision) | ||
| 2783 | do | ||
| 2784 | { | ||
| 2785 | --ndigits; | ||
| 2786 | *p++ = digits[ndigits]; | ||
| 2787 | } | ||
| 2788 | while (ndigits > precision); | ||
| 2789 | else | ||
| 2790 | *p++ = '0'; | ||
| 2791 | /* Here ndigits <= precision. */ | ||
| 2792 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 2793 | { | ||
| 2794 | *p++ = decimal_point_char (); | ||
| 2795 | for (; precision > ndigits; precision--) | ||
| 2796 | *p++ = '0'; | ||
| 2797 | while (ndigits > 0) | ||
| 2798 | { | ||
| 2799 | --ndigits; | ||
| 2800 | *p++ = digits[ndigits]; | ||
| 2801 | } | ||
| 2802 | } | ||
| 2803 | |||
| 2804 | free (digits); | ||
| 2805 | } | ||
| 2806 | else if (dp->conversion == 'e' || dp->conversion == 'E') | ||
| 2807 | { | ||
| 2808 | int exponent; | ||
| 2809 | |||
| 2810 | if (arg == 0.0L) | ||
| 2811 | { | ||
| 2812 | exponent = 0; | ||
| 2813 | *p++ = '0'; | ||
| 2814 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 2815 | { | ||
| 2816 | *p++ = decimal_point_char (); | ||
| 2817 | for (; precision > 0; precision--) | ||
| 2818 | *p++ = '0'; | ||
| 2819 | } | ||
| 2820 | } | ||
| 2821 | else | ||
| 2822 | { | ||
| 2823 | /* arg > 0.0L. */ | ||
| 2824 | int adjusted; | ||
| 2825 | char *digits; | ||
| 2826 | size_t ndigits; | ||
| 2827 | |||
| 2828 | exponent = floorlog10l (arg); | ||
| 2829 | adjusted = 0; | ||
| 2830 | for (;;) | ||
| 2831 | { | ||
| 2832 | digits = | ||
| 2833 | scale10_round_decimal_long_double (arg, | ||
| 2834 | (int)precision - exponent); | ||
| 2835 | if (digits == NULL) | ||
| 2836 | { | ||
| 2837 | END_LONG_DOUBLE_ROUNDING (); | ||
| 2838 | goto out_of_memory; | ||
| 2839 | } | ||
| 2840 | ndigits = strlen (digits); | ||
| 2841 | |||
| 2842 | if (ndigits == precision + 1) | ||
| 2843 | break; | ||
| 2844 | if (ndigits < precision | ||
| 2845 | || ndigits > precision + 2) | ||
| 2846 | /* The exponent was not guessed | ||
| 2847 | precisely enough. */ | ||
| 2848 | abort (); | ||
| 2849 | if (adjusted) | ||
| 2850 | /* None of two values of exponent is | ||
| 2851 | the right one. Prevent an endless | ||
| 2852 | loop. */ | ||
| 2853 | abort (); | ||
| 2854 | free (digits); | ||
| 2855 | if (ndigits == precision) | ||
| 2856 | exponent -= 1; | ||
| 2857 | else | ||
| 2858 | exponent += 1; | ||
| 2859 | adjusted = 1; | ||
| 2860 | } | ||
| 2861 | |||
| 2862 | /* Here ndigits = precision+1. */ | ||
| 2863 | *p++ = digits[--ndigits]; | ||
| 2864 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 2865 | { | ||
| 2866 | *p++ = decimal_point_char (); | ||
| 2867 | while (ndigits > 0) | ||
| 2868 | { | ||
| 2869 | --ndigits; | ||
| 2870 | *p++ = digits[ndigits]; | ||
| 2871 | } | ||
| 2872 | } | ||
| 2873 | |||
| 2874 | free (digits); | ||
| 2875 | } | ||
| 2876 | |||
| 2877 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
| 2878 | # if WIDE_CHAR_VERSION | ||
| 2879 | { | ||
| 2880 | static const wchar_t decimal_format[] = | ||
| 2881 | { '%', '+', '.', '2', 'd', '\0' }; | ||
| 2882 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 2883 | } | ||
| 2884 | while (*p != '\0') | ||
| 2885 | p++; | ||
| 2886 | # else | ||
| 2887 | if (sizeof (DCHAR_T) == 1) | ||
| 2888 | { | ||
| 2889 | sprintf ((char *) p, "%+.2d", exponent); | ||
| 2890 | while (*p != '\0') | ||
| 2891 | p++; | ||
| 2892 | } | ||
| 2893 | else | ||
| 2894 | { | ||
| 2895 | char expbuf[6 + 1]; | ||
| 2896 | const char *ep; | ||
| 2897 | sprintf (expbuf, "%+.2d", exponent); | ||
| 2898 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 2899 | p++; | ||
| 2900 | } | ||
| 2901 | # endif | ||
| 2902 | } | ||
| 2903 | else if (dp->conversion == 'g' || dp->conversion == 'G') | ||
| 2904 | { | ||
| 2905 | if (precision == 0) | ||
| 2906 | precision = 1; | ||
| 2907 | /* precision >= 1. */ | ||
| 2908 | |||
| 2909 | if (arg == 0.0L) | ||
| 2910 | /* The exponent is 0, >= -4, < precision. | ||
| 2911 | Use fixed-point notation. */ | ||
| 2912 | { | ||
| 2913 | size_t ndigits = precision; | ||
| 2914 | /* Number of trailing zeroes that have to be | ||
| 2915 | dropped. */ | ||
| 2916 | size_t nzeroes = | ||
| 2917 | (flags & FLAG_ALT ? 0 : precision - 1); | ||
| 2918 | |||
| 2919 | --ndigits; | ||
| 2920 | *p++ = '0'; | ||
| 2921 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 2922 | { | ||
| 2923 | *p++ = decimal_point_char (); | ||
| 2924 | while (ndigits > nzeroes) | ||
| 2925 | { | ||
| 2926 | --ndigits; | ||
| 2927 | *p++ = '0'; | ||
| 2928 | } | ||
| 2929 | } | ||
| 2930 | } | ||
| 2931 | else | ||
| 2932 | { | ||
| 2933 | /* arg > 0.0L. */ | ||
| 2934 | int exponent; | ||
| 2935 | int adjusted; | ||
| 2936 | char *digits; | ||
| 2937 | size_t ndigits; | ||
| 2938 | size_t nzeroes; | ||
| 2939 | |||
| 2940 | exponent = floorlog10l (arg); | ||
| 2941 | adjusted = 0; | ||
| 2942 | for (;;) | ||
| 2943 | { | ||
| 2944 | digits = | ||
| 2945 | scale10_round_decimal_long_double (arg, | ||
| 2946 | (int)(precision - 1) - exponent); | ||
| 2947 | if (digits == NULL) | ||
| 2948 | { | ||
| 2949 | END_LONG_DOUBLE_ROUNDING (); | ||
| 2950 | goto out_of_memory; | ||
| 2951 | } | ||
| 2952 | ndigits = strlen (digits); | ||
| 2953 | |||
| 2954 | if (ndigits == precision) | ||
| 2955 | break; | ||
| 2956 | if (ndigits < precision - 1 | ||
| 2957 | || ndigits > precision + 1) | ||
| 2958 | /* The exponent was not guessed | ||
| 2959 | precisely enough. */ | ||
| 2960 | abort (); | ||
| 2961 | if (adjusted) | ||
| 2962 | /* None of two values of exponent is | ||
| 2963 | the right one. Prevent an endless | ||
| 2964 | loop. */ | ||
| 2965 | abort (); | ||
| 2966 | free (digits); | ||
| 2967 | if (ndigits < precision) | ||
| 2968 | exponent -= 1; | ||
| 2969 | else | ||
| 2970 | exponent += 1; | ||
| 2971 | adjusted = 1; | ||
| 2972 | } | ||
| 2973 | /* Here ndigits = precision. */ | ||
| 2974 | |||
| 2975 | /* Determine the number of trailing zeroes | ||
| 2976 | that have to be dropped. */ | ||
| 2977 | nzeroes = 0; | ||
| 2978 | if ((flags & FLAG_ALT) == 0) | ||
| 2979 | while (nzeroes < ndigits | ||
| 2980 | && digits[nzeroes] == '0') | ||
| 2981 | nzeroes++; | ||
| 2982 | |||
| 2983 | /* The exponent is now determined. */ | ||
| 2984 | if (exponent >= -4 | ||
| 2985 | && exponent < (long)precision) | ||
| 2986 | { | ||
| 2987 | /* Fixed-point notation: | ||
| 2988 | max(exponent,0)+1 digits, then the | ||
| 2989 | decimal point, then the remaining | ||
| 2990 | digits without trailing zeroes. */ | ||
| 2991 | if (exponent >= 0) | ||
| 2992 | { | ||
| 2993 | size_t count = exponent + 1; | ||
| 2994 | /* Note: count <= precision = ndigits. */ | ||
| 2995 | for (; count > 0; count--) | ||
| 2996 | *p++ = digits[--ndigits]; | ||
| 2997 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 2998 | { | ||
| 2999 | *p++ = decimal_point_char (); | ||
| 3000 | while (ndigits > nzeroes) | ||
| 3001 | { | ||
| 3002 | --ndigits; | ||
| 3003 | *p++ = digits[ndigits]; | ||
| 3004 | } | ||
| 3005 | } | ||
| 3006 | } | ||
| 3007 | else | ||
| 3008 | { | ||
| 3009 | size_t count = -exponent - 1; | ||
| 3010 | *p++ = '0'; | ||
| 3011 | *p++ = decimal_point_char (); | ||
| 3012 | for (; count > 0; count--) | ||
| 3013 | *p++ = '0'; | ||
| 3014 | while (ndigits > nzeroes) | ||
| 3015 | { | ||
| 3016 | --ndigits; | ||
| 3017 | *p++ = digits[ndigits]; | ||
| 3018 | } | ||
| 3019 | } | ||
| 3020 | } | ||
| 3021 | else | ||
| 3022 | { | ||
| 3023 | /* Exponential notation. */ | ||
| 3024 | *p++ = digits[--ndigits]; | ||
| 3025 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 3026 | { | ||
| 3027 | *p++ = decimal_point_char (); | ||
| 3028 | while (ndigits > nzeroes) | ||
| 3029 | { | ||
| 3030 | --ndigits; | ||
| 3031 | *p++ = digits[ndigits]; | ||
| 3032 | } | ||
| 3033 | } | ||
| 3034 | *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */ | ||
| 3035 | # if WIDE_CHAR_VERSION | ||
| 3036 | { | ||
| 3037 | static const wchar_t decimal_format[] = | ||
| 3038 | { '%', '+', '.', '2', 'd', '\0' }; | ||
| 3039 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 3040 | } | ||
| 3041 | while (*p != '\0') | ||
| 3042 | p++; | ||
| 3043 | # else | ||
| 3044 | if (sizeof (DCHAR_T) == 1) | ||
| 3045 | { | ||
| 3046 | sprintf ((char *) p, "%+.2d", exponent); | ||
| 3047 | while (*p != '\0') | ||
| 3048 | p++; | ||
| 3049 | } | ||
| 3050 | else | ||
| 3051 | { | ||
| 3052 | char expbuf[6 + 1]; | ||
| 3053 | const char *ep; | ||
| 3054 | sprintf (expbuf, "%+.2d", exponent); | ||
| 3055 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 3056 | p++; | ||
| 3057 | } | ||
| 3058 | # endif | ||
| 3059 | } | ||
| 3060 | |||
| 3061 | free (digits); | ||
| 3062 | } | ||
| 3063 | } | ||
| 3064 | else | ||
| 3065 | abort (); | ||
| 3066 | # else | ||
| 3067 | /* arg is finite. */ | ||
| 3068 | abort (); | ||
| 3069 | # endif | ||
| 3070 | } | ||
| 3071 | |||
| 3072 | END_LONG_DOUBLE_ROUNDING (); | ||
| 3073 | } | ||
| 3074 | } | ||
| 3075 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
| 3076 | else | ||
| 3077 | # endif | ||
| 3078 | # endif | ||
| 3079 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
| 3080 | { | ||
| 3081 | double arg = a.arg[dp->arg_index].a.a_double; | ||
| 3082 | |||
| 3083 | if (isnand (arg)) | ||
| 3084 | { | ||
| 3085 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
| 3086 | { | ||
| 3087 | *p++ = 'N'; *p++ = 'A'; *p++ = 'N'; | ||
| 3088 | } | ||
| 3089 | else | ||
| 3090 | { | ||
| 3091 | *p++ = 'n'; *p++ = 'a'; *p++ = 'n'; | ||
| 3092 | } | ||
| 3093 | } | ||
| 3094 | else | ||
| 3095 | { | ||
| 3096 | int sign = 0; | ||
| 3097 | |||
| 3098 | if (signbit (arg)) /* arg < 0.0 or negative zero */ | ||
| 3099 | { | ||
| 3100 | sign = -1; | ||
| 3101 | arg = -arg; | ||
| 3102 | } | ||
| 3103 | |||
| 3104 | if (sign < 0) | ||
| 3105 | *p++ = '-'; | ||
| 3106 | else if (flags & FLAG_SHOWSIGN) | ||
| 3107 | *p++ = '+'; | ||
| 3108 | else if (flags & FLAG_SPACE) | ||
| 3109 | *p++ = ' '; | ||
| 3110 | |||
| 3111 | if (arg > 0.0 && arg + arg == arg) | ||
| 3112 | { | ||
| 3113 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
| 3114 | { | ||
| 3115 | *p++ = 'I'; *p++ = 'N'; *p++ = 'F'; | ||
| 3116 | } | ||
| 3117 | else | ||
| 3118 | { | ||
| 3119 | *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
| 3120 | } | ||
| 3121 | } | ||
| 3122 | else | ||
| 3123 | { | ||
| 3124 | # if NEED_PRINTF_DOUBLE | ||
| 3125 | pad_ptr = p; | ||
| 3126 | |||
| 3127 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
| 3128 | { | ||
| 3129 | char *digits; | ||
| 3130 | size_t ndigits; | ||
| 3131 | |||
| 3132 | digits = | ||
| 3133 | scale10_round_decimal_double (arg, precision); | ||
| 3134 | if (digits == NULL) | ||
| 3135 | goto out_of_memory; | ||
| 3136 | ndigits = strlen (digits); | ||
| 3137 | |||
| 3138 | if (ndigits > precision) | ||
| 3139 | do | ||
| 3140 | { | ||
| 3141 | --ndigits; | ||
| 3142 | *p++ = digits[ndigits]; | ||
| 3143 | } | ||
| 3144 | while (ndigits > precision); | ||
| 3145 | else | ||
| 3146 | *p++ = '0'; | ||
| 3147 | /* Here ndigits <= precision. */ | ||
| 3148 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 3149 | { | ||
| 3150 | *p++ = decimal_point_char (); | ||
| 3151 | for (; precision > ndigits; precision--) | ||
| 3152 | *p++ = '0'; | ||
| 3153 | while (ndigits > 0) | ||
| 3154 | { | ||
| 3155 | --ndigits; | ||
| 3156 | *p++ = digits[ndigits]; | ||
| 3157 | } | ||
| 3158 | } | ||
| 3159 | |||
| 3160 | free (digits); | ||
| 3161 | } | ||
| 3162 | else if (dp->conversion == 'e' || dp->conversion == 'E') | ||
| 3163 | { | ||
| 3164 | int exponent; | ||
| 3165 | |||
| 3166 | if (arg == 0.0) | ||
| 3167 | { | ||
| 3168 | exponent = 0; | ||
| 3169 | *p++ = '0'; | ||
| 3170 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 3171 | { | ||
| 3172 | *p++ = decimal_point_char (); | ||
| 3173 | for (; precision > 0; precision--) | ||
| 3174 | *p++ = '0'; | ||
| 3175 | } | ||
| 3176 | } | ||
| 3177 | else | ||
| 3178 | { | ||
| 3179 | /* arg > 0.0. */ | ||
| 3180 | int adjusted; | ||
| 3181 | char *digits; | ||
| 3182 | size_t ndigits; | ||
| 3183 | |||
| 3184 | exponent = floorlog10 (arg); | ||
| 3185 | adjusted = 0; | ||
| 3186 | for (;;) | ||
| 3187 | { | ||
| 3188 | digits = | ||
| 3189 | scale10_round_decimal_double (arg, | ||
| 3190 | (int)precision - exponent); | ||
| 3191 | if (digits == NULL) | ||
| 3192 | goto out_of_memory; | ||
| 3193 | ndigits = strlen (digits); | ||
| 3194 | |||
| 3195 | if (ndigits == precision + 1) | ||
| 3196 | break; | ||
| 3197 | if (ndigits < precision | ||
| 3198 | || ndigits > precision + 2) | ||
| 3199 | /* The exponent was not guessed | ||
| 3200 | precisely enough. */ | ||
| 3201 | abort (); | ||
| 3202 | if (adjusted) | ||
| 3203 | /* None of two values of exponent is | ||
| 3204 | the right one. Prevent an endless | ||
| 3205 | loop. */ | ||
| 3206 | abort (); | ||
| 3207 | free (digits); | ||
| 3208 | if (ndigits == precision) | ||
| 3209 | exponent -= 1; | ||
| 3210 | else | ||
| 3211 | exponent += 1; | ||
| 3212 | adjusted = 1; | ||
| 3213 | } | ||
| 3214 | |||
| 3215 | /* Here ndigits = precision+1. */ | ||
| 3216 | *p++ = digits[--ndigits]; | ||
| 3217 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 3218 | { | ||
| 3219 | *p++ = decimal_point_char (); | ||
| 3220 | while (ndigits > 0) | ||
| 3221 | { | ||
| 3222 | --ndigits; | ||
| 3223 | *p++ = digits[ndigits]; | ||
| 3224 | } | ||
| 3225 | } | ||
| 3226 | |||
| 3227 | free (digits); | ||
| 3228 | } | ||
| 3229 | |||
| 3230 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
| 3231 | # if WIDE_CHAR_VERSION | ||
| 3232 | { | ||
| 3233 | static const wchar_t decimal_format[] = | ||
| 3234 | /* Produce the same number of exponent digits | ||
| 3235 | as the native printf implementation. */ | ||
| 3236 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 3237 | { '%', '+', '.', '3', 'd', '\0' }; | ||
| 3238 | # else | ||
| 3239 | { '%', '+', '.', '2', 'd', '\0' }; | ||
| 3240 | # endif | ||
| 3241 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 3242 | } | ||
| 3243 | while (*p != '\0') | ||
| 3244 | p++; | ||
| 3245 | # else | ||
| 3246 | { | ||
| 3247 | static const char decimal_format[] = | ||
| 3248 | /* Produce the same number of exponent digits | ||
| 3249 | as the native printf implementation. */ | ||
| 3250 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 3251 | "%+.3d"; | ||
| 3252 | # else | ||
| 3253 | "%+.2d"; | ||
| 3254 | # endif | ||
| 3255 | if (sizeof (DCHAR_T) == 1) | ||
| 3256 | { | ||
| 3257 | sprintf ((char *) p, decimal_format, exponent); | ||
| 3258 | while (*p != '\0') | ||
| 3259 | p++; | ||
| 3260 | } | ||
| 3261 | else | ||
| 3262 | { | ||
| 3263 | char expbuf[6 + 1]; | ||
| 3264 | const char *ep; | ||
| 3265 | sprintf (expbuf, decimal_format, exponent); | ||
| 3266 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 3267 | p++; | ||
| 3268 | } | ||
| 3269 | } | ||
| 3270 | # endif | ||
| 3271 | } | ||
| 3272 | else if (dp->conversion == 'g' || dp->conversion == 'G') | ||
| 3273 | { | ||
| 3274 | if (precision == 0) | ||
| 3275 | precision = 1; | ||
| 3276 | /* precision >= 1. */ | ||
| 3277 | |||
| 3278 | if (arg == 0.0) | ||
| 3279 | /* The exponent is 0, >= -4, < precision. | ||
| 3280 | Use fixed-point notation. */ | ||
| 3281 | { | ||
| 3282 | size_t ndigits = precision; | ||
| 3283 | /* Number of trailing zeroes that have to be | ||
| 3284 | dropped. */ | ||
| 3285 | size_t nzeroes = | ||
| 3286 | (flags & FLAG_ALT ? 0 : precision - 1); | ||
| 3287 | |||
| 3288 | --ndigits; | ||
| 3289 | *p++ = '0'; | ||
| 3290 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 3291 | { | ||
| 3292 | *p++ = decimal_point_char (); | ||
| 3293 | while (ndigits > nzeroes) | ||
| 3294 | { | ||
| 3295 | --ndigits; | ||
| 3296 | *p++ = '0'; | ||
| 3297 | } | ||
| 3298 | } | ||
| 3299 | } | ||
| 3300 | else | ||
| 3301 | { | ||
| 3302 | /* arg > 0.0. */ | ||
| 3303 | int exponent; | ||
| 3304 | int adjusted; | ||
| 3305 | char *digits; | ||
| 3306 | size_t ndigits; | ||
| 3307 | size_t nzeroes; | ||
| 3308 | |||
| 3309 | exponent = floorlog10 (arg); | ||
| 3310 | adjusted = 0; | ||
| 3311 | for (;;) | ||
| 3312 | { | ||
| 3313 | digits = | ||
| 3314 | scale10_round_decimal_double (arg, | ||
| 3315 | (int)(precision - 1) - exponent); | ||
| 3316 | if (digits == NULL) | ||
| 3317 | goto out_of_memory; | ||
| 3318 | ndigits = strlen (digits); | ||
| 3319 | |||
| 3320 | if (ndigits == precision) | ||
| 3321 | break; | ||
| 3322 | if (ndigits < precision - 1 | ||
| 3323 | || ndigits > precision + 1) | ||
| 3324 | /* The exponent was not guessed | ||
| 3325 | precisely enough. */ | ||
| 3326 | abort (); | ||
| 3327 | if (adjusted) | ||
| 3328 | /* None of two values of exponent is | ||
| 3329 | the right one. Prevent an endless | ||
| 3330 | loop. */ | ||
| 3331 | abort (); | ||
| 3332 | free (digits); | ||
| 3333 | if (ndigits < precision) | ||
| 3334 | exponent -= 1; | ||
| 3335 | else | ||
| 3336 | exponent += 1; | ||
| 3337 | adjusted = 1; | ||
| 3338 | } | ||
| 3339 | /* Here ndigits = precision. */ | ||
| 3340 | |||
| 3341 | /* Determine the number of trailing zeroes | ||
| 3342 | that have to be dropped. */ | ||
| 3343 | nzeroes = 0; | ||
| 3344 | if ((flags & FLAG_ALT) == 0) | ||
| 3345 | while (nzeroes < ndigits | ||
| 3346 | && digits[nzeroes] == '0') | ||
| 3347 | nzeroes++; | ||
| 3348 | |||
| 3349 | /* The exponent is now determined. */ | ||
| 3350 | if (exponent >= -4 | ||
| 3351 | && exponent < (long)precision) | ||
| 3352 | { | ||
| 3353 | /* Fixed-point notation: | ||
| 3354 | max(exponent,0)+1 digits, then the | ||
| 3355 | decimal point, then the remaining | ||
| 3356 | digits without trailing zeroes. */ | ||
| 3357 | if (exponent >= 0) | ||
| 3358 | { | ||
| 3359 | size_t count = exponent + 1; | ||
| 3360 | /* Note: count <= precision = ndigits. */ | ||
| 3361 | for (; count > 0; count--) | ||
| 3362 | *p++ = digits[--ndigits]; | ||
| 3363 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 3364 | { | ||
| 3365 | *p++ = decimal_point_char (); | ||
| 3366 | while (ndigits > nzeroes) | ||
| 3367 | { | ||
| 3368 | --ndigits; | ||
| 3369 | *p++ = digits[ndigits]; | ||
| 3370 | } | ||
| 3371 | } | ||
| 3372 | } | ||
| 3373 | else | ||
| 3374 | { | ||
| 3375 | size_t count = -exponent - 1; | ||
| 3376 | *p++ = '0'; | ||
| 3377 | *p++ = decimal_point_char (); | ||
| 3378 | for (; count > 0; count--) | ||
| 3379 | *p++ = '0'; | ||
| 3380 | while (ndigits > nzeroes) | ||
| 3381 | { | ||
| 3382 | --ndigits; | ||
| 3383 | *p++ = digits[ndigits]; | ||
| 3384 | } | ||
| 3385 | } | ||
| 3386 | } | ||
| 3387 | else | ||
| 3388 | { | ||
| 3389 | /* Exponential notation. */ | ||
| 3390 | *p++ = digits[--ndigits]; | ||
| 3391 | if ((flags & FLAG_ALT) || ndigits > nzeroes) | ||
| 3392 | { | ||
| 3393 | *p++ = decimal_point_char (); | ||
| 3394 | while (ndigits > nzeroes) | ||
| 3395 | { | ||
| 3396 | --ndigits; | ||
| 3397 | *p++ = digits[ndigits]; | ||
| 3398 | } | ||
| 3399 | } | ||
| 3400 | *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */ | ||
| 3401 | # if WIDE_CHAR_VERSION | ||
| 3402 | { | ||
| 3403 | static const wchar_t decimal_format[] = | ||
| 3404 | /* Produce the same number of exponent digits | ||
| 3405 | as the native printf implementation. */ | ||
| 3406 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 3407 | { '%', '+', '.', '3', 'd', '\0' }; | ||
| 3408 | # else | ||
| 3409 | { '%', '+', '.', '2', 'd', '\0' }; | ||
| 3410 | # endif | ||
| 3411 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
| 3412 | } | ||
| 3413 | while (*p != '\0') | ||
| 3414 | p++; | ||
| 3415 | # else | ||
| 3416 | { | ||
| 3417 | static const char decimal_format[] = | ||
| 3418 | /* Produce the same number of exponent digits | ||
| 3419 | as the native printf implementation. */ | ||
| 3420 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 3421 | "%+.3d"; | ||
| 3422 | # else | ||
| 3423 | "%+.2d"; | ||
| 3424 | # endif | ||
| 3425 | if (sizeof (DCHAR_T) == 1) | ||
| 3426 | { | ||
| 3427 | sprintf ((char *) p, decimal_format, exponent); | ||
| 3428 | while (*p != '\0') | ||
| 3429 | p++; | ||
| 3430 | } | ||
| 3431 | else | ||
| 3432 | { | ||
| 3433 | char expbuf[6 + 1]; | ||
| 3434 | const char *ep; | ||
| 3435 | sprintf (expbuf, decimal_format, exponent); | ||
| 3436 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
| 3437 | p++; | ||
| 3438 | } | ||
| 3439 | } | ||
| 3440 | # endif | ||
| 3441 | } | ||
| 3442 | |||
| 3443 | free (digits); | ||
| 3444 | } | ||
| 3445 | } | ||
| 3446 | else | ||
| 3447 | abort (); | ||
| 3448 | # else | ||
| 3449 | /* arg is finite. */ | ||
| 3450 | if (!(arg == 0.0)) | ||
| 3451 | abort (); | ||
| 3452 | |||
| 3453 | pad_ptr = p; | ||
| 3454 | |||
| 3455 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
| 3456 | { | ||
| 3457 | *p++ = '0'; | ||
| 3458 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 3459 | { | ||
| 3460 | *p++ = decimal_point_char (); | ||
| 3461 | for (; precision > 0; precision--) | ||
| 3462 | *p++ = '0'; | ||
| 3463 | } | ||
| 3464 | } | ||
| 3465 | else if (dp->conversion == 'e' || dp->conversion == 'E') | ||
| 3466 | { | ||
| 3467 | *p++ = '0'; | ||
| 3468 | if ((flags & FLAG_ALT) || precision > 0) | ||
| 3469 | { | ||
| 3470 | *p++ = decimal_point_char (); | ||
| 3471 | for (; precision > 0; precision--) | ||
| 3472 | *p++ = '0'; | ||
| 3473 | } | ||
| 3474 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
| 3475 | *p++ = '+'; | ||
| 3476 | /* Produce the same number of exponent digits as | ||
| 3477 | the native printf implementation. */ | ||
| 3478 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 3479 | *p++ = '0'; | ||
| 3480 | # endif | ||
| 3481 | *p++ = '0'; | ||
| 3482 | *p++ = '0'; | ||
| 3483 | } | ||
| 3484 | else if (dp->conversion == 'g' || dp->conversion == 'G') | ||
| 3485 | { | ||
| 3486 | *p++ = '0'; | ||
| 3487 | if (flags & FLAG_ALT) | ||
| 3488 | { | ||
| 3489 | size_t ndigits = | ||
| 3490 | (precision > 0 ? precision - 1 : 0); | ||
| 3491 | *p++ = decimal_point_char (); | ||
| 3492 | for (; ndigits > 0; --ndigits) | ||
| 3493 | *p++ = '0'; | ||
| 3494 | } | ||
| 3495 | } | ||
| 3496 | else | ||
| 3497 | abort (); | ||
| 3498 | # endif | ||
| 3499 | } | ||
| 3500 | } | ||
| 3501 | } | ||
| 3502 | # endif | ||
| 3503 | |||
| 3504 | /* The generated string now extends from tmp to p, with the | ||
| 3505 | zero padding insertion point being at pad_ptr. */ | ||
| 3506 | if (has_width && p - tmp < width) | ||
| 3507 | { | ||
| 3508 | size_t pad = width - (p - tmp); | ||
| 3509 | DCHAR_T *end = p + pad; | ||
| 3510 | |||
| 3511 | if (flags & FLAG_LEFT) | ||
| 3512 | { | ||
| 3513 | /* Pad with spaces on the right. */ | ||
| 3514 | for (; pad > 0; pad--) | ||
| 3515 | *p++ = ' '; | ||
| 3516 | } | ||
| 3517 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
| 3518 | { | ||
| 3519 | /* Pad with zeroes. */ | ||
| 3520 | DCHAR_T *q = end; | ||
| 3521 | |||
| 3522 | while (p > pad_ptr) | ||
| 3523 | *--q = *--p; | ||
| 3524 | for (; pad > 0; pad--) | ||
| 3525 | *p++ = '0'; | ||
| 3526 | } | ||
| 3527 | else | ||
| 3528 | { | ||
| 3529 | /* Pad with spaces on the left. */ | ||
| 3530 | DCHAR_T *q = end; | ||
| 3531 | |||
| 3532 | while (p > tmp) | ||
| 3533 | *--q = *--p; | ||
| 3534 | for (; pad > 0; pad--) | ||
| 3535 | *p++ = ' '; | ||
| 3536 | } | ||
| 3537 | |||
| 3538 | p = end; | ||
| 3539 | } | ||
| 3540 | |||
| 3541 | { | ||
| 3542 | size_t count = p - tmp; | ||
| 3543 | |||
| 3544 | if (count >= tmp_length) | ||
| 3545 | /* tmp_length was incorrectly calculated - fix the | ||
| 3546 | code above! */ | ||
| 3547 | abort (); | ||
| 3548 | |||
| 3549 | /* Make room for the result. */ | ||
| 3550 | if (count >= allocated - length) | ||
| 3551 | { | ||
| 3552 | size_t n = xsum (length, count); | ||
| 3553 | |||
| 3554 | ENSURE_ALLOCATION (n); | ||
| 3555 | } | ||
| 3556 | |||
| 3557 | /* Append the result. */ | ||
| 3558 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); | ||
| 3559 | if (tmp != tmpbuf) | ||
| 3560 | free (tmp); | ||
| 3561 | length += count; | ||
| 3562 | } | ||
| 3563 | } | ||
| 3564 | #endif | ||
| 3565 | else | ||
| 3566 | { | ||
| 3567 | arg_type type = a.arg[dp->arg_index].type; | ||
| 3568 | int flags = dp->flags; | ||
| 3569 | #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3570 | int has_width; | ||
| 3571 | size_t width; | ||
| 3572 | #endif | ||
| 3573 | #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3574 | int has_precision; | ||
| 3575 | size_t precision; | ||
| 3576 | #endif | ||
| 3577 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3578 | int prec_ourselves; | ||
| 3579 | #else | ||
| 3580 | # define prec_ourselves 0 | ||
| 3581 | #endif | ||
| 3582 | #if NEED_PRINTF_FLAG_LEFTADJUST | ||
| 3583 | # define pad_ourselves 1 | ||
| 3584 | #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3585 | int pad_ourselves; | ||
| 3586 | #else | ||
| 3587 | # define pad_ourselves 0 | ||
| 3588 | #endif | ||
| 3589 | TCHAR_T *fbp; | ||
| 3590 | unsigned int prefix_count; | ||
| 3591 | int prefixes[2]; | ||
| 3592 | #if !USE_SNPRINTF | ||
| 3593 | size_t tmp_length; | ||
| 3594 | TCHAR_T tmpbuf[700]; | ||
| 3595 | TCHAR_T *tmp; | ||
| 3596 | #endif | ||
| 3597 | |||
| 3598 | #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3599 | has_width = 0; | ||
| 3600 | width = 0; | ||
| 3601 | if (dp->width_start != dp->width_end) | ||
| 3602 | { | ||
| 3603 | if (dp->width_arg_index != ARG_NONE) | ||
| 3604 | { | ||
| 3605 | int arg; | ||
| 3606 | |||
| 3607 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
| 3608 | abort (); | ||
| 3609 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
| 3610 | if (arg < 0) | ||
| 3611 | { | ||
| 3612 | /* "A negative field width is taken as a '-' flag | ||
| 3613 | followed by a positive field width." */ | ||
| 3614 | flags |= FLAG_LEFT; | ||
| 3615 | width = (unsigned int) (-arg); | ||
| 3616 | } | ||
| 3617 | else | ||
| 3618 | width = arg; | ||
| 3619 | } | ||
| 3620 | else | ||
| 3621 | { | ||
| 3622 | const FCHAR_T *digitp = dp->width_start; | ||
| 3623 | |||
| 3624 | do | ||
| 3625 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
| 3626 | while (digitp != dp->width_end); | ||
| 3627 | } | ||
| 3628 | has_width = 1; | ||
| 3629 | } | ||
| 3630 | #endif | ||
| 3631 | |||
| 3632 | #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3633 | has_precision = 0; | ||
| 3634 | precision = 6; | ||
| 3635 | if (dp->precision_start != dp->precision_end) | ||
| 3636 | { | ||
| 3637 | if (dp->precision_arg_index != ARG_NONE) | ||
| 3638 | { | ||
| 3639 | int arg; | ||
| 3640 | |||
| 3641 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
| 3642 | abort (); | ||
| 3643 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
| 3644 | /* "A negative precision is taken as if the precision | ||
| 3645 | were omitted." */ | ||
| 3646 | if (arg >= 0) | ||
| 3647 | { | ||
| 3648 | precision = arg; | ||
| 3649 | has_precision = 1; | ||
| 3650 | } | ||
| 3651 | } | ||
| 3652 | else | ||
| 3653 | { | ||
| 3654 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
| 3655 | |||
| 3656 | precision = 0; | ||
| 3657 | while (digitp != dp->precision_end) | ||
| 3658 | precision = xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
| 3659 | has_precision = 1; | ||
| 3660 | } | ||
| 3661 | } | ||
| 3662 | #endif | ||
| 3663 | |||
| 3664 | #if !USE_SNPRINTF | ||
| 3665 | /* Allocate a temporary buffer of sufficient size for calling | ||
| 3666 | sprintf. */ | ||
| 3667 | { | ||
| 321 | switch (dp->conversion) | 3668 | switch (dp->conversion) |
| 322 | { | 3669 | { |
| 323 | 3670 | ||
| 324 | case 'd': case 'i': case 'u': | 3671 | case 'd': case 'i': case 'u': |
| 325 | # ifdef HAVE_LONG_LONG_INT | 3672 | # if HAVE_LONG_LONG_INT |
| 326 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) | 3673 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
| 327 | tmp_length = | 3674 | tmp_length = |
| 328 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT | 3675 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
| @@ -352,7 +3699,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 352 | break; | 3699 | break; |
| 353 | 3700 | ||
| 354 | case 'o': | 3701 | case 'o': |
| 355 | # ifdef HAVE_LONG_LONG_INT | 3702 | # if HAVE_LONG_LONG_INT |
| 356 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) | 3703 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
| 357 | tmp_length = | 3704 | tmp_length = |
| 358 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT | 3705 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
| @@ -380,7 +3727,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 380 | break; | 3727 | break; |
| 381 | 3728 | ||
| 382 | case 'x': case 'X': | 3729 | case 'x': case 'X': |
| 383 | # ifdef HAVE_LONG_LONG_INT | 3730 | # if HAVE_LONG_LONG_INT |
| 384 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) | 3731 | if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
| 385 | tmp_length = | 3732 | tmp_length = |
| 386 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT | 3733 | (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
| @@ -408,7 +3755,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 408 | break; | 3755 | break; |
| 409 | 3756 | ||
| 410 | case 'f': case 'F': | 3757 | case 'f': case 'F': |
| 411 | # ifdef HAVE_LONG_DOUBLE | ||
| 412 | if (type == TYPE_LONGDOUBLE) | 3758 | if (type == TYPE_LONGDOUBLE) |
| 413 | tmp_length = | 3759 | tmp_length = |
| 414 | (unsigned int) (LDBL_MAX_EXP | 3760 | (unsigned int) (LDBL_MAX_EXP |
| @@ -418,7 +3764,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 418 | + 1 /* turn floor into ceil */ | 3764 | + 1 /* turn floor into ceil */ |
| 419 | + 10; /* sign, decimal point etc. */ | 3765 | + 10; /* sign, decimal point etc. */ |
| 420 | else | 3766 | else |
| 421 | # endif | ||
| 422 | tmp_length = | 3767 | tmp_length = |
| 423 | (unsigned int) (DBL_MAX_EXP | 3768 | (unsigned int) (DBL_MAX_EXP |
| 424 | * 0.30103 /* binary -> decimal */ | 3769 | * 0.30103 /* binary -> decimal */ |
| @@ -430,14 +3775,32 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 430 | break; | 3775 | break; |
| 431 | 3776 | ||
| 432 | case 'e': case 'E': case 'g': case 'G': | 3777 | case 'e': case 'E': case 'g': case 'G': |
| 433 | case 'a': case 'A': | ||
| 434 | tmp_length = | 3778 | tmp_length = |
| 435 | 12; /* sign, decimal point, exponent etc. */ | 3779 | 12; /* sign, decimal point, exponent etc. */ |
| 436 | tmp_length = xsum (tmp_length, precision); | 3780 | tmp_length = xsum (tmp_length, precision); |
| 437 | break; | 3781 | break; |
| 438 | 3782 | ||
| 3783 | case 'a': case 'A': | ||
| 3784 | if (type == TYPE_LONGDOUBLE) | ||
| 3785 | tmp_length = | ||
| 3786 | (unsigned int) (LDBL_DIG | ||
| 3787 | * 0.831 /* decimal -> hexadecimal */ | ||
| 3788 | ) | ||
| 3789 | + 1; /* turn floor into ceil */ | ||
| 3790 | else | ||
| 3791 | tmp_length = | ||
| 3792 | (unsigned int) (DBL_DIG | ||
| 3793 | * 0.831 /* decimal -> hexadecimal */ | ||
| 3794 | ) | ||
| 3795 | + 1; /* turn floor into ceil */ | ||
| 3796 | if (tmp_length < precision) | ||
| 3797 | tmp_length = precision; | ||
| 3798 | /* Account for sign, decimal point etc. */ | ||
| 3799 | tmp_length = xsum (tmp_length, 12); | ||
| 3800 | break; | ||
| 3801 | |||
| 439 | case 'c': | 3802 | case 'c': |
| 440 | # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION | 3803 | # if HAVE_WINT_T && !WIDE_CHAR_VERSION |
| 441 | if (type == TYPE_WIDE_CHAR) | 3804 | if (type == TYPE_WIDE_CHAR) |
| 442 | tmp_length = MB_CUR_MAX; | 3805 | tmp_length = MB_CUR_MAX; |
| 443 | else | 3806 | else |
| @@ -446,7 +3809,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 446 | break; | 3809 | break; |
| 447 | 3810 | ||
| 448 | case 's': | 3811 | case 's': |
| 449 | # ifdef HAVE_WCHAR_T | 3812 | # if HAVE_WCHAR_T |
| 450 | if (type == TYPE_WIDE_STRING) | 3813 | if (type == TYPE_WIDE_STRING) |
| 451 | { | 3814 | { |
| 452 | tmp_length = | 3815 | tmp_length = |
| @@ -474,95 +3837,216 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 474 | abort (); | 3837 | abort (); |
| 475 | } | 3838 | } |
| 476 | 3839 | ||
| 3840 | # if ENABLE_UNISTDIO | ||
| 3841 | /* Padding considers the number of characters, therefore the | ||
| 3842 | number of elements after padding may be | ||
| 3843 | > max (tmp_length, width) | ||
| 3844 | but is certainly | ||
| 3845 | <= tmp_length + width. */ | ||
| 3846 | tmp_length = xsum (tmp_length, width); | ||
| 3847 | # else | ||
| 3848 | /* Padding considers the number of elements, says POSIX. */ | ||
| 477 | if (tmp_length < width) | 3849 | if (tmp_length < width) |
| 478 | tmp_length = width; | 3850 | tmp_length = width; |
| 3851 | # endif | ||
| 479 | 3852 | ||
| 480 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | 3853 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ |
| 481 | } | 3854 | } |
| 482 | 3855 | ||
| 483 | if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T)) | 3856 | if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T)) |
| 484 | tmp = tmpbuf; | 3857 | tmp = tmpbuf; |
| 485 | else | 3858 | else |
| 486 | { | 3859 | { |
| 487 | size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T)); | 3860 | size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T)); |
| 488 | 3861 | ||
| 489 | if (size_overflow_p (tmp_memsize)) | 3862 | if (size_overflow_p (tmp_memsize)) |
| 490 | /* Overflow, would lead to out of memory. */ | 3863 | /* Overflow, would lead to out of memory. */ |
| 491 | goto out_of_memory; | 3864 | goto out_of_memory; |
| 492 | tmp = (CHAR_T *) malloc (tmp_memsize); | 3865 | tmp = (TCHAR_T *) malloc (tmp_memsize); |
| 493 | if (tmp == NULL) | 3866 | if (tmp == NULL) |
| 494 | /* Out of memory. */ | 3867 | /* Out of memory. */ |
| 495 | goto out_of_memory; | 3868 | goto out_of_memory; |
| 496 | } | 3869 | } |
| 497 | #endif | 3870 | #endif |
| 498 | 3871 | ||
| 3872 | /* Decide whether to handle the precision ourselves. */ | ||
| 3873 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 3874 | switch (dp->conversion) | ||
| 3875 | { | ||
| 3876 | case 'd': case 'i': case 'u': | ||
| 3877 | case 'o': | ||
| 3878 | case 'x': case 'X': case 'p': | ||
| 3879 | prec_ourselves = has_precision && (precision > 0); | ||
| 3880 | break; | ||
| 3881 | default: | ||
| 3882 | prec_ourselves = 0; | ||
| 3883 | break; | ||
| 3884 | } | ||
| 3885 | #endif | ||
| 3886 | |||
| 3887 | /* Decide whether to perform the padding ourselves. */ | ||
| 3888 | #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION) | ||
| 3889 | switch (dp->conversion) | ||
| 3890 | { | ||
| 3891 | # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO | ||
| 3892 | /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need | ||
| 3893 | to perform the padding after this conversion. Functions | ||
| 3894 | with unistdio extensions perform the padding based on | ||
| 3895 | character count rather than element count. */ | ||
| 3896 | case 'c': case 's': | ||
| 3897 | # endif | ||
| 3898 | # if NEED_PRINTF_FLAG_ZERO | ||
| 3899 | case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': | ||
| 3900 | case 'a': case 'A': | ||
| 3901 | # endif | ||
| 3902 | pad_ourselves = 1; | ||
| 3903 | break; | ||
| 3904 | default: | ||
| 3905 | pad_ourselves = prec_ourselves; | ||
| 3906 | break; | ||
| 3907 | } | ||
| 3908 | #endif | ||
| 3909 | |||
| 499 | /* Construct the format string for calling snprintf or | 3910 | /* Construct the format string for calling snprintf or |
| 500 | sprintf. */ | 3911 | sprintf. */ |
| 501 | p = buf; | 3912 | fbp = buf; |
| 502 | *p++ = '%'; | 3913 | *fbp++ = '%'; |
| 503 | if (dp->flags & FLAG_GROUP) | 3914 | #if NEED_PRINTF_FLAG_GROUPING |
| 504 | *p++ = '\''; | 3915 | /* The underlying implementation doesn't support the ' flag. |
| 505 | if (dp->flags & FLAG_LEFT) | 3916 | Produce no grouping characters in this case; this is |
| 506 | *p++ = '-'; | 3917 | acceptable because the grouping is locale dependent. */ |
| 507 | if (dp->flags & FLAG_SHOWSIGN) | 3918 | #else |
| 508 | *p++ = '+'; | 3919 | if (flags & FLAG_GROUP) |
| 509 | if (dp->flags & FLAG_SPACE) | 3920 | *fbp++ = '\''; |
| 510 | *p++ = ' '; | 3921 | #endif |
| 511 | if (dp->flags & FLAG_ALT) | 3922 | if (flags & FLAG_LEFT) |
| 512 | *p++ = '#'; | 3923 | *fbp++ = '-'; |
| 513 | if (dp->flags & FLAG_ZERO) | 3924 | if (flags & FLAG_SHOWSIGN) |
| 514 | *p++ = '0'; | 3925 | *fbp++ = '+'; |
| 515 | if (dp->width_start != dp->width_end) | 3926 | if (flags & FLAG_SPACE) |
| 3927 | *fbp++ = ' '; | ||
| 3928 | if (flags & FLAG_ALT) | ||
| 3929 | *fbp++ = '#'; | ||
| 3930 | if (!pad_ourselves) | ||
| 516 | { | 3931 | { |
| 517 | size_t n = dp->width_end - dp->width_start; | 3932 | if (flags & FLAG_ZERO) |
| 518 | memcpy (p, dp->width_start, n * sizeof (CHAR_T)); | 3933 | *fbp++ = '0'; |
| 519 | p += n; | 3934 | if (dp->width_start != dp->width_end) |
| 3935 | { | ||
| 3936 | size_t n = dp->width_end - dp->width_start; | ||
| 3937 | /* The width specification is known to consist only | ||
| 3938 | of standard ASCII characters. */ | ||
| 3939 | if (sizeof (FCHAR_T) == sizeof (TCHAR_T)) | ||
| 3940 | { | ||
| 3941 | memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T)); | ||
| 3942 | fbp += n; | ||
| 3943 | } | ||
| 3944 | else | ||
| 3945 | { | ||
| 3946 | const FCHAR_T *mp = dp->width_start; | ||
| 3947 | do | ||
| 3948 | *fbp++ = (unsigned char) *mp++; | ||
| 3949 | while (--n > 0); | ||
| 3950 | } | ||
| 3951 | } | ||
| 520 | } | 3952 | } |
| 521 | if (dp->precision_start != dp->precision_end) | 3953 | if (!prec_ourselves) |
| 522 | { | 3954 | { |
| 523 | size_t n = dp->precision_end - dp->precision_start; | 3955 | if (dp->precision_start != dp->precision_end) |
| 524 | memcpy (p, dp->precision_start, n * sizeof (CHAR_T)); | 3956 | { |
| 525 | p += n; | 3957 | size_t n = dp->precision_end - dp->precision_start; |
| 3958 | /* The precision specification is known to consist only | ||
| 3959 | of standard ASCII characters. */ | ||
| 3960 | if (sizeof (FCHAR_T) == sizeof (TCHAR_T)) | ||
| 3961 | { | ||
| 3962 | memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T)); | ||
| 3963 | fbp += n; | ||
| 3964 | } | ||
| 3965 | else | ||
| 3966 | { | ||
| 3967 | const FCHAR_T *mp = dp->precision_start; | ||
| 3968 | do | ||
| 3969 | *fbp++ = (unsigned char) *mp++; | ||
| 3970 | while (--n > 0); | ||
| 3971 | } | ||
| 3972 | } | ||
| 526 | } | 3973 | } |
| 527 | 3974 | ||
| 528 | switch (type) | 3975 | switch (type) |
| 529 | { | 3976 | { |
| 530 | #ifdef HAVE_LONG_LONG_INT | 3977 | #if HAVE_LONG_LONG_INT |
| 531 | case TYPE_LONGLONGINT: | 3978 | case TYPE_LONGLONGINT: |
| 532 | case TYPE_ULONGLONGINT: | 3979 | case TYPE_ULONGLONGINT: |
| 533 | *p++ = 'l'; | 3980 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
| 3981 | *fbp++ = 'I'; | ||
| 3982 | *fbp++ = '6'; | ||
| 3983 | *fbp++ = '4'; | ||
| 3984 | break; | ||
| 3985 | # else | ||
| 3986 | *fbp++ = 'l'; | ||
| 534 | /*FALLTHROUGH*/ | 3987 | /*FALLTHROUGH*/ |
| 3988 | # endif | ||
| 535 | #endif | 3989 | #endif |
| 536 | case TYPE_LONGINT: | 3990 | case TYPE_LONGINT: |
| 537 | case TYPE_ULONGINT: | 3991 | case TYPE_ULONGINT: |
| 538 | #ifdef HAVE_WINT_T | 3992 | #if HAVE_WINT_T |
| 539 | case TYPE_WIDE_CHAR: | 3993 | case TYPE_WIDE_CHAR: |
| 540 | #endif | 3994 | #endif |
| 541 | #ifdef HAVE_WCHAR_T | 3995 | #if HAVE_WCHAR_T |
| 542 | case TYPE_WIDE_STRING: | 3996 | case TYPE_WIDE_STRING: |
| 543 | #endif | 3997 | #endif |
| 544 | *p++ = 'l'; | 3998 | *fbp++ = 'l'; |
| 545 | break; | 3999 | break; |
| 546 | #ifdef HAVE_LONG_DOUBLE | ||
| 547 | case TYPE_LONGDOUBLE: | 4000 | case TYPE_LONGDOUBLE: |
| 548 | *p++ = 'L'; | 4001 | *fbp++ = 'L'; |
| 549 | break; | 4002 | break; |
| 550 | #endif | ||
| 551 | default: | 4003 | default: |
| 552 | break; | 4004 | break; |
| 553 | } | 4005 | } |
| 554 | *p = dp->conversion; | 4006 | #if NEED_PRINTF_DIRECTIVE_F |
| 4007 | if (dp->conversion == 'F') | ||
| 4008 | *fbp = 'f'; | ||
| 4009 | else | ||
| 4010 | #endif | ||
| 4011 | *fbp = dp->conversion; | ||
| 555 | #if USE_SNPRINTF | 4012 | #if USE_SNPRINTF |
| 556 | p[1] = '%'; | 4013 | # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)) |
| 557 | p[2] = 'n'; | 4014 | fbp[1] = '%'; |
| 558 | p[3] = '\0'; | 4015 | fbp[2] = 'n'; |
| 4016 | fbp[3] = '\0'; | ||
| 4017 | # else | ||
| 4018 | /* On glibc2 systems from glibc >= 2.3 - probably also older | ||
| 4019 | ones - we know that snprintf's returns value conforms to | ||
| 4020 | ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes. | ||
| 4021 | Therefore we can avoid using %n in this situation. | ||
| 4022 | On glibc2 systems from 2004-10-18 or newer, the use of %n | ||
| 4023 | in format strings in writable memory may crash the program | ||
| 4024 | (if compiled with _FORTIFY_SOURCE=2), so we should avoid it | ||
| 4025 | in this situation. */ | ||
| 4026 | /* On native Win32 systems (such as mingw), we can avoid using | ||
| 4027 | %n because: | ||
| 4028 | - Although the gl_SNPRINTF_TRUNCATION_C99 test fails, | ||
| 4029 | snprintf does not write more than the specified number | ||
| 4030 | of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes | ||
| 4031 | '4', '5', '6' into buf, not '4', '5', '\0'.) | ||
| 4032 | - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf | ||
| 4033 | allows us to recognize the case of an insufficient | ||
| 4034 | buffer size: it returns -1 in this case. | ||
| 4035 | On native Win32 systems (such as mingw) where the OS is | ||
| 4036 | Windows Vista, the use of %n in format strings by default | ||
| 4037 | crashes the program. See | ||
| 4038 | <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and | ||
| 4039 | <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx> | ||
| 4040 | So we should avoid %n in this situation. */ | ||
| 4041 | fbp[1] = '\0'; | ||
| 4042 | # endif | ||
| 559 | #else | 4043 | #else |
| 560 | p[1] = '\0'; | 4044 | fbp[1] = '\0'; |
| 561 | #endif | 4045 | #endif |
| 562 | 4046 | ||
| 563 | /* Construct the arguments for calling snprintf or sprintf. */ | 4047 | /* Construct the arguments for calling snprintf or sprintf. */ |
| 564 | prefix_count = 0; | 4048 | prefix_count = 0; |
| 565 | if (dp->width_arg_index != ARG_NONE) | 4049 | if (!pad_ourselves && dp->width_arg_index != ARG_NONE) |
| 566 | { | 4050 | { |
| 567 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | 4051 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) |
| 568 | abort (); | 4052 | abort (); |
| @@ -576,36 +4060,50 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 576 | } | 4060 | } |
| 577 | 4061 | ||
| 578 | #if USE_SNPRINTF | 4062 | #if USE_SNPRINTF |
| 4063 | /* The SNPRINTF result is appended after result[0..length]. | ||
| 4064 | The latter is an array of DCHAR_T; SNPRINTF appends an | ||
| 4065 | array of TCHAR_T to it. This is possible because | ||
| 4066 | sizeof (TCHAR_T) divides sizeof (DCHAR_T) and | ||
| 4067 | alignof (TCHAR_T) <= alignof (DCHAR_T). */ | ||
| 4068 | # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T)) | ||
| 4069 | /* Ensure that maxlen below will be >= 2. Needed on BeOS, | ||
| 4070 | where an snprintf() with maxlen==1 acts like sprintf(). */ | ||
| 4071 | ENSURE_ALLOCATION (xsum (length, | ||
| 4072 | (2 + TCHARS_PER_DCHAR - 1) | ||
| 4073 | / TCHARS_PER_DCHAR)); | ||
| 579 | /* Prepare checking whether snprintf returns the count | 4074 | /* Prepare checking whether snprintf returns the count |
| 580 | via %n. */ | 4075 | via %n. */ |
| 581 | ENSURE_ALLOCATION (xsum (length, 1)); | 4076 | *(TCHAR_T *) (result + length) = '\0'; |
| 582 | result[length] = '\0'; | ||
| 583 | #endif | 4077 | #endif |
| 584 | 4078 | ||
| 585 | for (;;) | 4079 | for (;;) |
| 586 | { | 4080 | { |
| 587 | size_t maxlen; | 4081 | int count = -1; |
| 588 | int count; | ||
| 589 | int retcount; | ||
| 590 | |||
| 591 | maxlen = allocated - length; | ||
| 592 | count = -1; | ||
| 593 | retcount = 0; | ||
| 594 | 4082 | ||
| 595 | #if USE_SNPRINTF | 4083 | #if USE_SNPRINTF |
| 4084 | int retcount = 0; | ||
| 4085 | size_t maxlen = allocated - length; | ||
| 4086 | /* SNPRINTF can fail if its second argument is | ||
| 4087 | > INT_MAX. */ | ||
| 4088 | if (maxlen > INT_MAX / TCHARS_PER_DCHAR) | ||
| 4089 | maxlen = INT_MAX / TCHARS_PER_DCHAR; | ||
| 4090 | maxlen = maxlen * TCHARS_PER_DCHAR; | ||
| 596 | # define SNPRINTF_BUF(arg) \ | 4091 | # define SNPRINTF_BUF(arg) \ |
| 597 | switch (prefix_count) \ | 4092 | switch (prefix_count) \ |
| 598 | { \ | 4093 | { \ |
| 599 | case 0: \ | 4094 | case 0: \ |
| 600 | retcount = SNPRINTF (result + length, maxlen, buf, \ | 4095 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ |
| 4096 | maxlen, buf, \ | ||
| 601 | arg, &count); \ | 4097 | arg, &count); \ |
| 602 | break; \ | 4098 | break; \ |
| 603 | case 1: \ | 4099 | case 1: \ |
| 604 | retcount = SNPRINTF (result + length, maxlen, buf, \ | 4100 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ |
| 4101 | maxlen, buf, \ | ||
| 605 | prefixes[0], arg, &count); \ | 4102 | prefixes[0], arg, &count); \ |
| 606 | break; \ | 4103 | break; \ |
| 607 | case 2: \ | 4104 | case 2: \ |
| 608 | retcount = SNPRINTF (result + length, maxlen, buf, \ | 4105 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ |
| 4106 | maxlen, buf, \ | ||
| 609 | prefixes[0], prefixes[1], arg, \ | 4107 | prefixes[0], prefixes[1], arg, \ |
| 610 | &count); \ | 4108 | &count); \ |
| 611 | break; \ | 4109 | break; \ |
| @@ -681,7 +4179,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 681 | SNPRINTF_BUF (arg); | 4179 | SNPRINTF_BUF (arg); |
| 682 | } | 4180 | } |
| 683 | break; | 4181 | break; |
| 684 | #ifdef HAVE_LONG_LONG_INT | 4182 | #if HAVE_LONG_LONG_INT |
| 685 | case TYPE_LONGLONGINT: | 4183 | case TYPE_LONGLONGINT: |
| 686 | { | 4184 | { |
| 687 | long long int arg = a.arg[dp->arg_index].a.a_longlongint; | 4185 | long long int arg = a.arg[dp->arg_index].a.a_longlongint; |
| @@ -701,21 +4199,19 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 701 | SNPRINTF_BUF (arg); | 4199 | SNPRINTF_BUF (arg); |
| 702 | } | 4200 | } |
| 703 | break; | 4201 | break; |
| 704 | #ifdef HAVE_LONG_DOUBLE | ||
| 705 | case TYPE_LONGDOUBLE: | 4202 | case TYPE_LONGDOUBLE: |
| 706 | { | 4203 | { |
| 707 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | 4204 | long double arg = a.arg[dp->arg_index].a.a_longdouble; |
| 708 | SNPRINTF_BUF (arg); | 4205 | SNPRINTF_BUF (arg); |
| 709 | } | 4206 | } |
| 710 | break; | 4207 | break; |
| 711 | #endif | ||
| 712 | case TYPE_CHAR: | 4208 | case TYPE_CHAR: |
| 713 | { | 4209 | { |
| 714 | int arg = a.arg[dp->arg_index].a.a_char; | 4210 | int arg = a.arg[dp->arg_index].a.a_char; |
| 715 | SNPRINTF_BUF (arg); | 4211 | SNPRINTF_BUF (arg); |
| 716 | } | 4212 | } |
| 717 | break; | 4213 | break; |
| 718 | #ifdef HAVE_WINT_T | 4214 | #if HAVE_WINT_T |
| 719 | case TYPE_WIDE_CHAR: | 4215 | case TYPE_WIDE_CHAR: |
| 720 | { | 4216 | { |
| 721 | wint_t arg = a.arg[dp->arg_index].a.a_wide_char; | 4217 | wint_t arg = a.arg[dp->arg_index].a.a_wide_char; |
| @@ -729,7 +4225,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 729 | SNPRINTF_BUF (arg); | 4225 | SNPRINTF_BUF (arg); |
| 730 | } | 4226 | } |
| 731 | break; | 4227 | break; |
| 732 | #ifdef HAVE_WCHAR_T | 4228 | #if HAVE_WCHAR_T |
| 733 | case TYPE_WIDE_STRING: | 4229 | case TYPE_WIDE_STRING: |
| 734 | { | 4230 | { |
| 735 | const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string; | 4231 | const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string; |
| @@ -756,7 +4252,8 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 756 | { | 4252 | { |
| 757 | /* Verify that snprintf() has NUL-terminated its | 4253 | /* Verify that snprintf() has NUL-terminated its |
| 758 | result. */ | 4254 | result. */ |
| 759 | if (count < maxlen && result[length + count] != '\0') | 4255 | if (count < maxlen |
| 4256 | && ((TCHAR_T *) (result + length)) [count] != '\0') | ||
| 760 | abort (); | 4257 | abort (); |
| 761 | /* Portability hack. */ | 4258 | /* Portability hack. */ |
| 762 | if (retcount > count) | 4259 | if (retcount > count) |
| @@ -766,11 +4263,11 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 766 | { | 4263 | { |
| 767 | /* snprintf() doesn't understand the '%n' | 4264 | /* snprintf() doesn't understand the '%n' |
| 768 | directive. */ | 4265 | directive. */ |
| 769 | if (p[1] != '\0') | 4266 | if (fbp[1] != '\0') |
| 770 | { | 4267 | { |
| 771 | /* Don't use the '%n' directive; instead, look | 4268 | /* Don't use the '%n' directive; instead, look |
| 772 | at the snprintf() return value. */ | 4269 | at the snprintf() return value. */ |
| 773 | p[1] = '\0'; | 4270 | fbp[1] = '\0'; |
| 774 | continue; | 4271 | continue; |
| 775 | } | 4272 | } |
| 776 | else | 4273 | else |
| @@ -806,37 +4303,339 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 806 | return NULL; | 4303 | return NULL; |
| 807 | } | 4304 | } |
| 808 | 4305 | ||
| 809 | #if !USE_SNPRINTF | 4306 | #if USE_SNPRINTF |
| 4307 | /* Handle overflow of the allocated buffer. | ||
| 4308 | If such an overflow occurs, a C99 compliant snprintf() | ||
| 4309 | returns a count >= maxlen. However, a non-compliant | ||
| 4310 | snprintf() function returns only count = maxlen - 1. To | ||
| 4311 | cover both cases, test whether count >= maxlen - 1. */ | ||
| 4312 | if ((unsigned int) count + 1 >= maxlen) | ||
| 4313 | { | ||
| 4314 | /* If maxlen already has attained its allowed maximum, | ||
| 4315 | allocating more memory will not increase maxlen. | ||
| 4316 | Instead of looping, bail out. */ | ||
| 4317 | if (maxlen == INT_MAX / TCHARS_PER_DCHAR) | ||
| 4318 | goto overflow; | ||
| 4319 | else | ||
| 4320 | { | ||
| 4321 | /* Need at least (count + 1) * sizeof (TCHAR_T) | ||
| 4322 | bytes. (The +1 is for the trailing NUL.) | ||
| 4323 | But ask for (count + 2) * sizeof (TCHAR_T) | ||
| 4324 | bytes, so that in the next round, we likely get | ||
| 4325 | maxlen > (unsigned int) count + 1 | ||
| 4326 | and so we don't get here again. | ||
| 4327 | And allocate proportionally, to avoid looping | ||
| 4328 | eternally if snprintf() reports a too small | ||
| 4329 | count. */ | ||
| 4330 | size_t n = | ||
| 4331 | xmax (xsum (length, | ||
| 4332 | ((unsigned int) count + 2 | ||
| 4333 | + TCHARS_PER_DCHAR - 1) | ||
| 4334 | / TCHARS_PER_DCHAR), | ||
| 4335 | xtimes (allocated, 2)); | ||
| 4336 | |||
| 4337 | ENSURE_ALLOCATION (n); | ||
| 4338 | continue; | ||
| 4339 | } | ||
| 4340 | } | ||
| 4341 | #endif | ||
| 4342 | |||
| 4343 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 4344 | if (prec_ourselves) | ||
| 4345 | { | ||
| 4346 | /* Handle the precision. */ | ||
| 4347 | TCHAR_T *prec_ptr = | ||
| 4348 | # if USE_SNPRINTF | ||
| 4349 | (TCHAR_T *) (result + length); | ||
| 4350 | # else | ||
| 4351 | tmp; | ||
| 4352 | # endif | ||
| 4353 | size_t prefix_count; | ||
| 4354 | size_t move; | ||
| 4355 | |||
| 4356 | prefix_count = 0; | ||
| 4357 | /* Put the additional zeroes after the sign. */ | ||
| 4358 | if (count >= 1 | ||
| 4359 | && (*prec_ptr == '-' || *prec_ptr == '+' | ||
| 4360 | || *prec_ptr == ' ')) | ||
| 4361 | prefix_count = 1; | ||
| 4362 | /* Put the additional zeroes after the 0x prefix if | ||
| 4363 | (flags & FLAG_ALT) || (dp->conversion == 'p'). */ | ||
| 4364 | else if (count >= 2 | ||
| 4365 | && prec_ptr[0] == '0' | ||
| 4366 | && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X')) | ||
| 4367 | prefix_count = 2; | ||
| 4368 | |||
| 4369 | move = count - prefix_count; | ||
| 4370 | if (precision > move) | ||
| 4371 | { | ||
| 4372 | /* Insert zeroes. */ | ||
| 4373 | size_t insert = precision - move; | ||
| 4374 | TCHAR_T *prec_end; | ||
| 4375 | |||
| 4376 | # if USE_SNPRINTF | ||
| 4377 | size_t n = | ||
| 4378 | xsum (length, | ||
| 4379 | (count + insert + TCHARS_PER_DCHAR - 1) | ||
| 4380 | / TCHARS_PER_DCHAR); | ||
| 4381 | length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR; | ||
| 4382 | ENSURE_ALLOCATION (n); | ||
| 4383 | length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR; | ||
| 4384 | prec_ptr = (TCHAR_T *) (result + length); | ||
| 4385 | # endif | ||
| 4386 | |||
| 4387 | prec_end = prec_ptr + count; | ||
| 4388 | prec_ptr += prefix_count; | ||
| 4389 | |||
| 4390 | while (prec_end > prec_ptr) | ||
| 4391 | { | ||
| 4392 | prec_end--; | ||
| 4393 | prec_end[insert] = prec_end[0]; | ||
| 4394 | } | ||
| 4395 | |||
| 4396 | prec_end += insert; | ||
| 4397 | do | ||
| 4398 | *--prec_end = '0'; | ||
| 4399 | while (prec_end > prec_ptr); | ||
| 4400 | |||
| 4401 | count += insert; | ||
| 4402 | } | ||
| 4403 | } | ||
| 4404 | #endif | ||
| 4405 | |||
| 4406 | #if !DCHAR_IS_TCHAR | ||
| 4407 | # if !USE_SNPRINTF | ||
| 810 | if (count >= tmp_length) | 4408 | if (count >= tmp_length) |
| 811 | /* tmp_length was incorrectly calculated - fix the | 4409 | /* tmp_length was incorrectly calculated - fix the |
| 812 | code above! */ | 4410 | code above! */ |
| 813 | abort (); | 4411 | abort (); |
| 4412 | # endif | ||
| 4413 | |||
| 4414 | /* Convert from TCHAR_T[] to DCHAR_T[]. */ | ||
| 4415 | if (dp->conversion == 'c' || dp->conversion == 's') | ||
| 4416 | { | ||
| 4417 | /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING | ||
| 4418 | TYPE_WIDE_STRING. | ||
| 4419 | The result string is not certainly ASCII. */ | ||
| 4420 | const TCHAR_T *tmpsrc; | ||
| 4421 | DCHAR_T *tmpdst; | ||
| 4422 | size_t tmpdst_len; | ||
| 4423 | /* This code assumes that TCHAR_T is 'char'. */ | ||
| 4424 | typedef int TCHAR_T_verify | ||
| 4425 | [2 * (sizeof (TCHAR_T) == 1) - 1]; | ||
| 4426 | # if USE_SNPRINTF | ||
| 4427 | tmpsrc = (TCHAR_T *) (result + length); | ||
| 4428 | # else | ||
| 4429 | tmpsrc = tmp; | ||
| 4430 | # endif | ||
| 4431 | tmpdst = NULL; | ||
| 4432 | tmpdst_len = 0; | ||
| 4433 | if (DCHAR_CONV_FROM_ENCODING (locale_charset (), | ||
| 4434 | iconveh_question_mark, | ||
| 4435 | tmpsrc, count, | ||
| 4436 | NULL, | ||
| 4437 | &tmpdst, &tmpdst_len) | ||
| 4438 | < 0) | ||
| 4439 | { | ||
| 4440 | int saved_errno = errno; | ||
| 4441 | if (!(result == resultbuf || result == NULL)) | ||
| 4442 | free (result); | ||
| 4443 | if (buf_malloced != NULL) | ||
| 4444 | free (buf_malloced); | ||
| 4445 | CLEANUP (); | ||
| 4446 | errno = saved_errno; | ||
| 4447 | return NULL; | ||
| 4448 | } | ||
| 4449 | ENSURE_ALLOCATION (xsum (length, tmpdst_len)); | ||
| 4450 | DCHAR_CPY (result + length, tmpdst, tmpdst_len); | ||
| 4451 | free (tmpdst); | ||
| 4452 | count = tmpdst_len; | ||
| 4453 | } | ||
| 4454 | else | ||
| 4455 | { | ||
| 4456 | /* The result string is ASCII. | ||
| 4457 | Simple 1:1 conversion. */ | ||
| 4458 | # if USE_SNPRINTF | ||
| 4459 | /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a | ||
| 4460 | no-op conversion, in-place on the array starting | ||
| 4461 | at (result + length). */ | ||
| 4462 | if (sizeof (DCHAR_T) != sizeof (TCHAR_T)) | ||
| 4463 | # endif | ||
| 4464 | { | ||
| 4465 | const TCHAR_T *tmpsrc; | ||
| 4466 | DCHAR_T *tmpdst; | ||
| 4467 | size_t n; | ||
| 4468 | |||
| 4469 | # if USE_SNPRINTF | ||
| 4470 | if (result == resultbuf) | ||
| 4471 | { | ||
| 4472 | tmpsrc = (TCHAR_T *) (result + length); | ||
| 4473 | /* ENSURE_ALLOCATION will not move tmpsrc | ||
| 4474 | (because it's part of resultbuf). */ | ||
| 4475 | ENSURE_ALLOCATION (xsum (length, count)); | ||
| 4476 | } | ||
| 4477 | else | ||
| 4478 | { | ||
| 4479 | /* ENSURE_ALLOCATION will move the array | ||
| 4480 | (because it uses realloc(). */ | ||
| 4481 | ENSURE_ALLOCATION (xsum (length, count)); | ||
| 4482 | tmpsrc = (TCHAR_T *) (result + length); | ||
| 4483 | } | ||
| 4484 | # else | ||
| 4485 | tmpsrc = tmp; | ||
| 4486 | ENSURE_ALLOCATION (xsum (length, count)); | ||
| 4487 | # endif | ||
| 4488 | tmpdst = result + length; | ||
| 4489 | /* Copy backwards, because of overlapping. */ | ||
| 4490 | tmpsrc += count; | ||
| 4491 | tmpdst += count; | ||
| 4492 | for (n = count; n > 0; n--) | ||
| 4493 | *--tmpdst = (unsigned char) *--tmpsrc; | ||
| 4494 | } | ||
| 4495 | } | ||
| 814 | #endif | 4496 | #endif |
| 815 | 4497 | ||
| 4498 | #if DCHAR_IS_TCHAR && !USE_SNPRINTF | ||
| 816 | /* Make room for the result. */ | 4499 | /* Make room for the result. */ |
| 817 | if (count >= maxlen) | 4500 | if (count > allocated - length) |
| 818 | { | 4501 | { |
| 819 | /* Need at least count bytes. But allocate | 4502 | /* Need at least count elements. But allocate |
| 820 | proportionally, to avoid looping eternally if | 4503 | proportionally. */ |
| 821 | snprintf() reports a too small count. */ | ||
| 822 | size_t n = | 4504 | size_t n = |
| 823 | xmax (xsum (length, count), xtimes (allocated, 2)); | 4505 | xmax (xsum (length, count), xtimes (allocated, 2)); |
| 824 | 4506 | ||
| 825 | ENSURE_ALLOCATION (n); | 4507 | ENSURE_ALLOCATION (n); |
| 826 | #if USE_SNPRINTF | 4508 | } |
| 827 | continue; | ||
| 828 | #endif | 4509 | #endif |
| 4510 | |||
| 4511 | /* Here count <= allocated - length. */ | ||
| 4512 | |||
| 4513 | /* Perform padding. */ | ||
| 4514 | #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
| 4515 | if (pad_ourselves && has_width) | ||
| 4516 | { | ||
| 4517 | size_t w; | ||
| 4518 | # if ENABLE_UNISTDIO | ||
| 4519 | /* Outside POSIX, it's preferrable to compare the width | ||
| 4520 | against the number of _characters_ of the converted | ||
| 4521 | value. */ | ||
| 4522 | w = DCHAR_MBSNLEN (result + length, count); | ||
| 4523 | # else | ||
| 4524 | /* The width is compared against the number of _bytes_ | ||
| 4525 | of the converted value, says POSIX. */ | ||
| 4526 | w = count; | ||
| 4527 | # endif | ||
| 4528 | if (w < width) | ||
| 4529 | { | ||
| 4530 | size_t pad = width - w; | ||
| 4531 | # if USE_SNPRINTF | ||
| 4532 | /* Make room for the result. */ | ||
| 4533 | if (xsum (count, pad) > allocated - length) | ||
| 4534 | { | ||
| 4535 | /* Need at least count + pad elements. But | ||
| 4536 | allocate proportionally. */ | ||
| 4537 | size_t n = | ||
| 4538 | xmax (xsum3 (length, count, pad), | ||
| 4539 | xtimes (allocated, 2)); | ||
| 4540 | |||
| 4541 | length += count; | ||
| 4542 | ENSURE_ALLOCATION (n); | ||
| 4543 | length -= count; | ||
| 4544 | } | ||
| 4545 | /* Here count + pad <= allocated - length. */ | ||
| 4546 | # endif | ||
| 4547 | { | ||
| 4548 | # if !DCHAR_IS_TCHAR || USE_SNPRINTF | ||
| 4549 | DCHAR_T * const rp = result + length; | ||
| 4550 | # else | ||
| 4551 | DCHAR_T * const rp = tmp; | ||
| 4552 | # endif | ||
| 4553 | DCHAR_T *p = rp + count; | ||
| 4554 | DCHAR_T *end = p + pad; | ||
| 4555 | DCHAR_T *pad_ptr; | ||
| 4556 | # if !DCHAR_IS_TCHAR | ||
| 4557 | if (dp->conversion == 'c' | ||
| 4558 | || dp->conversion == 's') | ||
| 4559 | /* No zero-padding for string directives. */ | ||
| 4560 | pad_ptr = NULL; | ||
| 4561 | else | ||
| 4562 | # endif | ||
| 4563 | { | ||
| 4564 | pad_ptr = (*rp == '-' ? rp + 1 : rp); | ||
| 4565 | /* No zero-padding of "inf" and "nan". */ | ||
| 4566 | if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z') | ||
| 4567 | || (*pad_ptr >= 'a' && *pad_ptr <= 'z')) | ||
| 4568 | pad_ptr = NULL; | ||
| 4569 | } | ||
| 4570 | /* The generated string now extends from rp to p, | ||
| 4571 | with the zero padding insertion point being at | ||
| 4572 | pad_ptr. */ | ||
| 4573 | |||
| 4574 | count = count + pad; /* = end - rp */ | ||
| 4575 | |||
| 4576 | if (flags & FLAG_LEFT) | ||
| 4577 | { | ||
| 4578 | /* Pad with spaces on the right. */ | ||
| 4579 | for (; pad > 0; pad--) | ||
| 4580 | *p++ = ' '; | ||
| 4581 | } | ||
| 4582 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
| 4583 | { | ||
| 4584 | /* Pad with zeroes. */ | ||
| 4585 | DCHAR_T *q = end; | ||
| 4586 | |||
| 4587 | while (p > pad_ptr) | ||
| 4588 | *--q = *--p; | ||
| 4589 | for (; pad > 0; pad--) | ||
| 4590 | *p++ = '0'; | ||
| 4591 | } | ||
| 4592 | else | ||
| 4593 | { | ||
| 4594 | /* Pad with spaces on the left. */ | ||
| 4595 | DCHAR_T *q = end; | ||
| 4596 | |||
| 4597 | while (p > rp) | ||
| 4598 | *--q = *--p; | ||
| 4599 | for (; pad > 0; pad--) | ||
| 4600 | *p++ = ' '; | ||
| 4601 | } | ||
| 4602 | } | ||
| 4603 | } | ||
| 829 | } | 4604 | } |
| 4605 | #endif | ||
| 830 | 4606 | ||
| 831 | #if USE_SNPRINTF | 4607 | #if DCHAR_IS_TCHAR && !USE_SNPRINTF |
| 4608 | if (count >= tmp_length) | ||
| 4609 | /* tmp_length was incorrectly calculated - fix the | ||
| 4610 | code above! */ | ||
| 4611 | abort (); | ||
| 4612 | #endif | ||
| 4613 | |||
| 4614 | /* Here still count <= allocated - length. */ | ||
| 4615 | |||
| 4616 | #if !DCHAR_IS_TCHAR || USE_SNPRINTF | ||
| 832 | /* The snprintf() result did fit. */ | 4617 | /* The snprintf() result did fit. */ |
| 833 | #else | 4618 | #else |
| 834 | /* Append the sprintf() result. */ | 4619 | /* Append the sprintf() result. */ |
| 835 | memcpy (result + length, tmp, count * sizeof (CHAR_T)); | 4620 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); |
| 4621 | #endif | ||
| 4622 | #if !USE_SNPRINTF | ||
| 836 | if (tmp != tmpbuf) | 4623 | if (tmp != tmpbuf) |
| 837 | free (tmp); | 4624 | free (tmp); |
| 838 | #endif | 4625 | #endif |
| 839 | 4626 | ||
| 4627 | #if NEED_PRINTF_DIRECTIVE_F | ||
| 4628 | if (dp->conversion == 'F') | ||
| 4629 | { | ||
| 4630 | /* Convert the %f result to upper case for %F. */ | ||
| 4631 | DCHAR_T *rp = result + length; | ||
| 4632 | size_t rc; | ||
| 4633 | for (rc = count; rc > 0; rc--, rp++) | ||
| 4634 | if (*rp >= 'a' && *rp <= 'z') | ||
| 4635 | *rp = *rp - 'a' + 'A'; | ||
| 4636 | } | ||
| 4637 | #endif | ||
| 4638 | |||
| 840 | length += count; | 4639 | length += count; |
| 841 | break; | 4640 | break; |
| 842 | } | 4641 | } |
| @@ -851,9 +4650,9 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 851 | if (result != resultbuf && length + 1 < allocated) | 4650 | if (result != resultbuf && length + 1 < allocated) |
| 852 | { | 4651 | { |
| 853 | /* Shrink the allocated memory if possible. */ | 4652 | /* Shrink the allocated memory if possible. */ |
| 854 | CHAR_T *memory; | 4653 | DCHAR_T *memory; |
| 855 | 4654 | ||
| 856 | memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T)); | 4655 | memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T)); |
| 857 | if (memory != NULL) | 4656 | if (memory != NULL) |
| 858 | result = memory; | 4657 | result = memory; |
| 859 | } | 4658 | } |
| @@ -868,6 +4667,17 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 868 | not have this limitation. */ | 4667 | not have this limitation. */ |
| 869 | return result; | 4668 | return result; |
| 870 | 4669 | ||
| 4670 | #if USE_SNPRINTF | ||
| 4671 | overflow: | ||
| 4672 | if (!(result == resultbuf || result == NULL)) | ||
| 4673 | free (result); | ||
| 4674 | if (buf_malloced != NULL) | ||
| 4675 | free (buf_malloced); | ||
| 4676 | CLEANUP (); | ||
| 4677 | errno = EOVERFLOW; | ||
| 4678 | return NULL; | ||
| 4679 | #endif | ||
| 4680 | |||
| 871 | out_of_memory: | 4681 | out_of_memory: |
| 872 | if (!(result == resultbuf || result == NULL)) | 4682 | if (!(result == resultbuf || result == NULL)) |
| 873 | free (result); | 4683 | free (result); |
| @@ -880,10 +4690,15 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar | |||
| 880 | } | 4690 | } |
| 881 | } | 4691 | } |
| 882 | 4692 | ||
| 4693 | #undef TCHARS_PER_DCHAR | ||
| 883 | #undef SNPRINTF | 4694 | #undef SNPRINTF |
| 884 | #undef USE_SNPRINTF | 4695 | #undef USE_SNPRINTF |
| 4696 | #undef DCHAR_CPY | ||
| 885 | #undef PRINTF_PARSE | 4697 | #undef PRINTF_PARSE |
| 886 | #undef DIRECTIVES | 4698 | #undef DIRECTIVES |
| 887 | #undef DIRECTIVE | 4699 | #undef DIRECTIVE |
| 888 | #undef CHAR_T | 4700 | #undef DCHAR_IS_TCHAR |
| 4701 | #undef TCHAR_T | ||
| 4702 | #undef DCHAR_T | ||
| 4703 | #undef FCHAR_T | ||
| 889 | #undef VASNPRINTF | 4704 | #undef VASNPRINTF |
