annotate lib/vasnprintf.c @ 276:23034db7dd8a 2.5

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