annotate lib/vasnprintf.c @ 425:67815606c5d9 3.0-beta2

Prevent NULL deref in lw_expr_destroy
author lost@l-w.ca
date Sun, 19 Sep 2010 10:27:47 -0600
parents 3c2e1f24cc7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
421
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1 /* vsprintf with automatic memory allocation.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2 Copyright (C) 1999, 2002-2010 Free Software Foundation, Inc.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
6 the Free Software Foundation; either version 3, or (at your option)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
7 any later version.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
8
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
12 GNU General Public License for more details.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
13
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
15 with this program; if not, write to the Free Software Foundation,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
17
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
18 /* This file can be parametrized with the following macros:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
19 VASNPRINTF The name of the function being defined.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
20 FCHAR_T The element type of the format string.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
21 DCHAR_T The element type of the destination (result) string.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
22 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
23 in the format string are ASCII. MUST be set if
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
24 FCHAR_T and DCHAR_T are not the same type.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
25 DIRECTIVE Structure denoting a format directive.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
26 Depends on FCHAR_T.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
27 DIRECTIVES Structure denoting the set of format directives of a
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
28 format string. Depends on FCHAR_T.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
29 PRINTF_PARSE Function that parses a format string.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
30 Depends on FCHAR_T.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
31 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
32 DCHAR_SET memset like function for DCHAR_T[] arrays.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
33 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
34 SNPRINTF The system's snprintf (or similar) function.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
35 This may be either snprintf or swprintf.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
36 TCHAR_T The element type of the argument and result string
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
37 of the said SNPRINTF function. This may be either
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
38 char or wchar_t. The code exploits that
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
39 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
40 alignof (TCHAR_T) <= alignof (DCHAR_T).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
41 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
42 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
43 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
44 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
45 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
46
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
47 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
48 This must come before <config.h> because <config.h> may include
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
49 <features.h>, and once <features.h> has been included, it's too late. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
50 #ifndef _GNU_SOURCE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
51 # define _GNU_SOURCE 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
52 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
53
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
54 #ifndef VASNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
55 # include <config.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
56 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
57 #ifndef IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
58 # include <alloca.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
59 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
60
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
61 /* Specification. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
62 #ifndef VASNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
63 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
64 # include "vasnwprintf.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
65 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
66 # include "vasnprintf.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
67 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
68 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
69
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
70 #include <locale.h> /* localeconv() */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
71 #include <stdio.h> /* snprintf(), sprintf() */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
72 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
73 #include <string.h> /* memcpy(), strlen() */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
74 #include <errno.h> /* errno */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
75 #include <limits.h> /* CHAR_BIT */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
76 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
77 #if HAVE_NL_LANGINFO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
78 # include <langinfo.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
79 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
80 #ifndef VASNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
81 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
82 # include "wprintf-parse.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
83 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
84 # include "printf-parse.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
85 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
86 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
87
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
88 /* Checked size_t computations. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
89 #include "xsize.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
90
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
91 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
92 # include <math.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
93 # include "float+.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
94 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
95
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
96 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
97 # include <math.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
98 # include "isnand-nolibm.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
99 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
100
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
101 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
102 # include <math.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
103 # include "isnanl-nolibm.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
104 # include "fpucw.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
105 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
106
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
107 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
108 # include <math.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
109 # include "isnand-nolibm.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
110 # include "printf-frexp.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
111 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
112
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
113 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
114 # include <math.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
115 # include "isnanl-nolibm.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
116 # include "printf-frexpl.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
117 # include "fpucw.h"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
118 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
119
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
120 /* Default parameters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
121 #ifndef VASNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
122 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
123 # define VASNPRINTF vasnwprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
124 # define FCHAR_T wchar_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
125 # define DCHAR_T wchar_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
126 # define TCHAR_T wchar_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
127 # define DCHAR_IS_TCHAR 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
128 # define DIRECTIVE wchar_t_directive
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
129 # define DIRECTIVES wchar_t_directives
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
130 # define PRINTF_PARSE wprintf_parse
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
131 # define DCHAR_CPY wmemcpy
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
132 # define DCHAR_SET wmemset
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
133 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
134 # define VASNPRINTF vasnprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
135 # define FCHAR_T char
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
136 # define DCHAR_T char
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
137 # define TCHAR_T char
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
138 # define DCHAR_IS_TCHAR 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
139 # define DIRECTIVE char_directive
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
140 # define DIRECTIVES char_directives
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
141 # define PRINTF_PARSE printf_parse
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
142 # define DCHAR_CPY memcpy
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
143 # define DCHAR_SET memset
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
144 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
145 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
146 #if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
147 /* TCHAR_T is wchar_t. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
148 # define USE_SNPRINTF 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
149 # if HAVE_DECL__SNWPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
150 /* On Windows, the function swprintf() has a different signature than
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
151 on Unix; we use the _snwprintf() function instead. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
152 # define SNPRINTF _snwprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
153 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
154 /* Unix. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
155 # define SNPRINTF swprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
156 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
157 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
158 /* TCHAR_T is char. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
159 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
160 But don't use it on BeOS, since BeOS snprintf produces no output if the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
161 size argument is >= 0x3000000.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
162 Also don't use it on Linux libc5, since there snprintf with size = 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
163 writes any output without bounds, like sprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
164 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
165 # define USE_SNPRINTF 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
166 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
167 # define USE_SNPRINTF 0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
168 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
169 # if HAVE_DECL__SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
170 /* Windows. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
171 # define SNPRINTF _snprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
172 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
173 /* Unix. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
174 # define SNPRINTF snprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
175 /* Here we need to call the native snprintf, not rpl_snprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
176 # undef snprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
177 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
178 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
179 /* Here we need to call the native sprintf, not rpl_sprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
180 #undef sprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
181
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
182 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
183 warnings in this file. Use -Dlint to suppress them. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
184 #ifdef lint
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
185 # define IF_LINT(Code) Code
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
186 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
187 # define IF_LINT(Code) /* empty */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
188 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
189
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
190 /* Avoid some warnings from "gcc -Wshadow".
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
191 This file doesn't use the exp() and remainder() functions. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
192 #undef exp
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
193 #define exp expo
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
194 #undef remainder
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
195 #define remainder rem
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
196
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
197 #if !USE_SNPRINTF && !WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
198 # if (HAVE_STRNLEN && !defined _AIX)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
199 # define local_strnlen strnlen
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
200 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
201 # ifndef local_strnlen_defined
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
202 # define local_strnlen_defined 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
203 static size_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
204 local_strnlen (const char *string, size_t maxlen)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
205 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
206 const char *end = memchr (string, '\0', maxlen);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
207 return end ? (size_t) (end - string) : maxlen;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
208 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
209 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
210 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
211 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
212
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
213 #if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T && (WIDE_CHAR_VERSION || DCHAR_IS_TCHAR)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
214 # if HAVE_WCSLEN
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
215 # define local_wcslen wcslen
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
216 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
217 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
218 a dependency towards this library, here is a local substitute.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
219 Define this substitute only once, even if this file is included
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
220 twice in the same compilation unit. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
221 # ifndef local_wcslen_defined
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
222 # define local_wcslen_defined 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
223 static size_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
224 local_wcslen (const wchar_t *s)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
225 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
226 const wchar_t *ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
227
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
228 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
229 ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
230 return ptr - s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
231 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
232 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
233 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
234 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
235
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
236 #if !USE_SNPRINTF && HAVE_WCHAR_T && WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
237 # if HAVE_WCSNLEN
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
238 # define local_wcsnlen wcsnlen
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
239 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
240 # ifndef local_wcsnlen_defined
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
241 # define local_wcsnlen_defined 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
242 static size_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
243 local_wcsnlen (const wchar_t *s, size_t maxlen)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
244 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
245 const wchar_t *ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
246
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
247 for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
248 ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
249 return ptr - s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
250 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
251 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
252 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
253 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
254
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
255 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
256 /* Determine the decimal-point character according to the current locale. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
257 # ifndef decimal_point_char_defined
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
258 # define decimal_point_char_defined 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
259 static char
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
260 decimal_point_char (void)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
261 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
262 const char *point;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
263 /* Determine it in a multithread-safe way. We know nl_langinfo is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
264 multithread-safe on glibc systems and MacOS X systems, but is not required
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
265 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
266 localeconv() is rarely multithread-safe. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
267 # if HAVE_NL_LANGINFO && (__GLIBC__ || (defined __APPLE__ && defined __MACH__))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
268 point = nl_langinfo (RADIXCHAR);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
269 # elif 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
270 char pointbuf[5];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
271 sprintf (pointbuf, "%#.0f", 1.0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
272 point = &pointbuf[1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
273 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
274 point = localeconv () -> decimal_point;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
275 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
276 /* The decimal point is always a single byte: either '.' or ','. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
277 return (point[0] != '\0' ? point[0] : '.');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
278 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
279 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
280 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
281
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
282 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
283
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
284 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
285 static int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
286 is_infinite_or_zero (double x)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
287 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
288 return isnand (x) || x + x == x;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
289 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
290
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
291 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
292
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
293 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
294
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
295 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
296 static int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
297 is_infinite_or_zerol (long double x)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
298 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
299 return isnanl (x) || x + x == x;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
300 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
301
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
302 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
303
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
304 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
305
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
306 /* Converting 'long double' to decimal without rare rounding bugs requires
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
307 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
308 (and slower) algorithms. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
309
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
310 typedef unsigned int mp_limb_t;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
311 # define GMP_LIMB_BITS 32
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
312 typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
313
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
314 typedef unsigned long long mp_twolimb_t;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
315 # define GMP_TWOLIMB_BITS 64
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
316 typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
317
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
318 /* Representation of a bignum >= 0. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
319 typedef struct
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
320 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
321 size_t nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
322 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
323 } mpn_t;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
324
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
325 /* Compute the product of two bignums >= 0.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
326 Return the allocated memory in case of success, NULL in case of memory
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
327 allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
328 static void *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
329 multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
330 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
331 const mp_limb_t *p1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
332 const mp_limb_t *p2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
333 size_t len1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
334 size_t len2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
335
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
336 if (src1.nlimbs <= src2.nlimbs)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
337 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
338 len1 = src1.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
339 p1 = src1.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
340 len2 = src2.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
341 p2 = src2.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
342 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
343 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
344 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
345 len1 = src2.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
346 p1 = src2.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
347 len2 = src1.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
348 p2 = src1.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
349 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
350 /* Now 0 <= len1 <= len2. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
351 if (len1 == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
352 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
353 /* src1 or src2 is zero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
354 dest->nlimbs = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
355 dest->limbs = (mp_limb_t *) malloc (1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
356 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
357 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
358 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
359 /* Here 1 <= len1 <= len2. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
360 size_t dlen;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
361 mp_limb_t *dp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
362 size_t k, i, j;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
363
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
364 dlen = len1 + len2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
365 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
366 if (dp == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
367 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
368 for (k = len2; k > 0; )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
369 dp[--k] = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
370 for (i = 0; i < len1; i++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
371 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
372 mp_limb_t digit1 = p1[i];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
373 mp_twolimb_t carry = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
374 for (j = 0; j < len2; j++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
375 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
376 mp_limb_t digit2 = p2[j];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
377 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
378 carry += dp[i + j];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
379 dp[i + j] = (mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
380 carry = carry >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
381 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
382 dp[i + len2] = (mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
383 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
384 /* Normalise. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
385 while (dlen > 0 && dp[dlen - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
386 dlen--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
387 dest->nlimbs = dlen;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
388 dest->limbs = dp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
389 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
390 return dest->limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
391 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
392
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
393 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
394 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
395 the remainder.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
396 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
397 q is incremented.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
398 Return the allocated memory in case of success, NULL in case of memory
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
399 allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
400 static void *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
401 divide (mpn_t a, mpn_t b, mpn_t *q)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
402 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
403 /* Algorithm:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
404 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
405 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
406 If m<n, then q:=0 and r:=a.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
407 If m>=n=1, perform a single-precision division:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
408 r:=0, j:=m,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
409 while j>0 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
410 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
411 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
412 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
413 Normalise [q[m-1],...,q[0]], yields q.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
414 If m>=n>1, perform a multiple-precision division:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
415 We have a/b < beta^(m-n+1).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
416 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
417 Shift a and b left by s bits, copying them. r:=a.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
418 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
419 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
420 Compute q* :
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
421 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
422 In case of overflow (q* >= beta) set q* := beta-1.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
423 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
424 and c3 := b[n-2] * q*.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
425 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
426 occurred. Furthermore 0 <= c3 < beta^2.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
427 If there was overflow and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
428 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
429 the next test can be skipped.}
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
430 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
431 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
432 If q* > 0:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
433 Put r := r - b * q* * beta^j. In detail:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
434 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
435 hence: u:=0, for i:=0 to n-1 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
436 u := u + q* * b[i],
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
437 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
438 u:=u div beta (+ 1, if carry in subtraction)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
439 r[n+j]:=r[n+j]-u.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
440 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
441 < q* + 1 <= beta,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
442 the carry u does not overflow.}
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
443 If a negative carry occurs, put q* := q* - 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
444 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
445 Set q[j] := q*.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
446 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
447 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
448 rest r.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
449 The room for q[j] can be allocated at the memory location of r[n+j].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
450 Finally, round-to-even:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
451 Shift r left by 1 bit.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
452 If r > b or if r = b and q[0] is odd, q := q+1.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
453 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
454 const mp_limb_t *a_ptr = a.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
455 size_t a_len = a.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
456 const mp_limb_t *b_ptr = b.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
457 size_t b_len = b.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
458 mp_limb_t *roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
459 mp_limb_t *tmp_roomptr = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
460 mp_limb_t *q_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
461 size_t q_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
462 mp_limb_t *r_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
463 size_t r_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
464
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
465 /* Allocate room for a_len+2 digits.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
466 (Need a_len+1 digits for the real division and 1 more digit for the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
467 final rounding of q.) */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
468 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
469 if (roomptr == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
470 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
471
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
472 /* Normalise a. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
473 while (a_len > 0 && a_ptr[a_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
474 a_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
475
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
476 /* Normalise b. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
477 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
478 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
479 if (b_len == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
480 /* Division by zero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
481 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
482 if (b_ptr[b_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
483 b_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
484 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
485 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
486 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
487
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
488 /* Here m = a_len >= 0 and n = b_len > 0. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
489
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
490 if (a_len < b_len)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
491 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
492 /* m<n: trivial case. q=0, r := copy of a. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
493 r_ptr = roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
494 r_len = a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
495 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
496 q_ptr = roomptr + a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
497 q_len = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
498 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
499 else if (b_len == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
500 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
501 /* n=1: single precision division.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
502 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
503 r_ptr = roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
504 q_ptr = roomptr + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
505 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
506 mp_limb_t den = b_ptr[0];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
507 mp_limb_t remainder = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
508 const mp_limb_t *sourceptr = a_ptr + a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
509 mp_limb_t *destptr = q_ptr + a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
510 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
511 for (count = a_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
512 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
513 mp_twolimb_t num =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
514 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
515 *--destptr = num / den;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
516 remainder = num % den;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
517 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
518 /* Normalise and store r. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
519 if (remainder > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
520 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
521 r_ptr[0] = remainder;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
522 r_len = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
523 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
524 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
525 r_len = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
526 /* Normalise q. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
527 q_len = a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
528 if (q_ptr[q_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
529 q_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
530 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
531 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
532 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
533 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
534 /* n>1: multiple precision division.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
535 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
536 beta^(m-n-1) <= a/b < beta^(m-n+1). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
537 /* Determine s. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
538 size_t s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
539 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
540 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
541 s = 31;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
542 if (msd >= 0x10000)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
543 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
544 msd = msd >> 16;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
545 s -= 16;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
546 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
547 if (msd >= 0x100)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
548 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
549 msd = msd >> 8;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
550 s -= 8;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
551 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
552 if (msd >= 0x10)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
553 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
554 msd = msd >> 4;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
555 s -= 4;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
556 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
557 if (msd >= 0x4)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
558 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
559 msd = msd >> 2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
560 s -= 2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
561 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
562 if (msd >= 0x2)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
563 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
564 msd = msd >> 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
565 s -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
566 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
567 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
568 /* 0 <= s < GMP_LIMB_BITS.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
569 Copy b, shifting it left by s bits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
570 if (s > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
571 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
572 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
573 if (tmp_roomptr == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
574 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
575 free (roomptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
576 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
577 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
578 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
579 const mp_limb_t *sourceptr = b_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
580 mp_limb_t *destptr = tmp_roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
581 mp_twolimb_t accu = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
582 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
583 for (count = b_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
584 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
585 accu += (mp_twolimb_t) *sourceptr++ << s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
586 *destptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
587 accu = accu >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
588 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
589 /* accu must be zero, since that was how s was determined. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
590 if (accu != 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
591 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
592 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
593 b_ptr = tmp_roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
594 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
595 /* Copy a, shifting it left by s bits, yields r.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
596 Memory layout:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
597 At the beginning: r = roomptr[0..a_len],
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
598 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
599 r_ptr = roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
600 if (s == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
601 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
602 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
603 r_ptr[a_len] = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
604 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
605 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
606 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
607 const mp_limb_t *sourceptr = a_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
608 mp_limb_t *destptr = r_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
609 mp_twolimb_t accu = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
610 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
611 for (count = a_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
612 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
613 accu += (mp_twolimb_t) *sourceptr++ << s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
614 *destptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
615 accu = accu >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
616 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
617 *destptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
618 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
619 q_ptr = roomptr + b_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
620 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
621 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
622 size_t j = a_len - b_len; /* m-n */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
623 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
624 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
625 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
626 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
627 /* Division loop, traversed m-n+1 times.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
628 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
629 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
630 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
631 mp_limb_t q_star;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
632 mp_limb_t c1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
633 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
634 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
635 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
636 mp_twolimb_t num =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
637 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
638 | r_ptr[j + b_len - 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
639 q_star = num / b_msd;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
640 c1 = num % b_msd;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
641 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
642 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
643 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
644 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
645 q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
646 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
647 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
648 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
649 {<= beta !}.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
650 If yes, jump directly to the subtraction loop.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
651 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
652 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
653 if (r_ptr[j + b_len] > b_msd
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
654 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
655 /* r[j+n] >= b[n-1]+1 or
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
656 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
657 carry. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
658 goto subtract;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
659 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
660 /* q_star = q*,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
661 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
662 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
663 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
664 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
665 mp_twolimb_t c3 = /* b[n-2] * q* */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
666 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
667 /* While c2 < c3, increase c2 and decrease c3.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
668 Consider c3-c2. While it is > 0, decrease it by
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
669 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
670 this can happen only twice. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
671 if (c3 > c2)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
672 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
673 q_star = q_star - 1; /* q* := q* - 1 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
674 if (c3 - c2 > b_msdd)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
675 q_star = q_star - 1; /* q* := q* - 1 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
676 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
677 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
678 if (q_star > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
679 subtract:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
680 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
681 /* Subtract r := r - b * q* * beta^j. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
682 mp_limb_t cr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
683 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
684 const mp_limb_t *sourceptr = b_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
685 mp_limb_t *destptr = r_ptr + j;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
686 mp_twolimb_t carry = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
687 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
688 for (count = b_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
689 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
690 /* Here 0 <= carry <= q*. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
691 carry =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
692 carry
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
693 + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
694 + (mp_limb_t) ~(*destptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
695 /* Here 0 <= carry <= beta*q* + beta-1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
696 *destptr++ = ~(mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
697 carry = carry >> GMP_LIMB_BITS; /* <= q* */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
698 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
699 cr = (mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
700 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
701 /* Subtract cr from r_ptr[j + b_len], then forget about
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
702 r_ptr[j + b_len]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
703 if (cr > r_ptr[j + b_len])
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
704 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
705 /* Subtraction gave a carry. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
706 q_star = q_star - 1; /* q* := q* - 1 */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
707 /* Add b back. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
708 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
709 const mp_limb_t *sourceptr = b_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
710 mp_limb_t *destptr = r_ptr + j;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
711 mp_limb_t carry = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
712 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
713 for (count = b_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
714 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
715 mp_limb_t source1 = *sourceptr++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
716 mp_limb_t source2 = *destptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
717 *destptr++ = source1 + source2 + carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
718 carry =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
719 (carry
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
720 ? source1 >= (mp_limb_t) ~source2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
721 : source1 > (mp_limb_t) ~source2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
722 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
723 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
724 /* Forget about the carry and about r[j+n]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
725 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
726 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
727 /* q* is determined. Store it as q[j]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
728 q_ptr[j] = q_star;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
729 if (j == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
730 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
731 j--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
732 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
733 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
734 r_len = b_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
735 /* Normalise q. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
736 if (q_ptr[q_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
737 q_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
738 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
739 b is shifted left by s bits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
740 /* Shift r right by s bits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
741 if (s > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
742 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
743 mp_limb_t ptr = r_ptr + r_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
744 mp_twolimb_t accu = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
745 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
746 for (count = r_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
747 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
748 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
749 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
750 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
751 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
752 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
753 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
754 /* Normalise r. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
755 while (r_len > 0 && r_ptr[r_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
756 r_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
757 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
758 /* Compare r << 1 with b. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
759 if (r_len > b_len)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
760 goto increment_q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
761 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
762 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
763 for (i = b_len;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
764 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
765 mp_limb_t r_i =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
766 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
767 | (i < r_len ? r_ptr[i] << 1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
768 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
769 if (r_i > b_i)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
770 goto increment_q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
771 if (r_i < b_i)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
772 goto keep_q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
773 if (i == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
774 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
775 i--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
776 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
777 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
778 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
779 /* q is odd. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
780 increment_q:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
781 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
782 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
783 for (i = 0; i < q_len; i++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
784 if (++(q_ptr[i]) != 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
785 goto keep_q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
786 q_ptr[q_len++] = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
787 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
788 keep_q:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
789 if (tmp_roomptr != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
790 free (tmp_roomptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
791 q->limbs = q_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
792 q->nlimbs = q_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
793 return roomptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
794 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
795
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
796 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
797 representation.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
798 Destroys the contents of a.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
799 Return the allocated memory - containing the decimal digits in low-to-high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
800 order, terminated with a NUL character - in case of success, NULL in case
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
801 of memory allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
802 static char *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
803 convert_to_decimal (mpn_t a, size_t extra_zeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
804 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
805 mp_limb_t *a_ptr = a.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
806 size_t a_len = a.nlimbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
807 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
808 size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
809 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
810 if (c_ptr != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
811 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
812 char *d_ptr = c_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
813 for (; extra_zeroes > 0; extra_zeroes--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
814 *d_ptr++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
815 while (a_len > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
816 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
817 /* Divide a by 10^9, in-place. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
818 mp_limb_t remainder = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
819 mp_limb_t *ptr = a_ptr + a_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
820 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
821 for (count = a_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
822 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
823 mp_twolimb_t num =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
824 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
825 *ptr = num / 1000000000;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
826 remainder = num % 1000000000;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
827 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
828 /* Store the remainder as 9 decimal digits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
829 for (count = 9; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
830 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
831 *d_ptr++ = '0' + (remainder % 10);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
832 remainder = remainder / 10;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
833 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
834 /* Normalize a. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
835 if (a_ptr[a_len - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
836 a_len--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
837 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
838 /* Remove leading zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
839 while (d_ptr > c_ptr && d_ptr[-1] == '0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
840 d_ptr--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
841 /* But keep at least one zero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
842 if (d_ptr == c_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
843 *d_ptr++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
844 /* Terminate the string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
845 *d_ptr = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
846 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
847 return c_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
848 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
849
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
850 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
851
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
852 /* Assuming x is finite and >= 0:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
853 write x as x = 2^e * m, where m is a bignum.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
854 Return the allocated memory in case of success, NULL in case of memory
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
855 allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
856 static void *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
857 decode_long_double (long double x, int *ep, mpn_t *mp)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
858 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
859 mpn_t m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
860 int exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
861 long double y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
862 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
863
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
864 /* Allocate memory for result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
865 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
866 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
867 if (m.limbs == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
868 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
869 /* Split into exponential part and mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
870 y = frexpl (x, &exp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
871 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
872 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
873 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
874 latter is an integer. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
875 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
876 I'm not sure whether it's safe to cast a 'long double' value between
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
877 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
878 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
879 doesn't matter). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
880 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
881 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
882 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
883 mp_limb_t hi, lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
884 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
885 hi = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
886 y -= hi;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
887 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
888 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
889 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
890 lo = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
891 y -= lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
892 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
893 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
894 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
895 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
896 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
897 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
898 mp_limb_t d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
899 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
900 d = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
901 y -= d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
902 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
903 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
904 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
905 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
906 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
907 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
908 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
909 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
910 mp_limb_t hi, lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
911 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
912 hi = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
913 y -= hi;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
914 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
915 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
916 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
917 lo = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
918 y -= lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
919 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
920 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
921 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
922 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
923 #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
924 precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
925 if (!(y == 0.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
926 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
927 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
928 /* Normalise. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
929 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
930 m.nlimbs--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
931 *mp = m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
932 *ep = exp - LDBL_MANT_BIT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
933 return m.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
934 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
935
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
936 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
937
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
938 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
939
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
940 /* Assuming x is finite and >= 0:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
941 write x as x = 2^e * m, where m is a bignum.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
942 Return the allocated memory in case of success, NULL in case of memory
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
943 allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
944 static void *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
945 decode_double (double x, int *ep, mpn_t *mp)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
946 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
947 mpn_t m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
948 int exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
949 double y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
950 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
951
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
952 /* Allocate memory for result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
953 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
954 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
955 if (m.limbs == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
956 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
957 /* Split into exponential part and mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
958 y = frexp (x, &exp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
959 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
960 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
961 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
962 latter is an integer. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
963 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
964 I'm not sure whether it's safe to cast a 'double' value between
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
965 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
966 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
967 doesn't matter). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
968 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
969 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
970 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
971 mp_limb_t hi, lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
972 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
973 hi = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
974 y -= hi;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
975 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
976 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
977 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
978 lo = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
979 y -= lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
980 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
981 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
982 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
983 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
984 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
985 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
986 mp_limb_t d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
987 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
988 d = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
989 y -= d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
990 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
991 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
992 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
993 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
994 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
995 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
996 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
997 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
998 mp_limb_t hi, lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
999 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1000 hi = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1001 y -= hi;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1002 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1003 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1004 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1005 lo = (int) y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1006 y -= lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1007 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1008 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1009 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1010 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1011 if (!(y == 0.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1012 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1013 /* Normalise. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1014 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1015 m.nlimbs--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1016 *mp = m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1017 *ep = exp - DBL_MANT_BIT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1018 return m.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1019 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1020
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1021 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1022
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1023 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1024 Returns the decimal representation of round (x * 10^n).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1025 Return the allocated memory - containing the decimal digits in low-to-high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1026 order, terminated with a NUL character - in case of success, NULL in case
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1027 of memory allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1028 static char *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1029 scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1030 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1031 int s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1032 size_t extra_zeroes;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1033 unsigned int abs_n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1034 unsigned int abs_s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1035 mp_limb_t *pow5_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1036 size_t pow5_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1037 unsigned int s_limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1038 unsigned int s_bits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1039 mpn_t pow5;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1040 mpn_t z;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1041 void *z_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1042 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1043
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1044 if (memory == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1045 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1046 /* x = 2^e * m, hence
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1047 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1048 = round (2^s * 5^n * m). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1049 s = e + n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1050 extra_zeroes = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1051 /* Factor out a common power of 10 if possible. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1052 if (s > 0 && n > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1053 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1054 extra_zeroes = (s < n ? s : n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1055 s -= extra_zeroes;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1056 n -= extra_zeroes;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1057 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1058 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1059 Before converting to decimal, we need to compute
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1060 z = round (2^s * 5^n * m). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1061 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1062 sign. 2.322 is slightly larger than log(5)/log(2). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1063 abs_n = (n >= 0 ? n : -n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1064 abs_s = (s >= 0 ? s : -s);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1065 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1066 + abs_s / GMP_LIMB_BITS + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1067 * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1068 if (pow5_ptr == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1069 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1070 free (memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1071 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1072 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1073 /* Initialize with 1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1074 pow5_ptr[0] = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1075 pow5_len = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1076 /* Multiply with 5^|n|. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1077 if (abs_n > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1078 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1079 static mp_limb_t const small_pow5[13 + 1] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1080 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1081 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1082 48828125, 244140625, 1220703125
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1083 };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1084 unsigned int n13;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1085 for (n13 = 0; n13 <= abs_n; n13 += 13)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1086 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1087 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1088 size_t j;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1089 mp_twolimb_t carry = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1090 for (j = 0; j < pow5_len; j++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1091 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1092 mp_limb_t digit2 = pow5_ptr[j];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1093 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1094 pow5_ptr[j] = (mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1095 carry = carry >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1096 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1097 if (carry > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1098 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1099 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1100 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1101 s_limbs = abs_s / GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1102 s_bits = abs_s % GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1103 if (n >= 0 ? s >= 0 : s <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1104 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1105 /* Multiply with 2^|s|. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1106 if (s_bits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1107 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1108 mp_limb_t *ptr = pow5_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1109 mp_twolimb_t accu = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1110 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1111 for (count = pow5_len; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1112 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1113 accu += (mp_twolimb_t) *ptr << s_bits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1114 *ptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1115 accu = accu >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1116 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1117 if (accu > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1118 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1119 *ptr = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1120 pow5_len++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1121 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1122 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1123 if (s_limbs > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1124 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1125 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1126 for (count = pow5_len; count > 0;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1127 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1128 count--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1129 pow5_ptr[s_limbs + count] = pow5_ptr[count];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1130 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1131 for (count = s_limbs; count > 0;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1132 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1133 count--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1134 pow5_ptr[count] = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1135 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1136 pow5_len += s_limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1137 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1138 pow5.limbs = pow5_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1139 pow5.nlimbs = pow5_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1140 if (n >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1141 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1142 /* Multiply m with pow5. No division needed. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1143 z_memory = multiply (m, pow5, &z);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1144 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1145 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1146 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1147 /* Divide m by pow5 and round. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1148 z_memory = divide (m, pow5, &z);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1149 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1150 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1151 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1152 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1153 pow5.limbs = pow5_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1154 pow5.nlimbs = pow5_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1155 if (n >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1156 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1157 /* n >= 0, s < 0.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1158 Multiply m with pow5, then divide by 2^|s|. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1159 mpn_t numerator;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1160 mpn_t denominator;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1161 void *tmp_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1162 tmp_memory = multiply (m, pow5, &numerator);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1163 if (tmp_memory == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1164 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1165 free (pow5_ptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1166 free (memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1167 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1168 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1169 /* Construct 2^|s|. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1170 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1171 mp_limb_t *ptr = pow5_ptr + pow5_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1172 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1173 for (i = 0; i < s_limbs; i++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1174 ptr[i] = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1175 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1176 denominator.limbs = ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1177 denominator.nlimbs = s_limbs + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1178 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1179 z_memory = divide (numerator, denominator, &z);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1180 free (tmp_memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1181 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1182 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1183 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1184 /* n < 0, s > 0.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1185 Multiply m with 2^s, then divide by pow5. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1186 mpn_t numerator;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1187 mp_limb_t *num_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1188 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1189 * sizeof (mp_limb_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1190 if (num_ptr == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1191 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1192 free (pow5_ptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1193 free (memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1194 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1195 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1196 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1197 mp_limb_t *destptr = num_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1198 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1199 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1200 for (i = 0; i < s_limbs; i++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1201 *destptr++ = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1202 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1203 if (s_bits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1204 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1205 const mp_limb_t *sourceptr = m.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1206 mp_twolimb_t accu = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1207 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1208 for (count = m.nlimbs; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1209 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1210 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1211 *destptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1212 accu = accu >> GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1213 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1214 if (accu > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1215 *destptr++ = (mp_limb_t) accu;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1216 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1217 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1218 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1219 const mp_limb_t *sourceptr = m.limbs;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1220 size_t count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1221 for (count = m.nlimbs; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1222 *destptr++ = *sourceptr++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1223 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1224 numerator.limbs = num_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1225 numerator.nlimbs = destptr - num_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1226 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1227 z_memory = divide (numerator, pow5, &z);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1228 free (num_ptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1229 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1230 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1231 free (pow5_ptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1232 free (memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1233
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1234 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1235
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1236 if (z_memory == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1237 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1238 digits = convert_to_decimal (z, extra_zeroes);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1239 free (z_memory);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1240 return digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1241 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1242
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1243 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1244
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1245 /* Assuming x is finite and >= 0, and n is an integer:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1246 Returns the decimal representation of round (x * 10^n).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1247 Return the allocated memory - containing the decimal digits in low-to-high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1248 order, terminated with a NUL character - in case of success, NULL in case
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1249 of memory allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1250 static char *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1251 scale10_round_decimal_long_double (long double x, int n)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1252 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1253 int e IF_LINT(= 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1254 mpn_t m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1255 void *memory = decode_long_double (x, &e, &m);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1256 return scale10_round_decimal_decoded (e, m, memory, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1257 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1258
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1259 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1260
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1261 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1262
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1263 /* Assuming x is finite and >= 0, and n is an integer:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1264 Returns the decimal representation of round (x * 10^n).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1265 Return the allocated memory - containing the decimal digits in low-to-high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1266 order, terminated with a NUL character - in case of success, NULL in case
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1267 of memory allocation failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1268 static char *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1269 scale10_round_decimal_double (double x, int n)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1270 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1271 int e IF_LINT(= 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1272 mpn_t m;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1273 void *memory = decode_double (x, &e, &m);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1274 return scale10_round_decimal_decoded (e, m, memory, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1275 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1276
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1277 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1278
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1279 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1280
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1281 /* Assuming x is finite and > 0:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1282 Return an approximation for n with 10^n <= x < 10^(n+1).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1283 The approximation is usually the right n, but may be off by 1 sometimes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1284 static int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1285 floorlog10l (long double x)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1286 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1287 int exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1288 long double y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1289 double z;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1290 double l;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1291
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1292 /* Split into exponential part and mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1293 y = frexpl (x, &exp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1294 if (!(y >= 0.0L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1295 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1296 if (y == 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1297 return INT_MIN;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1298 if (y < 0.5L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1299 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1300 while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1301 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1302 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1303 exp -= GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1304 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1305 if (y < (1.0L / (1 << 16)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1306 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1307 y *= 1.0L * (1 << 16);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1308 exp -= 16;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1309 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1310 if (y < (1.0L / (1 << 8)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1311 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1312 y *= 1.0L * (1 << 8);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1313 exp -= 8;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1314 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1315 if (y < (1.0L / (1 << 4)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1316 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1317 y *= 1.0L * (1 << 4);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1318 exp -= 4;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1319 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1320 if (y < (1.0L / (1 << 2)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1321 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1322 y *= 1.0L * (1 << 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1323 exp -= 2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1324 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1325 if (y < (1.0L / (1 << 1)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1326 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1327 y *= 1.0L * (1 << 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1328 exp -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1329 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1330 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1331 if (!(y >= 0.5L && y < 1.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1332 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1333 /* Compute an approximation for l = log2(x) = exp + log2(y). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1334 l = exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1335 z = y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1336 if (z < 0.70710678118654752444)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1337 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1338 z *= 1.4142135623730950488;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1339 l -= 0.5;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1340 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1341 if (z < 0.8408964152537145431)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1342 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1343 z *= 1.1892071150027210667;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1344 l -= 0.25;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1345 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1346 if (z < 0.91700404320467123175)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1347 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1348 z *= 1.0905077326652576592;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1349 l -= 0.125;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1350 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1351 if (z < 0.9576032806985736469)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1352 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1353 z *= 1.0442737824274138403;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1354 l -= 0.0625;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1355 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1356 /* Now 0.95 <= z <= 1.01. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1357 z = 1 - z;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1358 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1359 Four terms are enough to get an approximation with error < 10^-7. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1360 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1361 /* Finally multiply with log(2)/log(10), yields an approximation for
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1362 log10(x). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1363 l *= 0.30102999566398119523;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1364 /* Round down to the next integer. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1365 return (int) l + (l < 0 ? -1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1366 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1367
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1368 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1369
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1370 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1371
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1372 /* Assuming x is finite and > 0:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1373 Return an approximation for n with 10^n <= x < 10^(n+1).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1374 The approximation is usually the right n, but may be off by 1 sometimes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1375 static int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1376 floorlog10 (double x)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1377 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1378 int exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1379 double y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1380 double z;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1381 double l;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1382
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1383 /* Split into exponential part and mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1384 y = frexp (x, &exp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1385 if (!(y >= 0.0 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1386 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1387 if (y == 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1388 return INT_MIN;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1389 if (y < 0.5)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1390 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1391 while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1392 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1393 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1394 exp -= GMP_LIMB_BITS;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1395 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1396 if (y < (1.0 / (1 << 16)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1397 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1398 y *= 1.0 * (1 << 16);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1399 exp -= 16;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1400 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1401 if (y < (1.0 / (1 << 8)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1402 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1403 y *= 1.0 * (1 << 8);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1404 exp -= 8;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1405 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1406 if (y < (1.0 / (1 << 4)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1407 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1408 y *= 1.0 * (1 << 4);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1409 exp -= 4;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1410 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1411 if (y < (1.0 / (1 << 2)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1412 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1413 y *= 1.0 * (1 << 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1414 exp -= 2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1415 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1416 if (y < (1.0 / (1 << 1)))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1417 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1418 y *= 1.0 * (1 << 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1419 exp -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1420 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1421 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1422 if (!(y >= 0.5 && y < 1.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1423 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1424 /* Compute an approximation for l = log2(x) = exp + log2(y). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1425 l = exp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1426 z = y;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1427 if (z < 0.70710678118654752444)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1428 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1429 z *= 1.4142135623730950488;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1430 l -= 0.5;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1431 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1432 if (z < 0.8408964152537145431)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1433 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1434 z *= 1.1892071150027210667;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1435 l -= 0.25;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1436 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1437 if (z < 0.91700404320467123175)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1438 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1439 z *= 1.0905077326652576592;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1440 l -= 0.125;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1441 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1442 if (z < 0.9576032806985736469)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1443 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1444 z *= 1.0442737824274138403;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1445 l -= 0.0625;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1446 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1447 /* Now 0.95 <= z <= 1.01. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1448 z = 1 - z;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1449 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1450 Four terms are enough to get an approximation with error < 10^-7. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1451 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1452 /* Finally multiply with log(2)/log(10), yields an approximation for
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1453 log10(x). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1454 l *= 0.30102999566398119523;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1455 /* Round down to the next integer. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1456 return (int) l + (l < 0 ? -1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1457 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1458
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1459 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1460
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1461 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1462 a single '1' digit. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1463 static int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1464 is_borderline (const char *digits, size_t precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1465 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1466 for (; precision > 0; precision--, digits++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1467 if (*digits != '0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1468 return 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1469 if (*digits != '1')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1470 return 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1471 digits++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1472 return *digits == '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1473 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1474
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1475 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1476
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1477 DCHAR_T *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1478 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1479 const FCHAR_T *format, va_list args)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1480 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1481 DIRECTIVES d;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1482 arguments a;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1483
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1484 if (PRINTF_PARSE (format, &d, &a) < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1485 /* errno is already set. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1486 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1487
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1488 #define CLEANUP() \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1489 free (d.dir); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1490 if (a.arg) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1491 free (a.arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1492
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1493 if (PRINTF_FETCHARGS (args, &a) < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1494 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1495 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1496 errno = EINVAL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1497 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1498 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1499
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1500 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1501 size_t buf_neededlength;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1502 TCHAR_T *buf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1503 TCHAR_T *buf_malloced;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1504 const FCHAR_T *cp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1505 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1506 DIRECTIVE *dp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1507 /* Output string accumulator. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1508 DCHAR_T *result;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1509 size_t allocated;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1510 size_t length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1511
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1512 /* Allocate a small buffer that will hold a directive passed to
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1513 sprintf or snprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1514 buf_neededlength =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1515 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1516 #if HAVE_ALLOCA
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1517 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1518 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1519 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1520 buf_malloced = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1521 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1522 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1523 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1524 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1525 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1526 if (size_overflow_p (buf_memsize))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1527 goto out_of_memory_1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1528 buf = (TCHAR_T *) malloc (buf_memsize);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1529 if (buf == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1530 goto out_of_memory_1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1531 buf_malloced = buf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1532 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1533
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1534 if (resultbuf != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1535 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1536 result = resultbuf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1537 allocated = *lengthp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1538 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1539 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1540 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1541 result = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1542 allocated = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1543 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1544 length = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1545 /* Invariants:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1546 result is either == resultbuf or == NULL or malloc-allocated.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1547 If length > 0, then result != NULL. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1548
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1549 /* Ensures that allocated >= needed. Aborts through a jump to
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1550 out_of_memory if needed is SIZE_MAX or otherwise too big. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1551 #define ENSURE_ALLOCATION(needed) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1552 if ((needed) > allocated) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1553 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1554 size_t memory_size; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1555 DCHAR_T *memory; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1556 \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1557 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1558 if ((needed) > allocated) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1559 allocated = (needed); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1560 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1561 if (size_overflow_p (memory_size)) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1562 goto out_of_memory; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1563 if (result == resultbuf || result == NULL) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1564 memory = (DCHAR_T *) malloc (memory_size); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1565 else \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1566 memory = (DCHAR_T *) realloc (result, memory_size); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1567 if (memory == NULL) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1568 goto out_of_memory; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1569 if (result == resultbuf && length > 0) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1570 DCHAR_CPY (memory, result, length); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1571 result = memory; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1572 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1573
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1574 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1575 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1576 if (cp != dp->dir_start)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1577 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1578 size_t n = dp->dir_start - cp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1579 size_t augmented_length = xsum (length, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1580
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1581 ENSURE_ALLOCATION (augmented_length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1582 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1583 need that the format string contains only ASCII characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1584 if FCHAR_T and DCHAR_T are not the same type. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1585 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1586 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1587 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1588 length = augmented_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1589 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1590 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1591 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1592 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1593 result[length++] = (unsigned char) *cp++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1594 while (--n > 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1595 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1596 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1597 if (i == d.count)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1598 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1599
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1600 /* Execute a single directive. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1601 if (dp->conversion == '%')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1602 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1603 size_t augmented_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1604
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1605 if (!(dp->arg_index == ARG_NONE))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1606 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1607 augmented_length = xsum (length, 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1608 ENSURE_ALLOCATION (augmented_length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1609 result[length] = '%';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1610 length = augmented_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1611 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1612 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1613 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1614 if (!(dp->arg_index != ARG_NONE))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1615 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1616
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1617 if (dp->conversion == 'n')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1618 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1619 switch (a.arg[dp->arg_index].type)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1620 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1621 case TYPE_COUNT_SCHAR_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1622 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1623 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1624 case TYPE_COUNT_SHORT_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1625 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1626 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1627 case TYPE_COUNT_INT_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1628 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1629 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1630 case TYPE_COUNT_LONGINT_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1631 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1632 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1633 #if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1634 case TYPE_COUNT_LONGLONGINT_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1635 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1636 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1637 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1638 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1639 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1640 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1641 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1642 #if ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1643 /* The unistdio extensions. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1644 else if (dp->conversion == 'U')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1645 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1646 arg_type type = a.arg[dp->arg_index].type;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1647 int flags = dp->flags;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1648 int has_width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1649 size_t width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1650 int has_precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1651 size_t precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1652
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1653 has_width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1654 width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1655 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1656 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1657 if (dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1658 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1659 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1660
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1661 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1662 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1663 arg = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1664 if (arg < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1665 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1666 /* "A negative field width is taken as a '-' flag
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1667 followed by a positive field width." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1668 flags |= FLAG_LEFT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1669 width = (unsigned int) (-arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1670 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1671 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1672 width = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1673 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1674 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1675 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1676 const FCHAR_T *digitp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1677
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1678 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1679 width = xsum (xtimes (width, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1680 while (digitp != dp->width_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1681 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1682 has_width = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1683 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1684
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1685 has_precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1686 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1687 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1688 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1689 if (dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1690 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1691 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1692
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1693 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1694 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1695 arg = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1696 /* "A negative precision is taken as if the precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1697 were omitted." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1698 if (arg >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1699 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1700 precision = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1701 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1702 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1703 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1704 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1705 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1706 const FCHAR_T *digitp = dp->precision_start + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1707
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1708 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1709 while (digitp != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1710 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1711 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1712 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1713 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1714
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1715 switch (type)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1716 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1717 case TYPE_U8_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1718 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1719 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1720 const uint8_t *arg_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1721 size_t characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1722
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1723 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1724 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1725 /* Use only PRECISION characters, from the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1726 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1727 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1728 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1729 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1730 int count = u8_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1731 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1732 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1733 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1734 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1735 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1736 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1737 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1738 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1739 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1740 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1741 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1742 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1743 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1744 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1745 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1746 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1747 else if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1748 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1749 /* Use the entire string, and count the number of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1750 characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1751 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1752 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1753 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1754 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1755 int count = u8_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1756 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1757 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1758 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1759 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1760 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1761 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1762 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1763 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1764 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1765 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1766 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1767 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1768 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1769 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1770 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1771 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1772 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1773 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1774 /* Use the entire string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1775 arg_end = arg + u8_strlen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1776 /* The number of characters doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1777 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1778 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1779
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1780 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1781 && !(dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1782 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1783 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1784 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1785 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1786 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1787 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1788
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1789 # if DCHAR_IS_UINT8_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1790 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1791 size_t n = arg_end - arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1792 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1793 DCHAR_CPY (result + length, arg, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1794 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1795 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1796 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1797 { /* Convert. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1798 DCHAR_T *converted = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1799 size_t converted_len = allocated - length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1800 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1801 /* Convert from UTF-8 to locale encoding. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1802 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1803 u8_conv_to_encoding (locale_charset (),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1804 iconveh_question_mark,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1805 arg, arg_end - arg, NULL,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1806 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1807 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1808 /* Convert from UTF-8 to UTF-16/UTF-32. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1809 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1810 U8_TO_DCHAR (arg, arg_end - arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1811 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1812 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1813 if (converted == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1814 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1815 int saved_errno = errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1816 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1817 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1818 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1819 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1820 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1821 errno = saved_errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1822 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1823 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1824 if (converted != result + length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1825 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1826 ENSURE_ALLOCATION (xsum (length, converted_len));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1827 DCHAR_CPY (result + length, converted, converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1828 free (converted);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1829 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1830 length += converted_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1831 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1832 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1833
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1834 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1835 && (dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1836 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1837 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1838 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1839 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1840 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1841 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1842 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1843 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1844
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1845 case TYPE_U16_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1846 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1847 const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1848 const uint16_t *arg_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1849 size_t characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1850
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1851 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1852 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1853 /* Use only PRECISION characters, from the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1854 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1855 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1856 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1857 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1858 int count = u16_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1859 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1860 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1861 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1862 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1863 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1864 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1865 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1866 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1867 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1868 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1869 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1870 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1871 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1872 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1873 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1874 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1875 else if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1876 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1877 /* Use the entire string, and count the number of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1878 characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1879 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1880 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1881 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1882 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1883 int count = u16_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1884 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1885 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1886 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1887 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1888 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1889 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1890 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1891 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1892 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1893 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1894 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1895 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1896 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1897 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1898 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1899 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1900 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1901 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1902 /* Use the entire string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1903 arg_end = arg + u16_strlen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1904 /* The number of characters doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1905 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1906 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1907
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1908 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1909 && !(dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1910 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1911 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1912 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1913 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1914 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1915 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1916
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1917 # if DCHAR_IS_UINT16_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1918 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1919 size_t n = arg_end - arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1920 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1921 DCHAR_CPY (result + length, arg, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1922 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1923 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1924 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1925 { /* Convert. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1926 DCHAR_T *converted = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1927 size_t converted_len = allocated - length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1928 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1929 /* Convert from UTF-16 to locale encoding. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1930 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1931 u16_conv_to_encoding (locale_charset (),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1932 iconveh_question_mark,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1933 arg, arg_end - arg, NULL,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1934 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1935 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1936 /* Convert from UTF-16 to UTF-8/UTF-32. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1937 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1938 U16_TO_DCHAR (arg, arg_end - arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1939 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1940 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1941 if (converted == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1942 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1943 int saved_errno = errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1944 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1945 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1946 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1947 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1948 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1949 errno = saved_errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1950 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1951 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1952 if (converted != result + length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1953 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1954 ENSURE_ALLOCATION (xsum (length, converted_len));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1955 DCHAR_CPY (result + length, converted, converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1956 free (converted);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1957 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1958 length += converted_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1959 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1960 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1961
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1962 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1963 && (dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1964 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1965 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1966 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1967 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1968 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1969 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1970 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1971 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1972
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1973 case TYPE_U32_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1974 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1975 const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1976 const uint32_t *arg_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1977 size_t characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1978
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1979 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1980 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1981 /* Use only PRECISION characters, from the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1982 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1983 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1984 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1985 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1986 int count = u32_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1987 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1988 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1989 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1990 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1991 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1992 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1993 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1994 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1995 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1996 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1997 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1998 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1999 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2000 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2001 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2002 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2003 else if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2004 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2005 /* Use the entire string, and count the number of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2006 characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2007 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2008 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2009 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2010 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2011 int count = u32_strmblen (arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2012 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2013 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2014 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2015 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2016 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2017 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2018 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2019 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2020 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2021 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2022 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2023 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2024 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2025 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2026 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2027 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2028 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2029 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2030 /* Use the entire string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2031 arg_end = arg + u32_strlen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2032 /* The number of characters doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2033 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2034 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2035
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2036 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2037 && !(dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2038 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2039 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2040 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2041 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2042 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2043 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2044
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2045 # if DCHAR_IS_UINT32_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2046 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2047 size_t n = arg_end - arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2048 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2049 DCHAR_CPY (result + length, arg, n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2050 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2051 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2052 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2053 { /* Convert. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2054 DCHAR_T *converted = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2055 size_t converted_len = allocated - length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2056 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2057 /* Convert from UTF-32 to locale encoding. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2058 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2059 u32_conv_to_encoding (locale_charset (),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2060 iconveh_question_mark,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2061 arg, arg_end - arg, NULL,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2062 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2063 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2064 /* Convert from UTF-32 to UTF-8/UTF-16. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2065 converted =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2066 U32_TO_DCHAR (arg, arg_end - arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2067 converted, &converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2068 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2069 if (converted == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2070 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2071 int saved_errno = errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2072 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2073 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2074 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2075 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2076 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2077 errno = saved_errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2078 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2079 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2080 if (converted != result + length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2081 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2082 ENSURE_ALLOCATION (xsum (length, converted_len));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2083 DCHAR_CPY (result + length, converted, converted_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2084 free (converted);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2085 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2086 length += converted_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2087 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2088 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2089
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2090 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2091 && (dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2092 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2093 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2094 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2095 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2096 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2097 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2098 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2099 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2100
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2101 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2102 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2103 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2104 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2105 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2106 #if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2107 else if (dp->conversion == 's'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2108 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2109 && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2110 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2111 && a.arg[dp->arg_index].type == TYPE_WIDE_STRING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2112 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2113 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2114 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2115 /* The normal handling of the 's' directive below requires
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2116 allocating a temporary buffer. The determination of its
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2117 length (tmp_length), in the case when a precision is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2118 specified, below requires a conversion between a char[]
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2119 string and a wchar_t[] wide string. It could be done, but
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2120 we have no guarantee that the implementation of sprintf will
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2121 use the exactly same algorithm. Without this guarantee, it
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2122 is possible to have buffer overrun bugs. In order to avoid
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2123 such bugs, we implement the entire processing of the 's'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2124 directive ourselves. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2125 int flags = dp->flags;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2126 int has_width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2127 size_t width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2128 int has_precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2129 size_t precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2130
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2131 has_width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2132 width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2133 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2134 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2135 if (dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2136 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2137 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2138
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2139 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2140 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2141 arg = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2142 if (arg < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2143 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2144 /* "A negative field width is taken as a '-' flag
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2145 followed by a positive field width." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2146 flags |= FLAG_LEFT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2147 width = (unsigned int) (-arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2148 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2149 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2150 width = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2151 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2152 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2153 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2154 const FCHAR_T *digitp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2155
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2156 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2157 width = xsum (xtimes (width, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2158 while (digitp != dp->width_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2159 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2160 has_width = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2161 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2162
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2163 has_precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2164 precision = 6;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2165 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2166 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2167 if (dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2168 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2169 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2170
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2171 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2172 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2173 arg = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2174 /* "A negative precision is taken as if the precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2175 were omitted." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2176 if (arg >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2177 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2178 precision = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2179 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2180 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2181 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2182 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2183 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2184 const FCHAR_T *digitp = dp->precision_start + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2185
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2186 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2187 while (digitp != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2188 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2189 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2190 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2191 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2192
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2193 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2194 /* %s in vasnwprintf. See the specification of fwprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2195 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2196 const char *arg = a.arg[dp->arg_index].a.a_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2197 const char *arg_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2198 size_t characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2199
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2200 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2201 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2202 /* Use only as many bytes as needed to produce PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2203 wide characters, from the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2204 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2205 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2206 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2207 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2208 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2209 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2210 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2211 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2212 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2213 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2214 count = mbrlen (arg_end, MB_CUR_MAX, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2215 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2216 count = mblen (arg_end, MB_CUR_MAX);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2217 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2218 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2219 /* Found the terminating NUL. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2220 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2221 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2222 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2223 /* Invalid or incomplete multibyte character. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2224 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2225 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2226 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2227 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2228 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2229 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2230 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2231 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2232 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2233 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2234 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2235 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2236 else if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2237 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2238 /* Use the entire string, and count the number of wide
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2239 characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2240 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2241 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2242 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2243 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2244 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2245 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2246 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2247 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2248 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2249 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2250 count = mbrlen (arg_end, MB_CUR_MAX, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2251 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2252 count = mblen (arg_end, MB_CUR_MAX);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2253 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2254 if (count == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2255 /* Found the terminating NUL. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2256 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2257 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2258 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2259 /* Invalid or incomplete multibyte character. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2260 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2261 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2262 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2263 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2264 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2265 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2266 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2267 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2268 arg_end += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2269 characters++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2270 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2271 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2272 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2273 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2274 /* Use the entire string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2275 arg_end = arg + strlen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2276 /* The number of characters doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2277 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2278 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2279
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2280 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2281 && !(dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2282 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2283 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2284 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2285 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2286 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2287 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2288
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2289 if (has_precision || has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2290 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2291 /* We know the number of wide characters in advance. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2292 size_t remaining;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2293 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2294 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2295 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2296 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2297 ENSURE_ALLOCATION (xsum (length, characters));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2298 for (remaining = characters; remaining > 0; remaining--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2299 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2300 wchar_t wc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2301 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2302 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2303 count = mbrtowc (&wc, arg, arg_end - arg, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2304 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2305 count = mbtowc (&wc, arg, arg_end - arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2306 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2307 if (count <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2308 /* mbrtowc not consistent with mbrlen, or mbtowc
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2309 not consistent with mblen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2310 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2311 result[length++] = wc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2312 arg += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2313 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2314 if (!(arg == arg_end))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2315 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2316 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2317 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2318 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2319 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2320 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2321 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2322 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2323 while (arg < arg_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2324 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2325 wchar_t wc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2326 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2327 # if HAVE_MBRTOWC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2328 count = mbrtowc (&wc, arg, arg_end - arg, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2329 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2330 count = mbtowc (&wc, arg, arg_end - arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2331 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2332 if (count <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2333 /* mbrtowc not consistent with mbrlen, or mbtowc
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2334 not consistent with mblen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2335 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2336 ENSURE_ALLOCATION (xsum (length, 1));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2337 result[length++] = wc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2338 arg += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2339 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2340 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2341
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2342 if (has_width && width > characters
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2343 && (dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2344 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2345 size_t n = width - characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2346 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2347 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2348 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2349 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2350 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2351 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2352 /* %ls in vasnprintf. See the specification of fprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2353 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2354 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2355 const wchar_t *arg_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2356 size_t characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2357 # if !DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2358 /* This code assumes that TCHAR_T is 'char'. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2359 typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2360 TCHAR_T *tmpsrc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2361 DCHAR_T *tmpdst;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2362 size_t tmpdst_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2363 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2364 size_t w;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2365
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2366 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2367 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2368 /* Use only as many wide characters as needed to produce
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2369 at most PRECISION bytes, from the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2370 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2371 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2372 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2373 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2374 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2375 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2376 while (precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2377 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2378 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2379 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2380
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2381 if (*arg_end == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2382 /* Found the terminating null wide character. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2383 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2384 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2385 count = wcrtomb (cbuf, *arg_end, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2386 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2387 count = wctomb (cbuf, *arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2388 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2389 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2390 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2391 /* Cannot convert. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2392 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2393 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2394 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2395 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2396 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2397 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2398 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2399 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2400 if (precision < count)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2401 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2402 arg_end++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2403 characters += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2404 precision -= count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2405 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2406 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2407 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2408 else if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2409 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2410 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2411 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2412 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2413 /* Use the entire string, and count the number of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2414 bytes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2415 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2416 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2417 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2418 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2419 arg_end = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2420 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2421 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2422 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2423 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2424 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2425
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2426 if (*arg_end == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2427 /* Found the terminating null wide character. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2428 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2429 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2430 count = wcrtomb (cbuf, *arg_end, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2431 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2432 count = wctomb (cbuf, *arg_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2433 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2434 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2435 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2436 /* Cannot convert. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2437 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2438 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2439 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2440 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2441 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2442 errno = EILSEQ;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2443 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2444 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2445 arg_end++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2446 characters += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2447 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2448 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2449 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2450 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2451 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2452 /* Use the entire string. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2453 arg_end = arg + local_wcslen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2454 /* The number of bytes doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2455 characters = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2456 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2457 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2458
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2459 # if !DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2460 /* Convert the string into a piece of temporary memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2461 tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2462 if (tmpsrc == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2463 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2464 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2465 TCHAR_T *tmpptr = tmpsrc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2466 size_t remaining;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2467 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2468 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2469 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2470 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2471 for (remaining = characters; remaining > 0; )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2472 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2473 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2474 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2475
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2476 if (*arg == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2477 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2478 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2479 count = wcrtomb (cbuf, *arg, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2480 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2481 count = wctomb (cbuf, *arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2482 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2483 if (count <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2484 /* Inconsistency. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2485 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2486 memcpy (tmpptr, cbuf, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2487 tmpptr += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2488 arg++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2489 remaining -= count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2490 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2491 if (!(arg == arg_end))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2492 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2493 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2494
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2495 /* Convert from TCHAR_T[] to DCHAR_T[]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2496 tmpdst =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2497 DCHAR_CONV_FROM_ENCODING (locale_charset (),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2498 iconveh_question_mark,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2499 tmpsrc, characters,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2500 NULL,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2501 NULL, &tmpdst_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2502 if (tmpdst == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2503 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2504 int saved_errno = errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2505 free (tmpsrc);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2506 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2507 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2508 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2509 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2510 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2511 errno = saved_errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2512 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2513 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2514 free (tmpsrc);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2515 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2516
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2517 if (has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2518 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2519 # if ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2520 /* Outside POSIX, it's preferrable to compare the width
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2521 against the number of _characters_ of the converted
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2522 value. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2523 w = DCHAR_MBSNLEN (result + length, characters);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2524 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2525 /* The width is compared against the number of _bytes_
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2526 of the converted value, says POSIX. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2527 w = characters;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2528 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2529 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2530 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2531 /* w doesn't matter. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2532 w = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2533
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2534 if (has_width && width > w
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2535 && !(dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2536 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2537 size_t n = width - w;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2538 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2539 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2540 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2541 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2542
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2543 # if DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2544 if (has_precision || has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2545 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2546 /* We know the number of bytes in advance. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2547 size_t remaining;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2548 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2549 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2550 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2551 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2552 ENSURE_ALLOCATION (xsum (length, characters));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2553 for (remaining = characters; remaining > 0; )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2554 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2555 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2556 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2557
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2558 if (*arg == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2559 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2560 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2561 count = wcrtomb (cbuf, *arg, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2562 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2563 count = wctomb (cbuf, *arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2564 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2565 if (count <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2566 /* Inconsistency. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2567 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2568 memcpy (result + length, cbuf, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2569 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2570 arg++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2571 remaining -= count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2572 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2573 if (!(arg == arg_end))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2574 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2575 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2576 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2577 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2578 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2579 mbstate_t state;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2580 memset (&state, '\0', sizeof (mbstate_t));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2581 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2582 while (arg < arg_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2583 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2584 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2585 int count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2586
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2587 if (*arg == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2588 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2589 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2590 count = wcrtomb (cbuf, *arg, &state);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2591 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2592 count = wctomb (cbuf, *arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2593 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2594 if (count <= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2595 /* Inconsistency. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2596 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2597 ENSURE_ALLOCATION (xsum (length, count));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2598 memcpy (result + length, cbuf, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2599 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2600 arg++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2601 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2602 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2603 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2604 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2605 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2606 free (tmpdst);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2607 length += tmpdst_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2608 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2609
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2610 if (has_width && width > w
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2611 && (dp->flags & FLAG_LEFT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2612 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2613 size_t n = width - w;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2614 ENSURE_ALLOCATION (xsum (length, n));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2615 DCHAR_SET (result + length, ' ', n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2616 length += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2617 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2618 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2619 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2620 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2621 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2622 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2623 else if ((dp->conversion == 'a' || dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2624 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2625 && (0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2626 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2627 || a.arg[dp->arg_index].type == TYPE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2628 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2629 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2630 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2631 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2632 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2633 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2634 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2635 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2636 arg_type type = a.arg[dp->arg_index].type;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2637 int flags = dp->flags;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2638 int has_width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2639 size_t width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2640 int has_precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2641 size_t precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2642 size_t tmp_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2643 DCHAR_T tmpbuf[700];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2644 DCHAR_T *tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2645 DCHAR_T *pad_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2646 DCHAR_T *p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2647
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2648 has_width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2649 width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2650 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2651 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2652 if (dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2653 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2654 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2655
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2656 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2657 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2658 arg = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2659 if (arg < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2660 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2661 /* "A negative field width is taken as a '-' flag
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2662 followed by a positive field width." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2663 flags |= FLAG_LEFT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2664 width = (unsigned int) (-arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2665 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2666 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2667 width = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2668 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2669 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2670 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2671 const FCHAR_T *digitp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2672
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2673 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2674 width = xsum (xtimes (width, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2675 while (digitp != dp->width_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2676 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2677 has_width = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2678 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2679
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2680 has_precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2681 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2682 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2683 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2684 if (dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2685 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2686 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2687
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2688 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2689 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2690 arg = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2691 /* "A negative precision is taken as if the precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2692 were omitted." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2693 if (arg >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2694 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2695 precision = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2696 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2697 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2698 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2699 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2700 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2701 const FCHAR_T *digitp = dp->precision_start + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2702
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2703 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2704 while (digitp != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2705 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2706 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2707 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2708 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2709
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2710 /* Allocate a temporary buffer of sufficient size. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2711 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2712 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2713 (unsigned int) ((LDBL_DIG + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2714 * 0.831 /* decimal -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2715 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2716 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2717 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2718 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2719 (unsigned int) ((DBL_DIG + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2720 * 0.831 /* decimal -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2721 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2722 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2723 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2724 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2725 /* Account for sign, decimal point etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2726 tmp_length = xsum (tmp_length, 12);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2727
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2728 if (tmp_length < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2729 tmp_length = width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2730
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2731 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2732
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2733 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2734 tmp = tmpbuf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2735 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2736 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2737 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2738
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2739 if (size_overflow_p (tmp_memsize))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2740 /* Overflow, would lead to out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2741 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2742 tmp = (DCHAR_T *) malloc (tmp_memsize);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2743 if (tmp == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2744 /* Out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2745 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2746 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2747
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2748 pad_ptr = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2749 p = tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2750 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2751 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2752 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2753 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2754
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2755 if (isnanl (arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2756 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2757 if (dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2758 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2759 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2760 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2761 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2762 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2763 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2764 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2765 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2766 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2767 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2768 int sign = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2769 DECL_LONG_DOUBLE_ROUNDING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2770
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2771 BEGIN_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2772
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2773 if (signbit (arg)) /* arg < 0.0L or negative zero */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2774 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2775 sign = -1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2776 arg = -arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2777 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2778
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2779 if (sign < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2780 *p++ = '-';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2781 else if (flags & FLAG_SHOWSIGN)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2782 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2783 else if (flags & FLAG_SPACE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2784 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2785
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2786 if (arg > 0.0L && arg + arg == arg)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2787 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2788 if (dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2789 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2790 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2791 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2792 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2793 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2794 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2795 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2796 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2797 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2798 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2799 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2800 long double mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2801
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2802 if (arg > 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2803 mantissa = printf_frexpl (arg, &exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2804 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2805 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2806 exponent = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2807 mantissa = 0.0L;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2808 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2809
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2810 if (has_precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2811 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2812 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2813 /* Round the mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2814 long double tail = mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2815 size_t q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2816
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2817 for (q = precision; ; q--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2818 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2819 int digit = (int) tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2820 tail -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2821 if (q == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2822 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2823 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2824 tail = 1 - tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2825 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2826 tail = - tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2827 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2828 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2829 tail *= 16.0L;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2830 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2831 if (tail != 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2832 for (q = precision; q > 0; q--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2833 tail *= 0.0625L;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2834 mantissa += tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2835 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2836
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2837 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2838 *p++ = dp->conversion - 'A' + 'X';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2839 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2840 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2841 int digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2842
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2843 digit = (int) mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2844 mantissa -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2845 *p++ = '0' + digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2846 if ((flags & FLAG_ALT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2847 || mantissa > 0.0L || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2848 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2849 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2850 /* This loop terminates because we assume
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2851 that FLT_RADIX is a power of 2. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2852 while (mantissa > 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2853 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2854 mantissa *= 16.0L;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2855 digit = (int) mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2856 mantissa -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2857 *p++ = digit
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2858 + (digit < 10
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2859 ? '0'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2860 : dp->conversion - 10);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2861 if (precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2862 precision--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2863 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2864 while (precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2865 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2866 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2867 precision--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2868 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2869 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2870 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2871 *p++ = dp->conversion - 'A' + 'P';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2872 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2873 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2874 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2875 { '%', '+', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2876 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2877 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2878 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2879 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2880 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2881 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2882 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2883 sprintf ((char *) p, "%+d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2884 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2885 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2886 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2887 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2888 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2889 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2890 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2891 sprintf (expbuf, "%+d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2892 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2893 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2894 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2895 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2896 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2897
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2898 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2899 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2900 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2901 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2902 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2903 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2904 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2905 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2906 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2907 double arg = a.arg[dp->arg_index].a.a_double;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2908
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2909 if (isnand (arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2910 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2911 if (dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2912 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2913 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2914 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2915 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2916 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2917 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2918 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2919 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2920 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2921 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2922 int sign = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2923
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2924 if (signbit (arg)) /* arg < 0.0 or negative zero */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2925 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2926 sign = -1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2927 arg = -arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2928 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2929
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2930 if (sign < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2931 *p++ = '-';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2932 else if (flags & FLAG_SHOWSIGN)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2933 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2934 else if (flags & FLAG_SPACE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2935 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2936
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2937 if (arg > 0.0 && arg + arg == arg)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2938 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2939 if (dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2940 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2941 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2942 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2943 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2944 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2945 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2946 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2947 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2948 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2949 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2950 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2951 double mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2952
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2953 if (arg > 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2954 mantissa = printf_frexp (arg, &exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2955 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2956 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2957 exponent = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2958 mantissa = 0.0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2959 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2960
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2961 if (has_precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2962 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2963 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2964 /* Round the mantissa. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2965 double tail = mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2966 size_t q;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2967
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2968 for (q = precision; ; q--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2969 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2970 int digit = (int) tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2971 tail -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2972 if (q == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2973 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2974 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2975 tail = 1 - tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2976 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2977 tail = - tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2978 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2979 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2980 tail *= 16.0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2981 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2982 if (tail != 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2983 for (q = precision; q > 0; q--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2984 tail *= 0.0625;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2985 mantissa += tail;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2986 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2987
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2988 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2989 *p++ = dp->conversion - 'A' + 'X';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2990 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2991 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2992 int digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2993
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2994 digit = (int) mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2995 mantissa -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2996 *p++ = '0' + digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2997 if ((flags & FLAG_ALT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2998 || mantissa > 0.0 || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2999 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3000 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3001 /* This loop terminates because we assume
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3002 that FLT_RADIX is a power of 2. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3003 while (mantissa > 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3004 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3005 mantissa *= 16.0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3006 digit = (int) mantissa;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3007 mantissa -= digit;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3008 *p++ = digit
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3009 + (digit < 10
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3010 ? '0'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3011 : dp->conversion - 10);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3012 if (precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3013 precision--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3014 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3015 while (precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3016 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3017 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3018 precision--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3019 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3020 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3021 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3022 *p++ = dp->conversion - 'A' + 'P';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3023 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3024 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3025 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3026 { '%', '+', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3027 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3028 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3029 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3030 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3031 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3032 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3033 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3034 sprintf ((char *) p, "%+d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3035 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3036 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3037 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3038 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3039 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3040 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3041 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3042 sprintf (expbuf, "%+d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3043 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3044 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3045 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3046 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3047 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3048 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3049 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3050 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3051 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3052 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3053 /* The generated string now extends from tmp to p, with the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3054 zero padding insertion point being at pad_ptr. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3055 if (has_width && p - tmp < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3056 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3057 size_t pad = width - (p - tmp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3058 DCHAR_T *end = p + pad;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3059
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3060 if (flags & FLAG_LEFT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3061 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3062 /* Pad with spaces on the right. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3063 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3064 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3065 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3066 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3067 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3068 /* Pad with zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3069 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3070
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3071 while (p > pad_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3072 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3073 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3074 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3075 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3076 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3077 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3078 /* Pad with spaces on the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3079 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3080
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3081 while (p > tmp)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3082 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3083 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3084 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3085 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3086
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3087 p = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3088 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3089
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3090 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3091 size_t count = p - tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3092
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3093 if (count >= tmp_length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3094 /* tmp_length was incorrectly calculated - fix the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3095 code above! */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3096 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3097
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3098 /* Make room for the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3099 if (count >= allocated - length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3100 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3101 size_t n = xsum (length, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3102
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3103 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3104 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3105
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3106 /* Append the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3107 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3108 if (tmp != tmpbuf)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3109 free (tmp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3110 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3111 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3112 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3113 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3114 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3115 else if ((dp->conversion == 'f' || dp->conversion == 'F'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3116 || dp->conversion == 'e' || dp->conversion == 'E'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3117 || dp->conversion == 'g' || dp->conversion == 'G'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3118 || dp->conversion == 'a' || dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3119 && (0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3120 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3121 || a.arg[dp->arg_index].type == TYPE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3122 # elif NEED_PRINTF_INFINITE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3123 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3124 /* The systems (mingw) which produce wrong output
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3125 for Inf, -Inf, and NaN also do so for -0.0.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3126 Therefore we treat this case here as well. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3127 && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3128 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3129 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3130 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3131 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3132 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3133 /* Some systems produce wrong output for Inf,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3134 -Inf, and NaN. Some systems in this category
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3135 (IRIX 5.3) also do so for -0.0. Therefore we
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3136 treat this case here as well. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3137 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3138 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3139 ))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3140 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3141 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3142 arg_type type = a.arg[dp->arg_index].type;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3143 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3144 int flags = dp->flags;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3145 int has_width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3146 size_t width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3147 int has_precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3148 size_t precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3149 size_t tmp_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3150 DCHAR_T tmpbuf[700];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3151 DCHAR_T *tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3152 DCHAR_T *pad_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3153 DCHAR_T *p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3154
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3155 has_width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3156 width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3157 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3158 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3159 if (dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3160 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3161 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3162
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3163 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3164 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3165 arg = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3166 if (arg < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3167 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3168 /* "A negative field width is taken as a '-' flag
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3169 followed by a positive field width." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3170 flags |= FLAG_LEFT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3171 width = (unsigned int) (-arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3172 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3173 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3174 width = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3175 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3176 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3177 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3178 const FCHAR_T *digitp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3179
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3180 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3181 width = xsum (xtimes (width, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3182 while (digitp != dp->width_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3183 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3184 has_width = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3185 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3186
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3187 has_precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3188 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3189 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3190 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3191 if (dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3192 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3193 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3194
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3195 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3196 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3197 arg = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3198 /* "A negative precision is taken as if the precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3199 were omitted." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3200 if (arg >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3201 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3202 precision = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3203 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3204 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3205 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3206 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3207 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3208 const FCHAR_T *digitp = dp->precision_start + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3209
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3210 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3211 while (digitp != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3212 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3213 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3214 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3215 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3216
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3217 /* POSIX specifies the default precision to be 6 for %f, %F,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3218 %e, %E, but not for %g, %G. Implementations appear to use
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3219 the same default precision also for %g, %G. But for %a, %A,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3220 the default precision is 0. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3221 if (!has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3222 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3223 precision = 6;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3224
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3225 /* Allocate a temporary buffer of sufficient size. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3226 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3227 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3228 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3229 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3230 # elif NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3231 tmp_length = LDBL_DIG + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3232 # elif NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3233 tmp_length = DBL_DIG + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3234 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3235 tmp_length = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3236 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3237 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3238 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3239 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3240 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3241 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3242 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3243 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3244 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3245 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3246 if (!(isnanl (arg) || arg + arg == arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3247 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3248 /* arg is finite and nonzero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3249 int exponent = floorlog10l (arg < 0 ? -arg : arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3250 if (exponent >= 0 && tmp_length < exponent + precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3251 tmp_length = exponent + precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3252 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3253 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3254 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3255 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3256 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3257 if (type == TYPE_DOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3258 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3259 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3260 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3261 double arg = a.arg[dp->arg_index].a.a_double;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3262 if (!(isnand (arg) || arg + arg == arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3263 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3264 /* arg is finite and nonzero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3265 int exponent = floorlog10 (arg < 0 ? -arg : arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3266 if (exponent >= 0 && tmp_length < exponent + precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3267 tmp_length = exponent + precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3268 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3269 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3270 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3271 /* Account for sign, decimal point etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3272 tmp_length = xsum (tmp_length, 12);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3273
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3274 if (tmp_length < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3275 tmp_length = width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3276
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3277 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3278
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3279 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3280 tmp = tmpbuf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3281 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3282 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3283 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3284
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3285 if (size_overflow_p (tmp_memsize))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3286 /* Overflow, would lead to out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3287 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3288 tmp = (DCHAR_T *) malloc (tmp_memsize);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3289 if (tmp == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3290 /* Out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3291 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3292 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3293
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3294 pad_ptr = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3295 p = tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3296
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3297 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3298 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3299 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3300 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3301 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3302 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3303
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3304 if (isnanl (arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3305 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3306 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3307 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3308 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3309 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3310 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3311 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3312 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3313 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3314 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3315 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3316 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3317 int sign = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3318 DECL_LONG_DOUBLE_ROUNDING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3319
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3320 BEGIN_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3321
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3322 if (signbit (arg)) /* arg < 0.0L or negative zero */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3323 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3324 sign = -1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3325 arg = -arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3326 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3327
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3328 if (sign < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3329 *p++ = '-';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3330 else if (flags & FLAG_SHOWSIGN)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3331 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3332 else if (flags & FLAG_SPACE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3333 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3334
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3335 if (arg > 0.0L && arg + arg == arg)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3336 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3337 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3338 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3339 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3340 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3341 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3342 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3343 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3344 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3345 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3346 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3347 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3348 # if NEED_PRINTF_LONG_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3349 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3350
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3351 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3352 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3353 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3354 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3355
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3356 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3357 scale10_round_decimal_long_double (arg, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3358 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3359 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3360 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3361 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3362 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3363 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3364
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3365 if (ndigits > precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3366 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3367 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3368 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3369 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3370 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3371 while (ndigits > precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3372 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3373 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3374 /* Here ndigits <= precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3375 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3376 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3377 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3378 for (; precision > ndigits; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3379 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3380 while (ndigits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3381 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3382 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3383 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3384 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3385 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3386
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3387 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3388 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3389 else if (dp->conversion == 'e' || dp->conversion == 'E')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3390 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3391 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3392
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3393 if (arg == 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3394 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3395 exponent = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3396 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3397 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3398 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3399 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3400 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3401 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3402 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3403 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3404 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3405 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3406 /* arg > 0.0L. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3407 int adjusted;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3408 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3409 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3410
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3411 exponent = floorlog10l (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3412 adjusted = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3413 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3414 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3415 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3416 scale10_round_decimal_long_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3417 (int)precision - exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3418 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3419 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3420 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3421 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3422 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3423 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3424
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3425 if (ndigits == precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3426 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3427 if (ndigits < precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3428 || ndigits > precision + 2)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3429 /* The exponent was not guessed
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3430 precisely enough. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3431 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3432 if (adjusted)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3433 /* None of two values of exponent is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3434 the right one. Prevent an endless
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3435 loop. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3436 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3437 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3438 if (ndigits == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3439 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3440 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3441 exponent += 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3442 adjusted = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3443 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3444 /* Here ndigits = precision+1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3445 if (is_borderline (digits, precision))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3446 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3447 /* Maybe the exponent guess was too high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3448 and a smaller exponent can be reached
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3449 by turning a 10...0 into 9...9x. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3450 char *digits2 =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3451 scale10_round_decimal_long_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3452 (int)precision - exponent + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3453 if (digits2 == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3454 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3455 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3456 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3457 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3458 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3459 if (strlen (digits2) == precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3460 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3461 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3462 digits = digits2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3463 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3464 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3465 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3466 free (digits2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3467 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3468 /* Here ndigits = precision+1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3469
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3470 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3471 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3472 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3473 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3474 while (ndigits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3475 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3476 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3477 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3478 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3479 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3480
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3481 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3482 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3483
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3484 *p++ = dp->conversion; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3485 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3486 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3487 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3488 { '%', '+', '.', '2', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3489 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3490 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3491 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3492 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3493 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3494 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3495 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3496 sprintf ((char *) p, "%+.2d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3497 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3498 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3499 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3500 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3501 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3502 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3503 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3504 sprintf (expbuf, "%+.2d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3505 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3506 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3507 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3508 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3509 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3510 else if (dp->conversion == 'g' || dp->conversion == 'G')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3511 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3512 if (precision == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3513 precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3514 /* precision >= 1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3515
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3516 if (arg == 0.0L)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3517 /* The exponent is 0, >= -4, < precision.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3518 Use fixed-point notation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3519 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3520 size_t ndigits = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3521 /* Number of trailing zeroes that have to be
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3522 dropped. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3523 size_t nzeroes =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3524 (flags & FLAG_ALT ? 0 : precision - 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3525
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3526 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3527 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3528 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3529 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3530 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3531 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3532 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3533 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3534 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3535 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3536 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3537 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3538 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3539 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3540 /* arg > 0.0L. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3541 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3542 int adjusted;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3543 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3544 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3545 size_t nzeroes;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3546
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3547 exponent = floorlog10l (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3548 adjusted = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3549 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3550 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3551 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3552 scale10_round_decimal_long_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3553 (int)(precision - 1) - exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3554 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3555 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3556 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3557 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3558 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3559 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3560
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3561 if (ndigits == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3562 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3563 if (ndigits < precision - 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3564 || ndigits > precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3565 /* The exponent was not guessed
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3566 precisely enough. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3567 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3568 if (adjusted)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3569 /* None of two values of exponent is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3570 the right one. Prevent an endless
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3571 loop. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3572 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3573 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3574 if (ndigits < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3575 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3576 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3577 exponent += 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3578 adjusted = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3579 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3580 /* Here ndigits = precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3581 if (is_borderline (digits, precision - 1))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3582 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3583 /* Maybe the exponent guess was too high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3584 and a smaller exponent can be reached
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3585 by turning a 10...0 into 9...9x. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3586 char *digits2 =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3587 scale10_round_decimal_long_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3588 (int)(precision - 1) - exponent + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3589 if (digits2 == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3590 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3591 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3592 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3593 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3594 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3595 if (strlen (digits2) == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3596 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3597 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3598 digits = digits2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3599 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3600 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3601 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3602 free (digits2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3603 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3604 /* Here ndigits = precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3605
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3606 /* Determine the number of trailing zeroes
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3607 that have to be dropped. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3608 nzeroes = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3609 if ((flags & FLAG_ALT) == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3610 while (nzeroes < ndigits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3611 && digits[nzeroes] == '0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3612 nzeroes++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3613
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3614 /* The exponent is now determined. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3615 if (exponent >= -4
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3616 && exponent < (long)precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3617 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3618 /* Fixed-point notation:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3619 max(exponent,0)+1 digits, then the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3620 decimal point, then the remaining
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3621 digits without trailing zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3622 if (exponent >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3623 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3624 size_t count = exponent + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3625 /* Note: count <= precision = ndigits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3626 for (; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3627 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3628 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3629 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3630 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3631 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3632 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3633 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3634 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3635 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3636 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3637 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3638 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3639 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3640 size_t count = -exponent - 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3641 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3642 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3643 for (; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3644 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3645 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3646 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3647 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3648 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3649 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3650 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3651 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3652 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3653 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3654 /* Exponential notation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3655 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3656 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3657 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3658 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3659 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3660 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3661 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3662 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3663 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3664 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3665 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3666 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3667 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3668 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3669 { '%', '+', '.', '2', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3670 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3671 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3672 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3673 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3674 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3675 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3676 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3677 sprintf ((char *) p, "%+.2d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3678 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3679 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3680 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3681 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3682 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3683 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3684 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3685 sprintf (expbuf, "%+.2d", exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3686 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3687 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3688 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3689 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3690 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3691
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3692 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3693 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3694 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3695 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3696 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3697 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3698 /* arg is finite. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3699 if (!(arg == 0.0L))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3700 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3701
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3702 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3703
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3704 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3705 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3706 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3707 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3708 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3709 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3710 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3711 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3712 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3713 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3714 else if (dp->conversion == 'e' || dp->conversion == 'E')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3715 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3716 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3717 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3718 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3719 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3720 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3721 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3722 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3723 *p++ = dp->conversion; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3724 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3725 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3726 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3727 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3728 else if (dp->conversion == 'g' || dp->conversion == 'G')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3729 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3730 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3731 if (flags & FLAG_ALT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3732 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3733 size_t ndigits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3734 (precision > 0 ? precision - 1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3735 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3736 for (; ndigits > 0; --ndigits)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3737 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3738 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3739 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3740 else if (dp->conversion == 'a' || dp->conversion == 'A')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3741 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3742 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3743 *p++ = dp->conversion - 'A' + 'X';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3744 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3745 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3746 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3747 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3748 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3749 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3750 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3751 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3752 *p++ = dp->conversion - 'A' + 'P';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3753 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3754 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3755 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3756 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3757 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3758 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3759 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3760
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3761 END_LONG_DOUBLE_ROUNDING ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3762 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3763 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3764 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3765 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3766 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3767 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3768 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3769 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3770 double arg = a.arg[dp->arg_index].a.a_double;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3771
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3772 if (isnand (arg))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3773 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3774 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3775 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3776 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3777 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3778 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3779 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3780 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3781 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3782 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3783 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3784 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3785 int sign = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3786
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3787 if (signbit (arg)) /* arg < 0.0 or negative zero */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3788 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3789 sign = -1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3790 arg = -arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3791 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3792
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3793 if (sign < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3794 *p++ = '-';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3795 else if (flags & FLAG_SHOWSIGN)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3796 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3797 else if (flags & FLAG_SPACE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3798 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3799
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3800 if (arg > 0.0 && arg + arg == arg)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3801 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3802 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3803 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3804 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3805 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3806 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3807 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3808 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3809 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3810 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3811 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3812 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3813 # if NEED_PRINTF_DOUBLE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3814 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3815
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3816 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3817 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3818 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3819 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3820
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3821 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3822 scale10_round_decimal_double (arg, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3823 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3824 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3825 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3826
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3827 if (ndigits > precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3828 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3829 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3830 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3831 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3832 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3833 while (ndigits > precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3834 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3835 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3836 /* Here ndigits <= precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3837 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3838 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3839 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3840 for (; precision > ndigits; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3841 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3842 while (ndigits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3843 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3844 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3845 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3846 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3847 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3848
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3849 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3850 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3851 else if (dp->conversion == 'e' || dp->conversion == 'E')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3852 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3853 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3854
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3855 if (arg == 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3856 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3857 exponent = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3858 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3859 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3860 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3861 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3862 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3863 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3864 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3865 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3866 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3867 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3868 /* arg > 0.0. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3869 int adjusted;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3870 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3871 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3872
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3873 exponent = floorlog10 (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3874 adjusted = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3875 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3876 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3877 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3878 scale10_round_decimal_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3879 (int)precision - exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3880 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3881 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3882 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3883
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3884 if (ndigits == precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3885 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3886 if (ndigits < precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3887 || ndigits > precision + 2)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3888 /* The exponent was not guessed
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3889 precisely enough. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3890 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3891 if (adjusted)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3892 /* None of two values of exponent is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3893 the right one. Prevent an endless
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3894 loop. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3895 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3896 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3897 if (ndigits == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3898 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3899 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3900 exponent += 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3901 adjusted = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3902 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3903 /* Here ndigits = precision+1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3904 if (is_borderline (digits, precision))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3905 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3906 /* Maybe the exponent guess was too high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3907 and a smaller exponent can be reached
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3908 by turning a 10...0 into 9...9x. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3909 char *digits2 =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3910 scale10_round_decimal_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3911 (int)precision - exponent + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3912 if (digits2 == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3913 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3914 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3915 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3916 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3917 if (strlen (digits2) == precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3918 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3919 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3920 digits = digits2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3921 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3922 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3923 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3924 free (digits2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3925 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3926 /* Here ndigits = precision+1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3927
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3928 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3929 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3930 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3931 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3932 while (ndigits > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3933 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3934 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3935 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3936 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3937 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3938
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3939 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3940 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3941
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3942 *p++ = dp->conversion; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3943 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3944 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3945 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3946 /* Produce the same number of exponent digits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3947 as the native printf implementation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3948 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3949 { '%', '+', '.', '3', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3950 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3951 { '%', '+', '.', '2', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3952 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3953 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3954 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3955 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3956 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3957 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3958 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3959 static const char decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3960 /* Produce the same number of exponent digits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3961 as the native printf implementation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3962 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3963 "%+.3d";
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3964 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3965 "%+.2d";
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3966 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3967 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3968 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3969 sprintf ((char *) p, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3970 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3971 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3972 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3973 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3974 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3975 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3976 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3977 sprintf (expbuf, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3978 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3979 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3980 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3981 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3982 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3983 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3984 else if (dp->conversion == 'g' || dp->conversion == 'G')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3985 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3986 if (precision == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3987 precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3988 /* precision >= 1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3989
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3990 if (arg == 0.0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3991 /* The exponent is 0, >= -4, < precision.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3992 Use fixed-point notation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3993 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3994 size_t ndigits = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3995 /* Number of trailing zeroes that have to be
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3996 dropped. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3997 size_t nzeroes =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3998 (flags & FLAG_ALT ? 0 : precision - 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3999
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4000 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4001 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4002 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4003 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4004 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4005 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4006 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4007 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4008 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4009 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4010 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4011 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4012 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4013 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4014 /* arg > 0.0. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4015 int exponent;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4016 int adjusted;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4017 char *digits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4018 size_t ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4019 size_t nzeroes;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4020
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4021 exponent = floorlog10 (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4022 adjusted = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4023 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4024 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4025 digits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4026 scale10_round_decimal_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4027 (int)(precision - 1) - exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4028 if (digits == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4029 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4030 ndigits = strlen (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4031
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4032 if (ndigits == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4033 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4034 if (ndigits < precision - 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4035 || ndigits > precision + 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4036 /* The exponent was not guessed
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4037 precisely enough. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4038 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4039 if (adjusted)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4040 /* None of two values of exponent is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4041 the right one. Prevent an endless
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4042 loop. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4043 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4044 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4045 if (ndigits < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4046 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4047 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4048 exponent += 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4049 adjusted = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4050 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4051 /* Here ndigits = precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4052 if (is_borderline (digits, precision - 1))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4053 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4054 /* Maybe the exponent guess was too high
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4055 and a smaller exponent can be reached
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4056 by turning a 10...0 into 9...9x. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4057 char *digits2 =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4058 scale10_round_decimal_double (arg,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4059 (int)(precision - 1) - exponent + 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4060 if (digits2 == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4061 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4062 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4063 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4064 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4065 if (strlen (digits2) == precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4066 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4067 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4068 digits = digits2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4069 exponent -= 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4070 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4071 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4072 free (digits2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4073 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4074 /* Here ndigits = precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4075
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4076 /* Determine the number of trailing zeroes
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4077 that have to be dropped. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4078 nzeroes = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4079 if ((flags & FLAG_ALT) == 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4080 while (nzeroes < ndigits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4081 && digits[nzeroes] == '0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4082 nzeroes++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4083
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4084 /* The exponent is now determined. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4085 if (exponent >= -4
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4086 && exponent < (long)precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4087 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4088 /* Fixed-point notation:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4089 max(exponent,0)+1 digits, then the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4090 decimal point, then the remaining
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4091 digits without trailing zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4092 if (exponent >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4093 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4094 size_t count = exponent + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4095 /* Note: count <= precision = ndigits. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4096 for (; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4097 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4098 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4099 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4100 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4101 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4102 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4103 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4104 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4105 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4106 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4107 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4108 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4109 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4110 size_t count = -exponent - 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4111 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4112 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4113 for (; count > 0; count--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4114 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4115 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4116 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4117 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4118 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4119 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4120 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4121 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4122 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4123 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4124 /* Exponential notation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4125 *p++ = digits[--ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4126 if ((flags & FLAG_ALT) || ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4127 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4128 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4129 while (ndigits > nzeroes)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4130 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4131 --ndigits;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4132 *p++ = digits[ndigits];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4133 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4134 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4135 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4136 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4137 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4138 static const wchar_t decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4139 /* Produce the same number of exponent digits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4140 as the native printf implementation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4141 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4142 { '%', '+', '.', '3', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4143 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4144 { '%', '+', '.', '2', 'd', '\0' };
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4145 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4146 SNPRINTF (p, 6 + 1, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4147 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4148 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4149 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4150 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4151 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4152 static const char decimal_format[] =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4153 /* Produce the same number of exponent digits
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4154 as the native printf implementation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4155 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4156 "%+.3d";
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4157 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4158 "%+.2d";
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4159 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4160 if (sizeof (DCHAR_T) == 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4161 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4162 sprintf ((char *) p, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4163 while (*p != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4164 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4165 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4166 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4167 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4168 char expbuf[6 + 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4169 const char *ep;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4170 sprintf (expbuf, decimal_format, exponent);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4171 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4172 p++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4173 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4174 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4175 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4176 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4177
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4178 free (digits);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4179 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4180 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4181 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4182 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4183 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4184 /* arg is finite. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4185 if (!(arg == 0.0))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4186 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4187
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4188 pad_ptr = p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4189
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4190 if (dp->conversion == 'f' || dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4191 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4192 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4193 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4194 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4195 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4196 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4197 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4198 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4199 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4200 else if (dp->conversion == 'e' || dp->conversion == 'E')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4201 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4202 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4203 if ((flags & FLAG_ALT) || precision > 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4204 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4205 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4206 for (; precision > 0; precision--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4207 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4208 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4209 *p++ = dp->conversion; /* 'e' or 'E' */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4210 *p++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4211 /* Produce the same number of exponent digits as
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4212 the native printf implementation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4213 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4214 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4215 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4216 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4217 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4218 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4219 else if (dp->conversion == 'g' || dp->conversion == 'G')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4220 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4221 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4222 if (flags & FLAG_ALT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4223 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4224 size_t ndigits =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4225 (precision > 0 ? precision - 1 : 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4226 *p++ = decimal_point_char ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4227 for (; ndigits > 0; --ndigits)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4228 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4229 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4230 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4231 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4232 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4233 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4234 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4235 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4236 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4237 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4238
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4239 /* The generated string now extends from tmp to p, with the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4240 zero padding insertion point being at pad_ptr. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4241 if (has_width && p - tmp < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4242 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4243 size_t pad = width - (p - tmp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4244 DCHAR_T *end = p + pad;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4245
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4246 if (flags & FLAG_LEFT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4247 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4248 /* Pad with spaces on the right. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4249 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4250 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4251 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4252 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4253 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4254 /* Pad with zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4255 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4256
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4257 while (p > pad_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4258 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4259 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4260 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4261 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4262 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4263 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4264 /* Pad with spaces on the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4265 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4266
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4267 while (p > tmp)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4268 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4269 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4270 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4271 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4272
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4273 p = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4274 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4275
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4276 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4277 size_t count = p - tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4278
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4279 if (count >= tmp_length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4280 /* tmp_length was incorrectly calculated - fix the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4281 code above! */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4282 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4283
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4284 /* Make room for the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4285 if (count >= allocated - length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4286 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4287 size_t n = xsum (length, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4288
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4289 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4290 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4291
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4292 /* Append the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4293 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4294 if (tmp != tmpbuf)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4295 free (tmp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4296 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4297 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4298 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4299 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4300 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4301 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4302 arg_type type = a.arg[dp->arg_index].type;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4303 int flags = dp->flags;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4304 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4305 int has_width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4306 size_t width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4307 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4308 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4309 int has_precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4310 size_t precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4311 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4312 #if NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4313 int prec_ourselves;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4314 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4315 # define prec_ourselves 0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4316 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4317 #if NEED_PRINTF_FLAG_LEFTADJUST
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4318 # define pad_ourselves 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4319 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4320 int pad_ourselves;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4321 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4322 # define pad_ourselves 0
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4323 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4324 TCHAR_T *fbp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4325 unsigned int prefix_count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4326 int prefixes[2] IF_LINT (= { 0 });
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4327 #if !USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4328 size_t tmp_length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4329 TCHAR_T tmpbuf[700];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4330 TCHAR_T *tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4331 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4332
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4333 #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4334 has_width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4335 width = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4336 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4337 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4338 if (dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4339 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4340 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4341
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4342 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4343 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4344 arg = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4345 if (arg < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4346 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4347 /* "A negative field width is taken as a '-' flag
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4348 followed by a positive field width." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4349 flags |= FLAG_LEFT;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4350 width = (unsigned int) (-arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4351 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4352 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4353 width = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4354 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4355 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4356 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4357 const FCHAR_T *digitp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4358
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4359 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4360 width = xsum (xtimes (width, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4361 while (digitp != dp->width_end);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4362 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4363 has_width = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4364 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4365 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4366
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4367 #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4368 has_precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4369 precision = 6;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4370 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4371 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4372 if (dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4373 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4374 int arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4375
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4376 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4377 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4378 arg = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4379 /* "A negative precision is taken as if the precision
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4380 were omitted." */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4381 if (arg >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4382 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4383 precision = arg;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4384 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4385 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4386 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4387 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4388 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4389 const FCHAR_T *digitp = dp->precision_start + 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4390
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4391 precision = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4392 while (digitp != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4393 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4394 has_precision = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4395 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4396 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4397 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4398
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4399 /* Decide whether to handle the precision ourselves. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4400 #if NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4401 switch (dp->conversion)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4402 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4403 case 'd': case 'i': case 'u':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4404 case 'o':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4405 case 'x': case 'X': case 'p':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4406 prec_ourselves = has_precision && (precision > 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4407 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4408 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4409 prec_ourselves = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4410 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4411 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4412 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4413
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4414 /* Decide whether to perform the padding ourselves. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4415 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4416 switch (dp->conversion)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4417 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4418 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4419 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4420 to perform the padding after this conversion. Functions
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4421 with unistdio extensions perform the padding based on
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4422 character count rather than element count. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4423 case 'c': case 's':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4424 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4425 # if NEED_PRINTF_FLAG_ZERO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4426 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4427 case 'a': case 'A':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4428 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4429 pad_ourselves = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4430 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4431 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4432 pad_ourselves = prec_ourselves;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4433 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4434 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4435 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4436
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4437 #if !USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4438 /* Allocate a temporary buffer of sufficient size for calling
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4439 sprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4440 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4441 switch (dp->conversion)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4442 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4443
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4444 case 'd': case 'i': case 'u':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4445 # if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4446 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4447 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4448 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4449 * 0.30103 /* binary -> decimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4450 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4451 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4452 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4453 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4454 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4455 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4456 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4457 * 0.30103 /* binary -> decimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4458 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4459 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4460 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4461 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4462 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4463 * 0.30103 /* binary -> decimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4464 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4465 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4466 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4467 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4468 /* Multiply by 2, as an estimate for FLAG_GROUP. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4469 tmp_length = xsum (tmp_length, tmp_length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4470 /* Add 1, to account for a leading sign. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4471 tmp_length = xsum (tmp_length, 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4472 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4473
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4474 case 'o':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4475 # if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4476 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4477 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4478 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4479 * 0.333334 /* binary -> octal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4480 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4481 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4482 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4483 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4484 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4485 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4486 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4487 * 0.333334 /* binary -> octal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4488 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4489 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4490 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4491 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4492 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4493 * 0.333334 /* binary -> octal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4494 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4495 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4496 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4497 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4498 /* Add 1, to account for a leading sign. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4499 tmp_length = xsum (tmp_length, 1);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4500 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4501
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4502 case 'x': case 'X':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4503 # if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4504 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4505 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4506 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4507 * 0.25 /* binary -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4508 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4509 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4510 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4511 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4512 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4513 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4514 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4515 * 0.25 /* binary -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4516 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4517 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4518 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4519 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4520 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4521 * 0.25 /* binary -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4522 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4523 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4524 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4525 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4526 /* Add 2, to account for a leading sign or alternate form. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4527 tmp_length = xsum (tmp_length, 2);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4528 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4529
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4530 case 'f': case 'F':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4531 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4532 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4533 (unsigned int) (LDBL_MAX_EXP
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4534 * 0.30103 /* binary -> decimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4535 * 2 /* estimate for FLAG_GROUP */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4536 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4537 + 1 /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4538 + 10; /* sign, decimal point etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4539 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4540 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4541 (unsigned int) (DBL_MAX_EXP
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4542 * 0.30103 /* binary -> decimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4543 * 2 /* estimate for FLAG_GROUP */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4544 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4545 + 1 /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4546 + 10; /* sign, decimal point etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4547 tmp_length = xsum (tmp_length, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4548 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4549
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4550 case 'e': case 'E': case 'g': case 'G':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4551 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4552 12; /* sign, decimal point, exponent etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4553 tmp_length = xsum (tmp_length, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4554 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4555
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4556 case 'a': case 'A':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4557 if (type == TYPE_LONGDOUBLE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4558 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4559 (unsigned int) (LDBL_DIG
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4560 * 0.831 /* decimal -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4561 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4562 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4563 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4564 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4565 (unsigned int) (DBL_DIG
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4566 * 0.831 /* decimal -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4567 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4568 + 1; /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4569 if (tmp_length < precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4570 tmp_length = precision;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4571 /* Account for sign, decimal point etc. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4572 tmp_length = xsum (tmp_length, 12);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4573 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4574
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4575 case 'c':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4576 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4577 if (type == TYPE_WIDE_CHAR)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4578 tmp_length = MB_CUR_MAX;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4579 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4580 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4581 tmp_length = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4582 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4583
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4584 case 's':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4585 # if HAVE_WCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4586 if (type == TYPE_WIDE_STRING)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4587 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4588 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4589 /* ISO C says about %ls in fwprintf:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4590 "If the precision is not specified or is greater
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4591 than the size of the array, the array shall
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4592 contain a null wide character."
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4593 So if there is a precision, we must not use
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4594 wcslen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4595 const wchar_t *arg =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4596 a.arg[dp->arg_index].a.a_wide_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4597
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4598 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4599 tmp_length = local_wcsnlen (arg, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4600 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4601 tmp_length = local_wcslen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4602 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4603 /* ISO C says about %ls in fprintf:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4604 "If a precision is specified, no more than that
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4605 many bytes are written (including shift
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4606 sequences, if any), and the array shall contain
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4607 a null wide character if, to equal the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4608 multibyte character sequence length given by
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4609 the precision, the function would need to
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4610 access a wide character one past the end of the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4611 array."
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4612 So if there is a precision, we must not use
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4613 wcslen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4614 /* This case has already been handled above. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4615 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4616 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4617 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4618 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4619 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4620 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4621 # if WIDE_CHAR_VERSION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4622 /* ISO C says about %s in fwprintf:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4623 "If the precision is not specified or is greater
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4624 than the size of the converted array, the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4625 converted array shall contain a null wide
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4626 character."
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4627 So if there is a precision, we must not use
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4628 strlen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4629 /* This case has already been handled above. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4630 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4631 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4632 /* ISO C says about %s in fprintf:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4633 "If the precision is not specified or greater
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4634 than the size of the array, the array shall
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4635 contain a null character."
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4636 So if there is a precision, we must not use
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4637 strlen. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4638 const char *arg = a.arg[dp->arg_index].a.a_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4639
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4640 if (has_precision)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4641 tmp_length = local_strnlen (arg, precision);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4642 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4643 tmp_length = strlen (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4644 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4645 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4646 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4647
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4648 case 'p':
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4649 tmp_length =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4650 (unsigned int) (sizeof (void *) * CHAR_BIT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4651 * 0.25 /* binary -> hexadecimal */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4652 )
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4653 + 1 /* turn floor into ceil */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4654 + 2; /* account for leading 0x */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4655 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4656
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4657 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4658 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4659 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4660
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4661 if (!pad_ourselves)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4662 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4663 # if ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4664 /* Padding considers the number of characters, therefore
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4665 the number of elements after padding may be
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4666 > max (tmp_length, width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4667 but is certainly
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4668 <= tmp_length + width. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4669 tmp_length = xsum (tmp_length, width);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4670 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4671 /* Padding considers the number of elements,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4672 says POSIX. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4673 if (tmp_length < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4674 tmp_length = width;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4675 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4676 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4677
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4678 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4679 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4680
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4681 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4682 tmp = tmpbuf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4683 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4684 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4685 size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4686
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4687 if (size_overflow_p (tmp_memsize))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4688 /* Overflow, would lead to out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4689 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4690 tmp = (TCHAR_T *) malloc (tmp_memsize);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4691 if (tmp == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4692 /* Out of memory. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4693 goto out_of_memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4694 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4695 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4696
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4697 /* Construct the format string for calling snprintf or
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4698 sprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4699 fbp = buf;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4700 *fbp++ = '%';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4701 #if NEED_PRINTF_FLAG_GROUPING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4702 /* The underlying implementation doesn't support the ' flag.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4703 Produce no grouping characters in this case; this is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4704 acceptable because the grouping is locale dependent. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4705 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4706 if (flags & FLAG_GROUP)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4707 *fbp++ = '\'';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4708 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4709 if (flags & FLAG_LEFT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4710 *fbp++ = '-';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4711 if (flags & FLAG_SHOWSIGN)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4712 *fbp++ = '+';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4713 if (flags & FLAG_SPACE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4714 *fbp++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4715 if (flags & FLAG_ALT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4716 *fbp++ = '#';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4717 if (!pad_ourselves)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4718 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4719 if (flags & FLAG_ZERO)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4720 *fbp++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4721 if (dp->width_start != dp->width_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4722 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4723 size_t n = dp->width_end - dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4724 /* The width specification is known to consist only
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4725 of standard ASCII characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4726 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4727 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4728 memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4729 fbp += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4730 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4731 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4732 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4733 const FCHAR_T *mp = dp->width_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4734 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4735 *fbp++ = (unsigned char) *mp++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4736 while (--n > 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4737 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4738 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4739 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4740 if (!prec_ourselves)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4741 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4742 if (dp->precision_start != dp->precision_end)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4743 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4744 size_t n = dp->precision_end - dp->precision_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4745 /* The precision specification is known to consist only
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4746 of standard ASCII characters. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4747 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4748 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4749 memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4750 fbp += n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4751 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4752 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4753 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4754 const FCHAR_T *mp = dp->precision_start;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4755 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4756 *fbp++ = (unsigned char) *mp++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4757 while (--n > 0);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4758 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4759 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4760 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4761
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4762 switch (type)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4763 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4764 #if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4765 case TYPE_LONGLONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4766 case TYPE_ULONGLONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4767 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4768 *fbp++ = 'I';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4769 *fbp++ = '6';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4770 *fbp++ = '4';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4771 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4772 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4773 *fbp++ = 'l';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4774 /*FALLTHROUGH*/
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4775 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4776 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4777 case TYPE_LONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4778 case TYPE_ULONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4779 #if HAVE_WINT_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4780 case TYPE_WIDE_CHAR:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4781 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4782 #if HAVE_WCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4783 case TYPE_WIDE_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4784 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4785 *fbp++ = 'l';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4786 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4787 case TYPE_LONGDOUBLE:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4788 *fbp++ = 'L';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4789 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4790 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4791 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4792 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4793 #if NEED_PRINTF_DIRECTIVE_F
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4794 if (dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4795 *fbp = 'f';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4796 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4797 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4798 *fbp = dp->conversion;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4799 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4800 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4801 fbp[1] = '%';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4802 fbp[2] = 'n';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4803 fbp[3] = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4804 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4805 /* On glibc2 systems from glibc >= 2.3 - probably also older
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4806 ones - we know that snprintf's returns value conforms to
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4807 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4808 Therefore we can avoid using %n in this situation.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4809 On glibc2 systems from 2004-10-18 or newer, the use of %n
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4810 in format strings in writable memory may crash the program
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4811 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4812 in this situation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4813 /* On native Win32 systems (such as mingw), we can avoid using
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4814 %n because:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4815 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4816 snprintf does not write more than the specified number
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4817 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4818 '4', '5', '6' into buf, not '4', '5', '\0'.)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4819 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4820 allows us to recognize the case of an insufficient
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4821 buffer size: it returns -1 in this case.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4822 On native Win32 systems (such as mingw) where the OS is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4823 Windows Vista, the use of %n in format strings by default
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4824 crashes the program. See
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4825 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4826 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4827 So we should avoid %n in this situation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4828 fbp[1] = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4829 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4830 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4831 fbp[1] = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4832 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4833
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4834 /* Construct the arguments for calling snprintf or sprintf. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4835 prefix_count = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4836 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4837 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4838 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4839 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4840 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4841 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4842 if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4843 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4844 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4845 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4846 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4847 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4848
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4849 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4850 /* The SNPRINTF result is appended after result[0..length].
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4851 The latter is an array of DCHAR_T; SNPRINTF appends an
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4852 array of TCHAR_T to it. This is possible because
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4853 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4854 alignof (TCHAR_T) <= alignof (DCHAR_T). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4855 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4856 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4857 where an snprintf() with maxlen==1 acts like sprintf(). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4858 ENSURE_ALLOCATION (xsum (length,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4859 (2 + TCHARS_PER_DCHAR - 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4860 / TCHARS_PER_DCHAR));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4861 /* Prepare checking whether snprintf returns the count
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4862 via %n. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4863 *(TCHAR_T *) (result + length) = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4864 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4865
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4866 for (;;)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4867 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4868 int count = -1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4869
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4870 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4871 int retcount = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4872 size_t maxlen = allocated - length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4873 /* SNPRINTF can fail if its second argument is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4874 > INT_MAX. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4875 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4876 maxlen = INT_MAX / TCHARS_PER_DCHAR;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4877 maxlen = maxlen * TCHARS_PER_DCHAR;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4878 # define SNPRINTF_BUF(arg) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4879 switch (prefix_count) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4880 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4881 case 0: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4882 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4883 maxlen, buf, \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4884 arg, &count); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4885 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4886 case 1: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4887 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4888 maxlen, buf, \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4889 prefixes[0], arg, &count); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4890 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4891 case 2: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4892 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4893 maxlen, buf, \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4894 prefixes[0], prefixes[1], arg, \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4895 &count); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4896 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4897 default: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4898 abort (); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4899 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4900 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4901 # define SNPRINTF_BUF(arg) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4902 switch (prefix_count) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4903 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4904 case 0: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4905 count = sprintf (tmp, buf, arg); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4906 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4907 case 1: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4908 count = sprintf (tmp, buf, prefixes[0], arg); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4909 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4910 case 2: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4911 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4912 arg); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4913 break; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4914 default: \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4915 abort (); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4916 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4917 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4918
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4919 switch (type)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4920 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4921 case TYPE_SCHAR:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4922 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4923 int arg = a.arg[dp->arg_index].a.a_schar;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4924 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4925 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4926 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4927 case TYPE_UCHAR:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4928 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4929 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4930 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4931 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4932 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4933 case TYPE_SHORT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4934 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4935 int arg = a.arg[dp->arg_index].a.a_short;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4936 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4937 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4938 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4939 case TYPE_USHORT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4940 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4941 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4942 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4943 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4944 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4945 case TYPE_INT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4946 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4947 int arg = a.arg[dp->arg_index].a.a_int;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4948 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4949 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4950 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4951 case TYPE_UINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4952 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4953 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4954 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4955 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4956 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4957 case TYPE_LONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4958 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4959 long int arg = a.arg[dp->arg_index].a.a_longint;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4960 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4961 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4962 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4963 case TYPE_ULONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4964 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4965 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4966 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4967 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4968 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4969 #if HAVE_LONG_LONG_INT
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4970 case TYPE_LONGLONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4971 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4972 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4973 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4974 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4975 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4976 case TYPE_ULONGLONGINT:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4977 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4978 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4979 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4980 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4981 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4982 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4983 case TYPE_DOUBLE:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4984 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4985 double arg = a.arg[dp->arg_index].a.a_double;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4986 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4987 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4988 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4989 case TYPE_LONGDOUBLE:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4990 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4991 long double arg = a.arg[dp->arg_index].a.a_longdouble;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4992 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4993 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4994 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4995 case TYPE_CHAR:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4996 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4997 int arg = a.arg[dp->arg_index].a.a_char;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4998 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4999 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5000 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5001 #if HAVE_WINT_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5002 case TYPE_WIDE_CHAR:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5003 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5004 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5005 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5006 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5007 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5008 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5009 case TYPE_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5010 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5011 const char *arg = a.arg[dp->arg_index].a.a_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5012 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5013 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5014 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5015 #if HAVE_WCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5016 case TYPE_WIDE_STRING:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5017 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5018 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5019 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5020 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5021 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5022 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5023 case TYPE_POINTER:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5024 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5025 void *arg = a.arg[dp->arg_index].a.a_pointer;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5026 SNPRINTF_BUF (arg);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5027 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5028 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5029 default:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5030 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5031 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5032
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5033 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5034 /* Portability: Not all implementations of snprintf()
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5035 are ISO C 99 compliant. Determine the number of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5036 bytes that snprintf() has produced or would have
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5037 produced. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5038 if (count >= 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5039 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5040 /* Verify that snprintf() has NUL-terminated its
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5041 result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5042 if (count < maxlen
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5043 && ((TCHAR_T *) (result + length)) [count] != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5044 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5045 /* Portability hack. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5046 if (retcount > count)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5047 count = retcount;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5048 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5049 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5050 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5051 /* snprintf() doesn't understand the '%n'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5052 directive. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5053 if (fbp[1] != '\0')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5054 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5055 /* Don't use the '%n' directive; instead, look
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5056 at the snprintf() return value. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5057 fbp[1] = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5058 continue;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5059 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5060 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5061 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5062 /* Look at the snprintf() return value. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5063 if (retcount < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5064 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5065 /* HP-UX 10.20 snprintf() is doubly deficient:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5066 It doesn't understand the '%n' directive,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5067 *and* it returns -1 (rather than the length
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5068 that would have been required) when the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5069 buffer is too small. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5070 size_t bigger_need =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5071 xsum (xtimes (allocated, 2), 12);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5072 ENSURE_ALLOCATION (bigger_need);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5073 continue;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5074 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5075 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5076 count = retcount;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5077 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5078 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5079 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5080
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5081 /* Attempt to handle failure. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5082 if (count < 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5083 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5084 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5085 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5086 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5087 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5088 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5089 errno = EINVAL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5090 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5091 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5092
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5093 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5094 /* Handle overflow of the allocated buffer.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5095 If such an overflow occurs, a C99 compliant snprintf()
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5096 returns a count >= maxlen. However, a non-compliant
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5097 snprintf() function returns only count = maxlen - 1. To
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5098 cover both cases, test whether count >= maxlen - 1. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5099 if ((unsigned int) count + 1 >= maxlen)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5100 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5101 /* If maxlen already has attained its allowed maximum,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5102 allocating more memory will not increase maxlen.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5103 Instead of looping, bail out. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5104 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5105 goto overflow;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5106 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5107 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5108 /* Need at least (count + 1) * sizeof (TCHAR_T)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5109 bytes. (The +1 is for the trailing NUL.)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5110 But ask for (count + 2) * sizeof (TCHAR_T)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5111 bytes, so that in the next round, we likely get
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5112 maxlen > (unsigned int) count + 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5113 and so we don't get here again.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5114 And allocate proportionally, to avoid looping
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5115 eternally if snprintf() reports a too small
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5116 count. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5117 size_t n =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5118 xmax (xsum (length,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5119 ((unsigned int) count + 2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5120 + TCHARS_PER_DCHAR - 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5121 / TCHARS_PER_DCHAR),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5122 xtimes (allocated, 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5123
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5124 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5125 continue;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5126 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5127 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5128 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5129
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5130 #if NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5131 if (prec_ourselves)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5132 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5133 /* Handle the precision. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5134 TCHAR_T *prec_ptr =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5135 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5136 (TCHAR_T *) (result + length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5137 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5138 tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5139 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5140 size_t prefix_count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5141 size_t move;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5142
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5143 prefix_count = 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5144 /* Put the additional zeroes after the sign. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5145 if (count >= 1
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5146 && (*prec_ptr == '-' || *prec_ptr == '+'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5147 || *prec_ptr == ' '))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5148 prefix_count = 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5149 /* Put the additional zeroes after the 0x prefix if
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5150 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5151 else if (count >= 2
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5152 && prec_ptr[0] == '0'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5153 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5154 prefix_count = 2;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5155
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5156 move = count - prefix_count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5157 if (precision > move)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5158 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5159 /* Insert zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5160 size_t insert = precision - move;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5161 TCHAR_T *prec_end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5162
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5163 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5164 size_t n =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5165 xsum (length,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5166 (count + insert + TCHARS_PER_DCHAR - 1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5167 / TCHARS_PER_DCHAR);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5168 length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5169 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5170 length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5171 prec_ptr = (TCHAR_T *) (result + length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5172 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5173
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5174 prec_end = prec_ptr + count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5175 prec_ptr += prefix_count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5176
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5177 while (prec_end > prec_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5178 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5179 prec_end--;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5180 prec_end[insert] = prec_end[0];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5181 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5182
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5183 prec_end += insert;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5184 do
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5185 *--prec_end = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5186 while (prec_end > prec_ptr);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5187
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5188 count += insert;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5189 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5190 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5191 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5192
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5193 #if !USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5194 if (count >= tmp_length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5195 /* tmp_length was incorrectly calculated - fix the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5196 code above! */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5197 abort ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5198 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5199
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5200 #if !DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5201 /* Convert from TCHAR_T[] to DCHAR_T[]. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5202 if (dp->conversion == 'c' || dp->conversion == 's')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5203 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5204 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5205 TYPE_WIDE_STRING.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5206 The result string is not certainly ASCII. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5207 const TCHAR_T *tmpsrc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5208 DCHAR_T *tmpdst;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5209 size_t tmpdst_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5210 /* This code assumes that TCHAR_T is 'char'. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5211 typedef int TCHAR_T_verify
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5212 [2 * (sizeof (TCHAR_T) == 1) - 1];
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5213 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5214 tmpsrc = (TCHAR_T *) (result + length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5215 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5216 tmpsrc = tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5217 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5218 tmpdst =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5219 DCHAR_CONV_FROM_ENCODING (locale_charset (),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5220 iconveh_question_mark,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5221 tmpsrc, count,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5222 NULL,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5223 NULL, &tmpdst_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5224 if (tmpdst == NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5225 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5226 int saved_errno = errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5227 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5228 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5229 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5230 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5231 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5232 errno = saved_errno;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5233 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5234 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5235 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5236 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5237 free (tmpdst);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5238 count = tmpdst_len;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5239 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5240 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5241 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5242 /* The result string is ASCII.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5243 Simple 1:1 conversion. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5244 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5245 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5246 no-op conversion, in-place on the array starting
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5247 at (result + length). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5248 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5249 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5250 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5251 const TCHAR_T *tmpsrc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5252 DCHAR_T *tmpdst;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5253 size_t n;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5254
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5255 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5256 if (result == resultbuf)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5257 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5258 tmpsrc = (TCHAR_T *) (result + length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5259 /* ENSURE_ALLOCATION will not move tmpsrc
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5260 (because it's part of resultbuf). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5261 ENSURE_ALLOCATION (xsum (length, count));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5262 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5263 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5264 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5265 /* ENSURE_ALLOCATION will move the array
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5266 (because it uses realloc(). */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5267 ENSURE_ALLOCATION (xsum (length, count));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5268 tmpsrc = (TCHAR_T *) (result + length);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5269 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5270 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5271 tmpsrc = tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5272 ENSURE_ALLOCATION (xsum (length, count));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5273 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5274 tmpdst = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5275 /* Copy backwards, because of overlapping. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5276 tmpsrc += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5277 tmpdst += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5278 for (n = count; n > 0; n--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5279 *--tmpdst = (unsigned char) *--tmpsrc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5280 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5281 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5282 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5283
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5284 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5285 /* Make room for the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5286 if (count > allocated - length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5287 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5288 /* Need at least count elements. But allocate
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5289 proportionally. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5290 size_t n =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5291 xmax (xsum (length, count), xtimes (allocated, 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5292
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5293 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5294 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5295 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5296
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5297 /* Here count <= allocated - length. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5298
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5299 /* Perform padding. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5300 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5301 if (pad_ourselves && has_width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5302 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5303 size_t w;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5304 # if ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5305 /* Outside POSIX, it's preferrable to compare the width
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5306 against the number of _characters_ of the converted
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5307 value. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5308 w = DCHAR_MBSNLEN (result + length, count);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5309 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5310 /* The width is compared against the number of _bytes_
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5311 of the converted value, says POSIX. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5312 w = count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5313 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5314 if (w < width)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5315 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5316 size_t pad = width - w;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5317
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5318 /* Make room for the result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5319 if (xsum (count, pad) > allocated - length)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5320 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5321 /* Need at least count + pad elements. But
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5322 allocate proportionally. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5323 size_t n =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5324 xmax (xsum3 (length, count, pad),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5325 xtimes (allocated, 2));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5326
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5327 # if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5328 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5329 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5330 length -= count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5331 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5332 ENSURE_ALLOCATION (n);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5333 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5334 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5335 /* Here count + pad <= allocated - length. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5336
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5337 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5338 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5339 DCHAR_T * const rp = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5340 # else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5341 DCHAR_T * const rp = tmp;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5342 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5343 DCHAR_T *p = rp + count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5344 DCHAR_T *end = p + pad;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5345 DCHAR_T *pad_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5346 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5347 if (dp->conversion == 'c'
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5348 || dp->conversion == 's')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5349 /* No zero-padding for string directives. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5350 pad_ptr = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5351 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5352 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5353 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5354 pad_ptr = (*rp == '-' ? rp + 1 : rp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5355 /* No zero-padding of "inf" and "nan". */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5356 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5357 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5358 pad_ptr = NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5359 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5360 /* The generated string now extends from rp to p,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5361 with the zero padding insertion point being at
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5362 pad_ptr. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5363
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5364 count = count + pad; /* = end - rp */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5365
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5366 if (flags & FLAG_LEFT)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5367 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5368 /* Pad with spaces on the right. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5369 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5370 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5371 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5372 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5373 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5374 /* Pad with zeroes. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5375 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5376
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5377 while (p > pad_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5378 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5379 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5380 *p++ = '0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5381 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5382 else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5383 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5384 /* Pad with spaces on the left. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5385 DCHAR_T *q = end;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5386
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5387 while (p > rp)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5388 *--q = *--p;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5389 for (; pad > 0; pad--)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5390 *p++ = ' ';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5391 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5392 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5393 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5394 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5395 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5396
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5397 /* Here still count <= allocated - length. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5398
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5399 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5400 /* The snprintf() result did fit. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5401 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5402 /* Append the sprintf() result. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5403 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5404 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5405 #if !USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5406 if (tmp != tmpbuf)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5407 free (tmp);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5408 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5409
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5410 #if NEED_PRINTF_DIRECTIVE_F
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5411 if (dp->conversion == 'F')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5412 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5413 /* Convert the %f result to upper case for %F. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5414 DCHAR_T *rp = result + length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5415 size_t rc;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5416 for (rc = count; rc > 0; rc--, rp++)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5417 if (*rp >= 'a' && *rp <= 'z')
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5418 *rp = *rp - 'a' + 'A';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5419 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5420 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5421
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5422 length += count;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5423 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5424 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5425 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5426 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5427 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5428
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5429 /* Add the final NUL. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5430 ENSURE_ALLOCATION (xsum (length, 1));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5431 result[length] = '\0';
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5432
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5433 if (result != resultbuf && length + 1 < allocated)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5434 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5435 /* Shrink the allocated memory if possible. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5436 DCHAR_T *memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5437
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5438 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5439 if (memory != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5440 result = memory;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5441 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5442
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5443 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5444 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5445 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5446 *lengthp = length;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5447 /* Note that we can produce a big string of a length > INT_MAX. POSIX
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5448 says that snprintf() fails with errno = EOVERFLOW in this case, but
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5449 that's only because snprintf() returns an 'int'. This function does
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5450 not have this limitation. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5451 return result;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5452
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5453 #if USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5454 overflow:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5455 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5456 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5457 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5458 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5459 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5460 errno = EOVERFLOW;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5461 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5462 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5463
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5464 out_of_memory:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5465 if (!(result == resultbuf || result == NULL))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5466 free (result);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5467 if (buf_malloced != NULL)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5468 free (buf_malloced);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5469 out_of_memory_1:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5470 CLEANUP ();
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5471 errno = ENOMEM;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5472 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5473 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5474 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5475
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5476 #undef TCHARS_PER_DCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5477 #undef SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5478 #undef USE_SNPRINTF
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5479 #undef DCHAR_SET
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5480 #undef DCHAR_CPY
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5481 #undef PRINTF_PARSE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5482 #undef DIRECTIVES
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5483 #undef DIRECTIVE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5484 #undef DCHAR_IS_TCHAR
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5485 #undef TCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5486 #undef DCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5487 #undef FCHAR_T
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5488 #undef VASNPRINTF