annotate lib/vasnprintf.c @ 14079:97fc9a21a8fb

maint: update almost all copyright ranges to include 2011 Run the new "make update-copyright" rule.
author Jim Meyering <meyering@redhat.com>
date Sat, 01 Jan 2011 20:17:23 +0100
parents 87a95303747f
children 2113f76f7b44
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.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13885
diff changeset
2 Copyright (C) 1999, 2002-2011 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
13788
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
91 #include "verify.h"
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
92
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
93 #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
94 # include <math.h>
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
95 # include "float+.h"
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
96 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
97
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
98 #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
99 # 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
100 # 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
101 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
102
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
103 #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
104 # 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
105 # include "isnanl-nolibm.h"
8915
f15c03805bd9 Fix possible compilation error.
Bruno Haible <bruno@clisp.org>
parents: 8865
diff changeset
106 # include "fpucw.h"
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
107 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
108
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
109 #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
110 # 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
111 # include "isnand-nolibm.h"
8526
67e9a647f2fa Fix includes.
Bruno Haible <bruno@clisp.org>
parents: 8487
diff changeset
112 # 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
113 #endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
114
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
115 #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
116 # include <math.h>
8648
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
117 # include "isnanl-nolibm.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
118 # include "printf-frexpl.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
119 # include "fpucw.h"
8335
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
120 #endif
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
121
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
122 /* 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
123 #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
124 # 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
125 # 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
126 # 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
127 # 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
128 # 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
129 # 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
130 # 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
131 # 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
132 # 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
133 # define DCHAR_CPY wmemcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
134 # 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
135 # else
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 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
137 # 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
138 # 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
139 # 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
140 # 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
141 # 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
142 # 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
143 # 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
144 # define DCHAR_CPY memcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
145 # 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
146 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
147 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
148 #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
149 /* TCHAR_T is wchar_t. */
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
150 # define USE_SNPRINTF 1
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
151 # if HAVE_DECL__SNWPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
152 /* 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
153 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
154 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
155 MSVCRT function _snwprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
156 # if defined __MINGW32__
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 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
159 # define SNPRINTF _snwprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
160 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
161 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
162 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
163 # define SNPRINTF swprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
164 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
165 #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
166 /* TCHAR_T is char. */
9940
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
167 /* 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
168 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
169 size argument is >= 0x3000000.
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
170 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
171 writes any output without bounds, like sprintf. */
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
172 # 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
173 # define USE_SNPRINTF 1
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
174 # else
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
175 # define USE_SNPRINTF 0
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
176 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
177 # if HAVE_DECL__SNPRINTF
13197
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
178 /* 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
179 function _snprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
180 # if defined __MINGW32__
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
181 # define SNPRINTF snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
182 /* 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
183 # undef snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
184 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
185 # define SNPRINTF _snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
186 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
187 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
188 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
189 # define SNPRINTF snprintf
8417
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
190 /* 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
191 # undef snprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
192 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
193 #endif
8424
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
194 /* 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
195 #undef sprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
196
10102
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
197 /* 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
198 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
199 #ifdef lint
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
200 # define IF_LINT(Code) Code
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
201 #else
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
202 # define IF_LINT(Code) /* empty */
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
203 #endif
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
204
10084
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
205 /* Avoid some warnings from "gcc -Wshadow".
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
206 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
207 #undef exp
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
208 #define exp expo
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
209 #undef remainder
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
210 #define remainder rem
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
211
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
212 #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
213 # if (HAVE_STRNLEN && !defined _AIX)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
214 # define local_strnlen strnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
215 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
216 # ifndef local_strnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
217 # define local_strnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
218 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
219 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
220 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
221 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
222 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
223 }
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 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
226 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
227
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
228 #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
229 # if HAVE_WCSLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
230 # define local_wcslen wcslen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
231 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
232 /* 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
233 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
234 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
235 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
236 # ifndef local_wcslen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
237 # define local_wcslen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
238 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
239 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
240 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
241 const wchar_t *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 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
244 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
245 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
246 }
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 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
249 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
250
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
251 #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
252 # if HAVE_WCSNLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
253 # define local_wcsnlen wcsnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
254 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
255 # ifndef local_wcsnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
256 # define local_wcsnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
257 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
258 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
259 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
260 const wchar_t *ptr;
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 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
263 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
264 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
265 }
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 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
268 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
269
10450
efaa10653f4b Tweak last commit.
Bruno Haible <bruno@clisp.org>
parents: 10449
diff changeset
270 #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
271 /* 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
272 # 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
273 # 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
274 static char
11856
8b0d11ca9d01 build: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11804
diff changeset
275 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
276 {
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
277 const char *point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
278 /* 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
279 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
280 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
281 localeconv() is rarely multithread-safe. */
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
282 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (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
283 point = nl_langinfo (RADIXCHAR);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
284 # elif 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
285 char pointbuf[5];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
286 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
287 point = &pointbuf[1];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
288 # else
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
289 point = localeconv () -> decimal_point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
290 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
291 /* 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
292 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
293 }
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
294 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
295 #endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
296
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
297 #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
298
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
299 /* 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
300 static int
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
301 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
302 {
9646
a113e473cc98 Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents: 9489
diff changeset
303 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
304 }
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
305
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
306 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
307
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
308 #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
309
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
310 /* 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
311 static int
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
312 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
313 {
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
314 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
315 }
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
316
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
317 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
318
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
319 #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
320
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
321 /* 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
322 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
323 (and slower) algorithms. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
324
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
325 typedef unsigned int mp_limb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
326 # define GMP_LIMB_BITS 32
13788
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
327 verify (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
328
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
329 typedef unsigned long long mp_twolimb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
330 # define GMP_TWOLIMB_BITS 64
13788
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
331 verify (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
332
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
333 /* Representation of a bignum >= 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
334 typedef struct
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
335 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
336 size_t nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
337 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
338 } mpn_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
339
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
340 /* Compute the product of two bignums >= 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
341 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
342 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
343 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
344 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
345 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
346 const mp_limb_t *p1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
347 const mp_limb_t *p2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
348 size_t len1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
349 size_t len2;
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 if (src1.nlimbs <= src2.nlimbs)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
352 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
353 len1 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
354 p1 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
355 len2 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
356 p2 = src2.limbs;
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 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
359 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
360 len1 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
361 p1 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
362 len2 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
363 p2 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
364 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
365 /* Now 0 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
366 if (len1 == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
367 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
368 /* src1 or src2 is zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
369 dest->nlimbs = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
370 dest->limbs = (mp_limb_t *) malloc (1);
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 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
373 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
374 /* Here 1 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
375 size_t dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
376 mp_limb_t *dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
377 size_t k, i, j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
378
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
379 dlen = len1 + len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
380 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
381 if (dp == NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
382 return NULL;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
383 for (k = len2; k > 0; )
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
384 dp[--k] = 0;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
385 for (i = 0; i < len1; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
386 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
387 mp_limb_t digit1 = p1[i];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
388 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
389 for (j = 0; j < len2; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
390 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
391 mp_limb_t digit2 = p2[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
392 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
393 carry += dp[i + j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
394 dp[i + j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
395 carry = carry >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
396 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
397 dp[i + len2] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
398 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
399 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
400 while (dlen > 0 && dp[dlen - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
401 dlen--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
402 dest->nlimbs = dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
403 dest->limbs = dp;
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 return dest->limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
406 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
407
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
408 /* 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
409 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
410 the remainder.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
411 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
412 q is incremented.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
413 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
414 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
415 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
416 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
417 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
418 /* Algorithm:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
419 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
420 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
421 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
422 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
423 r:=0, j:=m,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
424 while j>0 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
425 {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
426 = 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
427 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
428 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
429 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
430 We have a/b < beta^(m-n+1).
11422
847931ff84ef typo fix in a comment
Jim Meyering <meyering@redhat.com>
parents: 11220
diff changeset
431 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
432 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
433 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
434 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
435 Compute q* :
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
436 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
437 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
438 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
439 and c3 := b[n-2] * q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
440 {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
441 occurred. Furthermore 0 <= c3 < beta^2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
442 If there was overflow and
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
443 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
444 the next test can be skipped.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
445 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
446 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
447 If q* > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
448 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
449 [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
450 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
451 u := u + q* * b[i],
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
452 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
453 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
454 r[n+j]:=r[n+j]-u.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
455 {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
456 < q* + 1 <= beta,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
457 the carry u does not overflow.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
458 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
459 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
460 Set q[j] := q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
461 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
462 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
463 rest r.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
464 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
465 Finally, round-to-even:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
466 Shift r left by 1 bit.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
467 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
468 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
469 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
470 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
471 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
472 size_t b_len = b.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
473 mp_limb_t *roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
474 mp_limb_t *tmp_roomptr = NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
475 mp_limb_t *q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
476 size_t q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
477 mp_limb_t *r_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
478 size_t r_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
479
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
480 /* Allocate room for a_len+2 digits.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
481 (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
482 final rounding of q.) */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
483 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
484 if (roomptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
485 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
486
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
487 /* Normalise a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
488 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
489 a_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
490
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
491 /* Normalise b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
492 for (;;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
493 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
494 if (b_len == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
495 /* Division by zero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
496 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
497 if (b_ptr[b_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
498 b_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
499 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
500 break;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
501 }
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 /* 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
504
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
505 if (a_len < b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
506 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
507 /* 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
508 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
509 r_len = a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
510 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
511 q_ptr = roomptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
512 q_len = 0;
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 else if (b_len == 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
515 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
516 /* n=1: single precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
517 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
518 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
519 q_ptr = roomptr + 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
520 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
521 mp_limb_t den = b_ptr[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
522 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
523 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
524 mp_limb_t *destptr = q_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
525 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
526 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
527 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
528 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
529 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
530 *--destptr = num / den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
531 remainder = num % den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
532 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
533 /* Normalise and store r. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
534 if (remainder > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
535 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
536 r_ptr[0] = remainder;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
537 r_len = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
538 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
539 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
540 r_len = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
541 /* Normalise q. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
542 q_len = a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
543 if (q_ptr[q_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
544 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
545 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
546 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
547 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
548 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
549 /* n>1: multiple precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
550 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
551 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
552 /* Determine s. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
553 size_t s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
554 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
555 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
556 s = 31;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
557 if (msd >= 0x10000)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
558 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
559 msd = msd >> 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
560 s -= 16;
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 if (msd >= 0x100)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
563 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
564 msd = msd >> 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
565 s -= 8;
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 if (msd >= 0x10)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
568 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
569 msd = msd >> 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
570 s -= 4;
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 if (msd >= 0x4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
573 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
574 msd = msd >> 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
575 s -= 2;
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 if (msd >= 0x2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
578 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
579 msd = msd >> 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
580 s -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
581 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
582 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
583 /* 0 <= s < GMP_LIMB_BITS.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
584 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
585 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
586 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
587 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
588 if (tmp_roomptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
589 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
590 free (roomptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
591 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
592 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
593 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
594 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
595 mp_limb_t *destptr = tmp_roomptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
596 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
597 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
598 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
599 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
600 accu += (mp_twolimb_t) *sourceptr++ << s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
601 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
602 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
603 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
604 /* 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
605 if (accu != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
606 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
607 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
608 b_ptr = tmp_roomptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
609 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
610 /* 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
611 Memory layout:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
612 At the beginning: r = roomptr[0..a_len],
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
613 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
614 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
615 if (s == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
616 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
617 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
618 r_ptr[a_len] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
619 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
620 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
621 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
622 const mp_limb_t *sourceptr = a_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
623 mp_limb_t *destptr = r_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
624 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
625 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
626 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
627 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
628 accu += (mp_twolimb_t) *sourceptr++ << s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
629 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
630 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
631 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
632 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
633 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
634 q_ptr = roomptr + b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
635 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
636 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
637 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
638 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
639 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
640 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
641 ((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
642 /* Division loop, traversed m-n+1 times.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
643 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
644 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
645 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
646 mp_limb_t q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
647 mp_limb_t c1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
648 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
649 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
650 /* 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
651 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
652 ((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
653 | r_ptr[j + b_len - 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
654 q_star = num / b_msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
655 c1 = num % b_msd;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
658 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
659 /* 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
660 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
661 /* 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
662 <==> 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
663 <==> 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
664 {<= beta !}.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
665 If yes, jump directly to the subtraction loop.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
666 (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
667 <==> 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
668 if (r_ptr[j + b_len] > b_msd
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
669 || (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
670 /* r[j+n] >= b[n-1]+1 or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
671 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
672 carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
673 goto subtract;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
674 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
675 /* q_star = q*,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
676 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
677 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
678 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
679 ((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
680 mp_twolimb_t c3 = /* b[n-2] * q* */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
681 (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
682 /* While c2 < c3, increase c2 and decrease c3.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
683 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
684 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
685 this can happen only twice. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
686 if (c3 > c2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
687 {
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 if (c3 - c2 > b_msdd)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
690 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
691 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
692 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
693 if (q_star > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
694 subtract:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
695 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
696 /* Subtract r := r - b * q* * beta^j. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
697 mp_limb_t cr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
698 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
699 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
700 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
701 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
702 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
703 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
704 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
705 /* Here 0 <= carry <= q*. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
706 carry =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
707 carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
708 + (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
709 + (mp_limb_t) ~(*destptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
710 /* Here 0 <= carry <= beta*q* + beta-1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
711 *destptr++ = ~(mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
712 carry = carry >> GMP_LIMB_BITS; /* <= q* */
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 cr = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
715 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
716 /* 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
717 r_ptr[j + b_len]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
718 if (cr > r_ptr[j + b_len])
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
719 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
720 /* Subtraction gave a carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
721 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
722 /* Add b back. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
723 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
724 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
725 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
726 mp_limb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
727 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
728 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
729 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
730 mp_limb_t source1 = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
731 mp_limb_t source2 = *destptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
732 *destptr++ = source1 + source2 + carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
733 carry =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
734 (carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
735 ? source1 >= (mp_limb_t) ~source2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
736 : source1 > (mp_limb_t) ~source2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
737 }
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 /* 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
740 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
741 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
742 /* q* is determined. Store it as q[j]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
743 q_ptr[j] = q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
744 if (j == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
745 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
746 j--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
747 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
748 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
749 r_len = b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
750 /* Normalise q. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
751 if (q_ptr[q_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
752 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
753 # 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
754 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
755 /* Shift r right by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
756 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
757 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
758 mp_limb_t ptr = r_ptr + r_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
759 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
760 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
761 for (count = r_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
762 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
763 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
764 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
765 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
766 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
767 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
768 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
769 /* Normalise r. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
770 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
771 r_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
772 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
773 /* Compare r << 1 with b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
774 if (r_len > b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
775 goto increment_q;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
776 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
777 size_t i;
8835
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
778 for (i = b_len;;)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
779 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
780 mp_limb_t r_i =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
781 (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
782 | (i < r_len ? r_ptr[i] << 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
783 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
784 if (r_i > b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
785 goto increment_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
786 if (r_i < b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
787 goto keep_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
788 if (i == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
789 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
790 i--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
791 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
792 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
793 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
794 /* q is odd. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
795 increment_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
796 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
797 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
798 for (i = 0; i < q_len; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
799 if (++(q_ptr[i]) != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
800 goto keep_q;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
801 q_ptr[q_len++] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
802 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
803 keep_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
804 if (tmp_roomptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
805 free (tmp_roomptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
806 q->limbs = q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
807 q->nlimbs = q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
808 return roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
809 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
810
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
811 /* 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
812 representation.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
813 Destroys the contents of a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
814 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
815 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
816 of memory allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
817 static char *
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
818 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
819 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
820 mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
821 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
822 /* 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
823 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
824 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
825 if (c_ptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
826 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
827 char *d_ptr = c_ptr;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
828 for (; extra_zeroes > 0; extra_zeroes--)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
829 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
830 while (a_len > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
831 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
832 /* Divide a by 10^9, in-place. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
833 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
834 mp_limb_t *ptr = a_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
835 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
836 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
837 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
838 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
839 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
840 *ptr = num / 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
841 remainder = num % 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
842 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
843 /* Store the remainder as 9 decimal digits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
844 for (count = 9; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
845 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
846 *d_ptr++ = '0' + (remainder % 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
847 remainder = remainder / 10;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
848 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
849 /* Normalize a. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
850 if (a_ptr[a_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
851 a_len--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
852 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
853 /* Remove leading zeroes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
854 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
855 d_ptr--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
856 /* But keep at least one zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
857 if (d_ptr == c_ptr)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
858 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
859 /* Terminate the string. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
860 *d_ptr = '\0';
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 return c_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
863 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
864
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
865 # 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
866
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
867 /* Assuming x is finite and >= 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
868 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
869 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
870 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
871 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
872 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
873 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
874 mpn_t m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
875 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
876 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
877 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
878
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
879 /* Allocate memory for result. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
880 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
881 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
882 if (m.limbs == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
883 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
884 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
885 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
886 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
887 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
888 /* 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
889 latter is an integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
890 /* 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
891 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
892 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
893 '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
894 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
895 # 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
896 # 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
897 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
898 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
899 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
900 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
901 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
902 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
903 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
904 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
905 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
906 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
907 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
908 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
909 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
910 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
911 # else
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
912 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
913 mp_limb_t d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
914 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
915 d = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
916 y -= d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
917 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
918 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
919 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
920 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
921 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
922 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
923 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
924 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
925 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
926 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
927 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
928 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
929 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
930 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
931 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
932 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
933 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
934 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
935 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
936 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
937 }
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
938 #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
939 precision. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
940 if (!(y == 0.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
941 abort ();
9489
940605ef6e9e Don't abort if the 'long double' type has excess precision.
Bruno Haible <bruno@clisp.org>
parents: 9462
diff changeset
942 #endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
943 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
944 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
945 m.nlimbs--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
946 *mp = m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
947 *ep = exp - LDBL_MANT_BIT;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
948 return m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
949 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
950
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
951 # endif
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 # 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
954
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
955 /* 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
956 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
957 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
958 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
959 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
960 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
961 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
962 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
963 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
964 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
965 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
966
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
967 /* 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
968 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
969 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
970 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
971 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
972 /* 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
973 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
974 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
975 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
976 /* 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
977 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
978 /* 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
979 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
980 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
981 '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
982 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
983 # 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
984 # 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
985 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
986 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
987 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
988 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
989 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
990 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
991 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
992 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
993 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
994 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
995 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
996 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
997 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
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 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1000 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1001 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
1002 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
1003 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
1004 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
1005 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1006 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
1007 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
1008 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1009 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1010 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1011 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
1012 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1013 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
1014 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
1015 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
1016 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
1017 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1018 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
1019 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
1020 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
1021 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
1022 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1023 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
1024 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
1025 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1026 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
1027 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1028 /* Normalise. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1029 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
1030 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
1031 *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
1032 *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
1033 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
1034 }
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 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1037
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1038 /* 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
1039 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
1040 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
1041 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
1042 of memory allocation failure. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1043 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
1044 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
1045 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1046 int s;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1047 size_t extra_zeroes;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1048 unsigned int abs_n;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1049 unsigned int abs_s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1050 mp_limb_t *pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1051 size_t pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1052 unsigned int s_limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1053 unsigned int s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1054 mpn_t pow5;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1055 mpn_t z;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1056 void *z_memory;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1057 char *digits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1058
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1059 if (memory == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1060 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1061 /* x = 2^e * m, hence
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1062 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
1063 = round (2^s * 5^n * m). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1064 s = e + n;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1065 extra_zeroes = 0;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1066 /* 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
1067 if (s > 0 && n > 0)
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1068 {
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1069 extra_zeroes = (s < n ? s : n);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1070 s -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1071 n -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1072 }
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1073 /* 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
1074 Before converting to decimal, we need to compute
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1075 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
1076 /* 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
1077 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
1078 abs_n = (n >= 0 ? n : -n);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1079 abs_s = (s >= 0 ? s : -s);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1080 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
1081 + abs_s / GMP_LIMB_BITS + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1082 * sizeof (mp_limb_t));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1083 if (pow5_ptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1084 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1085 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1086 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1087 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1088 /* Initialize with 1. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1089 pow5_ptr[0] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1090 pow5_len = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1091 /* Multiply with 5^|n|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1092 if (abs_n > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1093 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1094 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
1095 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1096 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
1097 48828125, 244140625, 1220703125
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1098 };
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1099 unsigned int n13;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1100 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
1101 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1102 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
1103 size_t j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1104 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1105 for (j = 0; j < pow5_len; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1106 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1107 mp_limb_t digit2 = pow5_ptr[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1108 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
1109 pow5_ptr[j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1110 carry = carry >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1111 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1112 if (carry > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1113 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1114 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1115 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1116 s_limbs = abs_s / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1117 s_bits = abs_s % GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1118 if (n >= 0 ? s >= 0 : s <= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1119 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1120 /* Multiply with 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1121 if (s_bits > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1122 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1123 mp_limb_t *ptr = pow5_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1124 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1125 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1126 for (count = pow5_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1127 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1128 accu += (mp_twolimb_t) *ptr << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1129 *ptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1130 accu = accu >> GMP_LIMB_BITS;
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 if (accu > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1133 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1134 *ptr = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1135 pow5_len++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1136 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1137 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1138 if (s_limbs > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1139 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1140 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1141 for (count = pow5_len; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1142 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1143 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1144 pow5_ptr[s_limbs + count] = pow5_ptr[count];
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 for (count = s_limbs; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1147 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1148 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1149 pow5_ptr[count] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1150 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1151 pow5_len += s_limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1152 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1153 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1154 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1155 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1156 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1157 /* Multiply m with pow5. No division needed. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1158 z_memory = multiply (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1159 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1160 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1161 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1162 /* Divide m by pow5 and round. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1163 z_memory = divide (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1164 }
8832
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 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1167 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1168 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1169 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1170 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1171 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1172 /* n >= 0, s < 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1173 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
1174 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1175 mpn_t denominator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1176 void *tmp_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1177 tmp_memory = multiply (m, pow5, &numerator);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1178 if (tmp_memory == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1179 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1180 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1181 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1182 return NULL;
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 /* Construct 2^|s|. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1185 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1186 mp_limb_t *ptr = pow5_ptr + pow5_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1187 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1188 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1189 ptr[i] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1190 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
1191 denominator.limbs = ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1192 denominator.nlimbs = s_limbs + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1193 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1194 z_memory = divide (numerator, denominator, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1195 free (tmp_memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1196 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1197 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1198 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1199 /* n < 0, s > 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1200 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
1201 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1202 mp_limb_t *num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1203 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
1204 * sizeof (mp_limb_t));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1205 if (num_ptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1206 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1207 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1208 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1209 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1210 }
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 mp_limb_t *destptr = num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1213 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1214 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1215 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1216 *destptr++ = 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 if (s_bits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1219 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1220 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1221 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1222 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1223 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1224 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1225 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1226 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1227 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1228 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1229 if (accu > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1230 *destptr++ = (mp_limb_t) accu;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1233 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1234 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1235 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1236 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1237 *destptr++ = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1238 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1239 numerator.limbs = num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1240 numerator.nlimbs = destptr - num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1241 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1242 z_memory = divide (numerator, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1243 free (num_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1244 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1245 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1246 free (pow5_ptr);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1247 free (memory);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1248
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1249 /* 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
1250
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1251 if (z_memory == NULL)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1252 return NULL;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1253 digits = convert_to_decimal (z, extra_zeroes);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1254 free (z_memory);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1255 return digits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1256 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1257
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1258 # 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
1259
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1260 /* 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
1261 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
1262 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
1263 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
1264 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
1265 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
1266 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
1267 {
10100
369c11a9858d avoid a warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10084
diff changeset
1268 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
1269 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
1270 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
1271 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
1272 }
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 # endif
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 # 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
1277
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1278 /* 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
1279 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
1280 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
1281 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
1282 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
1283 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
1284 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
1285 {
10101
daad35fae43c avoid another warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10100
diff changeset
1286 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
1287 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
1288 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
1289 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
1290 }
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 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1293
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1294 # 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
1295
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1296 /* Assuming x is finite and > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1297 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
1298 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
1299 static int
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1300 floorlog10l (long double x)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1301 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1302 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1303 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1304 double z;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1305 double l;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1306
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1307 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1308 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1309 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1310 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1311 if (y == 0.0L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1312 return INT_MIN;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1313 if (y < 0.5L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1314 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1315 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
1316 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1317 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
1318 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1319 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1320 if (y < (1.0L / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1321 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1322 y *= 1.0L * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1323 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1324 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1325 if (y < (1.0L / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1326 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1327 y *= 1.0L * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1328 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1329 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1330 if (y < (1.0L / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1331 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1332 y *= 1.0L * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1333 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1334 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1335 if (y < (1.0L / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1336 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1337 y *= 1.0L * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1338 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1339 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1340 if (y < (1.0L / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1341 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1342 y *= 1.0L * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1343 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1344 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1345 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1346 if (!(y >= 0.5L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1347 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1348 /* 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
1349 l = exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1350 z = y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1351 if (z < 0.70710678118654752444)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1352 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1353 z *= 1.4142135623730950488;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1354 l -= 0.5;
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 if (z < 0.8408964152537145431)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1357 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1358 z *= 1.1892071150027210667;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1359 l -= 0.25;
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 if (z < 0.91700404320467123175)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1362 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1363 z *= 1.0905077326652576592;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1364 l -= 0.125;
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 if (z < 0.9576032806985736469)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1367 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1368 z *= 1.0442737824274138403;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1369 l -= 0.0625;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1370 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1371 /* Now 0.95 <= z <= 1.01. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1372 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1373 /* 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
1374 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
1375 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
1376 /* 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
1377 log10(x). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1378 l *= 0.30102999566398119523;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1379 /* Round down to the next integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1380 return (int) l + (l < 0 ? -1 : 0);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1381 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1382
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1383 # endif
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 # 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
1386
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1387 /* 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
1388 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
1389 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
1390 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
1391 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
1392 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1393 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
1394 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
1395 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
1396 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
1397
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1398 /* 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
1399 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
1400 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
1401 abort ();
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.0)
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1403 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
1404 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
1405 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1406 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
1407 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1408 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
1409 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1410 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1411 if (y < (1.0 / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1412 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1413 y *= 1.0 * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1414 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1415 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1416 if (y < (1.0 / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1417 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1418 y *= 1.0 * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1419 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1420 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1421 if (y < (1.0 / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1422 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1423 y *= 1.0 * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1424 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1425 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1426 if (y < (1.0 / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1427 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1428 y *= 1.0 * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1429 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1430 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1431 if (y < (1.0 / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1432 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1433 y *= 1.0 * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1434 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1435 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1436 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1437 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
1438 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1439 /* 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
1440 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
1441 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
1442 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
1443 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1444 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
1445 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
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 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
1448 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1449 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
1450 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
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 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
1453 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1454 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
1455 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
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 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
1458 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1459 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
1460 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
1461 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1462 /* 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
1463 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1464 /* 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
1465 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
1466 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
1467 /* 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
1468 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
1469 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
1470 /* 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
1471 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
1472 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1473
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1474 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1475
9938
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1476 /* 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
1477 a single '1' digit. */
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1478 static int
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1479 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
1480 {
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1481 for (; precision > 0; precision--, digits++)
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1482 if (*digits != '0')
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 if (*digits != '1')
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1485 return 0;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1486 digits++;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1487 return *digits == '\0';
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1488 }
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1489
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1490 #endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1491
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1492 #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
1493
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1494 /* 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
1495 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
1496 translation unit. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1497 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1498 # 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
1499 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1500 # 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
1501 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1502
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1503 /* 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
1504 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
1505 static inline size_t
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1506 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
1507 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
1508 size_t precision, int pad_ourselves)
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 size_t tmp_length;
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 switch (conversion)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1513 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1514 case 'd': case 'i': case 'u':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1515 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1516 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
1517 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1518 (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
1519 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1520 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1521 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1522 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1523 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1524 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
1525 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1526 (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
1527 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1528 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1529 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1530 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1531 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1532 (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
1533 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1534 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1535 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1536 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1537 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1538 /* 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
1539 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
1540 /* 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
1541 tmp_length = xsum (tmp_length, 1);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1542 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1543
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1544 case 'o':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1545 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1546 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
1547 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1548 (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
1549 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1550 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1551 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1552 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1553 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1554 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
1555 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1556 (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
1557 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1558 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1559 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1560 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1561 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1562 (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
1563 * 0.333334 /* binary -> octal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1564 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1565 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1566 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1567 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1568 /* 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
1569 tmp_length = xsum (tmp_length, 1);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1570 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1571
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1572 case 'x': case 'X':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1573 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1574 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
1575 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1576 (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
1577 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1578 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1579 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1580 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1581 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1582 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
1583 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1584 (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
1585 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1586 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1587 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1588 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1589 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1590 (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
1591 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1592 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1593 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1594 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1595 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1596 /* 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
1597 tmp_length = xsum (tmp_length, 2);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1598 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1599
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1600 case 'f': case 'F':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1601 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1602 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1603 (unsigned int) (LDBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1604 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1605 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1606 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1607 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1608 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1609 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1610 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1611 (unsigned int) (DBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1612 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1613 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1614 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1615 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1616 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1617 tmp_length = xsum (tmp_length, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1618 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1619
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1620 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
1621 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1622 12; /* sign, decimal point, exponent etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1623 tmp_length = xsum (tmp_length, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1624 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1625
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1626 case 'a': case 'A':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1627 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1628 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1629 (unsigned int) (LDBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1630 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1631 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1632 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1633 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1634 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1635 (unsigned int) (DBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1636 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1637 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1638 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1639 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1640 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1641 /* Account for sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1642 tmp_length = xsum (tmp_length, 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1643 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1644
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1645 case 'c':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1646 # 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
1647 if (type == TYPE_WIDE_CHAR)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1648 tmp_length = MB_CUR_MAX;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1649 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1650 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1651 tmp_length = 1;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1652 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1653
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1654 case 's':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1655 # if HAVE_WCHAR_T
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1656 if (type == TYPE_WIDE_STRING)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1657 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1658 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1659 /* 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
1660 "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
1661 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
1662 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
1663 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
1664
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1665 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1666 tmp_length = local_wcsnlen (arg, precision);
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 tmp_length = local_wcslen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1669 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1670 /* 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
1671 "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
1672 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
1673 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
1674 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
1675 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
1676 array."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1677 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
1678 /* 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
1679 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1680 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1681 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1682 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1683 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1684 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1685 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1686 /* 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
1687 "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
1688 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
1689 null wide character."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1690 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
1691 /* 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
1692 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1693 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1694 /* 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
1695 "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
1696 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
1697 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
1698 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
1699
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1700 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1701 tmp_length = local_strnlen (arg, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1702 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1703 tmp_length = strlen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1704 # endif
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 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1707
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1708 case 'p':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1709 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1710 (unsigned int) (sizeof (void *) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1711 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1712 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1713 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1714 + 2; /* account for leading 0x */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1715 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1716
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1717 default:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1718 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1719 }
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 (!pad_ourselves)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1722 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1723 # if ENABLE_UNISTDIO
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1724 /* 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
1725 elements after padding may be
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1726 > max (tmp_length, width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1727 but is certainly
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1728 <= tmp_length + width. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1729 tmp_length = xsum (tmp_length, width);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1730 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1731 /* 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
1732 if (tmp_length < width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1733 tmp_length = width;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1734 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1735 }
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 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
1738
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1739 return tmp_length;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1740 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1741
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1742 #endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1743
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1744 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
1745 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1746 const FCHAR_T *format, va_list args)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1747 {
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1748 DIRECTIVES d;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1749 arguments a;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1750
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1751 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
1752 /* errno is already set. */
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1753 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1754
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1755 #define CLEANUP() \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1756 free (d.dir); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1757 if (a.arg) \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1758 free (a.arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1759
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1760 if (PRINTF_FETCHARGS (args, &a) < 0)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1761 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1762 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1763 errno = EINVAL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1764 return NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1765 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1766
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1767 {
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1768 size_t buf_neededlength;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1769 TCHAR_T *buf;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1770 TCHAR_T *buf_malloced;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1771 const FCHAR_T *cp;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1772 size_t i;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1773 DIRECTIVE *dp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1774 /* Output string accumulator. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1775 DCHAR_T *result;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1776 size_t allocated;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1777 size_t length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1778
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1779 /* 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
1780 sprintf or snprintf. */
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1781 buf_neededlength =
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1782 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
1783 #if HAVE_ALLOCA
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1784 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1785 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1786 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
1787 buf_malloced = NULL;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1788 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1789 else
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1790 #endif
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1791 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1792 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
1793 if (size_overflow_p (buf_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1794 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1795 buf = (TCHAR_T *) malloc (buf_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1796 if (buf == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1797 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1798 buf_malloced = buf;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1799 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1800
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1801 if (resultbuf != NULL)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1802 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1803 result = resultbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1804 allocated = *lengthp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1805 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1806 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1807 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1808 result = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1809 allocated = 0;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1810 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1811 length = 0;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1812 /* Invariants:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1813 result is either == resultbuf or == NULL or malloc-allocated.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1814 If length > 0, then result != NULL. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1815
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1816 /* 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
1817 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
1818 #define ENSURE_ALLOCATION(needed) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1819 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1820 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1821 size_t memory_size; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1822 DCHAR_T *memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1823 \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1824 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1825 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1826 allocated = (needed); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1827 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1828 if (size_overflow_p (memory_size)) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1829 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1830 if (result == resultbuf || result == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1831 memory = (DCHAR_T *) malloc (memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1832 else \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1833 memory = (DCHAR_T *) realloc (result, memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1834 if (memory == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1835 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1836 if (result == resultbuf && length > 0) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1837 DCHAR_CPY (memory, result, length); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1838 result = memory; \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1839 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1840
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1841 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
1842 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1843 if (cp != dp->dir_start)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1844 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1845 size_t n = dp->dir_start - cp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1846 size_t augmented_length = xsum (length, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1847
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1848 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1849 /* 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
1850 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
1851 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
1852 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1853 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1854 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
1855 length = augmented_length;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1858 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1859 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1860 result[length++] = (unsigned char) *cp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1861 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1862 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1863 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1864 if (i == d.count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1865 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1866
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1867 /* Execute a single directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1868 if (dp->conversion == '%')
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 size_t augmented_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1871
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1872 if (!(dp->arg_index == ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1873 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1874 augmented_length = xsum (length, 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1875 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1876 result[length] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1877 length = augmented_length;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1880 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1881 if (!(dp->arg_index != ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1882 abort ();
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 if (dp->conversion == 'n')
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 switch (a.arg[dp->arg_index].type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1887 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1888 case TYPE_COUNT_SCHAR_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1889 *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
1890 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1891 case TYPE_COUNT_SHORT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1892 *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
1893 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1894 case TYPE_COUNT_INT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1895 *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
1896 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1897 case TYPE_COUNT_LONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1898 *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
1899 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
1900 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1901 case TYPE_COUNT_LONGLONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1902 *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
1903 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1904 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1905 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1906 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1907 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1908 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1909 #if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1910 /* The unistdio extensions. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1911 else if (dp->conversion == 'U')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1912 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1913 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
1914 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1915 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1916 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1917 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1918 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1919
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1920 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1921 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1922 if (dp->width_start != dp->width_end)
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 if (dp->width_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1927
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1928 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
1929 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1930 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
1931 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1932 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1933 /* "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
1934 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1935 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1936 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1937 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1938 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1939 width = arg;
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 else
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 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1944
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1945 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1946 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1947 while (digitp != dp->width_end);
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 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1950 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1951
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1952 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1953 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1954 if (dp->precision_start != dp->precision_end)
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 if (dp->precision_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1959
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1960 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
1961 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1962 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
1963 /* "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
1964 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1965 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1966 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1967 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1968 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1969 }
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 else
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 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1974
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1975 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1976 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1977 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1978 has_precision = 1;
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 }
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 switch (type)
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 case TYPE_U8_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1985 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1986 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
1987 const uint8_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1988 size_t characters;
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 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1991 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1992 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1993 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1994 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1995 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1996 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1997 int count = u8_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2000 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2001 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2002 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2003 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2004 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2005 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2006 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2007 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2008 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2009 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2010 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2011 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2012 }
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 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2015 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2016 /* 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
2017 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2018 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2019 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2020 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2021 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2022 int count = u8_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2025 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2026 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2027 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2028 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2029 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2030 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2031 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2032 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2033 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2034 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2035 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2036 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2037 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2040 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2041 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2042 arg_end = arg + u8_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2043 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2044 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2045 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2046
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2047 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2048 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2049 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2050 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2051 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2052 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2053 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2054 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2055
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2056 # if DCHAR_IS_UINT8_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2057 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2058 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2059 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2060 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2061 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2062 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2063 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2064 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2065 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2066 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
2067 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2068 /* Convert from UTF-8 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2069 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2070 u8_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2071 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2072 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2073 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
2074 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2075 /* 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
2076 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2077 U8_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2078 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2079 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2080 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2081 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2082 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2083 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2084 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2085 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2086 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2087 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2088 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2089 return NULL;
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 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2092 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2093 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2094 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2095 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2096 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2097 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2098 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2099 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2100
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2101 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2102 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2103 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2104 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2105 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2106 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2107 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2108 }
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 break;
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 case TYPE_U16_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2113 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2114 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
2115 const uint16_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2116 size_t characters;
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 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2119 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2120 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2121 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2122 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2123 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2124 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2125 int count = u16_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2128 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2129 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2130 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2131 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2132 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2133 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2134 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2135 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2136 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2137 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2138 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2139 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2140 }
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 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2143 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2144 /* 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
2145 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2146 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2147 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2148 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2149 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2150 int count = u16_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2153 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2154 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2155 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2156 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2157 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2158 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2159 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2160 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2161 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2162 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2163 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2164 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2165 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2168 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2169 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2170 arg_end = arg + u16_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2171 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2172 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2173 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2174
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2175 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2176 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2177 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2178 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2179 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2180 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2181 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2182 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2183
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2184 # if DCHAR_IS_UINT16_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2185 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2186 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2187 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2188 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2189 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2190 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2191 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2192 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2193 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2194 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
2195 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2196 /* Convert from UTF-16 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2197 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2198 u16_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2199 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2200 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2201 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
2202 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2203 /* 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
2204 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2205 U16_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2206 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2207 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2208 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2209 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2210 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2211 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2212 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2213 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2214 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2215 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2216 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2217 return NULL;
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 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2220 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2221 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2222 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2223 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2224 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2225 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2226 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2227 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2228
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2229 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2230 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2231 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2232 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2233 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2234 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2235 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2236 }
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 break;
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 case TYPE_U32_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2241 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2242 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
2243 const uint32_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2244 size_t characters;
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 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2247 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2248 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2249 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2250 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2251 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2252 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2253 int count = u32_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2256 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2257 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2258 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2259 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2260 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2261 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2262 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2263 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2264 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2265 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2266 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2267 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2268 }
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 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2271 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2272 /* 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
2273 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2274 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2275 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2276 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2277 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2278 int count = u32_strmblen (arg_end);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2281 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2282 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2283 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2284 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2285 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2286 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2287 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2288 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2289 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2290 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2291 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2292 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2293 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2296 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2297 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2298 arg_end = arg + u32_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2299 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2300 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2301 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2302
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2303 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2304 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2305 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2306 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2307 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2308 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2309 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2310 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2311
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2312 # if DCHAR_IS_UINT32_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2313 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2314 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2315 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2316 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2317 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2318 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2319 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2320 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2321 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2322 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
2323 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2324 /* Convert from UTF-32 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2325 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2326 u32_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2327 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2328 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2329 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
2330 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2331 /* 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
2332 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2333 U32_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2334 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2335 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2336 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2337 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2338 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2339 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2340 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2341 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2342 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2343 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2344 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2345 return NULL;
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 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2348 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2349 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2350 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2351 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2352 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2353 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2354 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2355 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2356
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2357 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2358 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2359 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2360 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2361 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2362 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2363 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2364 }
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2367
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2368 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2369 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2370 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2371 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2372 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
2373 #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
2374 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
2375 # if WIDE_CHAR_VERSION
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 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2378 && 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
2379 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2380 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2381 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2382 /* 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
2383 allocating a temporary buffer. The determination of its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2384 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
2385 specified, below requires a conversion between a char[]
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2386 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
2387 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
2388 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
2389 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
2390 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
2391 directive ourselves. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2392 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2393 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2394 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2395 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2396 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2397
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2398 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2399 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2400 if (dp->width_start != dp->width_end)
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 if (dp->width_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2405
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2406 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
2407 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2408 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
2409 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2410 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2411 /* "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
2412 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2413 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2414 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2415 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2416 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2417 width = arg;
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 else
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 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2422
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2423 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2424 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2425 while (digitp != dp->width_end);
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 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2428 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2429
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2430 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2431 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2432 if (dp->precision_start != dp->precision_end)
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 if (dp->precision_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2437
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2438 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
2439 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2440 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
2441 /* "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
2442 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2443 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2444 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2445 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2446 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2447 }
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 else
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 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2452
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2453 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2454 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2455 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2456 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2457 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2458 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2459
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2460 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2461 /* %s in vasnwprintf. See the specification of fwprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2462 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2463 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
2464 const char *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2465 size_t characters;
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 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2468 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2469 /* 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
2470 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
2471 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2472 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2473 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
2474 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2475 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2476 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2477 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2478 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2479 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2480 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2481 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
2482 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2483 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
2484 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2485 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2486 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2487 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2488 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2489 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2490 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2491 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2492 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2493 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2494 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2495 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2496 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2497 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2498 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2499 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2500 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2501 }
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 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2504 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2505 /* 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
2506 characters. */
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2507 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2508 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2509 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
2510 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2511 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2512 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2513 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2514 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2515 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2516 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2517 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
2518 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2519 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
2520 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2521 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2522 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2523 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2524 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2525 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2526 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2527 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2528 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2529 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2530 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2531 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2532 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2533 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2534 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2535 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2536 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2537 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2540 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2541 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2542 arg_end = arg + strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2543 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2544 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2545 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2546
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2547 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2548 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2549 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2550 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2551 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2552 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2553 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2554 }
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 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2557 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2558 /* 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
2559 size_t remaining;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2560 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2561 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2562 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
2563 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2564 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2565 for (remaining = characters; remaining > 0; remaining--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2566 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2567 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2568 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2569 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2570 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
2571 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2572 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
2573 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2574 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2575 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2576 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2577 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2578 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2579 arg += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2580 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2581 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2582 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2583 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2584 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2585 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2586 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2587 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2588 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
2589 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2590 while (arg < arg_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2591 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2592 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2593 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2594 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2595 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
2596 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2597 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
2598 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2599 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2600 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2601 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2602 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2603 ENSURE_ALLOCATION (xsum (length, 1));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2604 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2605 arg += count;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2608
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2609 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2610 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2611 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2612 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2613 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2614 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2615 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2616 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2617 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2618 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2619 /* %ls in vasnprintf. See the specification of fprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2620 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2621 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
2622 const wchar_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2623 size_t characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2624 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2625 /* This code assumes that TCHAR_T is 'char'. */
13788
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
2626 verify (sizeof (TCHAR_T) == 1);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2627 TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2628 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2629 size_t tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2630 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2631 size_t w;
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 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2634 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2635 /* 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
2636 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
2637 # 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
2638 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2639 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
2640 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2641 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2642 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2643 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2644 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2645 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2646 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2647
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2648 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2649 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2650 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2651 # 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
2652 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
2653 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2654 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
2655 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2656 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2657 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2658 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2659 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2660 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2661 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2662 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2663 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2664 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2665 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2666 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2667 if (precision < count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2668 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2669 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2670 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2671 precision -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2672 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2673 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2674 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2675 else if (has_width)
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2676 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2677 else
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2678 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2679 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2680 /* 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
2681 bytes. */
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2682 # 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
2683 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2684 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
2685 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2686 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2687 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2688 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2689 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2690 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2691 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2692
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2693 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2694 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2695 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2696 # 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
2697 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
2698 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2699 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
2700 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2701 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2702 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2703 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2704 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2705 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2706 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2707 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2708 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2709 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2710 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2711 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2712 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2713 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2714 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2715 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2716 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2717 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2718 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2719 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2720 arg_end = arg + local_wcslen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2721 /* The number of bytes doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2722 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2723 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2724 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2725
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2726 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2727 /* 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
2728 tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2729 if (tmpsrc == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2730 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2731 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2732 TCHAR_T *tmpptr = tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2733 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
2734 # 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
2735 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2736 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
2737 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2738 for (remaining = characters; remaining > 0; )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2739 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2740 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2741 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2742
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2743 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2744 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2745 # 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
2746 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
2747 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2748 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2749 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2750 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2751 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2752 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2753 memcpy (tmpptr, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2754 tmpptr += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2755 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2756 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2757 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2758 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2759 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2760 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2761
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2762 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2763 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2764 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2765 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2766 tmpsrc, characters,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2767 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2768 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2769 if (tmpdst == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2770 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2771 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2772 free (tmpsrc);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2773 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2774 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2775 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2776 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2777 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2778 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2779 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2780 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2781 free (tmpsrc);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2782 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2783
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2784 if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2785 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2786 # if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2787 /* 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
2788 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2789 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2790 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
2791 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2792 /* 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
2793 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2794 w = characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2795 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2796 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2797 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2798 /* w doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2799 w = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2800
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2801 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2802 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2803 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2804 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2805 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2806 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2807 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2808 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2809
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2810 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2811 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2812 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2813 /* We know the number of bytes in advance. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2814 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
2815 # 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
2816 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2817 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
2818 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2819 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2820 for (remaining = characters; remaining > 0; )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2821 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2822 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2823 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2824
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2825 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2826 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2827 # 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
2828 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
2829 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2830 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2831 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2832 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2833 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2834 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2835 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2836 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2837 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2838 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2839 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2840 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2841 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2842 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2843 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2844 {
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2845 # 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
2846 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2847 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
2848 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2849 while (arg < arg_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2850 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2851 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2852 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2853
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2854 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2855 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2856 # 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
2857 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
2858 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2859 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2860 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2861 if (count <= 0)
13198
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2862 {
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2863 /* Cannot convert. */
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2864 if (!(result == resultbuf || result == NULL))
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2865 free (result);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2866 if (buf_malloced != NULL)
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2867 free (buf_malloced);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2868 CLEANUP ();
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2869 errno = EILSEQ;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2870 return NULL;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2871 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2872 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2873 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2874 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2875 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2876 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2877 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2878 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2879 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2880 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2881 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2882 length += tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2883 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2884
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2885 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2886 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2887 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2888 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2889 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2890 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2891 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2892 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2893 }
13323
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
2894 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2895 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2896 #endif
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) && !defined IN_LIBINTL
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2898 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
2899 # 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
2900 && (0
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2901 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2902 || 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
2903 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2904 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2905 || 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
2906 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2907 )
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2908 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2909 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2910 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2911 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
2912 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2913 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2914 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2915 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2916 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2917 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2918 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2919 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2920 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2921 DCHAR_T *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2922
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2923 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2924 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2925 if (dp->width_start != dp->width_end)
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 if (dp->width_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2930
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2931 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
2932 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2933 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
2934 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2935 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2936 /* "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
2937 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2938 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2939 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2940 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2941 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2942 width = arg;
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 else
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 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2947
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2948 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2949 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2950 while (digitp != dp->width_end);
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 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2953 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2954
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2955 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2956 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2957 if (dp->precision_start != dp->precision_end)
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 if (dp->precision_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2962
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2963 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
2964 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2965 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
2966 /* "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
2967 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2968 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2969 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2970 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2971 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2972 }
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 else
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 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2977
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2978 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2979 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2980 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2981 has_precision = 1;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2984
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2985 /* Allocate a temporary buffer of sufficient size. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2986 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2987 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2988 (unsigned int) ((LDBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2989 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2990 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2991 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2992 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2993 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2994 (unsigned int) ((DBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2995 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2996 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2997 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2998 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2999 tmp_length = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3000 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3001 tmp_length = xsum (tmp_length, 12);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3002
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3003 if (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3004 tmp_length = width;
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 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
3007
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3008 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3009 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3010 else
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 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
3013
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3014 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3015 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3016 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3017 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3018 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3019 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3020 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3021 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3022
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3023 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3024 p = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3025 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3026 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3027 # 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
3028 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
3029
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3030 if (isnanl (arg))
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 if (dp->conversion == 'A')
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 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
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 else
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 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3039 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3042 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3043 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3044 DECL_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 BEGIN_LONG_DOUBLE_ROUNDING ();
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 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
3049 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3050 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3051 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3052 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3053
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3054 if (sign < 0)
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_SHOWSIGN)
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 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3059 *p++ = ' ';
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 (arg > 0.0L && arg + arg == arg)
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 if (dp->conversion == 'A')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
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 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3070 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3073 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3074 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3075 long double mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3076
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3077 if (arg > 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3078 mantissa = printf_frexpl (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3079 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3080 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3081 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3082 mantissa = 0.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3083 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3084
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3085 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3086 && 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
3087 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3088 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3089 long double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3090 size_t 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 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3093 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3094 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3095 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3096 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3097 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3098 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
3099 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3100 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3101 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3102 break;
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 tail *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3105 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3106 if (tail != 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3107 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3108 tail *= 0.0625L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3109 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3110 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3111
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3112 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3113 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3114 pad_ptr = p;
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 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3117
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3118 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3119 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3120 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3121 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3122 || mantissa > 0.0L || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3123 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3124 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3125 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3126 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3127 while (mantissa > 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3128 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3129 mantissa *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3130 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3131 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3132 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3133 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3134 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3135 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3136 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3137 precision--;
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 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3140 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3141 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3142 precision--;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3145 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3146 *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
3147 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3148 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3149 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3150 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3151 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3152 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3153 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3154 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3155 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3156 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3157 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3158 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3159 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3160 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3163 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3164 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3165 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3166 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3167 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3168 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3169 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3170 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3171 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3172
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3173 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3174 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3175 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3176 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3177 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3178 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3179 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3180 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3181 # 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
3182 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
3183
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3184 if (isnand (arg))
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 if (dp->conversion == 'A')
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 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
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 else
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 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3193 }
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 else
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 int sign = 0;
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 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
3200 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3201 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3202 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3203 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3204
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3205 if (sign < 0)
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_SHOWSIGN)
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 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3210 *p++ = ' ';
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 (arg > 0.0 && arg + arg == arg)
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 if (dp->conversion == 'A')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
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 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3221 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3224 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3225 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3226 double mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3227
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3228 if (arg > 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3229 mantissa = printf_frexp (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3230 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3231 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3232 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3233 mantissa = 0.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3234 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3235
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3236 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3237 && 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
3238 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3239 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3240 double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3241 size_t 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 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3244 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3245 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3246 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3247 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3248 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3249 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
3250 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3251 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3252 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3253 break;
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 tail *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3256 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3257 if (tail != 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3258 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3259 tail *= 0.0625;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3260 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3261 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3262
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3263 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3264 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3265 pad_ptr = p;
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 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3268
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3269 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3270 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3271 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3272 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3273 || mantissa > 0.0 || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3274 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3275 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3276 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3277 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3278 while (mantissa > 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3279 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3280 mantissa *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3281 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3282 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3283 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3284 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3285 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3286 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3287 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3288 precision--;
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 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3291 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3292 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3293 precision--;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3296 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3297 *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
3298 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3299 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3300 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3301 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3302 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3303 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3304 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3305 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3306 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3307 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3308 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3309 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3310 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3311 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3314 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3315 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3316 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3317 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3318 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3319 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3320 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3321 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3322 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3323 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3324 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3325 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3326 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3327 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3328 /* 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
3329 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3330 if (has_width && p - tmp < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3331 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3332 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3333 DCHAR_T *end = p + pad;
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 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3336 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3337 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3338 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3339 *p++ = ' ';
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 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3342 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3343 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3344 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3345
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3346 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3347 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3348 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3349 *p++ = '0';
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3352 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3353 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3354 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3355
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3356 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3357 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3358 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3359 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3360 }
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 p = end;
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
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 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3367
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3368 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3369 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3370 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3371 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3372
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3373 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3374 if (count >= allocated - length)
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 size_t n = xsum (length, count);
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 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3379 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3380
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3381 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3382 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3383 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3384 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3385 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3386 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3387 }
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
3388 #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
3389 #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
3390 else if ((dp->conversion == 'f' || dp->conversion == 'F'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3391 || dp->conversion == 'e' || dp->conversion == 'E'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3392 || dp->conversion == 'g' || dp->conversion == 'G'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3393 || dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3394 && (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
3395 # if NEED_PRINTF_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
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3397 # elif NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3398 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3399 /* The systems (mingw) which produce wrong output
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3400 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
3401 Therefore we treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3402 && 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
3403 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3404 # if NEED_PRINTF_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
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3406 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3407 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3408 /* Some systems produce wrong output for Inf,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3409 -Inf, and NaN. Some systems in this category
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3410 (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
3411 treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3412 && 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
3413 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3414 ))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3415 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3416 # 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
3417 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
3418 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3419 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3420 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3421 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3422 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3423 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3424 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3425 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3426 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3427 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3428 DCHAR_T *p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3429
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3430 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3431 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3432 if (dp->width_start != dp->width_end)
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 if (dp->width_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3437
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3438 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
3439 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3440 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
3441 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3442 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3443 /* "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
3444 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3445 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3446 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3447 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3448 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3449 width = arg;
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 else
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 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3454
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3455 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3456 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3457 while (digitp != dp->width_end);
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 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3460 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3461
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3462 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3463 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3464 if (dp->precision_start != dp->precision_end)
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 if (dp->precision_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3469
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3470 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
3471 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3472 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
3473 /* "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
3474 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3475 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3476 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3477 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3478 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3479 }
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 else
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 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3484
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3485 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3486 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3487 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3488 has_precision = 1;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3491
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3492 /* 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
3493 %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
3494 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
3495 the default precision is 0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3496 if (!has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3497 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3498 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3499
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3500 /* 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
3501 # 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
3502 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
3503 # 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
3504 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
3505 # elif NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3506 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
3507 # elif NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3508 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
3509 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3510 tmp_length = 0;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3511 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3512 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3513 tmp_length = precision;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3514 # 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
3515 # 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
3516 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
3517 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3518 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3519 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3520 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
3521 if (!(isnanl (arg) || arg + arg == arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3522 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3523 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3524 int exponent = floorlog10l (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3525 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3526 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3527 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3528 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3529 # 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
3530 # 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
3531 # 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
3532 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
3533 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3534 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3535 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3536 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
3537 if (!(isnand (arg) || arg + arg == arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3538 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3539 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3540 int exponent = floorlog10 (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3541 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3542 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3543 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3544 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3545 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3546 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3547 tmp_length = xsum (tmp_length, 12);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3548
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3549 if (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3550 tmp_length = width;
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 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
3553
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3554 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3555 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3556 else
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 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
3559
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3560 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3561 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3562 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3563 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3564 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3565 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3566 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3567 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3568
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3569 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3570 p = tmp;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3571
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3572 # 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
3573 # 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
3574 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
3575 # endif
12421
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 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
3578
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3579 if (isnanl (arg))
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 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
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 else
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 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3588 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3591 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3592 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3593 DECL_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 BEGIN_LONG_DOUBLE_ROUNDING ();
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 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
3598 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3599 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3600 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3601 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3602
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3603 if (sign < 0)
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_SHOWSIGN)
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 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3608 *p++ = ' ';
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 (arg > 0.0L && arg + arg == arg)
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 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
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 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3619 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3620 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3621 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3622 {
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3623 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3624 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3627 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3628 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3629 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3630
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3631 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3632 scale10_round_decimal_long_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3633 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3634 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3635 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3636 goto out_of_memory;
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 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3639
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3640 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3641 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3642 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3643 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3644 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3645 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3646 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3647 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3648 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3649 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3650 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3651 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3652 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3653 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3654 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3655 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3656 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3657 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3658 *p++ = digits[ndigits];
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 }
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 free (digits);
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 else if (dp->conversion == 'e' || dp->conversion == 'E')
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 int exponent;
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 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3669 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3670 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3671 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3672 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3673 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3674 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3675 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3676 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3677 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3680 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3681 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3682 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3683 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3684 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3685
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3686 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3687 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3688 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3689 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3690 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3691 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3692 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3693 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3694 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3695 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3696 goto out_of_memory;
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 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3699
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3700 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3701 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3702 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3703 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3704 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3705 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3706 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3707 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3708 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3709 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3710 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3711 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3712 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3713 if (ndigits == precision)
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3716 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3717 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3718 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3719 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3720 if (is_borderline (digits, precision))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3721 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3722 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3723 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3724 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3725 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3726 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3727 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3728 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3729 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3730 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3731 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3732 goto out_of_memory;
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 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3735 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3736 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3737 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3738 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3739 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3740 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3741 free (digits2);
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 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3744
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3745 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3746 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3747 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3748 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3749 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3750 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3751 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3752 *p++ = digits[ndigits];
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 }
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 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3757 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3758
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3759 *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
3760 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3761 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3762 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3763 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3764 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3765 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3766 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3767 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
3768 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3769 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3770 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3771 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3772 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3773 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3776 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3777 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3778 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3779 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3780 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3781 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3782 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3783 # endif
12421
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 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3786 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3787 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3788 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3789 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3790
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3791 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3792 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3793 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3794 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3795 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3796 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3797 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3798 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3799 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3800
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3801 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3802 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3803 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3804 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3805 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3806 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3807 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3808 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3809 *p++ = '0';
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3814 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3815 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3816 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3817 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3818 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3819 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3820 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3821
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3822 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3823 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3824 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3825 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3826 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3827 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3828 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3829 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3830 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3831 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3832 goto out_of_memory;
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 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3835
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3836 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3837 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3838 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3839 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3840 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3841 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3842 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3843 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3844 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3845 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3846 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3847 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3848 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3849 if (ndigits < precision)
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3852 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3853 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3854 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3855 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3856 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3857 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3858 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3859 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3860 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3861 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3862 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3863 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3864 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3865 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3866 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3867 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3868 goto out_of_memory;
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 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3871 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3872 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3873 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3874 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3875 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3876 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3877 free (digits2);
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 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3880
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3881 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3882 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3883 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3884 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3885 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3886 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3887 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3888
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3889 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3890 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3891 && exponent < (long)precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3892 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3893 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3894 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3895 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3896 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3897 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3898 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3899 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3900 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3901 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3902 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3903 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3904 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3905 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3906 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3907 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3908 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3909 *p++ = digits[ndigits];
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3914 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3915 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3916 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3917 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3918 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3919 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3920 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3921 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3922 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3923 *p++ = digits[ndigits];
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3928 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3929 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3930 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3931 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3932 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3933 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3934 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3935 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3936 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3937 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3938 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3939 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3940 *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
3941 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3942 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3943 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3944 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3945 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3946 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3947 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3948 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
3949 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3950 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3951 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3952 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3953 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3954 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3957 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3958 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3959 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3960 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3961 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3962 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3963 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3964 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3965 }
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 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3968 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3969 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3970 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3971 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3972 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3973 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3974 if (!(arg == 0.0L))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3975 abort ();
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 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3980 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3981 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3982 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3983 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3984 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3985 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3986 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3987 }
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 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3990 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3991 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3992 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3993 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3994 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3995 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3996 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3997 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3998 *p++ = dp->conversion; /* 'e' or 'E' */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3999 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4000 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4001 *p++ = '0';
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 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4004 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4005 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4006 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4007 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4008 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4009 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4010 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4011 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4012 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4013 }
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 else if (dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4016 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4017 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4018 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4019 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4020 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4021 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4022 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4023 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4024 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4025 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4026 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4027 *p++ = dp->conversion - 'A' + 'P';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4028 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4029 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4030 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4031 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4032 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
4033 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4034 }
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 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4037 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4038 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4039 # 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
4040 else
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4041 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4042 # 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
4043 # 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
4044 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4045 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
4046
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4047 if (isnand (arg))
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 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
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 else
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 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4056 }
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 else
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 int sign = 0;
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 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
4063 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4064 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4065 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4066 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4067
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4068 if (sign < 0)
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_SHOWSIGN)
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 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4073 *p++ = ' ';
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 (arg > 0.0 && arg + arg == arg)
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 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
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 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4084 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4085 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4086 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4087 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4088 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4089 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4092 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4093 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4094 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4095
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4096 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4097 scale10_round_decimal_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4098 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4099 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4100 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4101
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4102 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4103 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4104 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4105 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4106 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4107 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4108 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4109 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4110 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4111 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4112 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4113 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4114 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4115 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4116 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4117 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4118 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4119 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4120 *p++ = digits[ndigits];
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 }
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 free (digits);
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 else if (dp->conversion == 'e' || dp->conversion == 'E')
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 int exponent;
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 if (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4131 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4132 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4133 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4134 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4135 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4136 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4137 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4138 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4139 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4142 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4143 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4144 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4145 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4146 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4147
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4148 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4149 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4150 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4151 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4152 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4153 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4154 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4155 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4156 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4157 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4158
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4159 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4160 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4161 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4162 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4163 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4164 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4165 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4166 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4167 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4168 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4169 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4170 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4171 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4172 if (ndigits == precision)
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4175 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4176 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4177 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4178 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4179 if (is_borderline (digits, precision))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4180 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4181 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4182 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4183 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4184 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4185 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4186 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4187 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4188 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4189 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4190 goto out_of_memory;
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 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4193 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4194 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4195 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4196 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4197 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4198 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4199 free (digits2);
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 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4202
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4203 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4204 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4205 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4206 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4207 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4208 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4209 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4210 *p++ = digits[ndigits];
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 }
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 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4215 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4216
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4217 *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
4218 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4219 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4220 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4221 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4222 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
4223 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4224 { '%', '+', '.', '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
4225 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4226 { '%', '+', '.', '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
4227 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4228 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4229 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4230 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4231 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
4232 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4233 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4234 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4235 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4236 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
4237 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4238 "%+.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
4239 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4240 "%+.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
4241 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4242 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4243 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4244 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4245 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4246 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4249 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4250 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4251 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4252 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4253 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4254 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4255 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4256 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4257 # endif
12421
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 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4260 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4261 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4262 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4263 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4264
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4265 if (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4266 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4267 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4268 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4269 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4270 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4271 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4272 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4273 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4274
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4275 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4276 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4277 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4278 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4279 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4280 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4281 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4282 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4283 *p++ = '0';
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4288 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4289 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4290 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4291 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4292 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4293 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4294 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4295
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4296 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4297 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4298 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4299 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4300 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4301 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4302 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4303 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4304 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4305 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4306
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4307 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4308 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4309 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4310 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4311 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4312 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4313 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4314 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4315 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4316 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4317 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4318 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4319 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4320 if (ndigits < precision)
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4323 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4324 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4325 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4326 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4327 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4328 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4329 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4330 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4331 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4332 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4333 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4334 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4335 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4336 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4337 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4338 goto out_of_memory;
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 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4341 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4342 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4343 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4344 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4345 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4346 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4347 free (digits2);
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 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4350
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4351 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4352 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4353 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4354 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4355 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4356 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4357 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4358
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4359 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4360 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4361 && exponent < (long)precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4362 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4363 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4364 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4365 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4366 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4367 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4368 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4369 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4370 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4371 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4372 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4373 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4374 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4375 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4376 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4377 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4378 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4379 *p++ = digits[ndigits];
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4384 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4385 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4386 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4387 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4388 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4389 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4390 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4391 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4392 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4393 *p++ = digits[ndigits];
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 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4398 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4399 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4400 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4401 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4402 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4403 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4404 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4405 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4406 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4407 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4408 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4409 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4410 *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
4411 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4412 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4413 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4414 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4415 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
4416 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4417 { '%', '+', '.', '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
4418 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4419 { '%', '+', '.', '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
4420 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4421 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4422 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4423 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4424 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
4425 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4426 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4427 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4428 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4429 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
4430 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4431 "%+.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
4432 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4433 "%+.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
4434 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4435 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4436 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4437 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4438 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4439 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4442 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4443 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4444 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4445 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4446 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4447 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4448 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4449 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4450 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4451 }
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 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4454 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4455 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4456 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4457 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
4458 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4459 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4460 if (!(arg == 0.0))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4461 abort ();
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 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4466 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4467 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4468 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4469 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4470 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4471 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4472 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4473 }
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 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4476 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4477 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4478 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4479 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4480 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4481 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4482 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4483 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4484 *p++ = dp->conversion; /* 'e' or 'E' */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4485 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4486 /* Produce the same number of exponent digits as
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4487 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
4488 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4489 *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
4490 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4491 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4492 *p++ = '0';
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 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4495 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4496 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4497 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4498 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4499 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4500 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4501 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4502 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4503 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4504 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4505 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4506 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4507 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
4508 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4509 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4510 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4511 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4512 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4513
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4514 /* 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
4515 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4516 if (has_width && p - tmp < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4517 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4518 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4519 DCHAR_T *end = p + pad;
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 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4522 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4523 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4524 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4525 *p++ = ' ';
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 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4528 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4529 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4530 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4531
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4532 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4533 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4534 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4535 *p++ = '0';
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4538 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4539 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4540 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4541
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4542 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4543 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4544 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4545 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4546 }
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 p = end;
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
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 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4553
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4554 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4555 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4556 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4557 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4558
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4559 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4560 if (count >= allocated - length)
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 size_t n = xsum (length, count);
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 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4565 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4566
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4567 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4568 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4569 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4570 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4571 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4572 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4573 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4574 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4575 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4576 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4577 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
4578 int flags = dp->flags;
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4579 #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
4580 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4581 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
4582 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4583 #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
4584 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4585 size_t precision;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4586 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4587 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4588 int prec_ourselves;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4589 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4590 # 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
4591 #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
4592 #if NEED_PRINTF_FLAG_LEFTADJUST
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4593 # 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
4594 #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
4595 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
4596 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4597 # 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
4598 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4599 TCHAR_T *fbp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4600 unsigned int prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4601 int prefixes[2] IF_LINT (= { 0 });
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4602 int orig_errno;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4603 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4604 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4605 TCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4606 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
4607 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4608
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4609 #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
4610 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4611 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4612 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4613 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4614 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4615 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4616 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4617
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4618 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
4619 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4620 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
4621 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4622 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4623 /* "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
4624 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4625 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4626 width = (unsigned int) (-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 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4630 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4631 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4632 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4633 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4634
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4635 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4636 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4637 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4638 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4639 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4640 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4641 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4642
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4643 #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
4644 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4645 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4646 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4647 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4648 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4649 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4650 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4651
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4652 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
4653 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4654 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
4655 /* "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
4656 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4657 if (arg >= 0)
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 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4660 has_precision = 1;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4663 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4664 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4665 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4666
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4667 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4668 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4669 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4670 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4671 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4672 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4673 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4674
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4675 /* Decide whether to handle the precision ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4676 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4677 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4678 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4679 case 'd': case 'i': case 'u':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4680 case 'o':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4681 case 'x': case 'X': case 'p':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4682 prec_ourselves = has_precision && (precision > 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 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4685 prec_ourselves = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4686 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4687 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4688 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4689
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4690 /* Decide whether to perform the padding ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4691 #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
4692 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4693 {
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4694 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4695 /* 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
4696 to perform the padding after this conversion. Functions
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4697 with unistdio extensions perform the padding based on
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4698 character count rather than element count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4699 case 'c': case 's':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4700 # endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4701 # if NEED_PRINTF_FLAG_ZERO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4702 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
4703 case 'a': case 'A':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4704 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4705 pad_ourselves = 1;
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 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4708 pad_ourselves = prec_ourselves;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4709 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4710 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4711 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4712
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4713 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4714 /* 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
4715 sprintf. */
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4716 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4717 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
4718 flags, width, has_precision, precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4719 pad_ourselves);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4720
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4721 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4722 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4723 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4724 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4725 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
4726
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4727 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4728 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4729 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4730 tmp = (TCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4731 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4732 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4733 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4734 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4735 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4736
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4737 /* Construct the format string for calling snprintf or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4738 sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4739 fbp = buf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4740 *fbp++ = '%';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4741 #if NEED_PRINTF_FLAG_GROUPING
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4742 /* The underlying implementation doesn't support the ' flag.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4743 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
4744 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
4745 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4746 if (flags & FLAG_GROUP)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4747 *fbp++ = '\'';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4748 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4749 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4750 *fbp++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4751 if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4752 *fbp++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4753 if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4754 *fbp++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4755 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4756 *fbp++ = '#';
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
4757 #if __GLIBC__ >= 2 && !defined __UCLIBC__
13841
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4758 if (flags & FLAG_LOCALIZED)
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4759 *fbp++ = 'I';
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4760 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4761 if (!pad_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4762 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4763 if (flags & FLAG_ZERO)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4764 *fbp++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4765 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4766 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4767 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
4768 /* The width specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4769 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4770 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4771 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4772 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
4773 fbp += n;
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 else
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 const FCHAR_T *mp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4778 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4779 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4780 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4781 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4782 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4783 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4784 if (!prec_ourselves)
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 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4787 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4788 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
4789 /* The precision specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4790 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4791 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4792 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4793 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
4794 fbp += n;
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 else
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 const FCHAR_T *mp = dp->precision_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4799 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4800 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4801 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4802 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4803 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4804 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4805
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4806 switch (type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4807 {
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4808 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4809 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4810 case TYPE_ULONGLONGINT:
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4811 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4812 *fbp++ = 'I';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4813 *fbp++ = '6';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4814 *fbp++ = '4';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4815 break;
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4816 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4817 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4818 /*FALLTHROUGH*/
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4819 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4820 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4821 case TYPE_LONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4822 case TYPE_ULONGINT:
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4823 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4824 case TYPE_WIDE_CHAR:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4825 #endif
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4826 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4827 case TYPE_WIDE_STRING:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4828 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4829 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4830 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4831 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4832 *fbp++ = 'L';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4833 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4834 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4835 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4836 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4837 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4838 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4839 *fbp = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4840 else
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4841 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4842 *fbp = dp->conversion;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4843 #if USE_SNPRINTF
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
4844 # if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4845 fbp[1] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4846 fbp[2] = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4847 fbp[3] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4848 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4849 /* On glibc2 systems from glibc >= 2.3 - probably also older
13883
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4850 ones - we know that snprintf's return value conforms to
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4851 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4852 gl_SNPRINTF_TRUNCATION_C99 pass.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4853 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
4854 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
4855 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
4856 (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
4857 in this situation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4858 /* 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
4859 %n because:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4860 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4861 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
4862 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
4863 '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
4864 - 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
4865 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
4866 buffer size: it returns -1 in this case.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4867 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
4868 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
4869 crashes the program. See
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4870 <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
4871 <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
4872 So we should avoid %n in this situation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4873 fbp[1] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4874 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4875 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4876 fbp[1] = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4877 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4878
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4879 /* Construct the arguments for calling snprintf or sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4880 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4881 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
4882 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4883 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
4884 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4885 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
4886 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4887 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
4888 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4889 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
4890 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4891 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
4892 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4893
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4894 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4895 /* 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
4896 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
4897 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
4898 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4899 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
4900 # 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
4901 /* 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
4902 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
4903 ENSURE_ALLOCATION (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4904 (2 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4905 / TCHARS_PER_DCHAR));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4906 /* Prepare checking whether snprintf returns the count
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4907 via %n. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4908 *(TCHAR_T *) (result + length) = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4909 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4910
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4911 orig_errno = errno;
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4912
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4913 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4914 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4915 int count = -1;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4916
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4917 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4918 int retcount = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4919 size_t maxlen = allocated - length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4920 /* SNPRINTF can fail if its second argument is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4921 > INT_MAX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4922 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4923 maxlen = INT_MAX / TCHARS_PER_DCHAR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4924 maxlen = maxlen * TCHARS_PER_DCHAR;
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4925 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4926 switch (prefix_count) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4927 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4928 case 0: \
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 arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4932 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4933 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4934 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4935 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4936 prefixes[0], arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4937 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4938 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4939 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4940 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4941 prefixes[0], prefixes[1], arg, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4942 &count); \
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 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4945 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4946 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4947 #else
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4948 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4949 switch (prefix_count) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4950 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4951 case 0: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4952 count = sprintf (tmp, buf, arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4953 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4954 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4955 count = sprintf (tmp, buf, prefixes[0], arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4956 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4957 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4958 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4959 arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4960 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4961 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4962 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4963 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4964 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4965
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
4966 errno = 0;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4967 switch (type)
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 case TYPE_SCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4970 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4971 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
4972 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4973 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4974 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4975 case TYPE_UCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4976 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4977 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
4978 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4979 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4980 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4981 case TYPE_SHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4982 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4983 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
4984 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4985 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4986 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4987 case TYPE_USHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4988 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4989 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
4990 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4991 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4992 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4993 case TYPE_INT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4994 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4995 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
4996 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4997 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4998 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4999 case TYPE_UINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5000 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5001 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
5002 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5003 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5004 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5005 case TYPE_LONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5006 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5007 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
5008 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5009 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5010 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5011 case TYPE_ULONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5012 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5013 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
5014 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5015 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5016 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5017 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5018 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5019 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5020 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
5021 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5022 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5023 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5024 case TYPE_ULONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5025 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5026 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
5027 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5028 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5029 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5030 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5031 case TYPE_DOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5032 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5033 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
5034 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5035 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5036 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5037 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5038 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5039 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
5040 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5041 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5042 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5043 case TYPE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5044 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5045 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
5046 SNPRINTF_BUF (arg);
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 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5049 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5050 case TYPE_WIDE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5051 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5052 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
5053 SNPRINTF_BUF (arg);
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 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5056 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5057 case TYPE_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5058 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5059 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
5060 SNPRINTF_BUF (arg);
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 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5063 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5064 case TYPE_WIDE_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5065 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5066 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
5067 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5068 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5069 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5070 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5071 case TYPE_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5072 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5073 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
5074 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5075 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5076 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5077 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5078 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5079 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5080
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5081 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5082 /* Portability: Not all implementations of snprintf()
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5083 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
5084 bytes that snprintf() has produced or would have
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5085 produced. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5086 if (count >= 0)
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 /* Verify that snprintf() has NUL-terminated its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5089 result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5090 if (count < maxlen
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5091 && ((TCHAR_T *) (result + length)) [count] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5092 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5093 /* Portability hack. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5094 if (retcount > count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5095 count = retcount;
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 /* snprintf() doesn't understand the '%n'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5100 directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5101 if (fbp[1] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5102 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5103 /* Don't use the '%n' directive; instead, look
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5104 at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5105 fbp[1] = '\0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5106 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5107 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5108 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5109 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5110 /* Look at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5111 if (retcount < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5112 {
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5113 # if !HAVE_SNPRINTF_RETVAL_C99
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5114 /* HP-UX 10.20 snprintf() is doubly deficient:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5115 It doesn't understand the '%n' directive,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5116 *and* it returns -1 (rather than the length
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5117 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
5118 buffer is too small.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5119 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
5120 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
5121 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
5122 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
5123 floating-point argument. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5124 size_t tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5125 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
5126 dp->conversion, type, flags,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5127 width, has_precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5128 precision, pad_ourselves);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5129
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5130 if (maxlen < tmp_length)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5131 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5132 /* 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
5133 this reallocation only once. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5134 size_t bigger_need =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5135 xsum (length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5136 xsum (tmp_length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5137 TCHARS_PER_DCHAR - 1)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5138 / TCHARS_PER_DCHAR);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5139 /* And always grow proportionally.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5140 (There may be several arguments, each
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5141 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
5142 previous one.) */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5143 size_t bigger_need2 =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5144 xsum (xtimes (allocated, 2), 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5145 if (bigger_need < bigger_need2)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5146 bigger_need = bigger_need2;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5147 ENSURE_ALLOCATION (bigger_need);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5148 continue;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5149 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5150 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5151 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5152 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5153 count = retcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5154 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5155 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5156 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5157
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5158 /* Attempt to handle failure. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5159 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5160 {
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5161 /* SNPRINTF or sprintf failed. Save and use the errno
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5162 that it has set, if any. */
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5163 int saved_errno = errno;
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5164
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5165 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5166 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5167 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5168 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5169 CLEANUP ();
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5170 errno =
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5171 (saved_errno != 0
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5172 ? saved_errno
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5173 : (dp->conversion == 'c' || dp->conversion == 's'
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5174 ? EILSEQ
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5175 : EINVAL));
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5176 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5177 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5178
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5179 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5180 /* Handle overflow of the allocated buffer.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5181 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
5182 returns a count >= maxlen. However, a non-compliant
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5183 snprintf() function returns only count = maxlen - 1. To
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5184 cover both cases, test whether count >= maxlen - 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5185 if ((unsigned int) count + 1 >= maxlen)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5186 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5187 /* If maxlen already has attained its allowed maximum,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5188 allocating more memory will not increase maxlen.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5189 Instead of looping, bail out. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5190 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5191 goto overflow;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5192 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5193 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5194 /* Need at least (count + 1) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5195 bytes. (The +1 is for the trailing NUL.)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5196 But ask for (count + 2) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5197 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
5198 maxlen > (unsigned int) count + 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5199 and so we don't get here again.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5200 And allocate proportionally, to avoid looping
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5201 eternally if snprintf() reports a too small
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5202 count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5203 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5204 xmax (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5205 ((unsigned int) count + 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5206 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5207 / TCHARS_PER_DCHAR),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5208 xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5209
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5210 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5211 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5212 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5213 }
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
5214 #endif
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5215
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5216 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5217 if (prec_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5218 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5219 /* Handle the precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5220 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
5221 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5222 (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
5223 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5224 tmp;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5225 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5226 size_t prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5227 size_t move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5228
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5229 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5230 /* Put the additional zeroes after the sign. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5231 if (count >= 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5232 && (*prec_ptr == '-' || *prec_ptr == '+'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5233 || *prec_ptr == ' '))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5234 prefix_count = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5235 /* 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
5236 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5237 else if (count >= 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5238 && prec_ptr[0] == '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5239 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5240 prefix_count = 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5241
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5242 move = count - prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5243 if (precision > move)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5244 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5245 /* Insert zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5246 size_t insert = precision - move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5247 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
5248
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5249 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5250 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5251 xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5252 (count + insert + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5253 / TCHARS_PER_DCHAR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5254 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
5255 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5256 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
5257 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
5258 # endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5259
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5260 prec_end = prec_ptr + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5261 prec_ptr += prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5262
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5263 while (prec_end > prec_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5264 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5265 prec_end--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5266 prec_end[insert] = prec_end[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5267 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5268
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5269 prec_end += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5270 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5271 *--prec_end = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5272 while (prec_end > prec_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5273
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5274 count += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5275 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5276 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5277 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5278
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5279 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5280 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5281 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5282 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5283 abort ();
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5284 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5285
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5286 #if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5287 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5288 if (dp->conversion == 'c' || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5289 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5290 /* 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
5291 TYPE_WIDE_STRING.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5292 The result string is not certainly ASCII. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5293 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5294 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5295 size_t tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5296 /* This code assumes that TCHAR_T is 'char'. */
13788
6f8ee3f6161c rewrite int foo[2*X-1] to verify(X) or to int foo[X?1:-1]
Paul Eggert <eggert@cs.ucla.edu>
parents: 13323
diff changeset
5297 verify (sizeof (TCHAR_T) == 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
5298 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5299 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
5300 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5301 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
5302 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5303 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5304 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5305 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5306 tmpsrc, count,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5307 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5308 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5309 if (tmpdst == NULL)
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 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5312 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5313 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5314 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5315 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5316 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5317 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5318 return NULL;
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 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5321 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5322 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5323 count = tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5324 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5325 else
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 /* The result string is ASCII.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5328 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
5329 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5330 /* 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
5331 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
5332 at (result + length). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5333 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
5334 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5335 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5336 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5337 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5338 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
5339
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5340 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5341 if (result == resultbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5342 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5343 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5344 /* ENSURE_ALLOCATION will not move tmpsrc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5345 (because it's part of resultbuf). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5346 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5347 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5348 else
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 /* ENSURE_ALLOCATION will move the array
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5351 (because it uses realloc(). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5352 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5353 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5354 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5355 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5356 tmpsrc = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5357 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
5358 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5359 tmpdst = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5360 /* Copy backwards, because of overlapping. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5361 tmpsrc += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5362 tmpdst += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5363 for (n = count; n > 0; n--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5364 *--tmpdst = (unsigned char) *--tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5365 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5366 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5367 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5368
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5369 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5370 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5371 if (count > allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5372 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5373 /* Need at least count elements. But allocate
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5374 proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5375 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5376 xmax (xsum (length, count), xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5377
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5378 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5379 }
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5380 #endif
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5381
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5382 /* Here count <= allocated - length. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5383
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5384 /* 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
5385 #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
5386 if (pad_ourselves && has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5387 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5388 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
5389 # if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5390 /* 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
5391 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5392 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5393 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
5394 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5395 /* 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
5396 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5397 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
5398 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5399 if (w < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5400 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5401 size_t pad = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5402
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5403 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5404 if (xsum (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 /* Need at least count + pad elements. But
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5407 allocate proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5408 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5409 xmax (xsum3 (length, count, pad),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5410 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
5411
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5412 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5413 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5414 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5415 length -= count;
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5416 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5417 ENSURE_ALLOCATION (n);
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5418 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5419 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5420 /* Here count + pad <= allocated - length. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5422 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5423 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5424 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
5425 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5426 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
5427 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5428 DCHAR_T *p = rp + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5429 DCHAR_T *end = p + pad;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5430 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
5431 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5432 if (dp->conversion == 'c'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5433 || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5434 /* No zero-padding for string directives. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5435 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5436 else
9672
c0fb9d4d860d Fix bug introduced on 2007-06-10.
Bruno Haible <bruno@clisp.org>
parents: 9651
diff changeset
5437 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5438 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5439 pad_ptr = (*rp == '-' ? rp + 1 : rp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5440 /* No zero-padding of "inf" and "nan". */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5441 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5442 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5443 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5444 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5445 /* 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
5446 with the zero padding insertion point being at
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5447 pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5448
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5449 count = count + pad; /* = end - rp */
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 if (flags & FLAG_LEFT)
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 right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5454 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5455 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5456 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5457 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5458 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5459 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5460 DCHAR_T *q = end;
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 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5463 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5464 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5465 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5466 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5467 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5468 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5469 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5470 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5471
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5472 while (p > rp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5473 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5474 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5475 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5476 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5477 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5478 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5479 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5480 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5481
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5482 /* Here still count <= allocated - length. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5483
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5484 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5485 /* The snprintf() result did fit. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5486 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5487 /* Append the sprintf() result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5488 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
5489 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5490 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5491 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5492 free (tmp);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5493 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5494
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5495 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5496 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5497 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5498 /* 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
5499 DCHAR_T *rp = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5500 size_t rc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5501 for (rc = count; rc > 0; rc--, rp++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5502 if (*rp >= 'a' && *rp <= 'z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5503 *rp = *rp - 'a' + 'A';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5504 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5505 #endif
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5506
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5507 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5508 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5509 }
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
5510 errno = orig_errno;
13323
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
5511 #undef pad_ourselves
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
5512 #undef prec_ourselves
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5513 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5514 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5515 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5516
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5517 /* Add the final NUL. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5518 ENSURE_ALLOCATION (xsum (length, 1));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5519 result[length] = '\0';
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5520
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5521 if (result != resultbuf && length + 1 < allocated)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5522 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5523 /* Shrink the allocated memory if possible. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5524 DCHAR_T *memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5525
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5526 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
5527 if (memory != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5528 result = memory;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5529 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5530
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5531 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5532 free (buf_malloced);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5533 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5534 *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
5535 /* 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
5536 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
5537 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
5538 not have this limitation. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5539 return result;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5540
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5541 #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
5542 overflow:
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5543 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
5544 free (result);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5545 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
5546 free (buf_malloced);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5547 CLEANUP ();
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5548 errno = EOVERFLOW;
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5549 return NULL;
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5550 #endif
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5551
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5552 out_of_memory:
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5553 if (!(result == resultbuf || result == NULL))
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5554 free (result);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5555 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5556 free (buf_malloced);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5557 out_of_memory_1:
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5558 CLEANUP ();
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5559 errno = ENOMEM;
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5560 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5561 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5562 }
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5563
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5564 #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
5565 #undef TCHARS_PER_DCHAR
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5566 #undef SNPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5567 #undef USE_SNPRINTF
11804
73f84ac92381 Undefine DCHAR_SET after use.
Bruno Haible <bruno@clisp.org>
parents: 11511
diff changeset
5568 #undef DCHAR_SET
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5569 #undef DCHAR_CPY
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5570 #undef PRINTF_PARSE
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5571 #undef DIRECTIVES
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5572 #undef DIRECTIVE
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5573 #undef DCHAR_IS_TCHAR
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5574 #undef TCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5575 #undef DCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5576 #undef FCHAR_T
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5577 #undef VASNPRINTF