annotate lib/vasnprintf.c @ 9489:940605ef6e9e

Don't abort if the 'long double' type has excess precision.
author Bruno Haible <bruno@clisp.org>
date Mon, 26 Nov 2007 21:13:57 +0100
parents 7bbddc0fae4a
children a113e473cc98
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* vsprintf with automatic memory allocation.
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
2 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
4 This program is free software; you can redistribute it and/or modify
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
5 it under the terms of the GNU General Public License as published by
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
12 GNU General Public License for more details.
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
14 You should have received a copy of the GNU General Public License along
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
15 with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5670
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
18 /* This file can be parametrized with the following macros:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
19 VASNPRINTF The name of the function being defined.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
20 FCHAR_T The element type of the format string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
21 DCHAR_T The element type of the destination (result) string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
22 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
23 in the format string are ASCII. MUST be set if
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
24 FCHAR_T and DCHAR_T are not the same type.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
25 DIRECTIVE Structure denoting a format directive.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
26 Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
27 DIRECTIVES Structure denoting the set of format directives of a
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
28 format string. Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
29 PRINTF_PARSE Function that parses a format string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
30 Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
31 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
32 DCHAR_SET memset like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
33 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
34 SNPRINTF The system's snprintf (or similar) function.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
35 This may be either snprintf or swprintf.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
36 TCHAR_T The element type of the argument and result string
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
37 of the said SNPRINTF function. This may be either
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
38 char or wchar_t. The code exploits that
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
39 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
40 alignof (TCHAR_T) <= alignof (DCHAR_T).
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
41 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
42 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
43 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
44 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
45 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
46
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 This must come before <config.h> because <config.h> may include
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 <features.h>, and once <features.h> has been included, it's too late. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #ifndef _GNU_SOURCE
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # define _GNU_SOURCE 1
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
54 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
55 # include <config.h>
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
56 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
57 #ifndef IN_LIBINTL
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
58 # include <alloca.h>
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
59 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /* Specification. */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
62 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
63 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
64 # include "vasnwprintf.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
65 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
66 # include "vasnprintf.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
67 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
68 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
8400
655fca11a0e6 The decimal point must be locale dependent.
Bruno Haible <bruno@clisp.org>
parents: 8343
diff changeset
70 #include <locale.h> /* localeconv() */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 #include <stdio.h> /* snprintf(), sprintf() */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #include <string.h> /* memcpy(), strlen() */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 #include <errno.h> /* errno */
7218
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
75 #include <limits.h> /* CHAR_BIT */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
8554
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
77 #if HAVE_NL_LANGINFO
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
78 # include <langinfo.h>
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
79 #endif
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
80 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
81 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
82 # include "wprintf-parse.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
83 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
84 # include "printf-parse.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
85 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
86 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
88 /* Checked size_t computations. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
89 #include "xsize.h"
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
90
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
91 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
92 # include <math.h>
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
93 # include "float+.h"
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
94 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
95
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
96 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
97 # include <math.h>
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
98 # include "isnan.h"
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
99 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
100
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
101 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
102 # include <math.h>
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
103 # include "isnanl-nolibm.h"
8915
f15c03805bd9 Fix possible compilation error.
Bruno Haible <bruno@clisp.org>
parents: 8865
diff changeset
104 # include "fpucw.h"
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
105 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
106
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
107 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
8655
9a272158fe43 Faster determination of the sign of a number.
Bruno Haible <bruno@clisp.org>
parents: 8648
diff changeset
108 # include <math.h>
8335
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
109 # include "isnan.h"
8526
67e9a647f2fa Fix includes.
Bruno Haible <bruno@clisp.org>
parents: 8487
diff changeset
110 # include "printf-frexp.h"
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
111 #endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
112
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
113 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
114 # include <math.h>
8648
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
115 # include "isnanl-nolibm.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
116 # include "printf-frexpl.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
117 # include "fpucw.h"
8335
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
118 #endif
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
119
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
120 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
121 #ifndef EOVERFLOW
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
122 # define EOVERFLOW E2BIG
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
123 #endif
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
124
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
125 #if HAVE_WCHAR_T
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
126 # if HAVE_WCSLEN
4523
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
127 # define local_wcslen wcslen
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
128 # else
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
129 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
130 a dependency towards this library, here is a local substitute.
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
131 Define this substitute only once, even if this file is included
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
132 twice in the same compilation unit. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
133 # ifndef local_wcslen_defined
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
134 # define local_wcslen_defined 1
4523
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
135 static size_t
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
136 local_wcslen (const wchar_t *s)
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
137 {
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
138 const wchar_t *ptr;
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
139
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
140 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
141 ;
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
142 return ptr - s;
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
143 }
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
144 # endif
4523
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
145 # endif
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
146 #endif
6cc4874a100b Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents: 4444
diff changeset
147
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
148 /* Default parameters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
149 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
150 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
151 # define VASNPRINTF vasnwprintf
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
152 # define FCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
153 # define DCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
154 # define TCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
155 # define DCHAR_IS_TCHAR 1
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
156 # define DIRECTIVE wchar_t_directive
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
157 # define DIRECTIVES wchar_t_directives
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
158 # define PRINTF_PARSE wprintf_parse
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
159 # define DCHAR_CPY wmemcpy
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
160 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
161 # define VASNPRINTF vasnprintf
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
162 # define FCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
163 # define DCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
164 # define TCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
165 # define DCHAR_IS_TCHAR 1
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
166 # define DIRECTIVE char_directive
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
167 # define DIRECTIVES char_directives
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
168 # define PRINTF_PARSE printf_parse
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
169 # define DCHAR_CPY memcpy
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
170 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
171 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
172 #if WIDE_CHAR_VERSION
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
173 /* TCHAR_T is wchar_t. */
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
174 # define USE_SNPRINTF 1
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
175 # if HAVE_DECL__SNWPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
176 /* On Windows, the function swprintf() has a different signature than
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
177 on Unix; we use the _snwprintf() function instead. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
178 # define SNPRINTF _snwprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
179 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
180 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
181 # define SNPRINTF swprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
182 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
183 #else
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
184 /* TCHAR_T is char. */
8831
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
185 # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
186 But don't use it on BeOS, since BeOS snprintf produces no output if the
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
187 size argument is >= 0x3000000. */
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
188 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
189 # define USE_SNPRINTF 1
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
190 # else
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
191 # define USE_SNPRINTF 0
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
192 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
193 # if HAVE_DECL__SNPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
194 /* Windows. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
195 # define SNPRINTF _snprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
196 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
197 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
198 # define SNPRINTF snprintf
8417
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
199 /* Here we need to call the native snprintf, not rpl_snprintf. */
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
200 # undef snprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
201 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
202 #endif
8424
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
203 /* Here we need to call the native sprintf, not rpl_sprintf. */
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
204 #undef sprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
205
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
206 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
8569
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
207 /* Determine the decimal-point character according to the current locale. */
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
208 # ifndef decimal_point_char_defined
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
209 # define decimal_point_char_defined 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
210 static char
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
211 decimal_point_char ()
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
212 {
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
213 const char *point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
214 /* Determine it in a multithread-safe way. We know nl_langinfo is
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
215 multithread-safe on glibc systems, but is not required to be multithread-
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
216 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
217 is rarely multithread-safe. */
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
218 # if HAVE_NL_LANGINFO && __GLIBC__
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
219 point = nl_langinfo (RADIXCHAR);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
220 # elif 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
221 char pointbuf[5];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
222 sprintf (pointbuf, "%#.0f", 1.0);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
223 point = &pointbuf[1];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
224 # else
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
225 point = localeconv () -> decimal_point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
226 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
227 /* The decimal point is always a single byte: either '.' or ','. */
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
228 return (point[0] != '\0' ? point[0] : '.');
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
229 }
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
230 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
231 #endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
232
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
233 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
234
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
235 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
236 static int
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
237 is_infinite_or_zero (double x)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
238 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
239 return isnan (x) || x + x == x;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
240 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
241
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
242 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
243
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
244 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
245
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
246 /* Equivalent to !isfinite(x), but does not require libm. */
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
247 static int
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
248 is_infinitel (long double x)
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
249 {
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
250 return isnanl (x) || (x + x == x && x != 0.0L);
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
251 }
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
252
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
253 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
254
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
255 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
256
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
257 /* Converting 'long double' to decimal without rare rounding bugs requires
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
258 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
259 (and slower) algorithms. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
260
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
261 typedef unsigned int mp_limb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
262 # define GMP_LIMB_BITS 32
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
263 typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
264
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
265 typedef unsigned long long mp_twolimb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
266 # define GMP_TWOLIMB_BITS 64
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
267 typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
268
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
269 /* Representation of a bignum >= 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
270 typedef struct
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
271 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
272 size_t nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
273 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
274 } mpn_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
275
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
276 /* Compute the product of two bignums >= 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
277 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
278 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
279 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
280 multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
281 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
282 const mp_limb_t *p1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
283 const mp_limb_t *p2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
284 size_t len1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
285 size_t len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
286
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
287 if (src1.nlimbs <= src2.nlimbs)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
288 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
289 len1 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
290 p1 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
291 len2 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
292 p2 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
293 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
294 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
295 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
296 len1 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
297 p1 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
298 len2 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
299 p2 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
300 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
301 /* Now 0 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
302 if (len1 == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
303 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
304 /* src1 or src2 is zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
305 dest->nlimbs = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
306 dest->limbs = (mp_limb_t *) malloc (1);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
307 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
308 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
309 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
310 /* Here 1 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
311 size_t dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
312 mp_limb_t *dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
313 size_t k, i, j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
314
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
315 dlen = len1 + len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
316 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
317 if (dp == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
318 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
319 for (k = len2; k > 0; )
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
320 dp[--k] = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
321 for (i = 0; i < len1; i++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
322 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
323 mp_limb_t digit1 = p1[i];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
324 mp_twolimb_t carry = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
325 for (j = 0; j < len2; j++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
326 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
327 mp_limb_t digit2 = p2[j];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
328 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
329 carry += dp[i + j];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
330 dp[i + j] = (mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
331 carry = carry >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
332 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
333 dp[i + len2] = (mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
334 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
335 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
336 while (dlen > 0 && dp[dlen - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
337 dlen--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
338 dest->nlimbs = dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
339 dest->limbs = dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
340 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
341 return dest->limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
342 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
343
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
344 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
345 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
346 the remainder.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
347 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
348 q is incremented.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
349 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
350 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
351 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
352 divide (mpn_t a, mpn_t b, mpn_t *q)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
353 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
354 /* Algorithm:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
355 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
356 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
357 If m<n, then q:=0 and r:=a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
358 If m>=n=1, perform a single-precision division:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
359 r:=0, j:=m,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
360 while j>0 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
361 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
362 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
363 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
364 Normalise [q[m-1],...,q[0]], yields q.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
365 If m>=n>1, perform a multiple-precision division:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
366 We have a/b < beta^(m-n+1).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
367 s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
368 Shift a and b left by s bits, copying them. r:=a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
369 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
370 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
371 Compute q* :
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
372 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
373 In case of overflow (q* >= beta) set q* := beta-1.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
374 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
375 and c3 := b[n-2] * q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
376 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
377 occurred. Furthermore 0 <= c3 < beta^2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
378 If there was overflow and
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
379 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
380 the next test can be skipped.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
381 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
382 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
383 If q* > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
384 Put r := r - b * q* * beta^j. In detail:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
385 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
386 hence: u:=0, for i:=0 to n-1 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
387 u := u + q* * b[i],
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
388 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
389 u:=u div beta (+ 1, if carry in subtraction)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
390 r[n+j]:=r[n+j]-u.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
391 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
392 < q* + 1 <= beta,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
393 the carry u does not overflow.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
394 If a negative carry occurs, put q* := q* - 1
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
395 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
396 Set q[j] := q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
397 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
398 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
399 rest r.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
400 The room for q[j] can be allocated at the memory location of r[n+j].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
401 Finally, round-to-even:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
402 Shift r left by 1 bit.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
403 If r > b or if r = b and q[0] is odd, q := q+1.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
404 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
405 const mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
406 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
407 const mp_limb_t *b_ptr = b.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
408 size_t b_len = b.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
409 mp_limb_t *roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
410 mp_limb_t *tmp_roomptr = NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
411 mp_limb_t *q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
412 size_t q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
413 mp_limb_t *r_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
414 size_t r_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
415
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
416 /* Allocate room for a_len+2 digits.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
417 (Need a_len+1 digits for the real division and 1 more digit for the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
418 final rounding of q.) */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
419 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
420 if (roomptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
421 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
422
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
423 /* Normalise a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
424 while (a_len > 0 && a_ptr[a_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
425 a_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
426
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
427 /* Normalise b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
428 for (;;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
429 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
430 if (b_len == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
431 /* Division by zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
432 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
433 if (b_ptr[b_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
434 b_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
435 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
436 break;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
437 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
438
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
439 /* Here m = a_len >= 0 and n = b_len > 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
440
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
441 if (a_len < b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
442 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
443 /* m<n: trivial case. q=0, r := copy of a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
444 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
445 r_len = a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
446 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
447 q_ptr = roomptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
448 q_len = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
449 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
450 else if (b_len == 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
451 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
452 /* n=1: single precision division.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
453 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
454 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
455 q_ptr = roomptr + 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
456 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
457 mp_limb_t den = b_ptr[0];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
458 mp_limb_t remainder = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
459 const mp_limb_t *sourceptr = a_ptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
460 mp_limb_t *destptr = q_ptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
461 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
462 for (count = a_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
463 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
464 mp_twolimb_t num =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
465 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
466 *--destptr = num / den;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
467 remainder = num % den;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
468 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
469 /* Normalise and store r. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
470 if (remainder > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
471 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
472 r_ptr[0] = remainder;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
473 r_len = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
474 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
475 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
476 r_len = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
477 /* Normalise q. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
478 q_len = a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
479 if (q_ptr[q_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
480 q_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
481 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
482 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
483 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
484 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
485 /* n>1: multiple precision division.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
486 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
487 beta^(m-n-1) <= a/b < beta^(m-n+1). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
488 /* Determine s. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
489 size_t s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
490 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
491 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
492 s = 31;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
493 if (msd >= 0x10000)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
494 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
495 msd = msd >> 16;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
496 s -= 16;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
497 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
498 if (msd >= 0x100)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
499 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
500 msd = msd >> 8;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
501 s -= 8;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
502 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
503 if (msd >= 0x10)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
504 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
505 msd = msd >> 4;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
506 s -= 4;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
507 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
508 if (msd >= 0x4)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
509 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
510 msd = msd >> 2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
511 s -= 2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
512 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
513 if (msd >= 0x2)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
514 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
515 msd = msd >> 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
516 s -= 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
517 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
518 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
519 /* 0 <= s < GMP_LIMB_BITS.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
520 Copy b, shifting it left by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
521 if (s > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
522 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
523 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
524 if (tmp_roomptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
525 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
526 free (roomptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
527 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
528 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
529 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
530 const mp_limb_t *sourceptr = b_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
531 mp_limb_t *destptr = tmp_roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
532 mp_twolimb_t accu = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
533 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
534 for (count = b_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
535 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
536 accu += (mp_twolimb_t) *sourceptr++ << s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
537 *destptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
538 accu = accu >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
539 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
540 /* accu must be zero, since that was how s was determined. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
541 if (accu != 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
542 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
543 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
544 b_ptr = tmp_roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
545 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
546 /* Copy a, shifting it left by s bits, yields r.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
547 Memory layout:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
548 At the beginning: r = roomptr[0..a_len],
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
549 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
550 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
551 if (s == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
552 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
553 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
554 r_ptr[a_len] = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
555 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
556 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
557 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
558 const mp_limb_t *sourceptr = a_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
559 mp_limb_t *destptr = r_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
560 mp_twolimb_t accu = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
561 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
562 for (count = a_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
563 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
564 accu += (mp_twolimb_t) *sourceptr++ << s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
565 *destptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
566 accu = accu >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
567 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
568 *destptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
569 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
570 q_ptr = roomptr + b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
571 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
572 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
573 size_t j = a_len - b_len; /* m-n */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
574 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
575 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
576 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
577 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
578 /* Division loop, traversed m-n+1 times.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
579 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
580 for (;;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
581 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
582 mp_limb_t q_star;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
583 mp_limb_t c1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
584 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
585 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
586 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
587 mp_twolimb_t num =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
588 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
589 | r_ptr[j + b_len - 1];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
590 q_star = num / b_msd;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
591 c1 = num % b_msd;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
592 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
593 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
594 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
595 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
596 q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
597 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
598 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
599 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
600 {<= beta !}.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
601 If yes, jump directly to the subtraction loop.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
602 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
603 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
604 if (r_ptr[j + b_len] > b_msd
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
605 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
606 /* r[j+n] >= b[n-1]+1 or
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
607 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
608 carry. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
609 goto subtract;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
610 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
611 /* q_star = q*,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
612 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
613 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
614 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
615 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
616 mp_twolimb_t c3 = /* b[n-2] * q* */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
617 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
618 /* While c2 < c3, increase c2 and decrease c3.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
619 Consider c3-c2. While it is > 0, decrease it by
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
620 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
621 this can happen only twice. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
622 if (c3 > c2)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
623 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
624 q_star = q_star - 1; /* q* := q* - 1 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
625 if (c3 - c2 > b_msdd)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
626 q_star = q_star - 1; /* q* := q* - 1 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
627 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
628 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
629 if (q_star > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
630 subtract:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
631 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
632 /* Subtract r := r - b * q* * beta^j. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
633 mp_limb_t cr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
634 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
635 const mp_limb_t *sourceptr = b_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
636 mp_limb_t *destptr = r_ptr + j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
637 mp_twolimb_t carry = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
638 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
639 for (count = b_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
640 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
641 /* Here 0 <= carry <= q*. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
642 carry =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
643 carry
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
644 + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
645 + (mp_limb_t) ~(*destptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
646 /* Here 0 <= carry <= beta*q* + beta-1. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
647 *destptr++ = ~(mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
648 carry = carry >> GMP_LIMB_BITS; /* <= q* */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
649 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
650 cr = (mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
651 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
652 /* Subtract cr from r_ptr[j + b_len], then forget about
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
653 r_ptr[j + b_len]. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
654 if (cr > r_ptr[j + b_len])
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
655 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
656 /* Subtraction gave a carry. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
657 q_star = q_star - 1; /* q* := q* - 1 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
658 /* Add b back. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
659 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
660 const mp_limb_t *sourceptr = b_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
661 mp_limb_t *destptr = r_ptr + j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
662 mp_limb_t carry = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
663 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
664 for (count = b_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
665 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
666 mp_limb_t source1 = *sourceptr++;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
667 mp_limb_t source2 = *destptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
668 *destptr++ = source1 + source2 + carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
669 carry =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
670 (carry
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
671 ? source1 >= (mp_limb_t) ~source2
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
672 : source1 > (mp_limb_t) ~source2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
673 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
674 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
675 /* Forget about the carry and about r[j+n]. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
676 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
677 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
678 /* q* is determined. Store it as q[j]. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
679 q_ptr[j] = q_star;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
680 if (j == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
681 break;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
682 j--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
683 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
684 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
685 r_len = b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
686 /* Normalise q. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
687 if (q_ptr[q_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
688 q_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
689 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
690 b is shifted left by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
691 /* Shift r right by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
692 if (s > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
693 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
694 mp_limb_t ptr = r_ptr + r_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
695 mp_twolimb_t accu = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
696 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
697 for (count = r_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
698 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
699 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
700 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
701 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
702 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
703 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
704 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
705 /* Normalise r. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
706 while (r_len > 0 && r_ptr[r_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
707 r_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
708 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
709 /* Compare r << 1 with b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
710 if (r_len > b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
711 goto increment_q;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
712 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
713 size_t i;
8835
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
714 for (i = b_len;;)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
715 {
8835
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
716 mp_limb_t r_i =
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
717 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
718 | (i < r_len ? r_ptr[i] << 1 : 0);
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
719 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
720 if (r_i > b_i)
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
721 goto increment_q;
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
722 if (r_i < b_i)
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
723 goto keep_q;
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
724 if (i == 0)
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
725 break;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
726 i--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
727 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
728 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
729 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
730 /* q is odd. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
731 increment_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
732 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
733 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
734 for (i = 0; i < q_len; i++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
735 if (++(q_ptr[i]) != 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
736 goto keep_q;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
737 q_ptr[q_len++] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
738 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
739 keep_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
740 if (tmp_roomptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
741 free (tmp_roomptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
742 q->limbs = q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
743 q->nlimbs = q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
744 return roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
745 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
746
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
747 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
748 representation.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
749 Destroys the contents of a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
750 Return the allocated memory - containing the decimal digits in low-to-high
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
751 order, terminated with a NUL character - in case of success, NULL in case
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
752 of memory allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
753 static char *
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
754 convert_to_decimal (mpn_t a, size_t extra_zeroes)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
755 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
756 mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
757 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
758 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
759 size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
760 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
761 if (c_ptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
762 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
763 char *d_ptr = c_ptr;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
764 for (; extra_zeroes > 0; extra_zeroes--)
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
765 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
766 while (a_len > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
767 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
768 /* Divide a by 10^9, in-place. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
769 mp_limb_t remainder = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
770 mp_limb_t *ptr = a_ptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
771 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
772 for (count = a_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
773 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
774 mp_twolimb_t num =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
775 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
776 *ptr = num / 1000000000;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
777 remainder = num % 1000000000;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
778 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
779 /* Store the remainder as 9 decimal digits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
780 for (count = 9; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
781 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
782 *d_ptr++ = '0' + (remainder % 10);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
783 remainder = remainder / 10;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
784 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
785 /* Normalize a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
786 if (a_ptr[a_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
787 a_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
788 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
789 /* Remove leading zeroes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
790 while (d_ptr > c_ptr && d_ptr[-1] == '0')
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
791 d_ptr--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
792 /* But keep at least one zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
793 if (d_ptr == c_ptr)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
794 *d_ptr++ = '0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
795 /* Terminate the string. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
796 *d_ptr = '\0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
797 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
798 return c_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
799 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
800
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
801 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
802
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
803 /* Assuming x is finite and >= 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
804 write x as x = 2^e * m, where m is a bignum.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
805 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
806 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
807 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
808 decode_long_double (long double x, int *ep, mpn_t *mp)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
809 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
810 mpn_t m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
811 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
812 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
813 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
814
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
815 /* Allocate memory for result. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
816 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
817 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
818 if (m.limbs == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
819 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
820 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
821 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
822 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
823 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
824 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
825 latter is an integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
826 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
827 I'm not sure whether it's safe to cast a 'long double' value between
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
828 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
829 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
830 doesn't matter). */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
831 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
832 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
833 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
834 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
835 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
836 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
837 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
838 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
839 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
840 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
841 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
842 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
843 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
844 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
845 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
846 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
847 # else
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
848 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
849 mp_limb_t d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
850 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
851 d = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
852 y -= d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
853 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
854 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
855 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
856 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
857 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
858 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
859 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
860 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
861 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
862 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
863 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
864 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
865 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
866 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
867 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
868 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
869 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
870 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
871 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
872 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
873 }
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
874 #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
875 precision. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
876 if (!(y == 0.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
877 abort ();
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
878 #endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
879 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
880 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
881 m.nlimbs--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
882 *mp = m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
883 *ep = exp - LDBL_MANT_BIT;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
884 return m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
885 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
886
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
887 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
888
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
889 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
890
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
891 /* Assuming x is finite and >= 0:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
892 write x as x = 2^e * m, where m is a bignum.
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
893 Return the allocated memory in case of success, NULL in case of memory
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
894 allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
895 static void *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
896 decode_double (double x, int *ep, mpn_t *mp)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
897 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
898 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
899 int exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
900 double y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
901 size_t i;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
902
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
903 /* Allocate memory for result. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
904 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
905 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
906 if (m.limbs == NULL)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
907 return NULL;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
908 /* Split into exponential part and mantissa. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
909 y = frexp (x, &exp);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
910 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
911 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
912 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
913 latter is an integer. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
914 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
915 I'm not sure whether it's safe to cast a 'double' value between
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
916 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
917 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
918 doesn't matter). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
919 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
920 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
921 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
922 mp_limb_t hi, lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
923 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
924 hi = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
925 y -= hi;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
926 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
927 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
928 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
929 lo = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
930 y -= lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
931 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
932 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
933 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
934 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
935 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
936 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
937 mp_limb_t d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
938 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
939 d = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
940 y -= d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
941 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
942 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
943 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
944 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
945 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
946 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
947 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
948 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
949 mp_limb_t hi, lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
950 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
951 hi = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
952 y -= hi;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
953 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
954 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
955 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
956 lo = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
957 y -= lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
958 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
959 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
960 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
961 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
962 if (!(y == 0.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
963 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
964 /* Normalise. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
965 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
966 m.nlimbs--;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
967 *mp = m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
968 *ep = exp - DBL_MANT_BIT;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
969 return m.limbs;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
970 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
971
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
972 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
973
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
974 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
975 Returns the decimal representation of round (x * 10^n).
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
976 Return the allocated memory - containing the decimal digits in low-to-high
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
977 order, terminated with a NUL character - in case of success, NULL in case
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
978 of memory allocation failure. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
979 static char *
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
980 scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
981 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
982 int s;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
983 size_t extra_zeroes;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
984 unsigned int abs_n;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
985 unsigned int abs_s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
986 mp_limb_t *pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
987 size_t pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
988 unsigned int s_limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
989 unsigned int s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
990 mpn_t pow5;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
991 mpn_t z;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
992 void *z_memory;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
993 char *digits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
994
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
995 if (memory == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
996 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
997 /* x = 2^e * m, hence
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
998 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
999 = round (2^s * 5^n * m). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1000 s = e + n;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1001 extra_zeroes = 0;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1002 /* Factor out a common power of 10 if possible. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1003 if (s > 0 && n > 0)
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1004 {
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1005 extra_zeroes = (s < n ? s : n);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1006 s -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1007 n -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1008 }
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1009 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1010 Before converting to decimal, we need to compute
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1011 z = round (2^s * 5^n * m). */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1012 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1013 sign. 2.322 is slightly larger than log(5)/log(2). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1014 abs_n = (n >= 0 ? n : -n);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1015 abs_s = (s >= 0 ? s : -s);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1016 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1017 + abs_s / GMP_LIMB_BITS + 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1018 * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1019 if (pow5_ptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1020 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1021 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1022 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1023 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1024 /* Initialize with 1. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1025 pow5_ptr[0] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1026 pow5_len = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1027 /* Multiply with 5^|n|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1028 if (abs_n > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1029 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1030 static mp_limb_t const small_pow5[13 + 1] =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1031 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1032 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1033 48828125, 244140625, 1220703125
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1034 };
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1035 unsigned int n13;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1036 for (n13 = 0; n13 <= abs_n; n13 += 13)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1037 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1038 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1039 size_t j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1040 mp_twolimb_t carry = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1041 for (j = 0; j < pow5_len; j++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1042 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1043 mp_limb_t digit2 = pow5_ptr[j];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1044 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1045 pow5_ptr[j] = (mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1046 carry = carry >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1047 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1048 if (carry > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1049 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1050 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1051 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1052 s_limbs = abs_s / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1053 s_bits = abs_s % GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1054 if (n >= 0 ? s >= 0 : s <= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1055 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1056 /* Multiply with 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1057 if (s_bits > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1058 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1059 mp_limb_t *ptr = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1060 mp_twolimb_t accu = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1061 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1062 for (count = pow5_len; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1063 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1064 accu += (mp_twolimb_t) *ptr << s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1065 *ptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1066 accu = accu >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1067 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1068 if (accu > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1069 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1070 *ptr = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1071 pow5_len++;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1072 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1073 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1074 if (s_limbs > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1075 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1076 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1077 for (count = pow5_len; count > 0;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1078 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1079 count--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1080 pow5_ptr[s_limbs + count] = pow5_ptr[count];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1081 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1082 for (count = s_limbs; count > 0;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1083 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1084 count--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1085 pow5_ptr[count] = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1086 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1087 pow5_len += s_limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1088 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1089 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1090 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1091 if (n >= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1092 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1093 /* Multiply m with pow5. No division needed. */
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1094 z_memory = multiply (m, pow5, &z);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1095 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1096 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1097 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1098 /* Divide m by pow5 and round. */
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1099 z_memory = divide (m, pow5, &z);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1100 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1101 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1102 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1103 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1104 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1105 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1106 if (n >= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1107 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1108 /* n >= 0, s < 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1109 Multiply m with pow5, then divide by 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1110 mpn_t numerator;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1111 mpn_t denominator;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1112 void *tmp_memory;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1113 tmp_memory = multiply (m, pow5, &numerator);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1114 if (tmp_memory == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1115 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1116 free (pow5_ptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1117 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1118 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1119 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1120 /* Construct 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1121 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1122 mp_limb_t *ptr = pow5_ptr + pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1123 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1124 for (i = 0; i < s_limbs; i++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1125 ptr[i] = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1126 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1127 denominator.limbs = ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1128 denominator.nlimbs = s_limbs + 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1129 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1130 z_memory = divide (numerator, denominator, &z);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1131 free (tmp_memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1132 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1133 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1134 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1135 /* n < 0, s > 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1136 Multiply m with 2^s, then divide by pow5. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1137 mpn_t numerator;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1138 mp_limb_t *num_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1139 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1140 * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1141 if (num_ptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1142 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1143 free (pow5_ptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1144 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1145 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1146 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1147 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1148 mp_limb_t *destptr = num_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1149 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1150 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1151 for (i = 0; i < s_limbs; i++)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1152 *destptr++ = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1153 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1154 if (s_bits > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1155 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1156 const mp_limb_t *sourceptr = m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1157 mp_twolimb_t accu = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1158 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1159 for (count = m.nlimbs; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1160 {
9445
e0f2eeb1796a Fix output of floating-point values with large exponent.
Bruno Haible <bruno@clisp.org>
parents: 9442
diff changeset
1161 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1162 *destptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1163 accu = accu >> GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1164 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1165 if (accu > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1166 *destptr++ = (mp_limb_t) accu;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1167 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1168 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1169 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1170 const mp_limb_t *sourceptr = m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1171 size_t count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1172 for (count = m.nlimbs; count > 0; count--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1173 *destptr++ = *sourceptr++;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1174 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1175 numerator.limbs = num_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1176 numerator.nlimbs = destptr - num_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1177 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1178 z_memory = divide (numerator, pow5, &z);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1179 free (num_ptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1180 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1181 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1182 free (pow5_ptr);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1183 free (memory);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1184
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1185 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1186
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1187 if (z_memory == NULL)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1188 return NULL;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1189 digits = convert_to_decimal (z, extra_zeroes);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1190 free (z_memory);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1191 return digits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1192 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1193
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1194 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1195
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1196 /* Assuming x is finite and >= 0, and n is an integer:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1197 Returns the decimal representation of round (x * 10^n).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1198 Return the allocated memory - containing the decimal digits in low-to-high
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1199 order, terminated with a NUL character - in case of success, NULL in case
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1200 of memory allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1201 static char *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1202 scale10_round_decimal_long_double (long double x, int n)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1203 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1204 int e;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1205 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1206 void *memory = decode_long_double (x, &e, &m);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1207 return scale10_round_decimal_decoded (e, m, memory, n);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1208 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1209
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1210 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1211
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1212 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1213
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1214 /* Assuming x is finite and >= 0, and n is an integer:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1215 Returns the decimal representation of round (x * 10^n).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1216 Return the allocated memory - containing the decimal digits in low-to-high
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1217 order, terminated with a NUL character - in case of success, NULL in case
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1218 of memory allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1219 static char *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1220 scale10_round_decimal_double (double x, int n)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1221 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1222 int e;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1223 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1224 void *memory = decode_double (x, &e, &m);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1225 return scale10_round_decimal_decoded (e, m, memory, n);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1226 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1227
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1228 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1229
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1230 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1231
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1232 /* Assuming x is finite and > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1233 Return an approximation for n with 10^n <= x < 10^(n+1).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1234 The approximation is usually the right n, but may be off by 1 sometimes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1235 static int
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1236 floorlog10l (long double x)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1237 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1238 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1239 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1240 double z;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1241 double l;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1242
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1243 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1244 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1245 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1246 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1247 if (y == 0.0L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1248 return INT_MIN;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1249 if (y < 0.5L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1250 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1251 while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1252 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1253 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1254 exp -= GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1255 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1256 if (y < (1.0L / (1 << 16)))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1257 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1258 y *= 1.0L * (1 << 16);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1259 exp -= 16;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1260 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1261 if (y < (1.0L / (1 << 8)))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1262 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1263 y *= 1.0L * (1 << 8);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1264 exp -= 8;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1265 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1266 if (y < (1.0L / (1 << 4)))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1267 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1268 y *= 1.0L * (1 << 4);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1269 exp -= 4;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1270 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1271 if (y < (1.0L / (1 << 2)))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1272 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1273 y *= 1.0L * (1 << 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1274 exp -= 2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1275 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1276 if (y < (1.0L / (1 << 1)))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1277 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1278 y *= 1.0L * (1 << 1);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1279 exp -= 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1280 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1281 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1282 if (!(y >= 0.5L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1283 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1284 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1285 l = exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1286 z = y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1287 if (z < 0.70710678118654752444)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1288 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1289 z *= 1.4142135623730950488;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1290 l -= 0.5;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1291 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1292 if (z < 0.8408964152537145431)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1293 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1294 z *= 1.1892071150027210667;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1295 l -= 0.25;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1296 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1297 if (z < 0.91700404320467123175)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1298 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1299 z *= 1.0905077326652576592;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1300 l -= 0.125;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1301 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1302 if (z < 0.9576032806985736469)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1303 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1304 z *= 1.0442737824274138403;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1305 l -= 0.0625;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1306 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1307 /* Now 0.95 <= z <= 1.01. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1308 z = 1 - z;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1309 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1310 Four terms are enough to get an approximation with error < 10^-7. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1311 l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1312 /* Finally multiply with log(2)/log(10), yields an approximation for
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1313 log10(x). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1314 l *= 0.30102999566398119523;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1315 /* Round down to the next integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1316 return (int) l + (l < 0 ? -1 : 0);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1317 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1318
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1319 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1320
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1321 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1322
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1323 /* Assuming x is finite and > 0:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1324 Return an approximation for n with 10^n <= x < 10^(n+1).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1325 The approximation is usually the right n, but may be off by 1 sometimes. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1326 static int
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1327 floorlog10 (double x)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1328 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1329 int exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1330 double y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1331 double z;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1332 double l;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1333
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1334 /* Split into exponential part and mantissa. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1335 y = frexp (x, &exp);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1336 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1337 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1338 if (y == 0.0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1339 return INT_MIN;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1340 if (y < 0.5)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1341 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1342 while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1343 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1344 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1345 exp -= GMP_LIMB_BITS;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1346 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1347 if (y < (1.0 / (1 << 16)))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1348 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1349 y *= 1.0 * (1 << 16);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1350 exp -= 16;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1351 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1352 if (y < (1.0 / (1 << 8)))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1353 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1354 y *= 1.0 * (1 << 8);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1355 exp -= 8;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1356 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1357 if (y < (1.0 / (1 << 4)))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1358 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1359 y *= 1.0 * (1 << 4);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1360 exp -= 4;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1361 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1362 if (y < (1.0 / (1 << 2)))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1363 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1364 y *= 1.0 * (1 << 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1365 exp -= 2;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1366 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1367 if (y < (1.0 / (1 << 1)))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1368 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1369 y *= 1.0 * (1 << 1);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1370 exp -= 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1371 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1372 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1373 if (!(y >= 0.5 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1374 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1375 /* Compute an approximation for l = log2(x) = exp + log2(y). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1376 l = exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1377 z = y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1378 if (z < 0.70710678118654752444)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1379 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1380 z *= 1.4142135623730950488;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1381 l -= 0.5;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1382 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1383 if (z < 0.8408964152537145431)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1384 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1385 z *= 1.1892071150027210667;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1386 l -= 0.25;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1387 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1388 if (z < 0.91700404320467123175)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1389 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1390 z *= 1.0905077326652576592;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1391 l -= 0.125;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1392 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1393 if (z < 0.9576032806985736469)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1394 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1395 z *= 1.0442737824274138403;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1396 l -= 0.0625;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1397 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1398 /* Now 0.95 <= z <= 1.01. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1399 z = 1 - z;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1400 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1401 Four terms are enough to get an approximation with error < 10^-7. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1402 l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1403 /* Finally multiply with log(2)/log(10), yields an approximation for
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1404 log10(x). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1405 l *= 0.30102999566398119523;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1406 /* Round down to the next integer. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1407 return (int) l + (l < 0 ? -1 : 0);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1408 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1409
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1410 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1411
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1412 #endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1413
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1414 DCHAR_T *
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1415 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1416 const FCHAR_T *format, va_list args)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1417 {
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1418 DIRECTIVES d;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1419 arguments a;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1420
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1421 if (PRINTF_PARSE (format, &d, &a) < 0)
9438
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1422 /* errno is already set. */
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1423 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1424
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1425 #define CLEANUP() \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1426 free (d.dir); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1427 if (a.arg) \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1428 free (a.arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1429
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1430 if (PRINTF_FETCHARGS (args, &a) < 0)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1431 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1432 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1433 errno = EINVAL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1434 return NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1435 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1436
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1437 {
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1438 size_t buf_neededlength;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1439 TCHAR_T *buf;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1440 TCHAR_T *buf_malloced;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1441 const FCHAR_T *cp;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1442 size_t i;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1443 DIRECTIVE *dp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1444 /* Output string accumulator. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1445 DCHAR_T *result;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1446 size_t allocated;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1447 size_t length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1448
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1449 /* Allocate a small buffer that will hold a directive passed to
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1450 sprintf or snprintf. */
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1451 buf_neededlength =
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1452 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1453 #if HAVE_ALLOCA
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1454 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1455 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1456 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1457 buf_malloced = NULL;
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1458 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1459 else
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1460 #endif
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1461 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1462 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1463 if (size_overflow_p (buf_memsize))
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1464 goto out_of_memory_1;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1465 buf = (TCHAR_T *) malloc (buf_memsize);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1466 if (buf == NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1467 goto out_of_memory_1;
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1468 buf_malloced = buf;
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1469 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1470
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1471 if (resultbuf != NULL)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1472 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1473 result = resultbuf;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1474 allocated = *lengthp;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1475 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1476 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1477 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1478 result = NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1479 allocated = 0;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1480 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1481 length = 0;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1482 /* Invariants:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1483 result is either == resultbuf or == NULL or malloc-allocated.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1484 If length > 0, then result != NULL. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1485
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1486 /* Ensures that allocated >= needed. Aborts through a jump to
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1487 out_of_memory if needed is SIZE_MAX or otherwise too big. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1488 #define ENSURE_ALLOCATION(needed) \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1489 if ((needed) > allocated) \
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1490 { \
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1491 size_t memory_size; \
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1492 DCHAR_T *memory; \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1493 \
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1494 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1495 if ((needed) > allocated) \
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1496 allocated = (needed); \
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1497 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1498 if (size_overflow_p (memory_size)) \
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1499 goto out_of_memory; \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1500 if (result == resultbuf || result == NULL) \
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1501 memory = (DCHAR_T *) malloc (memory_size); \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1502 else \
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1503 memory = (DCHAR_T *) realloc (result, memory_size); \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1504 if (memory == NULL) \
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1505 goto out_of_memory; \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1506 if (result == resultbuf && length > 0) \
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1507 DCHAR_CPY (memory, result, length); \
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1508 result = memory; \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1509 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1510
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1511 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1512 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1513 if (cp != dp->dir_start)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1514 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1515 size_t n = dp->dir_start - cp;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1516 size_t augmented_length = xsum (length, n);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1517
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1518 ENSURE_ALLOCATION (augmented_length);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1519 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1520 need that the format string contains only ASCII characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1521 if FCHAR_T and DCHAR_T are not the same type. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1522 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1523 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1524 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1525 length = augmented_length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1526 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1527 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1528 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1529 do
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1530 result[length++] = (unsigned char) *cp++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1531 while (--n > 0);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1532 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1533 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1534 if (i == d.count)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1535 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1536
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1537 /* Execute a single directive. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1538 if (dp->conversion == '%')
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1539 {
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1540 size_t augmented_length;
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1541
4886
e4317f1f2e2c Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents: 4879
diff changeset
1542 if (!(dp->arg_index == ARG_NONE))
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1543 abort ();
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1544 augmented_length = xsum (length, 1);
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1545 ENSURE_ALLOCATION (augmented_length);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1546 result[length] = '%';
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1547 length = augmented_length;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1548 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1549 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1550 {
4886
e4317f1f2e2c Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents: 4879
diff changeset
1551 if (!(dp->arg_index != ARG_NONE))
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1552 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1553
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1554 if (dp->conversion == 'n')
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1555 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1556 switch (a.arg[dp->arg_index].type)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1557 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1558 case TYPE_COUNT_SCHAR_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1559 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1560 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1561 case TYPE_COUNT_SHORT_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1562 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1563 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1564 case TYPE_COUNT_INT_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1565 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1566 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1567 case TYPE_COUNT_LONGINT_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1568 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1569 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
1570 #if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1571 case TYPE_COUNT_LONGLONGINT_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1572 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1573 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1574 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1575 default:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1576 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1577 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1578 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1579 #if ENABLE_UNISTDIO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1580 /* The unistdio extensions. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1581 else if (dp->conversion == 'U')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1582 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1583 arg_type type = a.arg[dp->arg_index].type;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1584 int flags = dp->flags;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1585 int has_width;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1586 size_t width;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1587 int has_precision;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1588 size_t precision;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1589
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1590 has_width = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1591 width = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1592 if (dp->width_start != dp->width_end)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1593 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1594 if (dp->width_arg_index != ARG_NONE)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1595 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1596 int arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1597
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1598 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1599 abort ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1600 arg = a.arg[dp->width_arg_index].a.a_int;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1601 if (arg < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1602 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1603 /* "A negative field width is taken as a '-' flag
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1604 followed by a positive field width." */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1605 flags |= FLAG_LEFT;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1606 width = (unsigned int) (-arg);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1607 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1608 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1609 width = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1610 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1611 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1612 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1613 const FCHAR_T *digitp = dp->width_start;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1614
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1615 do
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1616 width = xsum (xtimes (width, 10), *digitp++ - '0');
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1617 while (digitp != dp->width_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1618 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1619 has_width = 1;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1620 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1621
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1622 has_precision = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1623 precision = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1624 if (dp->precision_start != dp->precision_end)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1625 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1626 if (dp->precision_arg_index != ARG_NONE)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1627 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1628 int arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1629
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1630 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1631 abort ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1632 arg = a.arg[dp->precision_arg_index].a.a_int;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1633 /* "A negative precision is taken as if the precision
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1634 were omitted." */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1635 if (arg >= 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1636 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1637 precision = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1638 has_precision = 1;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1639 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1640 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1641 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1642 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1643 const FCHAR_T *digitp = dp->precision_start + 1;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1644
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1645 precision = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1646 while (digitp != dp->precision_end)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1647 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1648 has_precision = 1;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1649 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1650 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1651
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1652 switch (type)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1653 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1654 case TYPE_U8_STRING:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1655 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1656 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1657 const uint8_t *arg_end;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1658 size_t characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1659
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1660 if (has_precision)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1661 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1662 /* Use only PRECISION characters, from the left. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1663 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1664 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1665 for (; precision > 0; precision--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1666 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1667 int count = u8_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1668 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1669 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1670 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1671 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1672 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1673 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1674 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1675 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1676 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1677 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1678 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1679 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1680 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1681 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1682 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1683 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1684 else if (has_width)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1685 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1686 /* Use the entire string, and count the number of
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1687 characters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1688 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1689 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1690 for (;;)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1691 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1692 int count = u8_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1693 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1694 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1695 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1696 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1697 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1698 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1699 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1700 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1701 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1702 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1703 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1704 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1705 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1706 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1707 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1708 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1709 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1710 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1711 /* Use the entire string. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1712 arg_end = arg + u8_strlen (arg);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1713 /* The number of characters doesn't matter. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1714 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1715 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1716
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1717 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1718 && !(dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1719 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1720 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1721 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1722 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1723 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1724 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1725
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1726 # if DCHAR_IS_UINT8_T
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1727 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1728 size_t n = arg_end - arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1729 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1730 DCHAR_CPY (result + length, arg, n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1731 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1732 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1733 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1734 { /* Convert. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1735 DCHAR_T *converted = result + length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1736 size_t converted_len = allocated - length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1737 # if DCHAR_IS_TCHAR
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1738 /* Convert from UTF-8 to locale encoding. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1739 if (u8_conv_to_encoding (locale_charset (),
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1740 iconveh_question_mark,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1741 arg, arg_end - arg, NULL,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1742 &converted, &converted_len)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1743 < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1744 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1745 /* Convert from UTF-8 to UTF-16/UTF-32. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1746 converted =
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1747 U8_TO_DCHAR (arg, arg_end - arg,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1748 converted, &converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1749 if (converted == NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1750 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1751 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1752 int saved_errno = errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1753 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1754 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1755 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1756 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1757 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1758 errno = saved_errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1759 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1760 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1761 if (converted != result + length)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1762 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1763 ENSURE_ALLOCATION (xsum (length, converted_len));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1764 DCHAR_CPY (result + length, converted, converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1765 free (converted);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1766 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1767 length += converted_len;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1768 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1769 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1770
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1771 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1772 && (dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1773 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1774 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1775 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1776 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1777 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1778 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1779 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1780 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1781
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1782 case TYPE_U16_STRING:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1783 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1784 const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1785 const uint16_t *arg_end;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1786 size_t characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1787
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1788 if (has_precision)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1789 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1790 /* Use only PRECISION characters, from the left. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1791 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1792 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1793 for (; precision > 0; precision--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1794 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1795 int count = u16_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1796 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1797 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1798 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1799 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1800 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1801 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1802 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1803 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1804 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1805 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1806 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1807 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1808 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1809 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1810 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1811 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1812 else if (has_width)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1813 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1814 /* Use the entire string, and count the number of
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1815 characters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1816 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1817 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1818 for (;;)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1819 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1820 int count = u16_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1821 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1822 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1823 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1824 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1825 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1826 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1827 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1828 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1829 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1830 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1831 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1832 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1833 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1834 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1835 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1836 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1837 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1838 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1839 /* Use the entire string. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1840 arg_end = arg + u16_strlen (arg);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1841 /* The number of characters doesn't matter. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1842 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1843 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1844
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1845 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1846 && !(dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1847 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1848 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1849 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1850 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1851 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1852 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1853
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1854 # if DCHAR_IS_UINT16_T
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1855 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1856 size_t n = arg_end - arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1857 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1858 DCHAR_CPY (result + length, arg, n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1859 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1860 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1861 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1862 { /* Convert. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1863 DCHAR_T *converted = result + length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1864 size_t converted_len = allocated - length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1865 # if DCHAR_IS_TCHAR
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1866 /* Convert from UTF-16 to locale encoding. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1867 if (u16_conv_to_encoding (locale_charset (),
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1868 iconveh_question_mark,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1869 arg, arg_end - arg, NULL,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1870 &converted, &converted_len)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1871 < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1872 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1873 /* Convert from UTF-16 to UTF-8/UTF-32. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1874 converted =
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1875 U16_TO_DCHAR (arg, arg_end - arg,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1876 converted, &converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1877 if (converted == NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1878 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1879 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1880 int saved_errno = errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1881 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1882 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1883 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1884 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1885 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1886 errno = saved_errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1887 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1888 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1889 if (converted != result + length)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1890 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1891 ENSURE_ALLOCATION (xsum (length, converted_len));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1892 DCHAR_CPY (result + length, converted, converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1893 free (converted);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1894 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1895 length += converted_len;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1896 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1897 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1898
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1899 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1900 && (dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1901 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1902 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1903 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1904 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1905 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1906 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1907 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1908 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1909
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1910 case TYPE_U32_STRING:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1911 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1912 const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1913 const uint32_t *arg_end;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1914 size_t characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1915
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1916 if (has_precision)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1917 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1918 /* Use only PRECISION characters, from the left. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1919 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1920 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1921 for (; precision > 0; precision--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1922 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1923 int count = u32_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1924 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1925 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1926 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1927 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1928 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1929 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1930 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1931 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1932 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1933 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1934 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1935 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1936 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1937 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1938 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1939 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1940 else if (has_width)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1941 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1942 /* Use the entire string, and count the number of
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1943 characters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1944 arg_end = arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1945 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1946 for (;;)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1947 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1948 int count = u32_strmblen (arg_end);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1949 if (count == 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1950 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1951 if (count < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1952 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1953 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1954 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1955 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1956 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1957 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1958 errno = EILSEQ;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1959 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1960 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1961 arg_end += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1962 characters++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1963 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1964 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1965 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1966 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1967 /* Use the entire string. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1968 arg_end = arg + u32_strlen (arg);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1969 /* The number of characters doesn't matter. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1970 characters = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1971 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1972
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1973 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1974 && !(dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1975 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1976 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1977 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1978 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1979 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1980 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1981
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1982 # if DCHAR_IS_UINT32_T
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1983 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1984 size_t n = arg_end - arg;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1985 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1986 DCHAR_CPY (result + length, arg, n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1987 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1988 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1989 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1990 { /* Convert. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1991 DCHAR_T *converted = result + length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1992 size_t converted_len = allocated - length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1993 # if DCHAR_IS_TCHAR
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1994 /* Convert from UTF-32 to locale encoding. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1995 if (u32_conv_to_encoding (locale_charset (),
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1996 iconveh_question_mark,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1997 arg, arg_end - arg, NULL,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1998 &converted, &converted_len)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1999 < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2000 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2001 /* Convert from UTF-32 to UTF-8/UTF-16. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2002 converted =
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2003 U32_TO_DCHAR (arg, arg_end - arg,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2004 converted, &converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2005 if (converted == NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2006 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2007 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2008 int saved_errno = errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2009 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2010 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2011 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2012 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2013 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2014 errno = saved_errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2015 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2016 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2017 if (converted != result + length)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2018 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2019 ENSURE_ALLOCATION (xsum (length, converted_len));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2020 DCHAR_CPY (result + length, converted, converted_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2021 free (converted);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2022 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2023 length += converted_len;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2024 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2025 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2026
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2027 if (has_width && width > characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2028 && (dp->flags & FLAG_LEFT))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2029 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2030 size_t n = width - characters;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2031 ENSURE_ALLOCATION (xsum (length, n));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2032 DCHAR_SET (result + length, ' ', n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2033 length += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2034 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2035 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2036 break;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2037
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2038 default:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2039 abort ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2040 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2041 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2042 #endif
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2043 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2044 else if ((dp->conversion == 'a' || dp->conversion == 'A')
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2045 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2046 && (0
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2047 # if NEED_PRINTF_DOUBLE
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2048 || a.arg[dp->arg_index].type == TYPE_DOUBLE
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2049 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2050 # if NEED_PRINTF_LONG_DOUBLE
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2051 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2052 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2053 )
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2054 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2055 )
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2056 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2057 arg_type type = a.arg[dp->arg_index].type;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2058 int flags = dp->flags;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2059 int has_width;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2060 size_t width;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2061 int has_precision;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2062 size_t precision;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2063 size_t tmp_length;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2064 DCHAR_T tmpbuf[700];
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2065 DCHAR_T *tmp;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2066 DCHAR_T *pad_ptr;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2067 DCHAR_T *p;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2068
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2069 has_width = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2070 width = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2071 if (dp->width_start != dp->width_end)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2072 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2073 if (dp->width_arg_index != ARG_NONE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2074 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2075 int arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2076
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2077 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2078 abort ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2079 arg = a.arg[dp->width_arg_index].a.a_int;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2080 if (arg < 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2081 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2082 /* "A negative field width is taken as a '-' flag
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2083 followed by a positive field width." */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2084 flags |= FLAG_LEFT;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2085 width = (unsigned int) (-arg);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2086 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2087 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2088 width = arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2089 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2090 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2091 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2092 const FCHAR_T *digitp = dp->width_start;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2093
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2094 do
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2095 width = xsum (xtimes (width, 10), *digitp++ - '0');
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2096 while (digitp != dp->width_end);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2097 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2098 has_width = 1;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2099 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2100
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2101 has_precision = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2102 precision = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2103 if (dp->precision_start != dp->precision_end)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2104 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2105 if (dp->precision_arg_index != ARG_NONE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2106 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2107 int arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2108
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2109 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2110 abort ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2111 arg = a.arg[dp->precision_arg_index].a.a_int;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2112 /* "A negative precision is taken as if the precision
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2113 were omitted." */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2114 if (arg >= 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2115 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2116 precision = arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2117 has_precision = 1;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2118 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2119 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2120 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2121 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2122 const FCHAR_T *digitp = dp->precision_start + 1;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2123
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2124 precision = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2125 while (digitp != dp->precision_end)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2126 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2127 has_precision = 1;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2128 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2129 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2130
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2131 /* Allocate a temporary buffer of sufficient size. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2132 if (type == TYPE_LONGDOUBLE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2133 tmp_length =
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2134 (unsigned int) ((LDBL_DIG + 1)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2135 * 0.831 /* decimal -> hexadecimal */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2136 )
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2137 + 1; /* turn floor into ceil */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2138 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2139 tmp_length =
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2140 (unsigned int) ((DBL_DIG + 1)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2141 * 0.831 /* decimal -> hexadecimal */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2142 )
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2143 + 1; /* turn floor into ceil */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2144 if (tmp_length < precision)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2145 tmp_length = precision;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2146 /* Account for sign, decimal point etc. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2147 tmp_length = xsum (tmp_length, 12);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2148
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2149 if (tmp_length < width)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2150 tmp_length = width;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2151
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2152 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2153
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2154 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2155 tmp = tmpbuf;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2156 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2157 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2158 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2159
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2160 if (size_overflow_p (tmp_memsize))
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2161 /* Overflow, would lead to out of memory. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2162 goto out_of_memory;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2163 tmp = (DCHAR_T *) malloc (tmp_memsize);
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2164 if (tmp == NULL)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2165 /* Out of memory. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2166 goto out_of_memory;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2167 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2168
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2169 pad_ptr = NULL;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2170 p = tmp;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2171 if (type == TYPE_LONGDOUBLE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2172 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2173 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2174 long double arg = a.arg[dp->arg_index].a.a_longdouble;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2175
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2176 if (isnanl (arg))
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2177 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2178 if (dp->conversion == 'A')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2179 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2180 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2181 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2182 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2183 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2184 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2185 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2186 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2187 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2188 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2189 int sign = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2190 DECL_LONG_DOUBLE_ROUNDING
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2191
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2192 BEGIN_LONG_DOUBLE_ROUNDING ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2193
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2194 if (signbit (arg)) /* arg < 0.0L or negative zero */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2195 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2196 sign = -1;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2197 arg = -arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2198 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2199
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2200 if (sign < 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2201 *p++ = '-';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2202 else if (flags & FLAG_SHOWSIGN)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2203 *p++ = '+';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2204 else if (flags & FLAG_SPACE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2205 *p++ = ' ';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2206
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2207 if (arg > 0.0L && arg + arg == arg)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2208 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2209 if (dp->conversion == 'A')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2210 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2211 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2212 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2213 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2214 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2215 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2216 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2217 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2218 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2219 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2220 int exponent;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2221 long double mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2222
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2223 if (arg > 0.0L)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2224 mantissa = printf_frexpl (arg, &exponent);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2225 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2226 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2227 exponent = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2228 mantissa = 0.0L;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2229 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2230
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2231 if (has_precision
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2232 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2233 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2234 /* Round the mantissa. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2235 long double tail = mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2236 size_t q;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2237
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2238 for (q = precision; ; q--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2239 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2240 int digit = (int) tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2241 tail -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2242 if (q == 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2243 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2244 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2245 tail = 1 - tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2246 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2247 tail = - tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2248 break;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2249 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2250 tail *= 16.0L;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2251 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2252 if (tail != 0.0L)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2253 for (q = precision; q > 0; q--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2254 tail *= 0.0625L;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2255 mantissa += tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2256 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2257
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2258 *p++ = '0';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2259 *p++ = dp->conversion - 'A' + 'X';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2260 pad_ptr = p;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2261 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2262 int digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2263
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2264 digit = (int) mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2265 mantissa -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2266 *p++ = '0' + digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2267 if ((flags & FLAG_ALT)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2268 || mantissa > 0.0L || precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2269 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2270 *p++ = decimal_point_char ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2271 /* This loop terminates because we assume
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2272 that FLT_RADIX is a power of 2. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2273 while (mantissa > 0.0L)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2274 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2275 mantissa *= 16.0L;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2276 digit = (int) mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2277 mantissa -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2278 *p++ = digit
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2279 + (digit < 10
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2280 ? '0'
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2281 : dp->conversion - 10);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2282 if (precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2283 precision--;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2284 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2285 while (precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2286 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2287 *p++ = '0';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2288 precision--;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2289 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2290 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2291 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2292 *p++ = dp->conversion - 'A' + 'P';
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2293 # if WIDE_CHAR_VERSION
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2294 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2295 static const wchar_t decimal_format[] =
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2296 { '%', '+', 'd', '\0' };
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2297 SNPRINTF (p, 6 + 1, decimal_format, exponent);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2298 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2299 while (*p != '\0')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2300 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2301 # else
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2302 if (sizeof (DCHAR_T) == 1)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2303 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2304 sprintf ((char *) p, "%+d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2305 while (*p != '\0')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2306 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2307 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2308 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2309 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2310 char expbuf[6 + 1];
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2311 const char *ep;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2312 sprintf (expbuf, "%+d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2313 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2314 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2315 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2316 # endif
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2317 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2318
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2319 END_LONG_DOUBLE_ROUNDING ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2320 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2321 # else
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2322 abort ();
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2323 # endif
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2324 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2325 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2326 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2327 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2328 double arg = a.arg[dp->arg_index].a.a_double;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2329
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2330 if (isnan (arg))
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2331 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2332 if (dp->conversion == 'A')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2333 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2334 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2335 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2336 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2337 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2338 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2339 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2340 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2341 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2342 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2343 int sign = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2344
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2345 if (signbit (arg)) /* arg < 0.0 or negative zero */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2346 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2347 sign = -1;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2348 arg = -arg;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2349 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2350
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2351 if (sign < 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2352 *p++ = '-';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2353 else if (flags & FLAG_SHOWSIGN)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2354 *p++ = '+';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2355 else if (flags & FLAG_SPACE)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2356 *p++ = ' ';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2357
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2358 if (arg > 0.0 && arg + arg == arg)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2359 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2360 if (dp->conversion == 'A')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2361 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2362 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2363 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2364 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2365 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2366 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2367 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2368 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2369 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2370 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2371 int exponent;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2372 double mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2373
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2374 if (arg > 0.0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2375 mantissa = printf_frexp (arg, &exponent);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2376 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2377 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2378 exponent = 0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2379 mantissa = 0.0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2380 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2381
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2382 if (has_precision
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2383 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2384 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2385 /* Round the mantissa. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2386 double tail = mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2387 size_t q;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2388
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2389 for (q = precision; ; q--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2390 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2391 int digit = (int) tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2392 tail -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2393 if (q == 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2394 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2395 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2396 tail = 1 - tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2397 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2398 tail = - tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2399 break;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2400 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2401 tail *= 16.0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2402 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2403 if (tail != 0.0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2404 for (q = precision; q > 0; q--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2405 tail *= 0.0625;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2406 mantissa += tail;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2407 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2408
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2409 *p++ = '0';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2410 *p++ = dp->conversion - 'A' + 'X';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2411 pad_ptr = p;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2412 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2413 int digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2414
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2415 digit = (int) mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2416 mantissa -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2417 *p++ = '0' + digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2418 if ((flags & FLAG_ALT)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2419 || mantissa > 0.0 || precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2420 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2421 *p++ = decimal_point_char ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2422 /* This loop terminates because we assume
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2423 that FLT_RADIX is a power of 2. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2424 while (mantissa > 0.0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2425 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2426 mantissa *= 16.0;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2427 digit = (int) mantissa;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2428 mantissa -= digit;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2429 *p++ = digit
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2430 + (digit < 10
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2431 ? '0'
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2432 : dp->conversion - 10);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2433 if (precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2434 precision--;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2435 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2436 while (precision > 0)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2437 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2438 *p++ = '0';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2439 precision--;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2440 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2441 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2442 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2443 *p++ = dp->conversion - 'A' + 'P';
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2444 # if WIDE_CHAR_VERSION
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2445 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2446 static const wchar_t decimal_format[] =
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2447 { '%', '+', 'd', '\0' };
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2448 SNPRINTF (p, 6 + 1, decimal_format, exponent);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2449 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2450 while (*p != '\0')
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2451 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2452 # else
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2453 if (sizeof (DCHAR_T) == 1)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2454 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2455 sprintf ((char *) p, "%+d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2456 while (*p != '\0')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2457 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2458 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2459 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2460 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2461 char expbuf[6 + 1];
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2462 const char *ep;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2463 sprintf (expbuf, "%+d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2464 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2465 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2466 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2467 # endif
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2468 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2469 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2470 # else
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2471 abort ();
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2472 # endif
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2473 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2474 /* The generated string now extends from tmp to p, with the
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2475 zero padding insertion point being at pad_ptr. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2476 if (has_width && p - tmp < width)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2477 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2478 size_t pad = width - (p - tmp);
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2479 DCHAR_T *end = p + pad;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2480
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2481 if (flags & FLAG_LEFT)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2482 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2483 /* Pad with spaces on the right. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2484 for (; pad > 0; pad--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2485 *p++ = ' ';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2486 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2487 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2488 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2489 /* Pad with zeroes. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2490 DCHAR_T *q = end;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2491
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2492 while (p > pad_ptr)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2493 *--q = *--p;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2494 for (; pad > 0; pad--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2495 *p++ = '0';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2496 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2497 else
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2498 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2499 /* Pad with spaces on the left. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2500 DCHAR_T *q = end;
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2501
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2502 while (p > tmp)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2503 *--q = *--p;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2504 for (; pad > 0; pad--)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2505 *p++ = ' ';
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2506 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2507
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2508 p = end;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2509 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2510
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2511 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2512 size_t count = p - tmp;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2513
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2514 if (count >= tmp_length)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2515 /* tmp_length was incorrectly calculated - fix the
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2516 code above! */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2517 abort ();
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2518
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2519 /* Make room for the result. */
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2520 if (count >= allocated - length)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2521 {
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2522 size_t n = xsum (length, count);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2523
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2524 ENSURE_ALLOCATION (n);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2525 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2526
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2527 /* Append the result. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2528 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2529 if (tmp != tmpbuf)
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2530 free (tmp);
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2531 length += count;
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2532 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2533 }
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2534 #endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2535 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2536 else if ((dp->conversion == 'f' || dp->conversion == 'F'
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2537 || dp->conversion == 'e' || dp->conversion == 'E'
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2538 || dp->conversion == 'g' || dp->conversion == 'G'
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
2539 || dp->conversion == 'a' || dp->conversion == 'A')
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2540 && (0
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2541 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2542 || a.arg[dp->arg_index].type == TYPE_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2543 # elif NEED_PRINTF_INFINITE_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2544 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2545 /* The systems (mingw) which produce wrong output
8865
9e203d8ade65 Stricter check for printf result for NaN arguments.
Bruno Haible <bruno@clisp.org>
parents: 8860
diff changeset
2546 for Inf, -Inf, and NaN also do so for -0.0.
9e203d8ade65 Stricter check for printf result for NaN arguments.
Bruno Haible <bruno@clisp.org>
parents: 8860
diff changeset
2547 Therefore we treat this case here as well. */
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2548 && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2549 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2550 # if NEED_PRINTF_LONG_DOUBLE
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2551 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2552 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2553 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
8865
9e203d8ade65 Stricter check for printf result for NaN arguments.
Bruno Haible <bruno@clisp.org>
parents: 8860
diff changeset
2554 /* Some systems produce wrong output for Inf,
9e203d8ade65 Stricter check for printf result for NaN arguments.
Bruno Haible <bruno@clisp.org>
parents: 8860
diff changeset
2555 -Inf, and NaN. */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2556 && is_infinitel (a.arg[dp->arg_index].a.a_longdouble))
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2557 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2558 ))
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2559 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2560 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2561 arg_type type = a.arg[dp->arg_index].type;
8846
6fe000202977 Fix a warning in last patch.
Bruno Haible <bruno@clisp.org>
parents: 8840
diff changeset
2562 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2563 int flags = dp->flags;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2564 int has_width;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2565 size_t width;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2566 int has_precision;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2567 size_t precision;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2568 size_t tmp_length;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2569 DCHAR_T tmpbuf[700];
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2570 DCHAR_T *tmp;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2571 DCHAR_T *pad_ptr;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2572 DCHAR_T *p;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2573
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2574 has_width = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2575 width = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2576 if (dp->width_start != dp->width_end)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2577 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2578 if (dp->width_arg_index != ARG_NONE)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2579 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2580 int arg;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2581
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2582 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2583 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2584 arg = a.arg[dp->width_arg_index].a.a_int;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2585 if (arg < 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2586 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2587 /* "A negative field width is taken as a '-' flag
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2588 followed by a positive field width." */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2589 flags |= FLAG_LEFT;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2590 width = (unsigned int) (-arg);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2591 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2592 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2593 width = arg;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2594 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2595 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2596 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2597 const FCHAR_T *digitp = dp->width_start;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2598
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2599 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2600 width = xsum (xtimes (width, 10), *digitp++ - '0');
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2601 while (digitp != dp->width_end);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2602 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2603 has_width = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2604 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2605
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2606 has_precision = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2607 precision = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2608 if (dp->precision_start != dp->precision_end)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2609 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2610 if (dp->precision_arg_index != ARG_NONE)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2611 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2612 int arg;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2613
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2614 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2615 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2616 arg = a.arg[dp->precision_arg_index].a.a_int;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2617 /* "A negative precision is taken as if the precision
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2618 were omitted." */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2619 if (arg >= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2620 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2621 precision = arg;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2622 has_precision = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2623 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2624 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2625 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2626 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2627 const FCHAR_T *digitp = dp->precision_start + 1;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2628
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2629 precision = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2630 while (digitp != dp->precision_end)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2631 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2632 has_precision = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2633 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2634 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2635
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2636 /* POSIX specifies the default precision to be 6 for %f, %F,
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2637 %e, %E, but not for %g, %G. Implementations appear to use
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2638 the same default precision also for %g, %G. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2639 if (!has_precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2640 precision = 6;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2641
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2642 /* Allocate a temporary buffer of sufficient size. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2643 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2644 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2645 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2646 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2647 # elif NEED_PRINTF_LONG_DOUBLE
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2648 tmp_length = LDBL_DIG + 1;
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2649 # elif NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2650 tmp_length = DBL_DIG + 1;
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2651 # else
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2652 tmp_length = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2653 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2654 if (tmp_length < precision)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2655 tmp_length = precision;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2656 # if NEED_PRINTF_LONG_DOUBLE
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2657 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2658 if (type == TYPE_LONGDOUBLE)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2659 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2660 if (dp->conversion == 'f' || dp->conversion == 'F')
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2661 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2662 long double arg = a.arg[dp->arg_index].a.a_longdouble;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2663 if (!(isnanl (arg) || arg + arg == arg))
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2664 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2665 /* arg is finite and nonzero. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2666 int exponent = floorlog10l (arg < 0 ? -arg : arg);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2667 if (exponent >= 0 && tmp_length < exponent + precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2668 tmp_length = exponent + precision;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2669 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2670 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2671 # endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2672 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2673 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2674 if (type == TYPE_DOUBLE)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2675 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2676 if (dp->conversion == 'f' || dp->conversion == 'F')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2677 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2678 double arg = a.arg[dp->arg_index].a.a_double;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2679 if (!(isnan (arg) || arg + arg == arg))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2680 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2681 /* arg is finite and nonzero. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2682 int exponent = floorlog10 (arg < 0 ? -arg : arg);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2683 if (exponent >= 0 && tmp_length < exponent + precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2684 tmp_length = exponent + precision;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2685 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2686 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2687 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2688 /* Account for sign, decimal point etc. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2689 tmp_length = xsum (tmp_length, 12);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2690
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2691 if (tmp_length < width)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2692 tmp_length = width;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2693
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2694 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2695
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2696 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2697 tmp = tmpbuf;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2698 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2699 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2700 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2701
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2702 if (size_overflow_p (tmp_memsize))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2703 /* Overflow, would lead to out of memory. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2704 goto out_of_memory;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
2705 tmp = (DCHAR_T *) malloc (tmp_memsize);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2706 if (tmp == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2707 /* Out of memory. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2708 goto out_of_memory;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2709 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2710
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2711 pad_ptr = NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2712 p = tmp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2713
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2714 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
2715 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2716 if (type == TYPE_LONGDOUBLE)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2717 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2718 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2719 long double arg = a.arg[dp->arg_index].a.a_longdouble;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2720
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2721 if (isnanl (arg))
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2722 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2723 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2724 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2725 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2726 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2727 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2728 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2729 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2730 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2731 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2732 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2733 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2734 int sign = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2735 DECL_LONG_DOUBLE_ROUNDING
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2736
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2737 BEGIN_LONG_DOUBLE_ROUNDING ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2738
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2739 if (signbit (arg)) /* arg < 0.0L or negative zero */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2740 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2741 sign = -1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2742 arg = -arg;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2743 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2744
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2745 if (sign < 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2746 *p++ = '-';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2747 else if (flags & FLAG_SHOWSIGN)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2748 *p++ = '+';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2749 else if (flags & FLAG_SPACE)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2750 *p++ = ' ';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2751
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2752 if (arg > 0.0L && arg + arg == arg)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2753 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2754 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2755 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2756 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2757 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2758 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2759 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2760 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2761 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2762 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2763 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2764 {
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2765 # if NEED_PRINTF_LONG_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2766 pad_ptr = p;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2767
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2768 if (dp->conversion == 'f' || dp->conversion == 'F')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2769 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2770 char *digits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2771 size_t ndigits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2772
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2773 digits =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2774 scale10_round_decimal_long_double (arg, precision);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2775 if (digits == NULL)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2776 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2777 END_LONG_DOUBLE_ROUNDING ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2778 goto out_of_memory;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2779 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2780 ndigits = strlen (digits);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2781
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2782 if (ndigits > precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2783 do
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2784 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2785 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2786 *p++ = digits[ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2787 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2788 while (ndigits > precision);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2789 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2790 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2791 /* Here ndigits <= precision. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2792 if ((flags & FLAG_ALT) || precision > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2793 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2794 *p++ = decimal_point_char ();
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2795 for (; precision > ndigits; precision--)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2796 *p++ = '0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2797 while (ndigits > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2798 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2799 --ndigits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2800 *p++ = digits[ndigits];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2801 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2802 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2803
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2804 free (digits);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2805 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2806 else if (dp->conversion == 'e' || dp->conversion == 'E')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2807 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2808 int exponent;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2809
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2810 if (arg == 0.0L)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2811 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2812 exponent = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2813 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2814 if ((flags & FLAG_ALT) || precision > 0)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2815 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2816 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2817 for (; precision > 0; precision--)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2818 *p++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2819 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2820 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2821 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2822 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2823 /* arg > 0.0L. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2824 int adjusted;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2825 char *digits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2826 size_t ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2827
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2828 exponent = floorlog10l (arg);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2829 adjusted = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2830 for (;;)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2831 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2832 digits =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2833 scale10_round_decimal_long_double (arg,
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2834 (int)precision - exponent);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2835 if (digits == NULL)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2836 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2837 END_LONG_DOUBLE_ROUNDING ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2838 goto out_of_memory;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2839 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2840 ndigits = strlen (digits);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2841
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2842 if (ndigits == precision + 1)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2843 break;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2844 if (ndigits < precision
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2845 || ndigits > precision + 2)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2846 /* The exponent was not guessed
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2847 precisely enough. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2848 abort ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2849 if (adjusted)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2850 /* None of two values of exponent is
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2851 the right one. Prevent an endless
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2852 loop. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2853 abort ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2854 free (digits);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2855 if (ndigits == precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2856 exponent -= 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2857 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2858 exponent += 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2859 adjusted = 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2860 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2861
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2862 /* Here ndigits = precision+1. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2863 *p++ = digits[--ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2864 if ((flags & FLAG_ALT) || precision > 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2865 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2866 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2867 while (ndigits > 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2868 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2869 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2870 *p++ = digits[ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2871 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2872 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2873
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2874 free (digits);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2875 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2876
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2877 *p++ = dp->conversion; /* 'e' or 'E' */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
2878 # if WIDE_CHAR_VERSION
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2879 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2880 static const wchar_t decimal_format[] =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2881 { '%', '+', '.', '2', 'd', '\0' };
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2882 SNPRINTF (p, 6 + 1, decimal_format, exponent);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2883 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2884 while (*p != '\0')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2885 p++;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2886 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2887 if (sizeof (DCHAR_T) == 1)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2888 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2889 sprintf ((char *) p, "%+.2d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2890 while (*p != '\0')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2891 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2892 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2893 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2894 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2895 char expbuf[6 + 1];
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2896 const char *ep;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2897 sprintf (expbuf, "%+.2d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2898 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2899 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2900 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2901 # endif
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2902 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2903 else if (dp->conversion == 'g' || dp->conversion == 'G')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2904 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2905 if (precision == 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2906 precision = 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2907 /* precision >= 1. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2908
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2909 if (arg == 0.0L)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2910 /* The exponent is 0, >= -4, < precision.
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2911 Use fixed-point notation. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2912 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2913 size_t ndigits = precision;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2914 /* Number of trailing zeroes that have to be
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2915 dropped. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2916 size_t nzeroes =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2917 (flags & FLAG_ALT ? 0 : precision - 1);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2918
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2919 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2920 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2921 if ((flags & FLAG_ALT) || ndigits > nzeroes)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2922 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2923 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2924 while (ndigits > nzeroes)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2925 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2926 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2927 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2928 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2929 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2930 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2931 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2932 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2933 /* arg > 0.0L. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2934 int exponent;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2935 int adjusted;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2936 char *digits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2937 size_t ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2938 size_t nzeroes;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2939
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2940 exponent = floorlog10l (arg);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2941 adjusted = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2942 for (;;)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2943 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2944 digits =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2945 scale10_round_decimal_long_double (arg,
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2946 (int)(precision - 1) - exponent);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2947 if (digits == NULL)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2948 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2949 END_LONG_DOUBLE_ROUNDING ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2950 goto out_of_memory;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2951 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2952 ndigits = strlen (digits);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2953
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2954 if (ndigits == precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2955 break;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2956 if (ndigits < precision - 1
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2957 || ndigits > precision + 1)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2958 /* The exponent was not guessed
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2959 precisely enough. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2960 abort ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2961 if (adjusted)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2962 /* None of two values of exponent is
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2963 the right one. Prevent an endless
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2964 loop. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2965 abort ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2966 free (digits);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2967 if (ndigits < precision)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2968 exponent -= 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2969 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2970 exponent += 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2971 adjusted = 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2972 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2973 /* Here ndigits = precision. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2974
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2975 /* Determine the number of trailing zeroes
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2976 that have to be dropped. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2977 nzeroes = 0;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2978 if ((flags & FLAG_ALT) == 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2979 while (nzeroes < ndigits
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2980 && digits[nzeroes] == '0')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2981 nzeroes++;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2982
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2983 /* The exponent is now determined. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2984 if (exponent >= -4
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2985 && exponent < (long)precision)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
2986 {
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2987 /* Fixed-point notation:
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2988 max(exponent,0)+1 digits, then the
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2989 decimal point, then the remaining
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2990 digits without trailing zeroes. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2991 if (exponent >= 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2992 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2993 size_t count = exponent + 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2994 /* Note: count <= precision = ndigits. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2995 for (; count > 0; count--)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2996 *p++ = digits[--ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2997 if ((flags & FLAG_ALT) || ndigits > nzeroes)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2998 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
2999 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3000 while (ndigits > nzeroes)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3001 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3002 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3003 *p++ = digits[ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3004 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3005 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3006 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3007 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3008 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3009 size_t count = -exponent - 1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3010 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3011 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3012 for (; count > 0; count--)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3013 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3014 while (ndigits > nzeroes)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3015 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3016 --ndigits;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3017 *p++ = digits[ndigits];
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3018 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3019 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3020 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3021 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3022 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3023 /* Exponential notation. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3024 *p++ = digits[--ndigits];
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3025 if ((flags & FLAG_ALT) || ndigits > nzeroes)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3026 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3027 *p++ = decimal_point_char ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3028 while (ndigits > nzeroes)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3029 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3030 --ndigits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3031 *p++ = digits[ndigits];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3032 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3033 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3034 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3035 # if WIDE_CHAR_VERSION
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3036 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3037 static const wchar_t decimal_format[] =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3038 { '%', '+', '.', '2', 'd', '\0' };
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3039 SNPRINTF (p, 6 + 1, decimal_format, exponent);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3040 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3041 while (*p != '\0')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3042 p++;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3043 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3044 if (sizeof (DCHAR_T) == 1)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3045 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3046 sprintf ((char *) p, "%+.2d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3047 while (*p != '\0')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3048 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3049 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3050 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3051 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3052 char expbuf[6 + 1];
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3053 const char *ep;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3054 sprintf (expbuf, "%+.2d", exponent);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3055 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3056 p++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3057 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3058 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3059 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3060
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3061 free (digits);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3062 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3063 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3064 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3065 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3066 # else
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3067 /* arg is finite. */
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3068 abort ();
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3069 # endif
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3070 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3071
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3072 END_LONG_DOUBLE_ROUNDING ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3073 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3074 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3075 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3076 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3077 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3078 # endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3079 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3080 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3081 double arg = a.arg[dp->arg_index].a.a_double;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3082
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3083 if (isnan (arg))
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3084 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3085 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3086 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3087 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3088 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3089 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3090 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3091 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3092 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3093 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3094 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3095 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3096 int sign = 0;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3097
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3098 if (signbit (arg)) /* arg < 0.0 or negative zero */
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3099 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3100 sign = -1;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3101 arg = -arg;
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3102 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3103
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3104 if (sign < 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3105 *p++ = '-';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3106 else if (flags & FLAG_SHOWSIGN)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3107 *p++ = '+';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3108 else if (flags & FLAG_SPACE)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3109 *p++ = ' ';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3110
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3111 if (arg > 0.0 && arg + arg == arg)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3112 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3113 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3114 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3115 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3116 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3117 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3118 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3119 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3120 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3121 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3122 else
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3123 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3124 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3125 pad_ptr = p;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3126
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3127 if (dp->conversion == 'f' || dp->conversion == 'F')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3128 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3129 char *digits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3130 size_t ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3131
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3132 digits =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3133 scale10_round_decimal_double (arg, precision);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3134 if (digits == NULL)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3135 goto out_of_memory;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3136 ndigits = strlen (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3137
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3138 if (ndigits > precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3139 do
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3140 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3141 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3142 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3143 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3144 while (ndigits > precision);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3145 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3146 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3147 /* Here ndigits <= precision. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3148 if ((flags & FLAG_ALT) || precision > 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3149 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3150 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3151 for (; precision > ndigits; precision--)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3152 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3153 while (ndigits > 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3154 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3155 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3156 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3157 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3158 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3159
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3160 free (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3161 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3162 else if (dp->conversion == 'e' || dp->conversion == 'E')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3163 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3164 int exponent;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3165
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3166 if (arg == 0.0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3167 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3168 exponent = 0;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3169 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3170 if ((flags & FLAG_ALT) || precision > 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3171 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3172 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3173 for (; precision > 0; precision--)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3174 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3175 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3176 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3177 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3178 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3179 /* arg > 0.0. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3180 int adjusted;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3181 char *digits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3182 size_t ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3183
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3184 exponent = floorlog10 (arg);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3185 adjusted = 0;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3186 for (;;)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3187 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3188 digits =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3189 scale10_round_decimal_double (arg,
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3190 (int)precision - exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3191 if (digits == NULL)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3192 goto out_of_memory;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3193 ndigits = strlen (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3194
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3195 if (ndigits == precision + 1)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3196 break;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3197 if (ndigits < precision
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3198 || ndigits > precision + 2)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3199 /* The exponent was not guessed
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3200 precisely enough. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3201 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3202 if (adjusted)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3203 /* None of two values of exponent is
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3204 the right one. Prevent an endless
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3205 loop. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3206 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3207 free (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3208 if (ndigits == precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3209 exponent -= 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3210 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3211 exponent += 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3212 adjusted = 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3213 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3214
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3215 /* Here ndigits = precision+1. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3216 *p++ = digits[--ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3217 if ((flags & FLAG_ALT) || precision > 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3218 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3219 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3220 while (ndigits > 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3221 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3222 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3223 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3224 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3225 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3226
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3227 free (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3228 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3229
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3230 *p++ = dp->conversion; /* 'e' or 'E' */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3231 # if WIDE_CHAR_VERSION
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3232 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3233 static const wchar_t decimal_format[] =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3234 /* Produce the same number of exponent digits
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3235 as the native printf implementation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3236 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3237 { '%', '+', '.', '3', 'd', '\0' };
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3238 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3239 { '%', '+', '.', '2', 'd', '\0' };
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3240 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3241 SNPRINTF (p, 6 + 1, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3242 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3243 while (*p != '\0')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3244 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3245 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3246 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3247 static const char decimal_format[] =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3248 /* Produce the same number of exponent digits
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3249 as the native printf implementation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3250 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3251 "%+.3d";
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3252 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3253 "%+.2d";
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3254 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3255 if (sizeof (DCHAR_T) == 1)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3256 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3257 sprintf ((char *) p, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3258 while (*p != '\0')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3259 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3260 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3261 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3262 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3263 char expbuf[6 + 1];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3264 const char *ep;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3265 sprintf (expbuf, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3266 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3267 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3268 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3269 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3270 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3271 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3272 else if (dp->conversion == 'g' || dp->conversion == 'G')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3273 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3274 if (precision == 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3275 precision = 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3276 /* precision >= 1. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3277
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3278 if (arg == 0.0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3279 /* The exponent is 0, >= -4, < precision.
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3280 Use fixed-point notation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3281 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3282 size_t ndigits = precision;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3283 /* Number of trailing zeroes that have to be
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3284 dropped. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3285 size_t nzeroes =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3286 (flags & FLAG_ALT ? 0 : precision - 1);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3287
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3288 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3289 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3290 if ((flags & FLAG_ALT) || ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3291 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3292 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3293 while (ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3294 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3295 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3296 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3297 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3298 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3299 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3300 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3301 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3302 /* arg > 0.0. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3303 int exponent;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3304 int adjusted;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3305 char *digits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3306 size_t ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3307 size_t nzeroes;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3308
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3309 exponent = floorlog10 (arg);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3310 adjusted = 0;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3311 for (;;)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3312 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3313 digits =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3314 scale10_round_decimal_double (arg,
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3315 (int)(precision - 1) - exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3316 if (digits == NULL)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3317 goto out_of_memory;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3318 ndigits = strlen (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3319
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3320 if (ndigits == precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3321 break;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3322 if (ndigits < precision - 1
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3323 || ndigits > precision + 1)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3324 /* The exponent was not guessed
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3325 precisely enough. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3326 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3327 if (adjusted)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3328 /* None of two values of exponent is
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3329 the right one. Prevent an endless
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3330 loop. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3331 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3332 free (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3333 if (ndigits < precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3334 exponent -= 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3335 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3336 exponent += 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3337 adjusted = 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3338 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3339 /* Here ndigits = precision. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3340
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3341 /* Determine the number of trailing zeroes
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3342 that have to be dropped. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3343 nzeroes = 0;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3344 if ((flags & FLAG_ALT) == 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3345 while (nzeroes < ndigits
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3346 && digits[nzeroes] == '0')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3347 nzeroes++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3348
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3349 /* The exponent is now determined. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3350 if (exponent >= -4
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3351 && exponent < (long)precision)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3352 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3353 /* Fixed-point notation:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3354 max(exponent,0)+1 digits, then the
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3355 decimal point, then the remaining
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3356 digits without trailing zeroes. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3357 if (exponent >= 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3358 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3359 size_t count = exponent + 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3360 /* Note: count <= precision = ndigits. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3361 for (; count > 0; count--)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3362 *p++ = digits[--ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3363 if ((flags & FLAG_ALT) || ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3364 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3365 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3366 while (ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3367 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3368 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3369 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3370 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3371 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3372 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3373 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3374 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3375 size_t count = -exponent - 1;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3376 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3377 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3378 for (; count > 0; count--)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3379 *p++ = '0';
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3380 while (ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3381 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3382 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3383 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3384 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3385 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3386 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3387 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3388 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3389 /* Exponential notation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3390 *p++ = digits[--ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3391 if ((flags & FLAG_ALT) || ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3392 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3393 *p++ = decimal_point_char ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3394 while (ndigits > nzeroes)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3395 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3396 --ndigits;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3397 *p++ = digits[ndigits];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3398 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3399 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3400 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3401 # if WIDE_CHAR_VERSION
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3402 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3403 static const wchar_t decimal_format[] =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3404 /* Produce the same number of exponent digits
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3405 as the native printf implementation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3406 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3407 { '%', '+', '.', '3', 'd', '\0' };
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3408 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3409 { '%', '+', '.', '2', 'd', '\0' };
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3410 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3411 SNPRINTF (p, 6 + 1, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3412 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3413 while (*p != '\0')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3414 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3415 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3416 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3417 static const char decimal_format[] =
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3418 /* Produce the same number of exponent digits
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3419 as the native printf implementation. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3420 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3421 "%+.3d";
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3422 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3423 "%+.2d";
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3424 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3425 if (sizeof (DCHAR_T) == 1)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3426 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3427 sprintf ((char *) p, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3428 while (*p != '\0')
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3429 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3430 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3431 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3432 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3433 char expbuf[6 + 1];
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3434 const char *ep;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3435 sprintf (expbuf, decimal_format, exponent);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3436 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3437 p++;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3438 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3439 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3440 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3441 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3443 free (digits);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3444 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3445 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3446 else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3447 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3448 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3449 /* arg is finite. */
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3450 if (!(arg == 0.0))
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3451 abort ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3452
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3453 pad_ptr = p;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3454
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3455 if (dp->conversion == 'f' || dp->conversion == 'F')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3456 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3457 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3458 if ((flags & FLAG_ALT) || precision > 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3459 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3460 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3461 for (; precision > 0; precision--)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3462 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3463 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3464 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3465 else if (dp->conversion == 'e' || dp->conversion == 'E')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3466 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3467 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3468 if ((flags & FLAG_ALT) || precision > 0)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3469 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3470 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3471 for (; precision > 0; precision--)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3472 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3473 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3474 *p++ = dp->conversion; /* 'e' or 'E' */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3475 *p++ = '+';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3476 /* Produce the same number of exponent digits as
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3477 the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3478 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3479 *p++ = '0';
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3480 # endif
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3481 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3482 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3483 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3484 else if (dp->conversion == 'g' || dp->conversion == 'G')
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3485 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3486 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3487 if (flags & FLAG_ALT)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3488 {
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3489 size_t ndigits =
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3490 (precision > 0 ? precision - 1 : 0);
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3491 *p++ = decimal_point_char ();
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3492 for (; ndigits > 0; --ndigits)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3493 *p++ = '0';
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3494 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3495 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3496 else
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3497 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3498 # endif
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3499 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3500 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3501 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3502 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3503
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3504 /* The generated string now extends from tmp to p, with the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3505 zero padding insertion point being at pad_ptr. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3506 if (has_width && p - tmp < width)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3507 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3508 size_t pad = width - (p - tmp);
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3509 DCHAR_T *end = p + pad;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3510
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3511 if (flags & FLAG_LEFT)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3512 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3513 /* Pad with spaces on the right. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3514 for (; pad > 0; pad--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3515 *p++ = ' ';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3516 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3517 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3518 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3519 /* Pad with zeroes. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3520 DCHAR_T *q = end;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3521
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3522 while (p > pad_ptr)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3523 *--q = *--p;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3524 for (; pad > 0; pad--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3525 *p++ = '0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3526 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3527 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3528 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3529 /* Pad with spaces on the left. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3530 DCHAR_T *q = end;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3531
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3532 while (p > tmp)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3533 *--q = *--p;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3534 for (; pad > 0; pad--)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3535 *p++ = ' ';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3536 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3537
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3538 p = end;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3539 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3540
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3541 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3542 size_t count = p - tmp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3543
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3544 if (count >= tmp_length)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3545 /* tmp_length was incorrectly calculated - fix the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3546 code above! */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3547 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3548
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3549 /* Make room for the result. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3550 if (count >= allocated - length)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3551 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3552 size_t n = xsum (length, count);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3553
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3554 ENSURE_ALLOCATION (n);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3555 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3556
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3557 /* Append the result. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3558 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3559 if (tmp != tmpbuf)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3560 free (tmp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3561 length += count;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3562 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3563 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3564 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3565 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3566 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3567 arg_type type = a.arg[dp->arg_index].type;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3568 int flags = dp->flags;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3569 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3570 int has_width;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3571 size_t width;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3572 #endif
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3573 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3574 int has_precision;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3575 size_t precision;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3576 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3577 #if NEED_PRINTF_UNBOUNDED_PRECISION
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3578 int prec_ourselves;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3579 #else
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3580 # define prec_ourselves 0
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3581 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3582 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3583 int pad_ourselves;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3584 #else
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3585 # define pad_ourselves 0
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3586 #endif
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3587 TCHAR_T *fbp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3588 unsigned int prefix_count;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3589 int prefixes[2];
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
3590 #if !USE_SNPRINTF
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3591 size_t tmp_length;
8962
aea67e2678d9 Tweak last patch.
Bruno Haible <bruno@clisp.org>
parents: 8961
diff changeset
3592 TCHAR_T tmpbuf[700];
aea67e2678d9 Tweak last patch.
Bruno Haible <bruno@clisp.org>
parents: 8961
diff changeset
3593 TCHAR_T *tmp;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3594 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3595
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3596 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3597 has_width = 0;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3598 width = 0;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3599 if (dp->width_start != dp->width_end)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3600 {
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3601 if (dp->width_arg_index != ARG_NONE)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3602 {
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3603 int arg;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3604
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3605 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3606 abort ();
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3607 arg = a.arg[dp->width_arg_index].a.a_int;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3608 if (arg < 0)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3609 {
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3610 /* "A negative field width is taken as a '-' flag
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3611 followed by a positive field width." */
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3612 flags |= FLAG_LEFT;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3613 width = (unsigned int) (-arg);
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3614 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3615 else
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3616 width = arg;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3617 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3618 else
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3619 {
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
3620 const FCHAR_T *digitp = dp->width_start;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3621
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3622 do
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3623 width = xsum (xtimes (width, 10), *digitp++ - '0');
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3624 while (digitp != dp->width_end);
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3625 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3626 has_width = 1;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3627 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3628 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3629
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3630 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3631 has_precision = 0;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3632 precision = 6;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3633 if (dp->precision_start != dp->precision_end)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3634 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3635 if (dp->precision_arg_index != ARG_NONE)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3636 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3637 int arg;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3638
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3639 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3640 abort ();
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3641 arg = a.arg[dp->precision_arg_index].a.a_int;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3642 /* "A negative precision is taken as if the precision
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3643 were omitted." */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3644 if (arg >= 0)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3645 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3646 precision = arg;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3647 has_precision = 1;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3648 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3649 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3650 else
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3651 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3652 const FCHAR_T *digitp = dp->precision_start + 1;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3653
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3654 precision = 0;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3655 while (digitp != dp->precision_end)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3656 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3657 has_precision = 1;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3658 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3659 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3660 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3661
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3662 #if !USE_SNPRINTF
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3663 /* Allocate a temporary buffer of sufficient size for calling
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3664 sprintf. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3665 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3666 switch (dp->conversion)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3667 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3668
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3669 case 'd': case 'i': case 'u':
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3670 # if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3671 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3672 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3673 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3674 * 0.30103 /* binary -> decimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3675 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3676 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3677 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3678 # endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3679 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3680 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3681 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3682 * 0.30103 /* binary -> decimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3683 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3684 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3685 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3686 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3687 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3688 * 0.30103 /* binary -> decimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3689 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3690 + 1; /* turn floor into ceil */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3691 if (tmp_length < precision)
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3692 tmp_length = precision;
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3693 /* Multiply by 2, as an estimate for FLAG_GROUP. */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3694 tmp_length = xsum (tmp_length, tmp_length);
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3695 /* Add 1, to account for a leading sign. */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3696 tmp_length = xsum (tmp_length, 1);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3697 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3698
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3699 case 'o':
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3700 # if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3701 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3702 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3703 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3704 * 0.333334 /* binary -> octal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3705 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3706 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3707 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3708 # endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3709 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3710 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3711 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3712 * 0.333334 /* binary -> octal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3713 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3714 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3715 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3716 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3717 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3718 * 0.333334 /* binary -> octal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3719 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3720 + 1; /* turn floor into ceil */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3721 if (tmp_length < precision)
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3722 tmp_length = precision;
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3723 /* Add 1, to account for a leading sign. */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3724 tmp_length = xsum (tmp_length, 1);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3725 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3726
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3727 case 'x': case 'X':
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3728 # if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3729 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3730 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3731 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3732 * 0.25 /* binary -> hexadecimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3733 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3734 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3735 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3736 # endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3737 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3738 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3739 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3740 * 0.25 /* binary -> hexadecimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3741 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3742 + 1; /* turn floor into ceil */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3743 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3744 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3745 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3746 * 0.25 /* binary -> hexadecimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3747 )
6583
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3748 + 1; /* turn floor into ceil */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3749 if (tmp_length < precision)
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3750 tmp_length = precision;
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3751 /* Add 2, to account for a leading sign or alternate form. */
eb8d54cb67a4 Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3752 tmp_length = xsum (tmp_length, 2);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3753 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3754
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3755 case 'f': case 'F':
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3756 if (type == TYPE_LONGDOUBLE)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3757 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3758 (unsigned int) (LDBL_MAX_EXP
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3759 * 0.30103 /* binary -> decimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3760 * 2 /* estimate for FLAG_GROUP */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3761 )
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3762 + 1 /* turn floor into ceil */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3763 + 10; /* sign, decimal point etc. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3764 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3765 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3766 (unsigned int) (DBL_MAX_EXP
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3767 * 0.30103 /* binary -> decimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3768 * 2 /* estimate for FLAG_GROUP */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3769 )
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3770 + 1 /* turn floor into ceil */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3771 + 10; /* sign, decimal point etc. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3772 tmp_length = xsum (tmp_length, precision);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3773 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3774
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3775 case 'e': case 'E': case 'g': case 'G':
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3776 tmp_length =
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3777 12; /* sign, decimal point, exponent etc. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3778 tmp_length = xsum (tmp_length, precision);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3779 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3780
8254
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3781 case 'a': case 'A':
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3782 if (type == TYPE_LONGDOUBLE)
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3783 tmp_length =
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3784 (unsigned int) (LDBL_DIG
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3785 * 0.831 /* decimal -> hexadecimal */
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3786 )
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3787 + 1; /* turn floor into ceil */
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3788 else
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3789 tmp_length =
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3790 (unsigned int) (DBL_DIG
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3791 * 0.831 /* decimal -> hexadecimal */
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3792 )
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3793 + 1; /* turn floor into ceil */
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3794 if (tmp_length < precision)
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3795 tmp_length = precision;
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3796 /* Account for sign, decimal point etc. */
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3797 tmp_length = xsum (tmp_length, 12);
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3798 break;
bbea39ab5a58 Fix estimate of size needed for a 'a' or 'A' conversion.
Bruno Haible <bruno@clisp.org>
parents: 8051
diff changeset
3799
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3800 case 'c':
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3801 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3802 if (type == TYPE_WIDE_CHAR)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3803 tmp_length = MB_CUR_MAX;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3804 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3805 # endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3806 tmp_length = 1;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3807 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3808
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3809 case 's':
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3810 # if HAVE_WCHAR_T
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3811 if (type == TYPE_WIDE_STRING)
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3812 {
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3813 tmp_length =
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3814 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3815
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3816 # if !WIDE_CHAR_VERSION
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3817 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
3818 # endif
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3819 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3820 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3821 # endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3822 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3823 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3824
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3825 case 'p':
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3826 tmp_length =
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3827 (unsigned int) (sizeof (void *) * CHAR_BIT
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3828 * 0.25 /* binary -> hexadecimal */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3829 )
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3830 + 1 /* turn floor into ceil */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3831 + 2; /* account for leading 0x */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3832 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3833
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3834 default:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3835 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3836 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3837
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3838 # if ENABLE_UNISTDIO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3839 /* Padding considers the number of characters, therefore the
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3840 number of elements after padding may be
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3841 > max (tmp_length, width)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3842 but is certainly
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3843 <= tmp_length + width. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3844 tmp_length = xsum (tmp_length, width);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3845 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3846 /* Padding considers the number of elements, says POSIX. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3847 if (tmp_length < width)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3848 tmp_length = width;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3849 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3850
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3851 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3852 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3853
8962
aea67e2678d9 Tweak last patch.
Bruno Haible <bruno@clisp.org>
parents: 8961
diff changeset
3854 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3855 tmp = tmpbuf;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3856 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3857 {
8962
aea67e2678d9 Tweak last patch.
Bruno Haible <bruno@clisp.org>
parents: 8961
diff changeset
3858 size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3859
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3860 if (size_overflow_p (tmp_memsize))
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3861 /* Overflow, would lead to out of memory. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3862 goto out_of_memory;
8962
aea67e2678d9 Tweak last patch.
Bruno Haible <bruno@clisp.org>
parents: 8961
diff changeset
3863 tmp = (TCHAR_T *) malloc (tmp_memsize);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3864 if (tmp == NULL)
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3865 /* Out of memory. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
3866 goto out_of_memory;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3867 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3868 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3869
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3870 /* Decide whether to handle the precision ourselves. */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3871 #if NEED_PRINTF_UNBOUNDED_PRECISION
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3872 switch (dp->conversion)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3873 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3874 case 'd': case 'i': case 'u':
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3875 case 'o':
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3876 case 'x': case 'X': case 'p':
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3877 prec_ourselves = has_precision && (precision > 0);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3878 break;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3879 default:
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3880 prec_ourselves = 0;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3881 break;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3882 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3883 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3884
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3885 /* Decide whether to perform the padding ourselves. */
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3886 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3887 switch (dp->conversion)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3888 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3889 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3890 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3891 to perform the padding after this conversion. Functions
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3892 with unistdio extensions perform the padding based on
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3893 character count rather than element count. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3894 case 'c': case 's':
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3895 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3896 # if NEED_PRINTF_FLAG_ZERO
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3897 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3898 case 'a': case 'A':
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3899 # endif
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3900 pad_ourselves = 1;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3901 break;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3902 default:
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3903 pad_ourselves = prec_ourselves;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3904 break;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3905 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3906 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3907
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3908 /* Construct the format string for calling snprintf or
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3909 sprintf. */
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3910 fbp = buf;
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3911 *fbp++ = '%';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3912 #if NEED_PRINTF_FLAG_GROUPING
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3913 /* The underlying implementation doesn't support the ' flag.
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3914 Produce no grouping characters in this case; this is
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3915 acceptable because the grouping is locale dependent. */
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3916 #else
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3917 if (flags & FLAG_GROUP)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3918 *fbp++ = '\'';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
3919 #endif
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3920 if (flags & FLAG_LEFT)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3921 *fbp++ = '-';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3922 if (flags & FLAG_SHOWSIGN)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3923 *fbp++ = '+';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3924 if (flags & FLAG_SPACE)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3925 *fbp++ = ' ';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3926 if (flags & FLAG_ALT)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3927 *fbp++ = '#';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3928 if (!pad_ourselves)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3929 {
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3930 if (flags & FLAG_ZERO)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3931 *fbp++ = '0';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3932 if (dp->width_start != dp->width_end)
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3933 {
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3934 size_t n = dp->width_end - dp->width_start;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3935 /* The width specification is known to consist only
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3936 of standard ASCII characters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3937 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3938 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3939 memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3940 fbp += n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3941 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3942 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3943 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3944 const FCHAR_T *mp = dp->width_start;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3945 do
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3946 *fbp++ = (unsigned char) *mp++;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3947 while (--n > 0);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3948 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3949 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3950 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3951 if (!prec_ourselves)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3952 {
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3953 if (dp->precision_start != dp->precision_end)
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3954 {
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3955 size_t n = dp->precision_end - dp->precision_start;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3956 /* The precision specification is known to consist only
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3957 of standard ASCII characters. */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3958 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3959 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3960 memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3961 fbp += n;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3962 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3963 else
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3964 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3965 const FCHAR_T *mp = dp->precision_start;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3966 do
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3967 *fbp++ = (unsigned char) *mp++;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3968 while (--n > 0);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
3969 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3970 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3971 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3972
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3973 switch (type)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3974 {
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3975 #if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3976 case TYPE_LONGLONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3977 case TYPE_ULONGLONGINT:
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3978 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3979 *fbp++ = 'I';
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3980 *fbp++ = '6';
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3981 *fbp++ = '4';
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3982 break;
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3983 # else
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3984 *fbp++ = 'l';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3985 /*FALLTHROUGH*/
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
3986 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3987 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3988 case TYPE_LONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3989 case TYPE_ULONGINT:
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3990 #if HAVE_WINT_T
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3991 case TYPE_WIDE_CHAR:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3992 #endif
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
3993 #if HAVE_WCHAR_T
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3994 case TYPE_WIDE_STRING:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3995 #endif
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3996 *fbp++ = 'l';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3997 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3998 case TYPE_LONGDOUBLE:
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
3999 *fbp++ = 'L';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4000 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4001 default:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4002 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4003 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4004 #if NEED_PRINTF_DIRECTIVE_F
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4005 if (dp->conversion == 'F')
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4006 *fbp = 'f';
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4007 else
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4008 #endif
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4009 *fbp = dp->conversion;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4010 #if USE_SNPRINTF
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4011 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4012 fbp[1] = '%';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4013 fbp[2] = 'n';
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4014 fbp[3] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4015 # else
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4016 /* On glibc2 systems from glibc >= 2.3 - probably also older
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4017 ones - we know that snprintf's returns value conforms to
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4018 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4019 Therefore we can avoid using %n in this situation.
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4020 On glibc2 systems from 2004-10-18 or newer, the use of %n
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4021 in format strings in writable memory may crash the program
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4022 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4023 in this situation. */
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4024 fbp[1] = '\0';
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4025 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4026 #else
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4027 fbp[1] = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4028 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4029
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4030 /* Construct the arguments for calling snprintf or sprintf. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4031 prefix_count = 0;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4032 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4033 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4034 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4035 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4036 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4037 }
4886
e4317f1f2e2c Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents: 4879
diff changeset
4038 if (dp->precision_arg_index != ARG_NONE)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4039 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4040 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4041 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4042 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4043 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4044
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4045 #if USE_SNPRINTF
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4046 /* The SNPRINTF result is appended after result[0..length].
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4047 The latter is an array of DCHAR_T; SNPRINTF appends an
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4048 array of TCHAR_T to it. This is possible because
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4049 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4050 alignof (TCHAR_T) <= alignof (DCHAR_T). */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4051 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
9462
7bbddc0fae4a Document and work around a BeOS snprintf() bug.
Bruno Haible <bruno@clisp.org>
parents: 9461
diff changeset
4052 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
7bbddc0fae4a Document and work around a BeOS snprintf() bug.
Bruno Haible <bruno@clisp.org>
parents: 9461
diff changeset
4053 where an snprintf() with maxlen==1 acts like sprintf(). */
7bbddc0fae4a Document and work around a BeOS snprintf() bug.
Bruno Haible <bruno@clisp.org>
parents: 9461
diff changeset
4054 ENSURE_ALLOCATION (xsum (length,
7bbddc0fae4a Document and work around a BeOS snprintf() bug.
Bruno Haible <bruno@clisp.org>
parents: 9461
diff changeset
4055 (2 + TCHARS_PER_DCHAR - 1)
7bbddc0fae4a Document and work around a BeOS snprintf() bug.
Bruno Haible <bruno@clisp.org>
parents: 9461
diff changeset
4056 / TCHARS_PER_DCHAR));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4057 /* Prepare checking whether snprintf returns the count
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4058 via %n. */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4059 *(TCHAR_T *) (result + length) = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4060 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4061
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4062 for (;;)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4063 {
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4064 int count = -1;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4065
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4066 #if USE_SNPRINTF
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4067 int retcount = 0;
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4068 size_t maxlen = allocated - length;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4069 /* SNPRINTF can fail if its second argument is
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4070 > INT_MAX. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4071 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
9371
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4072 maxlen = INT_MAX / TCHARS_PER_DCHAR;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4073 maxlen = maxlen * TCHARS_PER_DCHAR;
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4074 # define SNPRINTF_BUF(arg) \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4075 switch (prefix_count) \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4076 { \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4077 case 0: \
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4078 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4079 maxlen, buf, \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4080 arg, &count); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4081 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4082 case 1: \
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4083 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4084 maxlen, buf, \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4085 prefixes[0], arg, &count); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4086 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4087 case 2: \
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4088 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4089 maxlen, buf, \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4090 prefixes[0], prefixes[1], arg, \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4091 &count); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4092 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4093 default: \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4094 abort (); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4095 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4096 #else
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4097 # define SNPRINTF_BUF(arg) \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4098 switch (prefix_count) \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4099 { \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4100 case 0: \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4101 count = sprintf (tmp, buf, arg); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4102 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4103 case 1: \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4104 count = sprintf (tmp, buf, prefixes[0], arg); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4105 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4106 case 2: \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4107 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4108 arg); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4109 break; \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4110 default: \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4111 abort (); \
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4112 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4113 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4114
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4115 switch (type)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4116 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4117 case TYPE_SCHAR:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4118 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4119 int arg = a.arg[dp->arg_index].a.a_schar;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4120 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4121 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4122 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4123 case TYPE_UCHAR:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4124 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4125 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4126 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4127 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4128 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4129 case TYPE_SHORT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4130 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4131 int arg = a.arg[dp->arg_index].a.a_short;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4132 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4133 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4134 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4135 case TYPE_USHORT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4136 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4137 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4138 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4139 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4140 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4141 case TYPE_INT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4142 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4143 int arg = a.arg[dp->arg_index].a.a_int;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4144 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4145 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4146 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4147 case TYPE_UINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4148 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4149 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4150 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4151 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4152 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4153 case TYPE_LONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4154 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4155 long int arg = a.arg[dp->arg_index].a.a_longint;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4156 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4157 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4158 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4159 case TYPE_ULONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4160 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4161 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4162 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4163 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4164 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4165 #if HAVE_LONG_LONG_INT
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4166 case TYPE_LONGLONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4167 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4168 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4169 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4170 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4171 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4172 case TYPE_ULONGLONGINT:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4173 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4174 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4175 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4176 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4177 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4178 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4179 case TYPE_DOUBLE:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4180 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4181 double arg = a.arg[dp->arg_index].a.a_double;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4182 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4183 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4184 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4185 case TYPE_LONGDOUBLE:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4186 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4187 long double arg = a.arg[dp->arg_index].a.a_longdouble;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4188 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4189 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4190 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4191 case TYPE_CHAR:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4192 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4193 int arg = a.arg[dp->arg_index].a.a_char;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4194 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4195 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4196 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4197 #if HAVE_WINT_T
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4198 case TYPE_WIDE_CHAR:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4199 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4200 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4201 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4202 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4203 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4204 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4205 case TYPE_STRING:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4206 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4207 const char *arg = a.arg[dp->arg_index].a.a_string;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4208 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4209 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4210 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4211 #if HAVE_WCHAR_T
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4212 case TYPE_WIDE_STRING:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4213 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4214 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4215 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4216 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4217 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4218 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4219 case TYPE_POINTER:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4220 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4221 void *arg = a.arg[dp->arg_index].a.a_pointer;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4222 SNPRINTF_BUF (arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4223 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4224 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4225 default:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4226 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4227 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4228
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4229 #if USE_SNPRINTF
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4230 /* Portability: Not all implementations of snprintf()
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4231 are ISO C 99 compliant. Determine the number of
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4232 bytes that snprintf() has produced or would have
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4233 produced. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4234 if (count >= 0)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4235 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4236 /* Verify that snprintf() has NUL-terminated its
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4237 result. */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4238 if (count < maxlen
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4239 && ((TCHAR_T *) (result + length)) [count] != '\0')
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4240 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4241 /* Portability hack. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4242 if (retcount > count)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4243 count = retcount;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4244 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4245 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4246 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4247 /* snprintf() doesn't understand the '%n'
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4248 directive. */
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4249 if (fbp[1] != '\0')
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4250 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4251 /* Don't use the '%n' directive; instead, look
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4252 at the snprintf() return value. */
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4253 fbp[1] = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4254 continue;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4255 }
4801
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4256 else
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4257 {
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4258 /* Look at the snprintf() return value. */
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4259 if (retcount < 0)
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4260 {
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4261 /* HP-UX 10.20 snprintf() is doubly deficient:
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4262 It doesn't understand the '%n' directive,
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4263 *and* it returns -1 (rather than the length
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4264 that would have been required) when the
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4265 buffer is too small. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4266 size_t bigger_need =
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4267 xsum (xtimes (allocated, 2), 12);
4801
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4268 ENSURE_ALLOCATION (bigger_need);
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4269 continue;
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4270 }
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4271 else
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4272 count = retcount;
83746ec4d74a Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents: 4704
diff changeset
4273 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4274 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4275 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4276
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4277 /* Attempt to handle failure. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4278 if (count < 0)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4279 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4280 if (!(result == resultbuf || result == NULL))
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4281 free (result);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4282 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4283 free (buf_malloced);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4284 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4285 errno = EINVAL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4286 return NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4287 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4288
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4289 #if USE_SNPRINTF
9439
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4290 /* Handle overflow of the allocated buffer.
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4291 If such an overflow occurs, a C99 compliant snprintf()
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4292 returns a count >= maxlen. However, a non-compliant
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4293 snprintf() function returns only count = maxlen - 1. To
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4294 cover both cases, test whether count >= maxlen - 1. */
9d292d1d891c Fix detection of overflow: don't assume that snprintf is C99 compliant.
Bruno Haible <bruno@clisp.org>
parents: 9438
diff changeset
4295 if ((unsigned int) count + 1 >= maxlen)
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
4296 {
9371
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4297 /* If maxlen already has attained its allowed maximum,
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4298 allocating more memory will not increase maxlen.
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4299 Instead of looping, bail out. */
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4300 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4301 goto overflow;
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4302 else
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4303 {
9461
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4304 /* Need at least (count + 1) * sizeof (TCHAR_T)
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4305 bytes. (The +1 is for the trailing NUL.)
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4306 But ask for (count + 2) * sizeof (TCHAR_T)
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4307 bytes, so that in the next round, we likely get
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4308 maxlen > (unsigned int) count + 1
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4309 and so we don't get here again.
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4310 And allocate proportionally, to avoid looping
9371
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4311 eternally if snprintf() reports a too small
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4312 count. */
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4313 size_t n =
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4314 xmax (xsum (length,
9461
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4315 ((unsigned int) count + 2
c6d483077434 Fix inefficiency introduced on 2007-11-03.
Bruno Haible <bruno@clisp.org>
parents: 9454
diff changeset
4316 + TCHARS_PER_DCHAR - 1)
9371
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4317 / TCHARS_PER_DCHAR),
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4318 xtimes (allocated, 2));
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
4319
9371
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4320 ENSURE_ALLOCATION (n);
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4321 continue;
8b3c48038257 Don't report an unjustified overflow error.
Bruno Haible <bruno@clisp.org>
parents: 9361
diff changeset
4322 }
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4323 }
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
4324 #endif
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4325
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4326 #if NEED_PRINTF_UNBOUNDED_PRECISION
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4327 if (prec_ourselves)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4328 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4329 /* Handle the precision. */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4330 TCHAR_T *prec_ptr =
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4331 # if USE_SNPRINTF
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4332 (TCHAR_T *) (result + length);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4333 # else
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4334 tmp;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4335 # endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4336 size_t prefix_count;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4337 size_t move;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4338
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4339 prefix_count = 0;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4340 /* Put the additional zeroes after the sign. */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4341 if (count >= 1
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4342 && (*prec_ptr == '-' || *prec_ptr == '+'
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4343 || *prec_ptr == ' '))
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4344 prefix_count = 1;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4345 /* Put the additional zeroes after the 0x prefix if
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4346 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4347 else if (count >= 2
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4348 && prec_ptr[0] == '0'
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4349 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4350 prefix_count = 2;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4351
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4352 move = count - prefix_count;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4353 if (precision > move)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4354 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4355 /* Insert zeroes. */
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4356 size_t insert = precision - move;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4357 TCHAR_T *prec_end;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4358
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4359 # if USE_SNPRINTF
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4360 size_t n =
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4361 xsum (length,
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4362 (count + insert + TCHARS_PER_DCHAR - 1)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4363 / TCHARS_PER_DCHAR);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4364 length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4365 ENSURE_ALLOCATION (n);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4366 length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4367 prec_ptr = (TCHAR_T *) (result + length);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4368 # endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4369
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4370 prec_end = prec_ptr + count;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4371 prec_ptr += prefix_count;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4372
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4373 while (prec_end > prec_ptr)
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4374 {
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4375 prec_end--;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4376 prec_end[insert] = prec_end[0];
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4377 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4378
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4379 prec_end += insert;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4380 do
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4381 *--prec_end = '0';
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4382 while (prec_end > prec_ptr);
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4383
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4384 count += insert;
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4385 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4386 }
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4387 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4388
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4389 #if !DCHAR_IS_TCHAR
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4390 # if !USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4391 if (count >= tmp_length)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4392 /* tmp_length was incorrectly calculated - fix the
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4393 code above! */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4394 abort ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4395 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4396
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4397 /* Convert from TCHAR_T[] to DCHAR_T[]. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4398 if (dp->conversion == 'c' || dp->conversion == 's')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4399 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4400 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4401 TYPE_WIDE_STRING.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4402 The result string is not certainly ASCII. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4403 const TCHAR_T *tmpsrc;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4404 DCHAR_T *tmpdst;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4405 size_t tmpdst_len;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4406 /* This code assumes that TCHAR_T is 'char'. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4407 typedef int TCHAR_T_verify
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4408 [2 * (sizeof (TCHAR_T) == 1) - 1];
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4409 # if USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4410 tmpsrc = (TCHAR_T *) (result + length);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4411 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4412 tmpsrc = tmp;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4413 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4414 tmpdst = NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4415 tmpdst_len = 0;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4416 if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4417 iconveh_question_mark,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4418 tmpsrc, count,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4419 NULL,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4420 &tmpdst, &tmpdst_len)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4421 < 0)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4422 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4423 int saved_errno = errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4424 if (!(result == resultbuf || result == NULL))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4425 free (result);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4426 if (buf_malloced != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4427 free (buf_malloced);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4428 CLEANUP ();
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4429 errno = saved_errno;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4430 return NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4431 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4432 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4433 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4434 free (tmpdst);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4435 count = tmpdst_len;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4436 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4437 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4438 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4439 /* The result string is ASCII.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4440 Simple 1:1 conversion. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4441 # if USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4442 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4443 no-op conversion, in-place on the array starting
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4444 at (result + length). */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4445 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4446 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4447 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4448 const TCHAR_T *tmpsrc;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4449 DCHAR_T *tmpdst;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4450 size_t n;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4451
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4452 # if USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4453 if (result == resultbuf)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4454 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4455 tmpsrc = (TCHAR_T *) (result + length);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4456 /* ENSURE_ALLOCATION will not move tmpsrc
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4457 (because it's part of resultbuf). */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4458 ENSURE_ALLOCATION (xsum (length, count));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4459 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4460 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4461 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4462 /* ENSURE_ALLOCATION will move the array
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4463 (because it uses realloc(). */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4464 ENSURE_ALLOCATION (xsum (length, count));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4465 tmpsrc = (TCHAR_T *) (result + length);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4466 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4467 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4468 tmpsrc = tmp;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4469 ENSURE_ALLOCATION (xsum (length, count));
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4470 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4471 tmpdst = result + length;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4472 /* Copy backwards, because of overlapping. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4473 tmpsrc += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4474 tmpdst += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4475 for (n = count; n > 0; n--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4476 *--tmpdst = (unsigned char) *--tmpsrc;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4477 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4478 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4479 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4480
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4481 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4482 /* Make room for the result. */
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4483 if (count > allocated - length)
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4484 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4485 /* Need at least count elements. But allocate
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4486 proportionally. */
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4487 size_t n =
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4488 xmax (xsum (length, count), xtimes (allocated, 2));
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4489
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4490 ENSURE_ALLOCATION (n);
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
4491 }
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4492 #endif
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4493
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4494 /* Here count <= allocated - length. */
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
4495
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4496 /* Perform padding. */
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4497 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4498 if (pad_ourselves && has_width)
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4499 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4500 size_t w;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4501 # if ENABLE_UNISTDIO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4502 /* Outside POSIX, it's preferrable to compare the width
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4503 against the number of _characters_ of the converted
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4504 value. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4505 w = DCHAR_MBSNLEN (result + length, count);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4506 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4507 /* The width is compared against the number of _bytes_
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4508 of the converted value, says POSIX. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4509 w = count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4510 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4511 if (w < width)
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4512 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4513 size_t pad = width - w;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4514 # if USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4515 /* Make room for the result. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4516 if (xsum (count, pad) > allocated - length)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4517 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4518 /* Need at least count + pad elements. But
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4519 allocate proportionally. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4520 size_t n =
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4521 xmax (xsum3 (length, count, pad),
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4522 xtimes (allocated, 2));
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4523
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4524 length += count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4525 ENSURE_ALLOCATION (n);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4526 length -= count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4527 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4528 /* Here count + pad <= allocated - length. */
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4529 # endif
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4530 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4531 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4532 DCHAR_T * const rp = result + length;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4533 # else
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4534 DCHAR_T * const rp = tmp;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4535 # endif
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4536 DCHAR_T *p = rp + count;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4537 DCHAR_T *end = p + pad;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4538 # if NEED_PRINTF_FLAG_ZERO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4539 DCHAR_T *pad_ptr;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4540 # if !DCHAR_IS_TCHAR
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4541 if (dp->conversion == 'c'
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4542 || dp->conversion == 's')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4543 /* No zero-padding for string directives. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4544 pad_ptr = NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4545 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4546 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4547 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4548 pad_ptr = (*rp == '-' ? rp + 1 : rp);
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4549 /* No zero-padding of "inf" and "nan". */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4550 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4551 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4552 pad_ptr = NULL;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4553 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4554 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4555 /* The generated string now extends from rp to p,
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4556 with the zero padding insertion point being at
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4557 pad_ptr. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4558
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4559 count = count + pad; /* = end - rp */
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4560
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4561 if (flags & FLAG_LEFT)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4562 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4563 /* Pad with spaces on the right. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4564 for (; pad > 0; pad--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4565 *p++ = ' ';
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4566 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4567 # if NEED_PRINTF_FLAG_ZERO
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4568 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4569 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4570 /* Pad with zeroes. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4571 DCHAR_T *q = end;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4572
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4573 while (p > pad_ptr)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4574 *--q = *--p;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4575 for (; pad > 0; pad--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4576 *p++ = '0';
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4577 }
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4578 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4579 else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4580 {
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4581 /* Pad with spaces on the left. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4582 DCHAR_T *q = end;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4583
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4584 while (p > rp)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4585 *--q = *--p;
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4586 for (; pad > 0; pad--)
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4587 *p++ = ' ';
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4588 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4589 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4590 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4591 }
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4592 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4593
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4594 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4595 if (count >= tmp_length)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4596 /* tmp_length was incorrectly calculated - fix the
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4597 code above! */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4598 abort ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4599 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4600
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
4601 /* Here still count <= allocated - length. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4602
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4603 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4604 /* The snprintf() result did fit. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4605 #else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4606 /* Append the sprintf() result. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4607 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4608 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4609 #if !USE_SNPRINTF
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4610 if (tmp != tmpbuf)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4611 free (tmp);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4612 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4613
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4614 #if NEED_PRINTF_DIRECTIVE_F
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4615 if (dp->conversion == 'F')
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4616 {
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4617 /* Convert the %f result to upper case for %F. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4618 DCHAR_T *rp = result + length;
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4619 size_t rc;
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4620 for (rc = count; rc > 0; rc--, rp++)
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4621 if (*rp >= 'a' && *rp <= 'z')
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4622 *rp = *rp - 'a' + 'A';
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4623 }
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4624 #endif
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4625
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4626 length += count;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4627 break;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4628 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4629 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4630 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4631 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4632
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4633 /* Add the final NUL. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4634 ENSURE_ALLOCATION (xsum (length, 1));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4635 result[length] = '\0';
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4636
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4637 if (result != resultbuf && length + 1 < allocated)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4638 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4639 /* Shrink the allocated memory if possible. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4640 DCHAR_T *memory;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4641
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4642 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4643 if (memory != NULL)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4644 result = memory;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4645 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4646
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4647 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4648 free (buf_malloced);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4649 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4650 *lengthp = length;
7218
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
4651 /* Note that we can produce a big string of a length > INT_MAX. POSIX
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
4652 says that snprintf() fails with errno = EOVERFLOW in this case, but
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
4653 that's only because snprintf() returns an 'int'. This function does
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
4654 not have this limitation. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4655 return result;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4656
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4657 overflow:
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4658 if (!(result == resultbuf || result == NULL))
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4659 free (result);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4660 if (buf_malloced != NULL)
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4661 free (buf_malloced);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4662 CLEANUP ();
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4663 errno = EOVERFLOW;
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4664 return NULL;
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
4665
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4666 out_of_memory:
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4667 if (!(result == resultbuf || result == NULL))
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4668 free (result);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4669 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4670 free (buf_malloced);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
4671 out_of_memory_1:
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4672 CLEANUP ();
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4673 errno = ENOMEM;
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
4674 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4675 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4676 }
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4677
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4678 #undef TCHARS_PER_DCHAR
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4679 #undef SNPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4680 #undef USE_SNPRINTF
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
4681 #undef DCHAR_CPY
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4682 #undef PRINTF_PARSE
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4683 #undef DIRECTIVES
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4684 #undef DIRECTIVE
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
4685 #undef DCHAR_IS_TCHAR
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4686 #undef TCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4687 #undef DCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
4688 #undef FCHAR_T
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4689 #undef VASNPRINTF