annotate lib/vasnprintf.c @ 13199:af2bd6fe2a98

vasnprintf: Correct handling of unconvertible wide string arguments.
author Bruno Haible <bruno@clisp.org>
date Sat, 10 Apr 2010 22:25:31 +0200
parents 2f3ddf3c58a5
children 5aa26fc2aec0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* vsprintf with automatic memory allocation.
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2 Copyright (C) 1999, 2002-2010 Free Software Foundation, Inc.
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
4 This program is free software; you can redistribute it and/or modify
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
5 it under the terms of the GNU General Public License as published by
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
12 GNU General Public License for more details.
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
14 You should have received a copy of the GNU General Public License along
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4428
diff changeset
15 with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5670
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
18 /* This file can be parametrized with the following macros:
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
19 VASNPRINTF The name of the function being defined.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
20 FCHAR_T The element type of the format string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
21 DCHAR_T The element type of the destination (result) string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
22 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
23 in the format string are ASCII. MUST be set if
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
24 FCHAR_T and DCHAR_T are not the same type.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
25 DIRECTIVE Structure denoting a format directive.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
26 Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
27 DIRECTIVES Structure denoting the set of format directives of a
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
28 format string. Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
29 PRINTF_PARSE Function that parses a format string.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
30 Depends on FCHAR_T.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
31 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
32 DCHAR_SET memset like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
33 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
34 SNPRINTF The system's snprintf (or similar) function.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
35 This may be either snprintf or swprintf.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
36 TCHAR_T The element type of the argument and result string
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
37 of the said SNPRINTF function. This may be either
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
38 char or wchar_t. The code exploits that
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
39 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
40 alignof (TCHAR_T) <= alignof (DCHAR_T).
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
41 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
42 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
43 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
44 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
45 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
46
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 This must come before <config.h> because <config.h> may include
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 <features.h>, and once <features.h> has been included, it's too late. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #ifndef _GNU_SOURCE
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # define _GNU_SOURCE 1
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
54 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
55 # include <config.h>
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
56 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
57 #ifndef IN_LIBINTL
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
58 # include <alloca.h>
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
59 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /* Specification. */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
62 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
63 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
64 # include "vasnwprintf.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
65 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
66 # include "vasnprintf.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
67 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
68 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
70 #include <locale.h> /* localeconv() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
71 #include <stdio.h> /* snprintf(), sprintf() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
72 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
73 #include <string.h> /* memcpy(), strlen() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
74 #include <errno.h> /* errno */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
75 #include <limits.h> /* CHAR_BIT */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
76 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
8554
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
77 #if HAVE_NL_LANGINFO
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
78 # include <langinfo.h>
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
79 #endif
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
80 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
81 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
82 # include "wprintf-parse.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
83 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
84 # include "printf-parse.h"
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
85 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
86 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
88 /* Checked size_t computations. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
89 #include "xsize.h"
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
90
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
91 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
92 # include <math.h>
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
93 # include "float+.h"
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
94 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
95
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
96 #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
97 # include <math.h>
10264
ebb7ea0c94e8 Rename isnand.h to isnand-nolibm.h, similarly for isnanf.h.
Ben Pfaff <blp@cs.stanford.edu>
parents: 10104
diff changeset
98 # include "isnand-nolibm.h"
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
99 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
100
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
101 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
102 # include <math.h>
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
103 # include "isnanl-nolibm.h"
8915
f15c03805bd9 Fix possible compilation error.
Bruno Haible <bruno@clisp.org>
parents: 8865
diff changeset
104 # include "fpucw.h"
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
105 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
106
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
107 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
8655
9a272158fe43 Faster determination of the sign of a number.
Bruno Haible <bruno@clisp.org>
parents: 8648
diff changeset
108 # include <math.h>
10264
ebb7ea0c94e8 Rename isnand.h to isnand-nolibm.h, similarly for isnanf.h.
Ben Pfaff <blp@cs.stanford.edu>
parents: 10104
diff changeset
109 # include "isnand-nolibm.h"
8526
67e9a647f2fa Fix includes.
Bruno Haible <bruno@clisp.org>
parents: 8487
diff changeset
110 # include "printf-frexp.h"
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
111 #endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
112
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
113 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
114 # include <math.h>
8648
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
115 # include "isnanl-nolibm.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
116 # include "printf-frexpl.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
117 # include "fpucw.h"
8335
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
118 #endif
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
119
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
120 /* Default parameters. */
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
121 #ifndef VASNPRINTF
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
122 # if WIDE_CHAR_VERSION
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
123 # define VASNPRINTF vasnwprintf
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
124 # define FCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
125 # define DCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
126 # define TCHAR_T wchar_t
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
127 # define DCHAR_IS_TCHAR 1
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
128 # define DIRECTIVE wchar_t_directive
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
129 # define DIRECTIVES wchar_t_directives
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
130 # define PRINTF_PARSE wprintf_parse
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
131 # define DCHAR_CPY wmemcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
132 # define DCHAR_SET wmemset
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
133 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
134 # define VASNPRINTF vasnprintf
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
135 # define FCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
136 # define DCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
137 # define TCHAR_T char
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
138 # define DCHAR_IS_TCHAR 1
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
139 # define DIRECTIVE char_directive
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
140 # define DIRECTIVES char_directives
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
141 # define PRINTF_PARSE printf_parse
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
142 # define DCHAR_CPY memcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
143 # define DCHAR_SET memset
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
144 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
145 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
146 #if WIDE_CHAR_VERSION
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
147 /* TCHAR_T is wchar_t. */
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
148 # define USE_SNPRINTF 1
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
149 # if HAVE_DECL__SNWPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
150 /* On Windows, the function swprintf() has a different signature than
13197
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
151 on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
152 instead. The mingw function snwprintf() has fewer bugs than the
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
153 MSVCRT function _snwprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
154 # if defined __MINGW32__
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
155 # define SNPRINTF snwprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
156 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
157 # define SNPRINTF _snwprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
158 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
159 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
160 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
161 # define SNPRINTF swprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
162 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
163 #else
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
164 /* TCHAR_T is char. */
9940
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
165 /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
8831
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
166 But don't use it on BeOS, since BeOS snprintf produces no output if the
9940
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
167 size argument is >= 0x3000000.
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
168 Also don't use it on Linux libc5, since there snprintf with size = 1
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
169 writes any output without bounds, like sprintf. */
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
170 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
8831
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
171 # define USE_SNPRINTF 1
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
172 # else
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
173 # define USE_SNPRINTF 0
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
174 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
175 # if HAVE_DECL__SNPRINTF
13197
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
176 /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
177 function _snprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
178 # if defined __MINGW32__
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
179 # define SNPRINTF snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
180 /* Here we need to call the native snprintf, not rpl_snprintf. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
181 # undef snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
182 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
183 # define SNPRINTF _snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
184 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
185 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
186 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
187 # define SNPRINTF snprintf
8417
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
188 /* Here we need to call the native snprintf, not rpl_snprintf. */
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
189 # undef snprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
190 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
191 #endif
8424
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
192 /* Here we need to call the native sprintf, not rpl_sprintf. */
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
193 #undef sprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
194
10102
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
195 /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
196 warnings in this file. Use -Dlint to suppress them. */
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
197 #ifdef lint
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
198 # define IF_LINT(Code) Code
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
199 #else
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
200 # define IF_LINT(Code) /* empty */
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
201 #endif
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
202
10084
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
203 /* Avoid some warnings from "gcc -Wshadow".
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
204 This file doesn't use the exp() and remainder() functions. */
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
205 #undef exp
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
206 #define exp expo
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
207 #undef remainder
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
208 #define remainder rem
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
209
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
210 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && !WIDE_CHAR_VERSION
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
211 # if (HAVE_STRNLEN && !defined _AIX)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
212 # define local_strnlen strnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
213 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
214 # ifndef local_strnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
215 # define local_strnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
216 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
217 local_strnlen (const char *string, size_t maxlen)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
218 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
219 const char *end = memchr (string, '\0', maxlen);
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
220 return end ? (size_t) (end - string) : maxlen;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
221 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
222 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
223 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
224 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
225
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
226 #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
227 # if HAVE_WCSLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
228 # define local_wcslen wcslen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
229 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
230 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
231 a dependency towards this library, here is a local substitute.
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
232 Define this substitute only once, even if this file is included
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
233 twice in the same compilation unit. */
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
234 # ifndef local_wcslen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
235 # define local_wcslen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
236 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
237 local_wcslen (const wchar_t *s)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
238 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
239 const wchar_t *ptr;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
240
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
241 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
242 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
243 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
244 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
245 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
246 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
247 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
248
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
249 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
250 # if HAVE_WCSNLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
251 # define local_wcsnlen wcsnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
252 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
253 # ifndef local_wcsnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
254 # define local_wcsnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
255 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
256 local_wcsnlen (const wchar_t *s, size_t maxlen)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
257 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
258 const wchar_t *ptr;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
259
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
260 for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
261 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
262 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
263 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
264 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
265 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
266 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
267
10450
efaa10653f4b Tweak last commit.
Bruno Haible <bruno@clisp.org>
parents: 10449
diff changeset
268 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
8569
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
269 /* Determine the decimal-point character according to the current locale. */
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
270 # ifndef decimal_point_char_defined
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
271 # define decimal_point_char_defined 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
272 static char
11856
8b0d11ca9d01 build: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11804
diff changeset
273 decimal_point_char (void)
8569
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
274 {
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
275 const char *point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
276 /* Determine it in a multithread-safe way. We know nl_langinfo is
12344
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
277 multithread-safe on glibc systems and MacOS X systems, but is not required
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
278 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
279 localeconv() is rarely multithread-safe. */
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
280 # if HAVE_NL_LANGINFO && (__GLIBC__ || (defined __APPLE__ && defined __MACH__))
8569
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
281 point = nl_langinfo (RADIXCHAR);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
282 # elif 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
283 char pointbuf[5];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
284 sprintf (pointbuf, "%#.0f", 1.0);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
285 point = &pointbuf[1];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
286 # else
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
287 point = localeconv () -> decimal_point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
288 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
289 /* The decimal point is always a single byte: either '.' or ','. */
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
290 return (point[0] != '\0' ? point[0] : '.');
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
291 }
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
292 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
293 #endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
294
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
295 #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
296
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
297 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
298 static int
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
299 is_infinite_or_zero (double x)
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
300 {
9646
a113e473cc98 Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents: 9489
diff changeset
301 return isnand (x) || x + x == x;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
302 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
303
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
304 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
305
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
306 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
307
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
308 /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
309 static int
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
310 is_infinite_or_zerol (long double x)
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
311 {
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
312 return isnanl (x) || x + x == x;
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
313 }
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
314
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
315 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
316
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
317 #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
318
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
319 /* Converting 'long double' to decimal without rare rounding bugs requires
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
320 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
321 (and slower) algorithms. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
322
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
323 typedef unsigned int mp_limb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
324 # define GMP_LIMB_BITS 32
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
325 typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
326
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
327 typedef unsigned long long mp_twolimb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
328 # define GMP_TWOLIMB_BITS 64
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
329 typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
330
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
331 /* Representation of a bignum >= 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
332 typedef struct
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
333 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
334 size_t nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
335 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
336 } mpn_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
337
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
338 /* Compute the product of two bignums >= 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
339 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
340 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
341 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
342 multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
343 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
344 const mp_limb_t *p1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
345 const mp_limb_t *p2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
346 size_t len1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
347 size_t len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
348
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
349 if (src1.nlimbs <= src2.nlimbs)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
350 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
351 len1 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
352 p1 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
353 len2 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
354 p2 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
355 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
356 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
357 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
358 len1 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
359 p1 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
360 len2 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
361 p2 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
362 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
363 /* Now 0 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
364 if (len1 == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
365 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
366 /* src1 or src2 is zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
367 dest->nlimbs = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
368 dest->limbs = (mp_limb_t *) malloc (1);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
369 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
370 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
371 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
372 /* Here 1 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
373 size_t dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
374 mp_limb_t *dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
375 size_t k, i, j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
376
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
377 dlen = len1 + len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
378 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
379 if (dp == NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
380 return NULL;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
381 for (k = len2; k > 0; )
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
382 dp[--k] = 0;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
383 for (i = 0; i < len1; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
384 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
385 mp_limb_t digit1 = p1[i];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
386 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
387 for (j = 0; j < len2; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
388 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
389 mp_limb_t digit2 = p2[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
390 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
391 carry += dp[i + j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
392 dp[i + j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
393 carry = carry >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
394 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
395 dp[i + len2] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
396 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
397 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
398 while (dlen > 0 && dp[dlen - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
399 dlen--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
400 dest->nlimbs = dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
401 dest->limbs = dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
402 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
403 return dest->limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
404 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
405
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
406 /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
407 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
408 the remainder.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
409 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
410 q is incremented.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
411 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
412 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
413 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
414 divide (mpn_t a, mpn_t b, mpn_t *q)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
415 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
416 /* Algorithm:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
417 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
418 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
419 If m<n, then q:=0 and r:=a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
420 If m>=n=1, perform a single-precision division:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
421 r:=0, j:=m,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
422 while j>0 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
423 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
424 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
425 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
426 Normalise [q[m-1],...,q[0]], yields q.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
427 If m>=n>1, perform a multiple-precision division:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
428 We have a/b < beta^(m-n+1).
11422
847931ff84ef typo fix in a comment
Jim Meyering <meyering@redhat.com>
parents: 11220
diff changeset
429 s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
430 Shift a and b left by s bits, copying them. r:=a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
431 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
432 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
433 Compute q* :
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
434 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
435 In case of overflow (q* >= beta) set q* := beta-1.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
436 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
437 and c3 := b[n-2] * q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
438 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
439 occurred. Furthermore 0 <= c3 < beta^2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
440 If there was overflow and
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
441 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
442 the next test can be skipped.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
443 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
444 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
445 If q* > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
446 Put r := r - b * q* * beta^j. In detail:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
447 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
448 hence: u:=0, for i:=0 to n-1 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
449 u := u + q* * b[i],
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
450 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
451 u:=u div beta (+ 1, if carry in subtraction)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
452 r[n+j]:=r[n+j]-u.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
453 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
454 < q* + 1 <= beta,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
455 the carry u does not overflow.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
456 If a negative carry occurs, put q* := q* - 1
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
457 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
458 Set q[j] := q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
459 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
460 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
461 rest r.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
462 The room for q[j] can be allocated at the memory location of r[n+j].
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
463 Finally, round-to-even:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
464 Shift r left by 1 bit.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
465 If r > b or if r = b and q[0] is odd, q := q+1.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
466 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
467 const mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
468 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
469 const mp_limb_t *b_ptr = b.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
470 size_t b_len = b.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
471 mp_limb_t *roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
472 mp_limb_t *tmp_roomptr = NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
473 mp_limb_t *q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
474 size_t q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
475 mp_limb_t *r_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
476 size_t r_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
477
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
478 /* Allocate room for a_len+2 digits.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
479 (Need a_len+1 digits for the real division and 1 more digit for the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
480 final rounding of q.) */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
481 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
482 if (roomptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
483 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
484
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
485 /* Normalise a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
486 while (a_len > 0 && a_ptr[a_len - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
487 a_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
488
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
489 /* Normalise b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
490 for (;;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
491 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
492 if (b_len == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
493 /* Division by zero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
494 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
495 if (b_ptr[b_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
496 b_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
497 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
498 break;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
499 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
500
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
501 /* Here m = a_len >= 0 and n = b_len > 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
502
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
503 if (a_len < b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
504 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
505 /* m<n: trivial case. q=0, r := copy of a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
506 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
507 r_len = a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
508 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
509 q_ptr = roomptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
510 q_len = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
511 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
512 else if (b_len == 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
513 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
514 /* n=1: single precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
515 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
516 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
517 q_ptr = roomptr + 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
518 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
519 mp_limb_t den = b_ptr[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
520 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
521 const mp_limb_t *sourceptr = a_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
522 mp_limb_t *destptr = q_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
523 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
524 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
525 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
526 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
527 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
528 *--destptr = num / den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
529 remainder = num % den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
530 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
531 /* Normalise and store r. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
532 if (remainder > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
533 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
534 r_ptr[0] = remainder;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
535 r_len = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
536 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
537 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
538 r_len = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
539 /* Normalise q. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
540 q_len = a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
541 if (q_ptr[q_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
542 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
543 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
544 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
545 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
546 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
547 /* n>1: multiple precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
548 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
549 beta^(m-n-1) <= a/b < beta^(m-n+1). */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
550 /* Determine s. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
551 size_t s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
552 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
553 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
554 s = 31;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
555 if (msd >= 0x10000)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
556 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
557 msd = msd >> 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
558 s -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
559 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
560 if (msd >= 0x100)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
561 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
562 msd = msd >> 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
563 s -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
564 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
565 if (msd >= 0x10)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
566 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
567 msd = msd >> 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
568 s -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
569 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
570 if (msd >= 0x4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
571 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
572 msd = msd >> 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
573 s -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
574 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
575 if (msd >= 0x2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
576 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
577 msd = msd >> 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
578 s -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
579 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
580 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
581 /* 0 <= s < GMP_LIMB_BITS.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
582 Copy b, shifting it left by s bits. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
583 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
584 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
585 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
586 if (tmp_roomptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
587 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
588 free (roomptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
589 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
590 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
591 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
592 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
593 mp_limb_t *destptr = tmp_roomptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
594 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
595 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
596 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
597 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
598 accu += (mp_twolimb_t) *sourceptr++ << s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
599 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
600 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
601 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
602 /* accu must be zero, since that was how s was determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
603 if (accu != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
604 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
605 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
606 b_ptr = tmp_roomptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
607 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
608 /* Copy a, shifting it left by s bits, yields r.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
609 Memory layout:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
610 At the beginning: r = roomptr[0..a_len],
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
611 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
612 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
613 if (s == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
614 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
615 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
616 r_ptr[a_len] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
617 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
618 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
619 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
620 const mp_limb_t *sourceptr = a_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
621 mp_limb_t *destptr = r_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
622 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
623 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
624 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
625 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
626 accu += (mp_twolimb_t) *sourceptr++ << s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
627 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
628 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
629 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
630 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
631 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
632 q_ptr = roomptr + b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
633 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
634 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
635 size_t j = a_len - b_len; /* m-n */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
636 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
637 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
638 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
639 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
640 /* Division loop, traversed m-n+1 times.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
641 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
642 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
643 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
644 mp_limb_t q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
645 mp_limb_t c1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
646 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
647 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
648 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
649 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
650 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
651 | r_ptr[j + b_len - 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
652 q_star = num / b_msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
653 c1 = num % b_msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
654 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
655 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
656 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
657 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
658 q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
659 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
660 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
661 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
662 {<= beta !}.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
663 If yes, jump directly to the subtraction loop.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
664 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
665 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
666 if (r_ptr[j + b_len] > b_msd
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
667 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
668 /* r[j+n] >= b[n-1]+1 or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
669 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
670 carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
671 goto subtract;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
672 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
673 /* q_star = q*,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
674 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
675 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
676 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
677 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
678 mp_twolimb_t c3 = /* b[n-2] * q* */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
679 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
680 /* While c2 < c3, increase c2 and decrease c3.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
681 Consider c3-c2. While it is > 0, decrease it by
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
682 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
683 this can happen only twice. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
684 if (c3 > c2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
685 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
686 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
687 if (c3 - c2 > b_msdd)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
688 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
689 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
690 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
691 if (q_star > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
692 subtract:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
693 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
694 /* Subtract r := r - b * q* * beta^j. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
695 mp_limb_t cr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
696 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
697 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
698 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
699 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
700 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
701 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
702 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
703 /* Here 0 <= carry <= q*. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
704 carry =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
705 carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
706 + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
707 + (mp_limb_t) ~(*destptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
708 /* Here 0 <= carry <= beta*q* + beta-1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
709 *destptr++ = ~(mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
710 carry = carry >> GMP_LIMB_BITS; /* <= q* */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
711 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
712 cr = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
713 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
714 /* Subtract cr from r_ptr[j + b_len], then forget about
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
715 r_ptr[j + b_len]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
716 if (cr > r_ptr[j + b_len])
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
717 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
718 /* Subtraction gave a carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
719 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
720 /* Add b back. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
721 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
722 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
723 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
724 mp_limb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
725 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
726 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
727 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
728 mp_limb_t source1 = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
729 mp_limb_t source2 = *destptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
730 *destptr++ = source1 + source2 + carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
731 carry =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
732 (carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
733 ? source1 >= (mp_limb_t) ~source2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
734 : source1 > (mp_limb_t) ~source2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
735 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
736 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
737 /* Forget about the carry and about r[j+n]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
738 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
739 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
740 /* q* is determined. Store it as q[j]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
741 q_ptr[j] = q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
742 if (j == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
743 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
744 j--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
745 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
746 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
747 r_len = b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
748 /* Normalise q. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
749 if (q_ptr[q_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
750 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
751 # if 0 /* Not needed here, since we need r only to compare it with b/2, and
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
752 b is shifted left by s bits. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
753 /* Shift r right by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
754 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
755 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
756 mp_limb_t ptr = r_ptr + r_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
757 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
758 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
759 for (count = r_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
760 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
761 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
762 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
763 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
764 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
765 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
766 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
767 /* Normalise r. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
768 while (r_len > 0 && r_ptr[r_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
769 r_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
770 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
771 /* Compare r << 1 with b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
772 if (r_len > b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
773 goto increment_q;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
774 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
775 size_t i;
8835
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
776 for (i = b_len;;)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
777 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
778 mp_limb_t r_i =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
779 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
780 | (i < r_len ? r_ptr[i] << 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
781 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
782 if (r_i > b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
783 goto increment_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
784 if (r_i < b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
785 goto keep_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
786 if (i == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
787 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
788 i--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
789 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
790 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
791 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
792 /* q is odd. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
793 increment_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
794 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
795 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
796 for (i = 0; i < q_len; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
797 if (++(q_ptr[i]) != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
798 goto keep_q;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
799 q_ptr[q_len++] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
800 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
801 keep_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
802 if (tmp_roomptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
803 free (tmp_roomptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
804 q->limbs = q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
805 q->nlimbs = q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
806 return roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
807 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
808
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
809 /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
810 representation.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
811 Destroys the contents of a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
812 Return the allocated memory - containing the decimal digits in low-to-high
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
813 order, terminated with a NUL character - in case of success, NULL in case
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
814 of memory allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
815 static char *
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
816 convert_to_decimal (mpn_t a, size_t extra_zeroes)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
817 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
818 mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
819 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
820 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
821 size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
822 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
823 if (c_ptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
824 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
825 char *d_ptr = c_ptr;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
826 for (; extra_zeroes > 0; extra_zeroes--)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
827 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
828 while (a_len > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
829 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
830 /* Divide a by 10^9, in-place. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
831 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
832 mp_limb_t *ptr = a_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
833 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
834 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
835 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
836 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
837 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
838 *ptr = num / 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
839 remainder = num % 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
840 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
841 /* Store the remainder as 9 decimal digits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
842 for (count = 9; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
843 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
844 *d_ptr++ = '0' + (remainder % 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
845 remainder = remainder / 10;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
846 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
847 /* Normalize a. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
848 if (a_ptr[a_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
849 a_len--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
850 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
851 /* Remove leading zeroes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
852 while (d_ptr > c_ptr && d_ptr[-1] == '0')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
853 d_ptr--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
854 /* But keep at least one zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
855 if (d_ptr == c_ptr)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
856 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
857 /* Terminate the string. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
858 *d_ptr = '\0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
859 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
860 return c_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
861 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
862
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
863 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
864
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
865 /* Assuming x is finite and >= 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
866 write x as x = 2^e * m, where m is a bignum.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
867 Return the allocated memory in case of success, NULL in case of memory
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
868 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
869 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
870 decode_long_double (long double x, int *ep, mpn_t *mp)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
871 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
872 mpn_t m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
873 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
874 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
875 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
876
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
877 /* Allocate memory for result. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
878 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
879 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
880 if (m.limbs == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
881 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
882 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
883 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
884 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
885 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
886 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
887 latter is an integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
888 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
889 I'm not sure whether it's safe to cast a 'long double' value between
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
890 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
891 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
892 doesn't matter). */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
893 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
894 # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
895 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
896 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
897 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
898 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
899 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
900 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
901 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
902 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
903 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
904 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
905 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
906 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
907 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
908 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
909 # else
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
910 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
911 mp_limb_t d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
912 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
913 d = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
914 y -= d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
915 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
916 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
917 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
918 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
919 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
920 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
921 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
922 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
923 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
924 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
925 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
926 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
927 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
928 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
929 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
930 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
931 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
932 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
933 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
934 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
935 }
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
936 #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
937 precision. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
938 if (!(y == 0.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
939 abort ();
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
940 #endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
941 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
942 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
943 m.nlimbs--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
944 *mp = m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
945 *ep = exp - LDBL_MANT_BIT;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
946 return m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
947 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
948
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
949 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
950
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
951 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
952
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
953 /* Assuming x is finite and >= 0:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
954 write x as x = 2^e * m, where m is a bignum.
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
955 Return the allocated memory in case of success, NULL in case of memory
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
956 allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
957 static void *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
958 decode_double (double x, int *ep, mpn_t *mp)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
959 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
960 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
961 int exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
962 double y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
963 size_t i;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
964
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
965 /* Allocate memory for result. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
966 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
967 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
968 if (m.limbs == NULL)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
969 return NULL;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
970 /* Split into exponential part and mantissa. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
971 y = frexp (x, &exp);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
972 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
973 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
974 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
975 latter is an integer. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
976 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
977 I'm not sure whether it's safe to cast a 'double' value between
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
978 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
979 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
980 doesn't matter). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
981 # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
982 # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
983 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
984 mp_limb_t hi, lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
985 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
986 hi = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
987 y -= hi;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
988 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
989 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
990 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
991 lo = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
992 y -= lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
993 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
994 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
995 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
996 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
997 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
998 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
999 mp_limb_t d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1000 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1001 d = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1002 y -= d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1003 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1004 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1005 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1006 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1007 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1008 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1009 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1010 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1011 mp_limb_t hi, lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1012 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1013 hi = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1014 y -= hi;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1015 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1016 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1017 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1018 lo = (int) y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1019 y -= lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1020 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1021 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1022 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1023 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1024 if (!(y == 0.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1025 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1026 /* Normalise. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1027 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1028 m.nlimbs--;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1029 *mp = m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1030 *ep = exp - DBL_MANT_BIT;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1031 return m.limbs;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1032 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1033
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1034 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1035
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1036 /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1037 Returns the decimal representation of round (x * 10^n).
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1038 Return the allocated memory - containing the decimal digits in low-to-high
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1039 order, terminated with a NUL character - in case of success, NULL in case
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1040 of memory allocation failure. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1041 static char *
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1042 scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1043 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1044 int s;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1045 size_t extra_zeroes;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1046 unsigned int abs_n;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1047 unsigned int abs_s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1048 mp_limb_t *pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1049 size_t pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1050 unsigned int s_limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1051 unsigned int s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1052 mpn_t pow5;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1053 mpn_t z;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1054 void *z_memory;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1055 char *digits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1056
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1057 if (memory == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1058 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1059 /* x = 2^e * m, hence
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1060 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1061 = round (2^s * 5^n * m). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1062 s = e + n;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1063 extra_zeroes = 0;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1064 /* Factor out a common power of 10 if possible. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1065 if (s > 0 && n > 0)
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1066 {
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1067 extra_zeroes = (s < n ? s : n);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1068 s -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1069 n -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1070 }
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1071 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1072 Before converting to decimal, we need to compute
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1073 z = round (2^s * 5^n * m). */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1074 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1075 sign. 2.322 is slightly larger than log(5)/log(2). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1076 abs_n = (n >= 0 ? n : -n);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1077 abs_s = (s >= 0 ? s : -s);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1078 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1079 + abs_s / GMP_LIMB_BITS + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1080 * sizeof (mp_limb_t));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1081 if (pow5_ptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1082 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1083 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1084 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1085 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1086 /* Initialize with 1. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1087 pow5_ptr[0] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1088 pow5_len = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1089 /* Multiply with 5^|n|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1090 if (abs_n > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1091 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1092 static mp_limb_t const small_pow5[13 + 1] =
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1093 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1094 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1095 48828125, 244140625, 1220703125
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1096 };
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1097 unsigned int n13;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1098 for (n13 = 0; n13 <= abs_n; n13 += 13)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1099 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1100 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1101 size_t j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1102 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1103 for (j = 0; j < pow5_len; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1104 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1105 mp_limb_t digit2 = pow5_ptr[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1106 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1107 pow5_ptr[j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1108 carry = carry >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1109 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1110 if (carry > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1111 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1112 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1113 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1114 s_limbs = abs_s / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1115 s_bits = abs_s % GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1116 if (n >= 0 ? s >= 0 : s <= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1117 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1118 /* Multiply with 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1119 if (s_bits > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1120 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1121 mp_limb_t *ptr = pow5_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1122 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1123 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1124 for (count = pow5_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1125 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1126 accu += (mp_twolimb_t) *ptr << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1127 *ptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1128 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1129 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1130 if (accu > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1131 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1132 *ptr = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1133 pow5_len++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1134 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1135 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1136 if (s_limbs > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1137 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1138 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1139 for (count = pow5_len; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1140 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1141 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1142 pow5_ptr[s_limbs + count] = pow5_ptr[count];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1143 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1144 for (count = s_limbs; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1145 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1146 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1147 pow5_ptr[count] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1148 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1149 pow5_len += s_limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1150 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1151 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1152 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1153 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1154 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1155 /* Multiply m with pow5. No division needed. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1156 z_memory = multiply (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1157 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1158 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1159 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1160 /* Divide m by pow5 and round. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1161 z_memory = divide (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1162 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1163 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1164 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1165 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1166 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1167 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1168 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1169 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1170 /* n >= 0, s < 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1171 Multiply m with pow5, then divide by 2^|s|. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1172 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1173 mpn_t denominator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1174 void *tmp_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1175 tmp_memory = multiply (m, pow5, &numerator);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1176 if (tmp_memory == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1177 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1178 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1179 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1180 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1181 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1182 /* Construct 2^|s|. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1183 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1184 mp_limb_t *ptr = pow5_ptr + pow5_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1185 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1186 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1187 ptr[i] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1188 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1189 denominator.limbs = ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1190 denominator.nlimbs = s_limbs + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1191 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1192 z_memory = divide (numerator, denominator, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1193 free (tmp_memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1194 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1195 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1196 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1197 /* n < 0, s > 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1198 Multiply m with 2^s, then divide by pow5. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1199 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1200 mp_limb_t *num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1201 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1202 * sizeof (mp_limb_t));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1203 if (num_ptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1204 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1205 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1206 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1207 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1208 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1209 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1210 mp_limb_t *destptr = num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1211 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1212 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1213 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1214 *destptr++ = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1215 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1216 if (s_bits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1217 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1218 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1219 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1220 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1221 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1222 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1223 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1224 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1225 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1226 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1227 if (accu > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1228 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1229 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1230 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1231 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1232 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1233 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1234 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1235 *destptr++ = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1236 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1237 numerator.limbs = num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1238 numerator.nlimbs = destptr - num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1239 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1240 z_memory = divide (numerator, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1241 free (num_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1242 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1243 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1244 free (pow5_ptr);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1245 free (memory);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1246
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1247 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1248
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1249 if (z_memory == NULL)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1250 return NULL;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1251 digits = convert_to_decimal (z, extra_zeroes);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1252 free (z_memory);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1253 return digits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1254 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1255
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1256 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1257
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1258 /* Assuming x is finite and >= 0, and n is an integer:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1259 Returns the decimal representation of round (x * 10^n).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1260 Return the allocated memory - containing the decimal digits in low-to-high
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1261 order, terminated with a NUL character - in case of success, NULL in case
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1262 of memory allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1263 static char *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1264 scale10_round_decimal_long_double (long double x, int n)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1265 {
10100
369c11a9858d avoid a warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10084
diff changeset
1266 int e IF_LINT(= 0);
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1267 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1268 void *memory = decode_long_double (x, &e, &m);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1269 return scale10_round_decimal_decoded (e, m, memory, n);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1270 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1271
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1272 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1273
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1274 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1275
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1276 /* Assuming x is finite and >= 0, and n is an integer:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1277 Returns the decimal representation of round (x * 10^n).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1278 Return the allocated memory - containing the decimal digits in low-to-high
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1279 order, terminated with a NUL character - in case of success, NULL in case
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1280 of memory allocation failure. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1281 static char *
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1282 scale10_round_decimal_double (double x, int n)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1283 {
10101
daad35fae43c avoid another warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10100
diff changeset
1284 int e IF_LINT(= 0);
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1285 mpn_t m;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1286 void *memory = decode_double (x, &e, &m);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1287 return scale10_round_decimal_decoded (e, m, memory, n);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1288 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1289
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1290 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1291
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1292 # if NEED_PRINTF_LONG_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1293
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1294 /* Assuming x is finite and > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1295 Return an approximation for n with 10^n <= x < 10^(n+1).
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1296 The approximation is usually the right n, but may be off by 1 sometimes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1297 static int
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1298 floorlog10l (long double x)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1299 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1300 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1301 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1302 double z;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1303 double l;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1304
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1305 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1306 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1307 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1308 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1309 if (y == 0.0L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1310 return INT_MIN;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1311 if (y < 0.5L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1312 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1313 while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1314 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1315 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1316 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1317 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1318 if (y < (1.0L / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1319 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1320 y *= 1.0L * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1321 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1322 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1323 if (y < (1.0L / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1324 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1325 y *= 1.0L * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1326 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1327 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1328 if (y < (1.0L / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1329 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1330 y *= 1.0L * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1331 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1332 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1333 if (y < (1.0L / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1334 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1335 y *= 1.0L * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1336 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1337 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1338 if (y < (1.0L / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1339 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1340 y *= 1.0L * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1341 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1342 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1343 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1344 if (!(y >= 0.5L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1345 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1346 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1347 l = exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1348 z = y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1349 if (z < 0.70710678118654752444)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1350 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1351 z *= 1.4142135623730950488;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1352 l -= 0.5;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1353 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1354 if (z < 0.8408964152537145431)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1355 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1356 z *= 1.1892071150027210667;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1357 l -= 0.25;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1358 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1359 if (z < 0.91700404320467123175)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1360 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1361 z *= 1.0905077326652576592;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1362 l -= 0.125;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1363 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1364 if (z < 0.9576032806985736469)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1365 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1366 z *= 1.0442737824274138403;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1367 l -= 0.0625;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1368 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1369 /* Now 0.95 <= z <= 1.01. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1370 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1371 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1372 Four terms are enough to get an approximation with error < 10^-7. */
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1373 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1374 /* Finally multiply with log(2)/log(10), yields an approximation for
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1375 log10(x). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1376 l *= 0.30102999566398119523;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1377 /* Round down to the next integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1378 return (int) l + (l < 0 ? -1 : 0);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1379 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1380
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1381 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1382
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1383 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1384
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1385 /* Assuming x is finite and > 0:
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1386 Return an approximation for n with 10^n <= x < 10^(n+1).
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1387 The approximation is usually the right n, but may be off by 1 sometimes. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1388 static int
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1389 floorlog10 (double x)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1390 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1391 int exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1392 double y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1393 double z;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1394 double l;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1395
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1396 /* Split into exponential part and mantissa. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1397 y = frexp (x, &exp);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1398 if (!(y >= 0.0 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1399 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1400 if (y == 0.0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1401 return INT_MIN;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1402 if (y < 0.5)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1403 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1404 while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1405 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1406 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1407 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1408 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1409 if (y < (1.0 / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1410 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1411 y *= 1.0 * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1412 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1413 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1414 if (y < (1.0 / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1415 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1416 y *= 1.0 * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1417 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1418 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1419 if (y < (1.0 / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1420 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1421 y *= 1.0 * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1422 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1423 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1424 if (y < (1.0 / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1425 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1426 y *= 1.0 * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1427 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1428 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1429 if (y < (1.0 / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1430 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1431 y *= 1.0 * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1432 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1433 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1434 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1435 if (!(y >= 0.5 && y < 1.0))
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1436 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1437 /* Compute an approximation for l = log2(x) = exp + log2(y). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1438 l = exp;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1439 z = y;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1440 if (z < 0.70710678118654752444)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1441 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1442 z *= 1.4142135623730950488;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1443 l -= 0.5;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1444 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1445 if (z < 0.8408964152537145431)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1446 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1447 z *= 1.1892071150027210667;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1448 l -= 0.25;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1449 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1450 if (z < 0.91700404320467123175)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1451 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1452 z *= 1.0905077326652576592;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1453 l -= 0.125;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1454 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1455 if (z < 0.9576032806985736469)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1456 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1457 z *= 1.0442737824274138403;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1458 l -= 0.0625;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1459 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1460 /* Now 0.95 <= z <= 1.01. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1461 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1462 /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1463 Four terms are enough to get an approximation with error < 10^-7. */
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1464 l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1465 /* Finally multiply with log(2)/log(10), yields an approximation for
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1466 log10(x). */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1467 l *= 0.30102999566398119523;
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1468 /* Round down to the next integer. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1469 return (int) l + (l < 0 ? -1 : 0);
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1470 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1471
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1472 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1473
9938
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1474 /* Tests whether a string of digits consists of exactly PRECISION zeroes and
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1475 a single '1' digit. */
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1476 static int
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1477 is_borderline (const char *digits, size_t precision)
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1478 {
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1479 for (; precision > 0; precision--, digits++)
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1480 if (*digits != '0')
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1481 return 0;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1482 if (*digits != '1')
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1483 return 0;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1484 digits++;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1485 return *digits == '\0';
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1486 }
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1487
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1488 #endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1489
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1490 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1491
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1492 /* Use a different function name, to make it possible that the 'wchar_t'
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1493 parametrization and the 'char' parametrization get compiled in the same
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1494 translation unit. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1495 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1496 # define MAX_ROOM_NEEDED wmax_room_needed
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1497 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1498 # define MAX_ROOM_NEEDED max_room_needed
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1499 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1500
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1501 /* Returns the number of TCHAR_T units needed as temporary space for the result
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1502 of sprintf or SNPRINTF of a single conversion directive. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1503 static inline size_t
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1504 MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1505 arg_type type, int flags, size_t width, int has_precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1506 size_t precision, int pad_ourselves)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1507 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1508 size_t tmp_length;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1509
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1510 switch (conversion)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1511 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1512 case 'd': case 'i': case 'u':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1513 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1514 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1515 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1516 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1517 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1518 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1519 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1520 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1521 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1522 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1523 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1524 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1525 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1526 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1527 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1528 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1529 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1530 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1531 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1532 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1533 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1534 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1535 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1536 /* Multiply by 2, as an estimate for FLAG_GROUP. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1537 tmp_length = xsum (tmp_length, tmp_length);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1538 /* Add 1, to account for a leading sign. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1539 tmp_length = xsum (tmp_length, 1);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1540 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1541
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1542 case 'o':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1543 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1544 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1545 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1546 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1547 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1548 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1549 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1550 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1551 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1552 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1553 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1554 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1555 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1556 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1557 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1558 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1559 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1560 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1561 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1562 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1563 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1564 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1565 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1566 /* Add 1, to account for a leading sign. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1567 tmp_length = xsum (tmp_length, 1);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1568 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1569
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1570 case 'x': case 'X':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1571 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1572 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1573 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1574 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1575 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1576 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1577 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1578 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1579 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1580 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1581 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1582 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1583 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1584 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1585 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1586 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1587 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1588 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1589 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1590 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1591 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1592 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1593 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1594 /* Add 2, to account for a leading sign or alternate form. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1595 tmp_length = xsum (tmp_length, 2);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1596 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1597
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1598 case 'f': case 'F':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1599 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1600 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1601 (unsigned int) (LDBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1602 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1603 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1604 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1605 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1606 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1607 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1608 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1609 (unsigned int) (DBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1610 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1611 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1612 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1613 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1614 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1615 tmp_length = xsum (tmp_length, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1616 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1617
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1618 case 'e': case 'E': case 'g': case 'G':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1619 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1620 12; /* sign, decimal point, exponent etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1621 tmp_length = xsum (tmp_length, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1622 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1623
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1624 case 'a': case 'A':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1625 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1626 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1627 (unsigned int) (LDBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1628 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1629 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1630 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1631 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1632 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1633 (unsigned int) (DBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1634 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1635 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1636 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1637 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1638 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1639 /* Account for sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1640 tmp_length = xsum (tmp_length, 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1641 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1642
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1643 case 'c':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1644 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1645 if (type == TYPE_WIDE_CHAR)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1646 tmp_length = MB_CUR_MAX;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1647 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1648 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1649 tmp_length = 1;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1650 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1651
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1652 case 's':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1653 # if HAVE_WCHAR_T
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1654 if (type == TYPE_WIDE_STRING)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1655 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1656 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1657 /* ISO C says about %ls in fwprintf:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1658 "If the precision is not specified or is greater than the size
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1659 of the array, the array shall contain a null wide character."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1660 So if there is a precision, we must not use wcslen. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1661 const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1662
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1663 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1664 tmp_length = local_wcsnlen (arg, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1665 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1666 tmp_length = local_wcslen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1667 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1668 /* ISO C says about %ls in fprintf:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1669 "If a precision is specified, no more than that many bytes are
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1670 written (including shift sequences, if any), and the array
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1671 shall contain a null wide character if, to equal the multibyte
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1672 character sequence length given by the precision, the function
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1673 would need to access a wide character one past the end of the
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1674 array."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1675 So if there is a precision, we must not use wcslen. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1676 /* This case has already been handled separately in VASNPRINTF. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1677 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1678 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1679 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1680 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1681 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1682 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1683 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1684 /* ISO C says about %s in fwprintf:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1685 "If the precision is not specified or is greater than the size
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1686 of the converted array, the converted array shall contain a
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1687 null wide character."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1688 So if there is a precision, we must not use strlen. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1689 /* This case has already been handled separately in VASNPRINTF. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1690 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1691 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1692 /* ISO C says about %s in fprintf:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1693 "If the precision is not specified or greater than the size of
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1694 the array, the array shall contain a null character."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1695 So if there is a precision, we must not use strlen. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1696 const char *arg = ap->arg[arg_index].a.a_string;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1697
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1698 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1699 tmp_length = local_strnlen (arg, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1700 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1701 tmp_length = strlen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1702 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1703 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1704 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1705
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1706 case 'p':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1707 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1708 (unsigned int) (sizeof (void *) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1709 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1710 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1711 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1712 + 2; /* account for leading 0x */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1713 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1714
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1715 default:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1716 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1717 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1718
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1719 if (!pad_ourselves)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1720 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1721 # if ENABLE_UNISTDIO
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1722 /* Padding considers the number of characters, therefore the number of
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1723 elements after padding may be
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1724 > max (tmp_length, width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1725 but is certainly
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1726 <= tmp_length + width. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1727 tmp_length = xsum (tmp_length, width);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1728 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1729 /* Padding considers the number of elements, says POSIX. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1730 if (tmp_length < width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1731 tmp_length = width;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1732 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1733 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1734
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1735 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1736
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1737 return tmp_length;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1738 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1739
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1740 #endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1741
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1742 DCHAR_T *
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1743 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1744 const FCHAR_T *format, va_list args)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1745 {
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1746 DIRECTIVES d;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1747 arguments a;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1748
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1749 if (PRINTF_PARSE (format, &d, &a) < 0)
9438
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1750 /* errno is already set. */
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1751 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1752
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1753 #define CLEANUP() \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1754 free (d.dir); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1755 if (a.arg) \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1756 free (a.arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1757
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1758 if (PRINTF_FETCHARGS (args, &a) < 0)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1759 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1760 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1761 errno = EINVAL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1762 return NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1763 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1764
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1765 {
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1766 size_t buf_neededlength;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1767 TCHAR_T *buf;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1768 TCHAR_T *buf_malloced;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1769 const FCHAR_T *cp;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1770 size_t i;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1771 DIRECTIVE *dp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1772 /* Output string accumulator. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1773 DCHAR_T *result;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1774 size_t allocated;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1775 size_t length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1776
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1777 /* Allocate a small buffer that will hold a directive passed to
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1778 sprintf or snprintf. */
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1779 buf_neededlength =
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1780 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1781 #if HAVE_ALLOCA
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1782 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1783 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1784 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1785 buf_malloced = NULL;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1786 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1787 else
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1788 #endif
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1789 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1790 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1791 if (size_overflow_p (buf_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1792 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1793 buf = (TCHAR_T *) malloc (buf_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1794 if (buf == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1795 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1796 buf_malloced = buf;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1797 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1798
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1799 if (resultbuf != NULL)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1800 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1801 result = resultbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1802 allocated = *lengthp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1803 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1804 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1805 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1806 result = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1807 allocated = 0;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1808 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1809 length = 0;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1810 /* Invariants:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1811 result is either == resultbuf or == NULL or malloc-allocated.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1812 If length > 0, then result != NULL. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1813
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1814 /* Ensures that allocated >= needed. Aborts through a jump to
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1815 out_of_memory if needed is SIZE_MAX or otherwise too big. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1816 #define ENSURE_ALLOCATION(needed) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1817 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1818 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1819 size_t memory_size; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1820 DCHAR_T *memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1821 \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1822 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1823 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1824 allocated = (needed); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1825 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1826 if (size_overflow_p (memory_size)) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1827 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1828 if (result == resultbuf || result == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1829 memory = (DCHAR_T *) malloc (memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1830 else \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1831 memory = (DCHAR_T *) realloc (result, memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1832 if (memory == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1833 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1834 if (result == resultbuf && length > 0) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1835 DCHAR_CPY (memory, result, length); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1836 result = memory; \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1837 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1838
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1839 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1840 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1841 if (cp != dp->dir_start)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1842 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1843 size_t n = dp->dir_start - cp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1844 size_t augmented_length = xsum (length, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1845
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1846 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1847 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1848 need that the format string contains only ASCII characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1849 if FCHAR_T and DCHAR_T are not the same type. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1850 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1851 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1852 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1853 length = augmented_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1854 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1855 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1856 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1857 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1858 result[length++] = (unsigned char) *cp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1859 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1860 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1861 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1862 if (i == d.count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1863 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1864
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1865 /* Execute a single directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1866 if (dp->conversion == '%')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1867 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1868 size_t augmented_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1869
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1870 if (!(dp->arg_index == ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1871 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1872 augmented_length = xsum (length, 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1873 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1874 result[length] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1875 length = augmented_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1876 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1877 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1878 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1879 if (!(dp->arg_index != ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1880 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1881
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1882 if (dp->conversion == 'n')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1883 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1884 switch (a.arg[dp->arg_index].type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1885 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1886 case TYPE_COUNT_SCHAR_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1887 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1888 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1889 case TYPE_COUNT_SHORT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1890 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1891 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1892 case TYPE_COUNT_INT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1893 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1894 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1895 case TYPE_COUNT_LONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1896 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1897 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
1898 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1899 case TYPE_COUNT_LONGLONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1900 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1901 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1902 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1903 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1904 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1905 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1906 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1907 #if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1908 /* The unistdio extensions. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1909 else if (dp->conversion == 'U')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1910 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1911 arg_type type = a.arg[dp->arg_index].type;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1912 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1913 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1914 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1915 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1916 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1917
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1918 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1919 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1920 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1921 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1922 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1923 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1924 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1925
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1926 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1927 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1928 arg = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1929 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1930 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1931 /* "A negative field width is taken as a '-' flag
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1932 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1933 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1934 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1935 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1936 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1937 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1938 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1939 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1940 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1941 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1942
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1943 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1944 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1945 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1946 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1947 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1948 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1949
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1950 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1951 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1952 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1953 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1954 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1955 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1956 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1957
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1958 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1959 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1960 arg = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1961 /* "A negative precision is taken as if the precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1962 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1963 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1964 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1965 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1966 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1967 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1968 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1969 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1970 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1971 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1972
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1973 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1974 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1975 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1976 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1977 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1978 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1979
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1980 switch (type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1981 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1982 case TYPE_U8_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1983 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1984 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1985 const uint8_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1986 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1987
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1988 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1989 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1990 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1991 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1992 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1993 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1994 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1995 int count = u8_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1996 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1997 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1998 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1999 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2000 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2001 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2002 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2003 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2004 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2005 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2006 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2007 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2008 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2009 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2010 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2011 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2012 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2013 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2014 /* Use the entire string, and count the number of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2015 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2016 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2017 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2018 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2019 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2020 int count = u8_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2021 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2022 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2023 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2024 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2025 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2026 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2027 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2028 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2029 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2030 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2031 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2032 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2033 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2034 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2035 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2036 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2037 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2038 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2039 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2040 arg_end = arg + u8_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2041 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2042 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2043 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2044
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2045 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2046 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2047 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2048 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2049 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2050 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2051 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2052 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2053
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2054 # if DCHAR_IS_UINT8_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2055 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2056 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2057 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2058 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2059 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2060 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2061 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2062 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2063 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2064 size_t converted_len = allocated - length;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2065 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2066 /* Convert from UTF-8 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2067 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2068 u8_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2069 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2070 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2071 converted, &converted_len);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2072 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2073 /* Convert from UTF-8 to UTF-16/UTF-32. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2074 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2075 U8_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2076 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2077 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2078 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2079 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2080 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2081 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2082 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2083 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2084 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2085 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2086 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2087 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2088 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2089 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2090 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2091 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2092 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2093 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2094 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2095 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2096 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2097 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2098
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2099 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2100 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2101 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2102 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2103 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2104 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2105 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2106 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2107 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2108 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2109
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2110 case TYPE_U16_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2111 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2112 const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2113 const uint16_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2114 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2115
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2116 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2117 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2118 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2119 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2120 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2121 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2122 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2123 int count = u16_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2124 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2125 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2126 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2127 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2128 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2129 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2130 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2131 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2132 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2133 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2134 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2135 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2136 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2137 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2138 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2139 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2140 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2141 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2142 /* Use the entire string, and count the number of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2143 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2144 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2145 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2146 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2147 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2148 int count = u16_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2149 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2150 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2151 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2152 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2153 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2154 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2155 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2156 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2157 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2158 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2159 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2160 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2161 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2162 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2163 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2164 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2165 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2166 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2167 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2168 arg_end = arg + u16_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2169 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2170 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2171 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2172
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2173 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2174 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2175 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2176 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2177 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2178 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2179 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2180 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2181
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2182 # if DCHAR_IS_UINT16_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2183 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2184 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2185 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2186 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2187 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2188 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2189 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2190 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2191 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2192 size_t converted_len = allocated - length;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2193 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2194 /* Convert from UTF-16 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2195 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2196 u16_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2197 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2198 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2199 converted, &converted_len);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2200 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2201 /* Convert from UTF-16 to UTF-8/UTF-32. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2202 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2203 U16_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2204 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2205 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2206 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2207 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2208 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2209 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2210 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2211 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2212 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2213 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2214 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2215 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2216 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2217 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2218 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2219 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2220 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2221 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2222 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2223 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2224 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2225 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2226
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2227 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2228 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2229 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2230 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2231 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2232 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2233 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2234 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2235 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2236 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2237
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2238 case TYPE_U32_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2239 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2240 const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2241 const uint32_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2242 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2243
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2244 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2245 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2246 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2247 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2248 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2249 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2250 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2251 int count = u32_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2252 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2253 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2254 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2255 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2256 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2257 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2258 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2259 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2260 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2261 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2262 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2263 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2264 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2265 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2266 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2267 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2268 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2269 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2270 /* Use the entire string, and count the number of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2271 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2272 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2273 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2274 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2275 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2276 int count = u32_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2277 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2278 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2279 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2280 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2281 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2282 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2283 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2284 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2285 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2286 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2287 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2288 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2289 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2290 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2291 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2292 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2293 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2294 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2295 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2296 arg_end = arg + u32_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2297 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2298 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2299 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2300
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2301 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2302 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2303 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2304 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2305 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2306 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2307 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2308 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2309
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2310 # if DCHAR_IS_UINT32_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2311 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2312 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2313 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2314 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2315 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2316 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2317 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2318 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2319 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2320 size_t converted_len = allocated - length;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2321 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2322 /* Convert from UTF-32 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2323 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2324 u32_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2325 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2326 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2327 converted, &converted_len);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2328 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2329 /* Convert from UTF-32 to UTF-8/UTF-16. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2330 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2331 U32_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2332 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2333 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2334 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2335 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2336 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2337 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2338 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2339 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2340 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2341 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2342 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2343 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2344 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2345 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2346 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2347 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2348 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2349 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2350 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2351 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2352 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2353 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2354
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2355 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2356 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2357 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2358 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2359 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2360 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2361 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2362 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2363 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2364 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2365
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2366 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2367 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2368 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2369 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2370 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
2371 #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2372 else if (dp->conversion == 's'
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2373 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2374 && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2375 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2376 && a.arg[dp->arg_index].type == TYPE_WIDE_STRING
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2377 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2378 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2379 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2380 /* The normal handling of the 's' directive below requires
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2381 allocating a temporary buffer. The determination of its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2382 length (tmp_length), in the case when a precision is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2383 specified, below requires a conversion between a char[]
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2384 string and a wchar_t[] wide string. It could be done, but
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2385 we have no guarantee that the implementation of sprintf will
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2386 use the exactly same algorithm. Without this guarantee, it
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2387 is possible to have buffer overrun bugs. In order to avoid
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2388 such bugs, we implement the entire processing of the 's'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2389 directive ourselves. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2390 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2391 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2392 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2393 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2394 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2395
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2396 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2397 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2398 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2399 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2400 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2401 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2402 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2403
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2404 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2405 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2406 arg = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2407 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2408 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2409 /* "A negative field width is taken as a '-' flag
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2410 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2411 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2412 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2413 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2414 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2415 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2416 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2417 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2418 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2419 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2420
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2421 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2422 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2423 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2424 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2425 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2426 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2427
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2428 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2429 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2430 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2431 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2432 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2433 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2434 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2435
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2436 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2437 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2438 arg = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2439 /* "A negative precision is taken as if the precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2440 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2441 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2442 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2443 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2444 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2445 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2446 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2447 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2448 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2449 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2450
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2451 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2452 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2453 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2454 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2455 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2456 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2457
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2458 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2459 /* %s in vasnwprintf. See the specification of fwprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2460 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2461 const char *arg = a.arg[dp->arg_index].a.a_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2462 const char *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2463 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2464
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2465 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2466 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2467 /* Use only as many bytes as needed to produce PRECISION
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2468 wide characters, from the left. */
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2469 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2470 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2471 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2472 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2473 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2474 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2475 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2476 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2477 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2478 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2479 count = mbrlen (arg_end, MB_CUR_MAX, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2480 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2481 count = mblen (arg_end, MB_CUR_MAX);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2482 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2483 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2484 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2485 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2486 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2487 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2488 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2489 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2490 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2491 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2492 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2493 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2494 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2495 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2496 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2497 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2498 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2499 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2500 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2501 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2502 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2503 /* Use the entire string, and count the number of wide
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2504 characters. */
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2505 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2506 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2507 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2508 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2509 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2510 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2511 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2512 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2513 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2514 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2515 count = mbrlen (arg_end, MB_CUR_MAX, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2516 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2517 count = mblen (arg_end, MB_CUR_MAX);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2518 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2519 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2520 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2521 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2522 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2523 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2524 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2525 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2526 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2527 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2528 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2529 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2530 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2531 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2532 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2533 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2534 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2535 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2536 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2537 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2538 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2539 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2540 arg_end = arg + strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2541 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2542 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2543 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2544
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2545 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2546 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2547 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2548 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2549 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2550 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2551 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2552 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2553
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2554 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2555 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2556 /* We know the number of wide characters in advance. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2557 size_t remaining;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2558 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2559 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2560 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2561 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2562 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2563 for (remaining = characters; remaining > 0; remaining--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2564 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2565 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2566 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2567 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2568 count = mbrtowc (&wc, arg, arg_end - arg, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2569 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2570 count = mbtowc (&wc, arg, arg_end - arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2571 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2572 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2573 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2574 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2575 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2576 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2577 arg += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2578 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2579 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2580 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2581 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2582 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2583 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2584 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2585 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2586 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2587 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2588 while (arg < arg_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2589 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2590 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2591 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2592 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2593 count = mbrtowc (&wc, arg, arg_end - arg, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2594 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2595 count = mbtowc (&wc, arg, arg_end - arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2596 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2597 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2598 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2599 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2600 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2601 ENSURE_ALLOCATION (xsum (length, 1));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2602 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2603 arg += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2604 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2605 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2606
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2607 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2608 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2609 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2610 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2611 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2612 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2613 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2614 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2615 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2616 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2617 /* %ls in vasnprintf. See the specification of fprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2618 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2619 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2620 const wchar_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2621 size_t characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2622 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2623 /* This code assumes that TCHAR_T is 'char'. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2624 typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2625 TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2626 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2627 size_t tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2628 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2629 size_t w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2630
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2631 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2632 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2633 /* Use only as many wide characters as needed to produce
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2634 at most PRECISION bytes, from the left. */
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2635 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2636 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2637 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2638 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2639 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2640 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2641 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2642 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2643 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2644 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2645
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2646 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2647 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2648 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2649 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2650 count = wcrtomb (cbuf, *arg_end, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2651 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2652 count = wctomb (cbuf, *arg_end);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2653 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2654 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2655 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2656 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2657 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2658 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2659 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2660 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2661 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2662 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2663 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2664 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2665 if (precision < count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2666 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2667 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2668 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2669 precision -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2670 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2671 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2672 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2673 else if (has_width)
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2674 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2675 else
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2676 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2677 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2678 /* Use the entire string, and count the number of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2679 bytes. */
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2680 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2681 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2682 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2683 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2684 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2685 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2686 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2687 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2688 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2689 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2690
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2691 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2692 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2693 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2694 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2695 count = wcrtomb (cbuf, *arg_end, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2696 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2697 count = wctomb (cbuf, *arg_end);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2698 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2699 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2700 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2701 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2702 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2703 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2704 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2705 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2706 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2707 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2708 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2709 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2710 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2711 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2712 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2713 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2714 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2715 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2716 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2717 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2718 arg_end = arg + local_wcslen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2719 /* The number of bytes doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2720 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2721 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2722 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2723
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2724 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2725 /* Convert the string into a piece of temporary memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2726 tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2727 if (tmpsrc == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2728 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2729 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2730 TCHAR_T *tmpptr = tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2731 size_t remaining;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2732 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2733 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2734 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2735 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2736 for (remaining = characters; remaining > 0; )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2737 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2738 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2739 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2740
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2741 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2742 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2743 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2744 count = wcrtomb (cbuf, *arg, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2745 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2746 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2747 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2748 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2749 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2750 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2751 memcpy (tmpptr, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2752 tmpptr += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2753 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2754 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2755 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2756 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2757 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2758 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2759
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2760 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2761 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2762 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2763 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2764 tmpsrc, characters,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2765 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2766 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2767 if (tmpdst == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2768 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2769 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2770 free (tmpsrc);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2771 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2772 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2773 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2774 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2775 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2776 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2777 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2778 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2779 free (tmpsrc);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2780 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2781
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2782 if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2783 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2784 # if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2785 /* Outside POSIX, it's preferrable to compare the width
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2786 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2787 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2788 w = DCHAR_MBSNLEN (result + length, characters);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2789 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2790 /* The width is compared against the number of _bytes_
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2791 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2792 w = characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2793 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2794 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2795 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2796 /* w doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2797 w = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2798
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2799 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2800 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2801 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2802 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2803 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2804 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2805 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2806 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2807
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2808 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2809 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2810 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2811 /* We know the number of bytes in advance. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2812 size_t remaining;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2813 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2814 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2815 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2816 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2817 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2818 for (remaining = characters; remaining > 0; )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2819 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2820 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2821 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2822
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2823 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2824 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2825 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2826 count = wcrtomb (cbuf, *arg, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2827 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2828 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2829 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2830 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2831 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2832 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2833 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2834 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2835 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2836 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2837 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2838 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2839 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2840 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2841 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2842 {
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2843 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2844 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2845 memset (&state, '\0', sizeof (mbstate_t));
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2846 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2847 while (arg < arg_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2848 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2849 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2850 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2851
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2852 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2853 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2854 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2855 count = wcrtomb (cbuf, *arg, &state);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2856 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2857 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2858 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2859 if (count <= 0)
13198
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2860 {
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2861 /* Cannot convert. */
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2862 if (!(result == resultbuf || result == NULL))
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2863 free (result);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2864 if (buf_malloced != NULL)
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2865 free (buf_malloced);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2866 CLEANUP ();
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2867 errno = EILSEQ;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2868 return NULL;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2869 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2870 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2871 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2872 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2873 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2874 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2875 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2876 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2877 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2878 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2879 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2880 length += tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2881 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2882
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2883 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2884 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2885 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2886 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2887 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2888 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2889 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2890 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2891 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2892 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2893 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2894 #endif
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2895 #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2896 else if ((dp->conversion == 'a' || dp->conversion == 'A')
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2897 # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2898 && (0
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2899 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2900 || a.arg[dp->arg_index].type == TYPE_DOUBLE
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2901 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2902 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2903 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2904 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2905 )
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2906 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2907 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2908 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2909 arg_type type = a.arg[dp->arg_index].type;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2910 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2911 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2912 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2913 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2914 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2915 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2916 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2917 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2918 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2919 DCHAR_T *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2920
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2921 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2922 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2923 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2924 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2925 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2926 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2927 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2928
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2929 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2930 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2931 arg = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2932 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2933 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2934 /* "A negative field width is taken as a '-' flag
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2935 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2936 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2937 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2938 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2939 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2940 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2941 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2942 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2943 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2944 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2945
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2946 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2947 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2948 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2949 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2950 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2951 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2952
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2953 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2954 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2955 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2956 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2957 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2958 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2959 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2960
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2961 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2962 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2963 arg = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2964 /* "A negative precision is taken as if the precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2965 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2966 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2967 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2968 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2969 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2970 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2971 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2972 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2973 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2974 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2975
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2976 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2977 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2978 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2979 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2980 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2981 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2982
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2983 /* Allocate a temporary buffer of sufficient size. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2984 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2985 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2986 (unsigned int) ((LDBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2987 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2988 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2989 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2990 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2991 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2992 (unsigned int) ((DBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2993 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2994 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2995 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2996 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2997 tmp_length = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2998 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2999 tmp_length = xsum (tmp_length, 12);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3000
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3001 if (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3002 tmp_length = width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3003
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3004 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3005
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3006 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3007 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3008 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3009 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3010 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3011
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3012 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3013 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3014 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3015 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3016 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3017 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3018 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3019 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3020
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3021 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3022 p = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3023 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3024 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3025 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3026 long double arg = a.arg[dp->arg_index].a.a_longdouble;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3027
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3028 if (isnanl (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3029 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3030 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3031 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3032 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3033 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3034 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3035 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3036 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3037 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3038 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3039 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3040 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3041 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3042 DECL_LONG_DOUBLE_ROUNDING
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3043
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3044 BEGIN_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3045
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3046 if (signbit (arg)) /* arg < 0.0L or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3047 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3048 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3049 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3050 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3051
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3052 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3053 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3054 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3055 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3056 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3057 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3058
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3059 if (arg > 0.0L && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3060 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3061 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3062 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3063 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3064 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3065 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3066 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3067 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3068 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3069 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3070 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3071 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3072 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3073 long double mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3074
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3075 if (arg > 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3076 mantissa = printf_frexpl (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3077 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3078 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3079 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3080 mantissa = 0.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3081 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3082
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3083 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3084 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3085 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3086 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3087 long double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3088 size_t q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3089
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3090 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3091 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3092 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3093 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3094 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3095 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3096 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3097 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3098 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3099 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3100 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3101 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3102 tail *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3103 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3104 if (tail != 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3105 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3106 tail *= 0.0625L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3107 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3108 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3109
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3110 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3111 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3112 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3113 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3114 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3115
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3116 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3117 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3118 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3119 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3120 || mantissa > 0.0L || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3121 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3122 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3123 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3124 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3125 while (mantissa > 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3126 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3127 mantissa *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3128 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3129 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3130 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3131 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3132 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3133 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3134 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3135 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3136 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3137 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3138 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3139 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3140 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3141 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3142 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3143 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3144 *p++ = dp->conversion - 'A' + 'P';
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3145 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3146 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3147 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3148 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3149 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3150 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3151 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3152 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3153 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3154 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3155 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3156 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3157 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3158 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3159 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3160 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3161 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3162 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3163 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3164 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3165 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3166 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3167 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3168 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3169 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3170
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3171 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3172 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3173 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3174 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3175 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3176 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3177 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3178 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3179 # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3180 double arg = a.arg[dp->arg_index].a.a_double;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3181
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3182 if (isnand (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3183 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3184 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3185 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3186 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3187 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3188 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3189 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3190 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3191 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3192 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3193 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3194 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3195 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3196
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3197 if (signbit (arg)) /* arg < 0.0 or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3198 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3199 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3200 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3201 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3202
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3203 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3204 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3205 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3206 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3207 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3208 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3209
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3210 if (arg > 0.0 && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3211 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3212 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3213 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3214 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3215 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3216 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3217 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3218 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3219 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3220 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3221 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3222 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3223 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3224 double mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3225
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3226 if (arg > 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3227 mantissa = printf_frexp (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3228 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3229 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3230 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3231 mantissa = 0.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3232 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3233
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3234 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3235 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3236 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3237 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3238 double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3239 size_t q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3240
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3241 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3242 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3243 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3244 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3245 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3246 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3247 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3248 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3249 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3250 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3251 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3252 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3253 tail *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3254 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3255 if (tail != 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3256 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3257 tail *= 0.0625;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3258 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3259 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3260
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3261 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3262 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3263 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3264 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3265 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3266
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3267 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3268 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3269 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3270 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3271 || mantissa > 0.0 || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3272 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3273 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3274 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3275 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3276 while (mantissa > 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3277 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3278 mantissa *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3279 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3280 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3281 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3282 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3283 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3284 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3285 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3286 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3287 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3288 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3289 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3290 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3291 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3292 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3293 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3294 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3295 *p++ = dp->conversion - 'A' + 'P';
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3296 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3297 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3298 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3299 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3300 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3301 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3302 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3303 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3304 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3305 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3306 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3307 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3308 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3309 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3310 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3311 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3312 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3313 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3314 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3315 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3316 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3317 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3318 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3319 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3320 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3321 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3322 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3323 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3324 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3325 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3326 /* The generated string now extends from tmp to p, with the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3327 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3328 if (has_width && p - tmp < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3329 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3330 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3331 DCHAR_T *end = p + pad;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3332
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3333 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3334 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3335 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3336 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3337 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3338 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3339 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3340 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3341 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3342 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3343
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3344 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3345 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3346 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3347 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3348 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3349 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3350 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3351 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3352 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3353
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3354 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3355 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3356 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3357 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3358 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3359
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3360 p = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3361 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3362
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3363 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3364 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3365
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3366 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3367 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3368 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3369 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3370
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3371 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3372 if (count >= allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3373 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3374 size_t n = xsum (length, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3375
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3376 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3377 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3378
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3379 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3380 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3381 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3382 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3383 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3384 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3385 }
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
3386 #endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3387 #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3388 else if ((dp->conversion == 'f' || dp->conversion == 'F'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3389 || dp->conversion == 'e' || dp->conversion == 'E'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3390 || dp->conversion == 'g' || dp->conversion == 'G'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3391 || dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3392 && (0
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3393 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3394 || a.arg[dp->arg_index].type == TYPE_DOUBLE
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3395 # elif NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3396 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3397 /* The systems (mingw) which produce wrong output
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3398 for Inf, -Inf, and NaN also do so for -0.0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3399 Therefore we treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3400 && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3401 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3402 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3403 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3404 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3405 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3406 /* Some systems produce wrong output for Inf,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3407 -Inf, and NaN. Some systems in this category
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3408 (IRIX 5.3) also do so for -0.0. Therefore we
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3409 treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3410 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3411 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3412 ))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3413 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3414 # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3415 arg_type type = a.arg[dp->arg_index].type;
8846
6fe000202977 Fix a warning in last patch.
Bruno Haible <bruno@clisp.org>
parents: 8840
diff changeset
3416 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3417 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3418 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3419 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3420 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3421 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3422 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3423 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3424 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3425 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3426 DCHAR_T *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3427
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3428 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3429 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3430 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3431 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3432 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3433 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3434 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3435
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3436 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3437 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3438 arg = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3439 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3440 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3441 /* "A negative field width is taken as a '-' flag
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3442 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3443 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3444 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3445 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3446 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3447 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3448 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3449 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3450 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3451 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3452
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3453 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3454 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3455 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3456 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3457 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3458 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3459
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3460 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3461 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3462 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3463 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3464 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3465 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3466 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3467
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3468 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3469 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3470 arg = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3471 /* "A negative precision is taken as if the precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3472 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3473 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3474 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3475 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3476 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3477 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3478 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3479 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3480 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3481 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3482
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3483 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3484 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3485 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3486 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3487 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3488 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3489
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3490 /* POSIX specifies the default precision to be 6 for %f, %F,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3491 %e, %E, but not for %g, %G. Implementations appear to use
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3492 the same default precision also for %g, %G. But for %a, %A,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3493 the default precision is 0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3494 if (!has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3495 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3496 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3497
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3498 /* Allocate a temporary buffer of sufficient size. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3499 # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3500 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3501 # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3502 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3503 # elif NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3504 tmp_length = LDBL_DIG + 1;
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3505 # elif NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3506 tmp_length = DBL_DIG + 1;
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3507 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3508 tmp_length = 0;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3509 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3510 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3511 tmp_length = precision;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3512 # if NEED_PRINTF_LONG_DOUBLE
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3513 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3514 if (type == TYPE_LONGDOUBLE)
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3515 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3516 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3517 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3518 long double arg = a.arg[dp->arg_index].a.a_longdouble;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3519 if (!(isnanl (arg) || arg + arg == arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3520 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3521 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3522 int exponent = floorlog10l (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3523 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3524 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3525 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3526 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3527 # endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3528 # if NEED_PRINTF_DOUBLE
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3529 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3530 if (type == TYPE_DOUBLE)
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3531 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3532 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3533 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3534 double arg = a.arg[dp->arg_index].a.a_double;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3535 if (!(isnand (arg) || arg + arg == arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3536 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3537 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3538 int exponent = floorlog10 (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3539 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3540 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3541 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3542 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3543 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3544 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3545 tmp_length = xsum (tmp_length, 12);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3546
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3547 if (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3548 tmp_length = width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3549
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3550 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3551
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3552 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3553 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3554 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3555 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3556 size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3557
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3558 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3559 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3560 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3561 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3562 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3563 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3564 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3565 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3566
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3567 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3568 p = tmp;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3569
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3570 # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3571 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3572 if (type == TYPE_LONGDOUBLE)
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3573 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3574 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3575 long double arg = a.arg[dp->arg_index].a.a_longdouble;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3576
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3577 if (isnanl (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3578 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3579 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3580 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3581 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3582 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3583 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3584 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3585 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3586 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3587 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3588 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3589 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3590 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3591 DECL_LONG_DOUBLE_ROUNDING
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3592
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3593 BEGIN_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3594
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3595 if (signbit (arg)) /* arg < 0.0L or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3596 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3597 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3598 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3599 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3600
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3601 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3602 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3603 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3604 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3605 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3606 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3607
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3608 if (arg > 0.0L && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3609 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3610 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3611 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3612 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3613 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3614 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3615 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3616 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3617 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3618 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3619 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3620 {
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3621 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3622 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3623
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3624 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3625 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3626 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3627 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3628
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3629 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3630 scale10_round_decimal_long_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3631 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3632 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3633 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3634 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3635 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3636 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3637
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3638 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3639 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3640 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3641 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3642 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3643 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3644 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3645 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3646 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3647 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3648 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3649 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3650 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3651 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3652 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3653 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3654 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3655 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3656 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3657 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3658 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3659
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3660 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3661 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3662 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3663 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3664 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3665
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3666 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3667 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3668 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3669 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3670 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3671 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3672 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3673 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3674 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3675 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3676 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3677 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3678 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3679 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3680 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3681 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3682 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3683
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3684 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3685 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3686 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3687 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3688 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3689 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3690 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3691 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3692 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3693 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3694 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3695 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3696 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3697
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3698 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3699 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3700 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3701 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3702 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3703 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3704 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3705 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3706 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3707 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3708 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3709 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3710 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3711 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3712 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3713 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3714 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3715 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3716 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3717 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3718 if (is_borderline (digits, precision))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3719 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3720 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3721 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3722 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3723 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3724 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3725 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3726 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3727 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3728 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3729 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3730 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3731 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3732 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3733 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3734 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3735 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3736 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3737 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3738 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3739 free (digits2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3740 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3741 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3742
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3743 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3744 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3745 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3746 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3747 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3748 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3749 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3750 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3751 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3752 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3753
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3754 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3755 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3756
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3757 *p++ = dp->conversion; /* 'e' or 'E' */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3758 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3759 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3760 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3761 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3762 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3763 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3764 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3765 p++;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3766 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3767 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3768 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3769 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3770 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3771 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3772 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3773 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3774 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3775 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3776 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3777 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3778 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3779 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3780 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3781 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3782 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3783 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3784 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3785 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3786 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3787 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3788
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3789 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3790 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3791 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3792 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3793 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3794 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3795 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3796 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3797 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3798
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3799 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3800 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3801 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3802 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3803 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3804 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3805 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3806 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3807 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3808 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3809 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3810 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3811 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3812 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3813 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3814 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3815 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3816 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3817 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3818 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3819
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3820 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3821 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3822 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3823 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3824 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3825 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3826 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3827 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3828 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3829 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3830 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3831 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3832 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3833
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3834 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3835 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3836 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3837 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3838 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3839 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3840 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3841 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3842 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3843 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3844 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3845 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3846 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3847 if (ndigits < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3848 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3849 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3850 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3851 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3852 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3853 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3854 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3855 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3856 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3857 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3858 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3859 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3860 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3861 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3862 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3863 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3864 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3865 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3866 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3867 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3868 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3869 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3870 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3871 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3872 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3873 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3874 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3875 free (digits2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3876 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3877 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3878
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3879 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3880 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3881 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3882 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3883 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3884 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3885 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3886
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3887 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3888 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3889 && exponent < (long)precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3890 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3891 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3892 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3893 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3894 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3895 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3896 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3897 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3898 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3899 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3900 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3901 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3902 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3903 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3904 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3905 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3906 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3907 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3908 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3909 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3910 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3911 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3912 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3913 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3914 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3915 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3916 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3917 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3918 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3919 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3920 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3921 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3922 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3923 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3924 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3925 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3926 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3927 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3928 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3929 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3930 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3931 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3932 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3933 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3934 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3935 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3936 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3937 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3938 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3939 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3940 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3941 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3942 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3943 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3944 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3945 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3946 p++;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3947 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3948 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3949 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3950 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3951 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3952 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3953 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3954 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3955 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3956 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3957 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3958 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3959 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3960 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3961 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3962 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3963 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3964
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3965 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3966 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3967 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3968 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3969 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3970 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3971 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3972 if (!(arg == 0.0L))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3973 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3974
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3975 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3976
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3977 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3978 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3979 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3980 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3981 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3982 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3983 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3984 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3985 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3986 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3987 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3988 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3989 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3990 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3991 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3992 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3993 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3994 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3995 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3996 *p++ = dp->conversion; /* 'e' or 'E' */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3997 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3998 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3999 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4000 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4001 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4002 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4003 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4004 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4005 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4006 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4007 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4008 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4009 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4010 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4011 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4012 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4013 else if (dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4014 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4015 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4016 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4017 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4018 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4019 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4020 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4021 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4022 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4023 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4024 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4025 *p++ = dp->conversion - 'A' + 'P';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4026 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4027 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4028 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4029 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4030 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
4031 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4032 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4033
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4034 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4035 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4036 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4037 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4038 else
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4039 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4040 # endif
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4041 # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4042 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4043 double arg = a.arg[dp->arg_index].a.a_double;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4044
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4045 if (isnand (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4046 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4047 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4048 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4049 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4050 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4051 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4052 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4053 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4054 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4055 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4056 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4057 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4058 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4059
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4060 if (signbit (arg)) /* arg < 0.0 or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4061 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4062 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4063 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4064 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4065
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4066 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4067 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4068 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4069 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4070 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4071 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4072
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4073 if (arg > 0.0 && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4074 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4075 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4076 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4077 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4078 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4079 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4080 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4081 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4082 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4083 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4084 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4085 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4086 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4087 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4088
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4089 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4090 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4091 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4092 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4093
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4094 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4095 scale10_round_decimal_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4096 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4097 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4098 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4099
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4100 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4101 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4102 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4103 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4104 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4105 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4106 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4107 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4108 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4109 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4110 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4111 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4112 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4113 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4114 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4115 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4116 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4117 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4118 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4119 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4120 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4121
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4122 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4123 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4124 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4125 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4126 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4127
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4128 if (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4129 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4130 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4131 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4132 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4133 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4134 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4135 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4136 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4137 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4138 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4139 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4140 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4141 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4142 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4143 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4144 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4145
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4146 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4147 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4148 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4149 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4150 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4151 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4152 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4153 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4154 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4155 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4156
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4157 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4158 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4159 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4160 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4161 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4162 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4163 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4164 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4165 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4166 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4167 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4168 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4169 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4170 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4171 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4172 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4173 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4174 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4175 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4176 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4177 if (is_borderline (digits, precision))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4178 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4179 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4180 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4181 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4182 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4183 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4184 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4185 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4186 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4187 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4188 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4189 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4190 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4191 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4192 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4193 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4194 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4195 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4196 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4197 free (digits2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4198 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4199 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4200
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4201 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4202 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4203 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4204 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4205 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4206 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4207 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4208 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4209 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4210 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4211
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4212 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4213 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4214
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4215 *p++ = dp->conversion; /* 'e' or 'E' */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4216 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4217 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4218 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4219 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4220 as the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4221 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4222 { '%', '+', '.', '3', 'd', '\0' };
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4223 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4224 { '%', '+', '.', '2', 'd', '\0' };
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4225 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4226 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4227 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4228 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4229 p++;
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4230 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4231 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4232 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4233 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4234 as the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4235 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4236 "%+.3d";
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4237 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4238 "%+.2d";
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4239 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4240 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4241 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4242 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4243 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4244 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4245 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4246 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4247 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4248 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4249 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4250 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4251 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4252 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4253 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4254 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4255 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4256 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4257 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4258 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4259 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4260 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4261 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4262
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4263 if (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4264 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4265 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4266 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4267 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4268 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4269 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4270 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4271 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4272
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4273 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4274 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4275 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4276 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4277 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4278 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4279 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4280 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4281 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4282 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4283 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4284 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4285 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4286 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4287 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4288 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4289 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4290 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4291 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4292 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4293
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4294 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4295 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4296 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4297 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4298 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4299 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4300 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4301 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4302 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4303 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4304
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4305 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4306 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4307 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4308 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4309 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4310 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4311 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4312 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4313 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4314 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4315 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4316 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4317 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4318 if (ndigits < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4319 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4320 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4321 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4322 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4323 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4324 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4325 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4326 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4327 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4328 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4329 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4330 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4331 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4332 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4333 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4334 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4335 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4336 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4337 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4338 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4339 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4340 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4341 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4342 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4343 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4344 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4345 free (digits2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4346 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4347 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4348
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4349 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4350 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4351 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4352 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4353 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4354 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4355 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4356
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4357 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4358 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4359 && exponent < (long)precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4360 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4361 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4362 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4363 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4364 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4365 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4366 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4367 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4368 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4369 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4370 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4371 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4372 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4373 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4374 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4375 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4376 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4377 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4378 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4379 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4380 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4381 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4382 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4383 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4384 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4385 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4386 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4387 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4388 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4389 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4390 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4391 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4392 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4393 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4394 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4395 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4396 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4397 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4398 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4399 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4400 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4401 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4402 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4403 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4404 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4405 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4406 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4407 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4408 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4409 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4410 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4411 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4412 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4413 as the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4414 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4415 { '%', '+', '.', '3', 'd', '\0' };
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4416 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4417 { '%', '+', '.', '2', 'd', '\0' };
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4418 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4419 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4420 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4421 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4422 p++;
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4423 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4424 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4425 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4426 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4427 as the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4428 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4429 "%+.3d";
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4430 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4431 "%+.2d";
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4432 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4433 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4434 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4435 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4436 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4437 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4438 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4439 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4440 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4441 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4442 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4443 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4444 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4445 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4446 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4447 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4448 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4449 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4450
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4451 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4452 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4453 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4454 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4455 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4456 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4457 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4458 if (!(arg == 0.0))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4459 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4460
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4461 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4462
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4463 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4464 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4465 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4466 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4467 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4468 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4469 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4470 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4471 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4472 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4473 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4474 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4475 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4476 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4477 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4478 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4479 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4480 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4481 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4482 *p++ = dp->conversion; /* 'e' or 'E' */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4483 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4484 /* Produce the same number of exponent digits as
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4485 the native printf implementation. */
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4486 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4487 *p++ = '0';
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4488 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4489 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4490 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4491 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4492 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4493 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4494 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4495 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4496 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4497 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4498 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4499 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4500 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4501 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4502 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4503 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4504 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4505 abort ();
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4506 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4507 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4508 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4509 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4510 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4511
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4512 /* The generated string now extends from tmp to p, with the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4513 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4514 if (has_width && p - tmp < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4515 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4516 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4517 DCHAR_T *end = p + pad;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4518
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4519 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4520 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4521 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4522 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4523 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4524 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4525 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4526 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4527 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4528 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4529
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4530 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4531 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4532 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4533 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4534 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4535 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4536 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4537 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4538 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4539
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4540 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4541 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4542 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4543 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4544 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4545
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4546 p = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4547 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4548
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4549 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4550 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4551
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4552 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4553 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4554 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4555 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4556
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4557 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4558 if (count >= allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4559 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4560 size_t n = xsum (length, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4561
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4562 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4563 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4564
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4565 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4566 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4567 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4568 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4569 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4570 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4571 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4572 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4573 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4574 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4575 arg_type type = a.arg[dp->arg_index].type;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4576 int flags = dp->flags;
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4577 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4578 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4579 size_t width;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4580 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4581 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4582 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4583 size_t precision;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4584 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4585 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4586 int prec_ourselves;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4587 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4588 # define prec_ourselves 0
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4589 #endif
9674
b69f1141e94f Fix *printf behaviour regarding the left-adjust flag on HP-UX 10.20.
Bruno Haible <bruno@clisp.org>
parents: 9672
diff changeset
4590 #if NEED_PRINTF_FLAG_LEFTADJUST
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4591 # define pad_ourselves 1
9674
b69f1141e94f Fix *printf behaviour regarding the left-adjust flag on HP-UX 10.20.
Bruno Haible <bruno@clisp.org>
parents: 9672
diff changeset
4592 #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4593 int pad_ourselves;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4594 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4595 # define pad_ourselves 0
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4596 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4597 TCHAR_T *fbp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4598 unsigned int prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4599 int prefixes[2] IF_LINT (= { 0 });
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4600 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4601 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4602 TCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4603 TCHAR_T *tmp;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4604 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4605
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4606 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4607 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4608 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4609 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4610 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4611 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4612 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4613 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4614
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4615 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4616 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4617 arg = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4618 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4619 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4620 /* "A negative field width is taken as a '-' flag
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4621 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4622 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4623 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4624 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4625 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4626 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4627 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4628 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4629 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4630 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4631
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4632 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4633 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4634 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4635 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4636 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4637 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4638 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4639
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4640 #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4641 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4642 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4643 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4644 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4645 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4646 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4647 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4648
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4649 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4650 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4651 arg = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4652 /* "A negative precision is taken as if the precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4653 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4654 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4655 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4656 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4657 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4658 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4659 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4660 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4661 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4662 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4663
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4664 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4665 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4666 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4667 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4668 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4669 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4670 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4671
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4672 /* Decide whether to handle the precision ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4673 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4674 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4675 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4676 case 'd': case 'i': case 'u':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4677 case 'o':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4678 case 'x': case 'X': case 'p':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4679 prec_ourselves = has_precision && (precision > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4680 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4681 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4682 prec_ourselves = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4683 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4684 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4685 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4686
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4687 /* Decide whether to perform the padding ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4688 #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4689 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4690 {
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4691 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4692 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4693 to perform the padding after this conversion. Functions
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4694 with unistdio extensions perform the padding based on
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4695 character count rather than element count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4696 case 'c': case 's':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4697 # endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4698 # if NEED_PRINTF_FLAG_ZERO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4699 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4700 case 'a': case 'A':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4701 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4702 pad_ourselves = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4703 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4704 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4705 pad_ourselves = prec_ourselves;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4706 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4707 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4708 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4709
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4710 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4711 /* Allocate a temporary buffer of sufficient size for calling
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4712 sprintf. */
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4713 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4714 MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4715 flags, width, has_precision, precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4716 pad_ourselves);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4717
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4718 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4719 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4720 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4721 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4722 size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4723
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4724 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4725 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4726 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4727 tmp = (TCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4728 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4729 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4730 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4731 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4732 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4733
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4734 /* Construct the format string for calling snprintf or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4735 sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4736 fbp = buf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4737 *fbp++ = '%';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4738 #if NEED_PRINTF_FLAG_GROUPING
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4739 /* The underlying implementation doesn't support the ' flag.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4740 Produce no grouping characters in this case; this is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4741 acceptable because the grouping is locale dependent. */
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4742 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4743 if (flags & FLAG_GROUP)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4744 *fbp++ = '\'';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4745 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4746 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4747 *fbp++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4748 if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4749 *fbp++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4750 if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4751 *fbp++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4752 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4753 *fbp++ = '#';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4754 if (!pad_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4755 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4756 if (flags & FLAG_ZERO)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4757 *fbp++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4758 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4759 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4760 size_t n = dp->width_end - dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4761 /* The width specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4762 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4763 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4764 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4765 memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4766 fbp += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4767 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4768 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4769 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4770 const FCHAR_T *mp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4771 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4772 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4773 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4774 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4775 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4776 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4777 if (!prec_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4778 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4779 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4780 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4781 size_t n = dp->precision_end - dp->precision_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4782 /* The precision specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4783 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4784 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4785 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4786 memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4787 fbp += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4788 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4789 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4790 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4791 const FCHAR_T *mp = dp->precision_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4792 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4793 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4794 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4795 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4796 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4797 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4798
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4799 switch (type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4800 {
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4801 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4802 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4803 case TYPE_ULONGLONGINT:
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4804 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4805 *fbp++ = 'I';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4806 *fbp++ = '6';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4807 *fbp++ = '4';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4808 break;
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4809 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4810 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4811 /*FALLTHROUGH*/
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4812 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4813 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4814 case TYPE_LONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4815 case TYPE_ULONGINT:
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4816 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4817 case TYPE_WIDE_CHAR:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4818 #endif
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4819 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4820 case TYPE_WIDE_STRING:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4821 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4822 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4823 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4824 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4825 *fbp++ = 'L';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4826 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4827 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4828 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4829 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4830 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4831 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4832 *fbp = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4833 else
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4834 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4835 *fbp = dp->conversion;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4836 #if USE_SNPRINTF
9676
cbba7bfe013b Don't use %n on native Woe32 systems, to avoid a crash on Windows Vista systems.
Bruno Haible <bruno@clisp.org>
parents: 9674
diff changeset
4837 # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4838 fbp[1] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4839 fbp[2] = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4840 fbp[3] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4841 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4842 /* On glibc2 systems from glibc >= 2.3 - probably also older
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4843 ones - we know that snprintf's returns value conforms to
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4844 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4845 Therefore we can avoid using %n in this situation.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4846 On glibc2 systems from 2004-10-18 or newer, the use of %n
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4847 in format strings in writable memory may crash the program
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4848 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4849 in this situation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4850 /* On native Win32 systems (such as mingw), we can avoid using
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4851 %n because:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4852 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4853 snprintf does not write more than the specified number
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4854 of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4855 '4', '5', '6' into buf, not '4', '5', '\0'.)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4856 - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4857 allows us to recognize the case of an insufficient
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4858 buffer size: it returns -1 in this case.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4859 On native Win32 systems (such as mingw) where the OS is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4860 Windows Vista, the use of %n in format strings by default
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4861 crashes the program. See
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4862 <http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4863 <http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4864 So we should avoid %n in this situation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4865 fbp[1] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4866 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4867 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4868 fbp[1] = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4869 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4870
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4871 /* Construct the arguments for calling snprintf or sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4872 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4873 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4874 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4875 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4876 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4877 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4878 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4879 if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4880 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4881 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4882 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4883 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4884 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4885
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4886 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4887 /* The SNPRINTF result is appended after result[0..length].
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4888 The latter is an array of DCHAR_T; SNPRINTF appends an
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4889 array of TCHAR_T to it. This is possible because
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4890 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4891 alignof (TCHAR_T) <= alignof (DCHAR_T). */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
4892 # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4893 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4894 where an snprintf() with maxlen==1 acts like sprintf(). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4895 ENSURE_ALLOCATION (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4896 (2 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4897 / TCHARS_PER_DCHAR));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4898 /* Prepare checking whether snprintf returns the count
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4899 via %n. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4900 *(TCHAR_T *) (result + length) = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4901 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4902
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4903 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4904 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4905 int count = -1;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4906
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4907 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4908 int retcount = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4909 size_t maxlen = allocated - length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4910 /* SNPRINTF can fail if its second argument is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4911 > INT_MAX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4912 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4913 maxlen = INT_MAX / TCHARS_PER_DCHAR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4914 maxlen = maxlen * TCHARS_PER_DCHAR;
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4915 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4916 switch (prefix_count) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4917 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4918 case 0: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4919 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4920 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4921 arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4922 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4923 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4924 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4925 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4926 prefixes[0], arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4927 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4928 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4929 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4930 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4931 prefixes[0], prefixes[1], arg, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4932 &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4933 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4934 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4935 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4936 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4937 #else
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4938 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4939 switch (prefix_count) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4940 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4941 case 0: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4942 count = sprintf (tmp, buf, arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4943 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4944 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4945 count = sprintf (tmp, buf, prefixes[0], arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4946 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4947 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4948 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4949 arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4950 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4951 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4952 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4953 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4954 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4955
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4956 switch (type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4957 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4958 case TYPE_SCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4959 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4960 int arg = a.arg[dp->arg_index].a.a_schar;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4961 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4962 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4963 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4964 case TYPE_UCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4965 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4966 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4967 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4968 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4969 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4970 case TYPE_SHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4971 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4972 int arg = a.arg[dp->arg_index].a.a_short;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4973 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4974 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4975 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4976 case TYPE_USHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4977 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4978 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4979 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4980 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4981 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4982 case TYPE_INT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4983 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4984 int arg = a.arg[dp->arg_index].a.a_int;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4985 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4986 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4987 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4988 case TYPE_UINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4989 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4990 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4991 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4992 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4993 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4994 case TYPE_LONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4995 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4996 long int arg = a.arg[dp->arg_index].a.a_longint;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4997 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4998 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4999 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5000 case TYPE_ULONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5001 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5002 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5003 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5004 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5005 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5006 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5007 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5008 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5009 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5010 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5011 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5012 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5013 case TYPE_ULONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5014 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5015 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5016 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5017 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5018 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5019 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5020 case TYPE_DOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5021 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5022 double arg = a.arg[dp->arg_index].a.a_double;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5023 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5024 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5025 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5026 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5027 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5028 long double arg = a.arg[dp->arg_index].a.a_longdouble;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5029 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5030 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5031 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5032 case TYPE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5033 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5034 int arg = a.arg[dp->arg_index].a.a_char;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5035 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5036 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5037 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5038 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5039 case TYPE_WIDE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5040 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5041 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5042 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5043 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5044 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5045 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5046 case TYPE_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5047 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5048 const char *arg = a.arg[dp->arg_index].a.a_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5049 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5050 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5051 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5052 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5053 case TYPE_WIDE_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5054 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5055 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5056 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5057 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5058 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5059 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5060 case TYPE_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5061 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5062 void *arg = a.arg[dp->arg_index].a.a_pointer;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5063 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5064 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5065 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5066 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5067 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5068 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5069
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5070 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5071 /* Portability: Not all implementations of snprintf()
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5072 are ISO C 99 compliant. Determine the number of
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5073 bytes that snprintf() has produced or would have
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5074 produced. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5075 if (count >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5076 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5077 /* Verify that snprintf() has NUL-terminated its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5078 result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5079 if (count < maxlen
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5080 && ((TCHAR_T *) (result + length)) [count] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5081 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5082 /* Portability hack. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5083 if (retcount > count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5084 count = retcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5085 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5086 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5087 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5088 /* snprintf() doesn't understand the '%n'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5089 directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5090 if (fbp[1] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5091 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5092 /* Don't use the '%n' directive; instead, look
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5093 at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5094 fbp[1] = '\0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5095 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5096 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5097 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5098 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5099 /* Look at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5100 if (retcount < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5101 {
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5102 # if !HAVE_SNPRINTF_RETVAL_C99
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5103 /* HP-UX 10.20 snprintf() is doubly deficient:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5104 It doesn't understand the '%n' directive,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5105 *and* it returns -1 (rather than the length
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5106 that would have been required) when the
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5107 buffer is too small.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5108 But a failure at this point can also come
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5109 from other reasons than a too small buffer,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5110 such as an invalid wide string argument to
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5111 the %ls directive, or possibly an invalid
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5112 floating-point argument. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5113 size_t tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5114 MAX_ROOM_NEEDED (&a, dp->arg_index,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5115 dp->conversion, type, flags,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5116 width, has_precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5117 precision, pad_ourselves);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5118
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5119 if (maxlen < tmp_length)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5120 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5121 /* Make more room. But try to do through
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5122 this reallocation only once. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5123 size_t bigger_need =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5124 xsum (length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5125 xsum (tmp_length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5126 TCHARS_PER_DCHAR - 1)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5127 / TCHARS_PER_DCHAR);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5128 /* And always grow proportionally.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5129 (There may be several arguments, each
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5130 needing a little more room than the
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5131 previous one.) */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5132 size_t bigger_need2 =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5133 xsum (xtimes (allocated, 2), 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5134 if (bigger_need < bigger_need2)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5135 bigger_need = bigger_need2;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5136 ENSURE_ALLOCATION (bigger_need);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5137 continue;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5138 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5139 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5140 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5141 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5142 count = retcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5143 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5144 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5145 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5146
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5147 /* Attempt to handle failure. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5148 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5149 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5150 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5151 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5152 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5153 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5154 CLEANUP ();
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5155 errno =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5156 (dp->conversion == 'c' || dp->conversion == 's'
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5157 ? EILSEQ
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5158 : EINVAL);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5159 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5160 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5161
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5162 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5163 /* Handle overflow of the allocated buffer.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5164 If such an overflow occurs, a C99 compliant snprintf()
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5165 returns a count >= maxlen. However, a non-compliant
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5166 snprintf() function returns only count = maxlen - 1. To
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5167 cover both cases, test whether count >= maxlen - 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5168 if ((unsigned int) count + 1 >= maxlen)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5169 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5170 /* If maxlen already has attained its allowed maximum,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5171 allocating more memory will not increase maxlen.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5172 Instead of looping, bail out. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5173 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5174 goto overflow;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5175 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5176 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5177 /* Need at least (count + 1) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5178 bytes. (The +1 is for the trailing NUL.)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5179 But ask for (count + 2) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5180 bytes, so that in the next round, we likely get
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5181 maxlen > (unsigned int) count + 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5182 and so we don't get here again.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5183 And allocate proportionally, to avoid looping
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5184 eternally if snprintf() reports a too small
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5185 count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5186 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5187 xmax (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5188 ((unsigned int) count + 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5189 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5190 / TCHARS_PER_DCHAR),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5191 xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5192
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5193 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5194 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5195 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5196 }
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
5197 #endif
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5198
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5199 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5200 if (prec_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5201 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5202 /* Handle the precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5203 TCHAR_T *prec_ptr =
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5204 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5205 (TCHAR_T *) (result + length);
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5206 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5207 tmp;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5208 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5209 size_t prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5210 size_t move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5211
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5212 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5213 /* Put the additional zeroes after the sign. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5214 if (count >= 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5215 && (*prec_ptr == '-' || *prec_ptr == '+'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5216 || *prec_ptr == ' '))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5217 prefix_count = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5218 /* Put the additional zeroes after the 0x prefix if
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5219 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5220 else if (count >= 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5221 && prec_ptr[0] == '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5222 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5223 prefix_count = 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5224
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5225 move = count - prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5226 if (precision > move)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5227 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5228 /* Insert zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5229 size_t insert = precision - move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5230 TCHAR_T *prec_end;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5231
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5232 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5233 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5234 xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5235 (count + insert + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5236 / TCHARS_PER_DCHAR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5237 length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5238 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5239 length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5240 prec_ptr = (TCHAR_T *) (result + length);
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5241 # endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5242
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5243 prec_end = prec_ptr + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5244 prec_ptr += prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5245
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5246 while (prec_end > prec_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5247 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5248 prec_end--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5249 prec_end[insert] = prec_end[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5250 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5251
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5252 prec_end += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5253 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5254 *--prec_end = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5255 while (prec_end > prec_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5256
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5257 count += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5258 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5259 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5260 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5261
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5262 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5263 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5264 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5265 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5266 abort ();
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5267 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5268
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5269 #if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5270 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5271 if (dp->conversion == 'c' || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5272 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5273 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5274 TYPE_WIDE_STRING.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5275 The result string is not certainly ASCII. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5276 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5277 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5278 size_t tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5279 /* This code assumes that TCHAR_T is 'char'. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5280 typedef int TCHAR_T_verify
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5281 [2 * (sizeof (TCHAR_T) == 1) - 1];
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5282 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5283 tmpsrc = (TCHAR_T *) (result + length);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5284 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5285 tmpsrc = tmp;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5286 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5287 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5288 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5289 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5290 tmpsrc, count,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5291 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5292 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5293 if (tmpdst == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5294 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5295 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5296 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5297 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5298 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5299 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5300 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5301 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5302 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5303 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5304 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5305 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5306 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5307 count = tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5308 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5309 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5310 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5311 /* The result string is ASCII.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5312 Simple 1:1 conversion. */
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5313 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5314 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5315 no-op conversion, in-place on the array starting
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5316 at (result + length). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5317 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5318 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5319 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5320 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5321 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5322 size_t n;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5323
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5324 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5325 if (result == resultbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5326 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5327 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5328 /* ENSURE_ALLOCATION will not move tmpsrc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5329 (because it's part of resultbuf). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5330 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5331 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5332 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5333 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5334 /* ENSURE_ALLOCATION will move the array
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5335 (because it uses realloc(). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5336 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5337 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5338 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5339 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5340 tmpsrc = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5341 ENSURE_ALLOCATION (xsum (length, count));
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5342 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5343 tmpdst = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5344 /* Copy backwards, because of overlapping. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5345 tmpsrc += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5346 tmpdst += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5347 for (n = count; n > 0; n--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5348 *--tmpdst = (unsigned char) *--tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5349 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5350 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5351 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5352
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5353 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5354 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5355 if (count > allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5356 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5357 /* Need at least count elements. But allocate
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5358 proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5359 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5360 xmax (xsum (length, count), xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5361
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5362 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5363 }
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5364 #endif
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5365
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5366 /* Here count <= allocated - length. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5367
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5368 /* Perform padding. */
9674
b69f1141e94f Fix *printf behaviour regarding the left-adjust flag on HP-UX 10.20.
Bruno Haible <bruno@clisp.org>
parents: 9672
diff changeset
5369 #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5370 if (pad_ourselves && has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5371 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5372 size_t w;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5373 # if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5374 /* Outside POSIX, it's preferrable to compare the width
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5375 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5376 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5377 w = DCHAR_MBSNLEN (result + length, count);
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5378 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5379 /* The width is compared against the number of _bytes_
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5380 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5381 w = count;
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5382 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5383 if (w < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5384 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5385 size_t pad = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5386
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5387 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5388 if (xsum (count, pad) > allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5389 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5390 /* Need at least count + pad elements. But
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5391 allocate proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5392 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5393 xmax (xsum3 (length, count, pad),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5394 xtimes (allocated, 2));
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5395
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5396 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5397 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5398 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5399 length -= count;
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5400 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5401 ENSURE_ALLOCATION (n);
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5402 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5403 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5404 /* Here count + pad <= allocated - length. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5405
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5406 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5407 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5408 DCHAR_T * const rp = result + length;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5409 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5410 DCHAR_T * const rp = tmp;
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5411 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5412 DCHAR_T *p = rp + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5413 DCHAR_T *end = p + pad;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5414 DCHAR_T *pad_ptr;
9827
32d08ee66484 Fix a bug in unistdio routines, introduced on 2007-06-10.
Bruno Haible <bruno@clisp.org>
parents: 9676
diff changeset
5415 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5416 if (dp->conversion == 'c'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5417 || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5418 /* No zero-padding for string directives. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5419 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5420 else
9672
c0fb9d4d860d Fix bug introduced on 2007-06-10.
Bruno Haible <bruno@clisp.org>
parents: 9651
diff changeset
5421 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5422 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5423 pad_ptr = (*rp == '-' ? rp + 1 : rp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5424 /* No zero-padding of "inf" and "nan". */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5425 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5426 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5427 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5428 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5429 /* The generated string now extends from rp to p,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5430 with the zero padding insertion point being at
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5431 pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5432
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5433 count = count + pad; /* = end - rp */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5434
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5435 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5436 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5437 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5438 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5439 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5440 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5441 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5442 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5443 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5444 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5445
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5446 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5447 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5448 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5449 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5450 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5451 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5452 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5453 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5454 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5455
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5456 while (p > rp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5457 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5458 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5459 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5460 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5461 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5462 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5463 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5464 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5465
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5466 /* Here still count <= allocated - length. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5467
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5468 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5469 /* The snprintf() result did fit. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5470 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5471 /* Append the sprintf() result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5472 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5473 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5474 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5475 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5476 free (tmp);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5477 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5478
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5479 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5480 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5481 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5482 /* Convert the %f result to upper case for %F. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5483 DCHAR_T *rp = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5484 size_t rc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5485 for (rc = count; rc > 0; rc--, rp++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5486 if (*rp >= 'a' && *rp <= 'z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5487 *rp = *rp - 'a' + 'A';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5488 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5489 #endif
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5490
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5491 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5492 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5493 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5494 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5495 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5496 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5497
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5498 /* Add the final NUL. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5499 ENSURE_ALLOCATION (xsum (length, 1));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5500 result[length] = '\0';
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5501
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5502 if (result != resultbuf && length + 1 < allocated)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5503 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5504 /* Shrink the allocated memory if possible. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5505 DCHAR_T *memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5506
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5507 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5508 if (memory != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5509 result = memory;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5510 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5511
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5512 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5513 free (buf_malloced);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5514 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5515 *lengthp = length;
7218
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
5516 /* Note that we can produce a big string of a length > INT_MAX. POSIX
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
5517 says that snprintf() fails with errno = EOVERFLOW in this case, but
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
5518 that's only because snprintf() returns an 'int'. This function does
0fd12ba5cfc6 Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents: 6583
diff changeset
5519 not have this limitation. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5520 return result;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5521
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5522 #if USE_SNPRINTF
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5523 overflow:
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5524 if (!(result == resultbuf || result == NULL))
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5525 free (result);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5526 if (buf_malloced != NULL)
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5527 free (buf_malloced);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5528 CLEANUP ();
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5529 errno = EOVERFLOW;
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5530 return NULL;
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5531 #endif
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5532
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5533 out_of_memory:
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5534 if (!(result == resultbuf || result == NULL))
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5535 free (result);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5536 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5537 free (buf_malloced);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5538 out_of_memory_1:
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5539 CLEANUP ();
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5540 errno = ENOMEM;
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5541 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5542 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5543 }
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5544
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5545 #undef MAX_ROOM_NEEDED
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5546 #undef TCHARS_PER_DCHAR
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5547 #undef SNPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5548 #undef USE_SNPRINTF
11804
73f84ac92381 Undefine DCHAR_SET after use.
Bruno Haible <bruno@clisp.org>
parents: 11511
diff changeset
5549 #undef DCHAR_SET
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5550 #undef DCHAR_CPY
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5551 #undef PRINTF_PARSE
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5552 #undef DIRECTIVES
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5553 #undef DIRECTIVE
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5554 #undef DCHAR_IS_TCHAR
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5555 #undef TCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5556 #undef DCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5557 #undef FCHAR_T
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5558 #undef VASNPRINTF