annotate lib/vasnprintf.c @ 17102:9e72d3927af1

binary-io, eealloc, mbfile, mbiter, mbutil, xsize: better 'inline' * lib/binary-io.c, lib/eealloc.c, lib/mbfile.c, lib/mbiter.c: * lib/mbuiter.c, lib/xsize.c: New files. * lib/binary-io.h (BINARY_IO_INLINE): * lib/eealloc.h (EEALLOC_INLINE): * lib/mbfile.h (MBFILE_INLINE): * lib/mbiter.h (MBITER_INLINE): * lib/mbuiter.h (MBUITER_INLINE): * lib/xsize.h (XSIZE_INLINE): New macros. Replace all uses of 'static inline' with them. Use _GL_INLINE_HEADER_BEGIN, _GL_INLINE_HEADER_END. * m4/eealloc.m4 (gl_EEALLOC): * m4/mbfile.m4 (gl_MBFILE): * m4/mbiter.m4 (gl_MBITER): * m4/xsize.m4 (gl_XSIZE): Do not require AC_C_INLINE. * modules/binary-io (Files, lib_SOURCES): Add lib/binary-io.c * modules/eealloc (Files, lib_SOURCES): Add lib/eealloc.c. * modules/mbfile (Files, lib_SOURCES): Add lib/mbfile.c. * modules/mbiter (Files, lib_SOURCES): Add lib/mbiter.c. * modules/mbuiter (Files, lib_SOURCES): Add lib/mbuiter.c. * modules/xsize (Files, lib_SOURCES): Add lib/xsize.c. * modules/binary-io, modules/eealloc, modules/mbfile: * modules/mbiter, modules/mbuiter: (Depends-on): Add extern-inline.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 29 Aug 2012 23:13:42 -0700
parents 498a2211d839
children dd46d4e6beea
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.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 15959
diff changeset
2 Copyright (C) 1999, 2002-2012 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
16366
bb182ee4a09d maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 16358
diff changeset
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
17 /* 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 /* 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
47 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
48 <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
49 #ifndef _GNU_SOURCE
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 # define _GNU_SOURCE 1
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
53 #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
54 # 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
55 #endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
56 #ifndef IN_LIBINTL
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
57 # include <alloca.h>
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
58 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* 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
61 #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
62 # 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
63 # 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
64 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
65 # 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
66 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
67 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
69 #include <locale.h> /* localeconv() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
70 #include <stdio.h> /* snprintf(), sprintf() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
71 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
72 #include <string.h> /* memcpy(), strlen() */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
73 #include <errno.h> /* errno */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
74 #include <limits.h> /* CHAR_BIT */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
75 #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
76 #if HAVE_NL_LANGINFO
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
77 # include <langinfo.h>
35eb5062216c Prefer nl_langinfo over localeconv.
Bruno Haible <bruno@clisp.org>
parents: 8531
diff changeset
78 #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
79 #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
80 # 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
81 # 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
82 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
83 # 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
84 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
85 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
87 /* Checked size_t computations. */
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
88 #include "xsize.h"
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
89
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
90 #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
91
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
92 #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
93 # include <math.h>
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
94 # include "float+.h"
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
95 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
96
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
97 #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
98 # 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
99 # 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
100 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
101
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
102 #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
103 # 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
104 # include "isnanl-nolibm.h"
8915
f15c03805bd9 Fix possible compilation error.
Bruno Haible <bruno@clisp.org>
parents: 8865
diff changeset
105 # include "fpucw.h"
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
106 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
107
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
108 #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
109 # 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
110 # include "isnand-nolibm.h"
8526
67e9a647f2fa Fix includes.
Bruno Haible <bruno@clisp.org>
parents: 8487
diff changeset
111 # 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
112 #endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
113
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
114 #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
115 # include <math.h>
8648
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
116 # include "isnanl-nolibm.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
117 # include "printf-frexpl.h"
359d135f748c Assume 'long double' exists.
Bruno Haible <bruno@clisp.org>
parents: 8569
diff changeset
118 # include "fpucw.h"
8335
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
119 #endif
2c9de3b6ba5b New module 'vasnprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents: 8254
diff changeset
120
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
121 /* 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
122 #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
123 # 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
124 # 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
125 # 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
126 # 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
127 # 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
128 # 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
129 # 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
130 # 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
131 # 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
132 # define DCHAR_CPY wmemcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
133 # 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
134 # else
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
135 # define 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
136 # 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
137 # 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
138 # 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
139 # 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
140 # 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
141 # 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
142 # 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
143 # define DCHAR_CPY memcpy
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
144 # 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
145 # endif
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
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
147 #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
148 /* TCHAR_T is wchar_t. */
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
149 # define USE_SNPRINTF 1
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
150 # if HAVE_DECL__SNWPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
151 /* 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
152 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
153 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
154 MSVCRT function _snwprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
155 # if defined __MINGW32__
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
156 # define SNPRINTF snwprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
157 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
158 # define SNPRINTF _snwprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
159 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
160 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
161 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
162 # define SNPRINTF swprintf
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
163 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
164 #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
165 /* TCHAR_T is char. */
9940
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
166 /* 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
167 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
168 size argument is >= 0x3000000.
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
169 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
170 writes any output without bounds, like sprintf. */
0471a8660b70 Work around snprintf bug on Linux libc5.
Bruno Haible <bruno@clisp.org>
parents: 9939
diff changeset
171 # 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
172 # define USE_SNPRINTF 1
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
173 # else
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
174 # define USE_SNPRINTF 0
d3f907c33d15 Work around broken snprintf function on BeOS.
Bruno Haible <bruno@clisp.org>
parents: 8830
diff changeset
175 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
176 # if HAVE_DECL__SNPRINTF
13197
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
177 /* 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
178 function _snprintf(), so prefer that. */
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
179 # if defined __MINGW32__
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
180 # define SNPRINTF snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
181 /* 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
182 # undef snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
183 # else
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
184 # define SNPRINTF _snprintf
5faa02e0b292 vasnprintf: Fix multiple test failures on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12552
diff changeset
185 # endif
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
186 # else
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
187 /* Unix. */
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
188 # define SNPRINTF snprintf
8417
ee40afd85f2c Avoid an endless recursion.
Bruno Haible <bruno@clisp.org>
parents: 8400
diff changeset
189 /* 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
190 # undef snprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
191 # endif
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
192 #endif
8424
ec3450ce9889 Use the system's sprintf function.
Bruno Haible <bruno@clisp.org>
parents: 8417
diff changeset
193 /* 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
194 #undef sprintf
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
195
10102
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
196 /* 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
197 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
198 #ifdef lint
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
199 # define IF_LINT(Code) Code
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
200 #else
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
201 # define IF_LINT(Code) /* empty */
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
202 #endif
8d61236d099c Avoid one more warning from gcc.
Bruno Haible <bruno@clisp.org>
parents: 10101
diff changeset
203
10084
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
204 /* Avoid some warnings from "gcc -Wshadow".
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
205 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
206 #undef exp
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
207 #define exp expo
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
208 #undef remainder
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
209 #define remainder rem
e4945463e1bf Avoid some warnings from "gcc -Wshadow".
Bruno Haible <bruno@clisp.org>
parents: 9940
diff changeset
210
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
211 #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
212 # if (HAVE_STRNLEN && !defined _AIX)
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
213 # define local_strnlen strnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
214 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
215 # ifndef local_strnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
216 # define local_strnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
217 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
218 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
219 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
220 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
221 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
222 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
223 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
224 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
225 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
226
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
227 #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
228 # if HAVE_WCSLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
229 # define local_wcslen wcslen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
230 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
231 /* 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
232 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
233 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
234 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
235 # ifndef local_wcslen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
236 # define local_wcslen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
237 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
238 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
239 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
240 const wchar_t *ptr;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
241
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
242 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
243 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
244 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
245 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
246 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
247 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
248 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
249
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
250 #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
251 # if HAVE_WCSNLEN
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
252 # define local_wcsnlen wcsnlen
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
253 # else
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
254 # ifndef local_wcsnlen_defined
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
255 # define local_wcsnlen_defined 1
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
256 static size_t
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
257 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
258 {
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
259 const wchar_t *ptr;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
260
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
261 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
262 ;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
263 return ptr - s;
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
264 }
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
265 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
266 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
267 #endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
268
10450
efaa10653f4b Tweak last commit.
Bruno Haible <bruno@clisp.org>
parents: 10449
diff changeset
269 #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
270 /* 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
271 # 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
272 # 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
273 static char
11856
8b0d11ca9d01 build: avoid some compiler warnings
Eric Blake <ebb9@byu.net>
parents: 11804
diff changeset
274 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
275 {
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
276 const char *point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
277 /* Determine it in a multithread-safe way. We know nl_langinfo is
16935
498a2211d839 Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents: 16366
diff changeset
278 multithread-safe on glibc systems and Mac OS X systems, but is not required
12344
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
279 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
9f98da6e2275 vasnprintf: Tiny optimization.
Bruno Haible <bruno@clisp.org>
parents: 12306
diff changeset
280 localeconv() is rarely multithread-safe. */
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
281 # 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
282 point = nl_langinfo (RADIXCHAR);
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
283 # elif 1
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
284 char pointbuf[5];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
285 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
286 point = &pointbuf[1];
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
287 # else
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
288 point = localeconv () -> decimal_point;
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
289 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
290 /* 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
291 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
292 }
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
293 # endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
294 #endif
378e85482afa Use multithread-safe primitives for determining the decimal point character.
Bruno Haible <bruno@clisp.org>
parents: 8554
diff changeset
295
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
296 #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
297
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
298 /* 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
299 static int
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
300 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
301 {
9646
a113e473cc98 Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents: 9489
diff changeset
302 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
303 }
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 #endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
306
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
307 #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
308
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
309 /* 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
310 static int
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
311 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
312 {
10448
a5c27eb6fed3 vasnprintf: support Irix 5.3
Eric Blake <ebb9@byu.net>
parents: 10394
diff changeset
313 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
314 }
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 #endif
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
317
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
318 #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
319
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
320 /* 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
321 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
322 (and slower) algorithms. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
323
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
324 typedef unsigned int mp_limb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
325 # 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
326 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
327
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
328 typedef unsigned long long mp_twolimb_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
329 # 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
330 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
331
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
332 /* Representation of a bignum >= 0. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
333 typedef struct
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
334 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
335 size_t nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
336 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
337 } mpn_t;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
338
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
339 /* Compute the product of two bignums >= 0.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
340 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
341 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
342 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
343 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
344 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
345 const mp_limb_t *p1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
346 const mp_limb_t *p2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
347 size_t len1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
348 size_t len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
349
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
350 if (src1.nlimbs <= src2.nlimbs)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
351 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
352 len1 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
353 p1 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
354 len2 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
355 p2 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
356 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
357 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
358 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
359 len1 = src2.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
360 p1 = src2.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
361 len2 = src1.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
362 p2 = src1.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
363 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
364 /* Now 0 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
365 if (len1 == 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
366 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
367 /* src1 or src2 is zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
368 dest->nlimbs = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
369 dest->limbs = (mp_limb_t *) malloc (1);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
370 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
371 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
372 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
373 /* Here 1 <= len1 <= len2. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
374 size_t dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
375 mp_limb_t *dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
376 size_t k, i, j;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
377
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
378 dlen = len1 + len2;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
379 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
380 if (dp == NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
381 return NULL;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
382 for (k = len2; k > 0; )
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
383 dp[--k] = 0;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
384 for (i = 0; i < len1; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
385 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
386 mp_limb_t digit1 = p1[i];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
387 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
388 for (j = 0; j < len2; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
389 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
390 mp_limb_t digit2 = p2[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
391 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
392 carry += dp[i + j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
393 dp[i + j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
394 carry = carry >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
395 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
396 dp[i + len2] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
397 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
398 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
399 while (dlen > 0 && dp[dlen - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
400 dlen--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
401 dest->nlimbs = dlen;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
402 dest->limbs = dp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
403 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
404 return dest->limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
405 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
406
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
407 /* 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
408 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
409 the remainder.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
410 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
411 q is incremented.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
412 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
413 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
414 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
415 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
416 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
417 /* Algorithm:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
418 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
419 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
420 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
421 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
422 r:=0, j:=m,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
423 while j>0 do
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
424 {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
425 = 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
426 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
427 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
428 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
429 We have a/b < beta^(m-n+1).
11422
847931ff84ef typo fix in a comment
Jim Meyering <meyering@redhat.com>
parents: 11220
diff changeset
430 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
431 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
432 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
433 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
434 Compute q* :
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
435 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
436 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
437 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
438 and c3 := b[n-2] * q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
439 {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
440 occurred. Furthermore 0 <= c3 < beta^2.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
441 If there was overflow and
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
442 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
443 the next test can be skipped.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
444 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
445 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
446 If q* > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
447 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
448 [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
449 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
450 u := u + q* * b[i],
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
451 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
452 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
453 r[n+j]:=r[n+j]-u.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
454 {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
455 < q* + 1 <= beta,
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
456 the carry u does not overflow.}
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
457 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
458 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
459 Set q[j] := q*.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
460 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
461 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
462 rest r.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
463 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
464 Finally, round-to-even:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
465 Shift r left by 1 bit.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
466 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
467 */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
468 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
469 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
470 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
471 size_t b_len = b.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
472 mp_limb_t *roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
473 mp_limb_t *tmp_roomptr = NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
474 mp_limb_t *q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
475 size_t q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
476 mp_limb_t *r_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
477 size_t r_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
478
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
479 /* Allocate room for a_len+2 digits.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
480 (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
481 final rounding of q.) */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
482 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
483 if (roomptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
484 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
485
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
486 /* Normalise a. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
487 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
488 a_len--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
489
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
490 /* Normalise b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
491 for (;;)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
492 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
493 if (b_len == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
494 /* Division by zero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
495 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
496 if (b_ptr[b_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
497 b_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
498 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
499 break;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
500 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
501
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
502 /* 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
503
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
504 if (a_len < b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
505 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
506 /* 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
507 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
508 r_len = a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
509 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
510 q_ptr = roomptr + a_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
511 q_len = 0;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
512 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
513 else if (b_len == 1)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
514 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
515 /* n=1: single precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
516 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
517 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
518 q_ptr = roomptr + 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
519 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
520 mp_limb_t den = b_ptr[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
521 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
522 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
523 mp_limb_t *destptr = q_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
524 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
525 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
526 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
527 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
528 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
529 *--destptr = num / den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
530 remainder = num % den;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
531 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
532 /* Normalise and store r. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
533 if (remainder > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
534 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
535 r_ptr[0] = remainder;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
536 r_len = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
537 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
538 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
539 r_len = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
540 /* Normalise q. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
541 q_len = a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
542 if (q_ptr[q_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
543 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
544 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
545 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
546 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
547 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
548 /* n>1: multiple precision division.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
549 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
550 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
551 /* Determine s. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
552 size_t s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
553 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
554 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
15959
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
555 /* Determine s = GMP_LIMB_BITS - integer_length (msd).
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
556 Code copied from gnulib's integer_length.c. */
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
557 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
558 s = __builtin_clz (msd);
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
559 # else
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
560 # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
561 if (GMP_LIMB_BITS <= DBL_MANT_BIT)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
562 {
15959
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
563 /* Use 'double' operations.
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
564 Assumes an IEEE 754 'double' implementation. */
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
565 # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
566 # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
567 # define NWORDS \
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
568 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
569 union { double value; unsigned int word[NWORDS]; } m;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
570
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
571 /* Use a single integer to floating-point conversion. */
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
572 m.value = msd;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
573
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
574 s = GMP_LIMB_BITS
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
575 - (((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT) & DBL_EXP_MASK)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
576 - DBL_EXP_BIAS);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
577 }
15959
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
578 else
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
579 # undef NWORDS
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
580 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
581 {
15959
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
582 s = 31;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
583 if (msd >= 0x10000)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
584 {
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
585 msd = msd >> 16;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
586 s -= 16;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
587 }
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
588 if (msd >= 0x100)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
589 {
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
590 msd = msd >> 8;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
591 s -= 8;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
592 }
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
593 if (msd >= 0x10)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
594 {
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
595 msd = msd >> 4;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
596 s -= 4;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
597 }
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
598 if (msd >= 0x4)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
599 {
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
600 msd = msd >> 2;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
601 s -= 2;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
602 }
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
603 if (msd >= 0x2)
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
604 {
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
605 msd = msd >> 1;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
606 s -= 1;
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
607 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
608 }
15959
a18c505b7981 vasnprintf: Optimize bit search operation.
Bruno Haible <bruno@clisp.org>
parents: 15958
diff changeset
609 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
610 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
611 /* 0 <= s < GMP_LIMB_BITS.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
612 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
613 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
614 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
615 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
616 if (tmp_roomptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
617 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
618 free (roomptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
619 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
620 }
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 = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
623 mp_limb_t *destptr = tmp_roomptr;
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 = b_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 /* 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
633 if (accu != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
634 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
635 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
636 b_ptr = tmp_roomptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
637 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
638 /* 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
639 Memory layout:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
640 At the beginning: r = roomptr[0..a_len],
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
641 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
642 r_ptr = roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
643 if (s == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
644 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
645 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
646 r_ptr[a_len] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
647 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
648 else
12421
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 const mp_limb_t *sourceptr = a_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
651 mp_limb_t *destptr = r_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
652 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
653 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
654 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
655 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
656 accu += (mp_twolimb_t) *sourceptr++ << s;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
657 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
658 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
659 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
660 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
661 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
662 q_ptr = roomptr + b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
663 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
664 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
665 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
666 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
667 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
668 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
669 ((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
670 /* Division loop, traversed m-n+1 times.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
671 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
672 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
673 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
674 mp_limb_t q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
675 mp_limb_t c1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
676 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
677 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
678 /* 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
679 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
680 ((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
681 | r_ptr[j + b_len - 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
682 q_star = num / b_msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
683 c1 = num % b_msd;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
684 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
685 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
686 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
687 /* 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
688 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
689 /* 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
690 <==> 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
691 <==> 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
692 {<= beta !}.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
693 If yes, jump directly to the subtraction loop.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
694 (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
695 <==> 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
696 if (r_ptr[j + b_len] > b_msd
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
697 || (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
698 /* r[j+n] >= b[n-1]+1 or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
699 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
700 carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
701 goto subtract;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
702 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
703 /* q_star = q*,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
704 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
705 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
706 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
707 ((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
708 mp_twolimb_t c3 = /* b[n-2] * q* */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
709 (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
710 /* While c2 < c3, increase c2 and decrease c3.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
711 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
712 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
713 this can happen only twice. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
714 if (c3 > c2)
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 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
717 if (c3 - c2 > b_msdd)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
718 q_star = q_star - 1; /* q* := q* - 1 */
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
721 if (q_star > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
722 subtract:
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 /* Subtract r := r - b * q* * beta^j. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
725 mp_limb_t cr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
726 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
727 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
728 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
729 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
730 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
731 for (count = b_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
732 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
733 /* Here 0 <= carry <= q*. */
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 carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
736 + (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
737 + (mp_limb_t) ~(*destptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
738 /* Here 0 <= carry <= beta*q* + beta-1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
739 *destptr++ = ~(mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
740 carry = carry >> GMP_LIMB_BITS; /* <= q* */
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 cr = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
743 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
744 /* 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
745 r_ptr[j + b_len]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
746 if (cr > r_ptr[j + b_len])
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
747 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
748 /* Subtraction gave a carry. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
749 q_star = q_star - 1; /* q* := q* - 1 */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
750 /* Add b back. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
751 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
752 const mp_limb_t *sourceptr = b_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
753 mp_limb_t *destptr = r_ptr + j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
754 mp_limb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
755 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
756 for (count = b_len; count > 0; count--)
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 source1 = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
759 mp_limb_t source2 = *destptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
760 *destptr++ = source1 + source2 + carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
761 carry =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
762 (carry
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
763 ? source1 >= (mp_limb_t) ~source2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
764 : source1 > (mp_limb_t) ~source2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
765 }
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 /* 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
768 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
769 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
770 /* q* is determined. Store it as q[j]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
771 q_ptr[j] = q_star;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
772 if (j == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
773 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
774 j--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
775 }
8832
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 r_len = b_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
778 /* Normalise q. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
779 if (q_ptr[q_len - 1] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
780 q_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
781 # 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
782 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
783 /* Shift r right by s bits. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
784 if (s > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
785 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
786 mp_limb_t ptr = r_ptr + r_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
787 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
788 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
789 for (count = r_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
790 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
791 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
792 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
793 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
794 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
795 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
796 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
797 /* Normalise r. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
798 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
799 r_len--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
800 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
801 /* Compare r << 1 with b. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
802 if (r_len > b_len)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
803 goto increment_q;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
804 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
805 size_t i;
8835
c06bc412ffc3 Fix small bug in 'divide' function.
Bruno Haible <bruno@clisp.org>
parents: 8832
diff changeset
806 for (i = b_len;;)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
807 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
808 mp_limb_t r_i =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
809 (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
810 | (i < r_len ? r_ptr[i] << 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
811 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
812 if (r_i > b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
813 goto increment_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
814 if (r_i < b_i)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
815 goto keep_q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
816 if (i == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
817 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
818 i--;
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 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
821 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
822 /* q is odd. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
823 increment_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
824 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
825 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
826 for (i = 0; i < q_len; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
827 if (++(q_ptr[i]) != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
828 goto keep_q;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
829 q_ptr[q_len++] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
830 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
831 keep_q:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
832 if (tmp_roomptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
833 free (tmp_roomptr);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
834 q->limbs = q_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
835 q->nlimbs = q_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
836 return roomptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
837 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
838
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
839 /* 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
840 representation.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
841 Destroys the contents of a.
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
842 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
843 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
844 of memory allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
845 static char *
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
846 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
847 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
848 mp_limb_t *a_ptr = a.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
849 size_t a_len = a.nlimbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
850 /* 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
851 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
852 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
853 if (c_ptr != NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
854 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
855 char *d_ptr = c_ptr;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
856 for (; extra_zeroes > 0; extra_zeroes--)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
857 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
858 while (a_len > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
859 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
860 /* Divide a by 10^9, in-place. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
861 mp_limb_t remainder = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
862 mp_limb_t *ptr = a_ptr + a_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
863 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
864 for (count = a_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
865 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
866 mp_twolimb_t num =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
867 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
868 *ptr = num / 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
869 remainder = num % 1000000000;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
870 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
871 /* Store the remainder as 9 decimal digits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
872 for (count = 9; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
873 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
874 *d_ptr++ = '0' + (remainder % 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
875 remainder = remainder / 10;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
876 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
877 /* Normalize a. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
878 if (a_ptr[a_len - 1] == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
879 a_len--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
880 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
881 /* Remove leading zeroes. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
882 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
883 d_ptr--;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
884 /* But keep at least one zero. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
885 if (d_ptr == c_ptr)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
886 *d_ptr++ = '0';
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
887 /* Terminate the string. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
888 *d_ptr = '\0';
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
889 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
890 return c_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
891 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
892
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
893 # if 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
894
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
895 /* Assuming x is finite and >= 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
896 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
897 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
898 allocation failure. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
899 static void *
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
900 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
901 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
902 mpn_t m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
903 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
904 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
905 size_t i;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
906
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
907 /* Allocate memory for result. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
908 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
909 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
910 if (m.limbs == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
911 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
912 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
913 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
914 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
915 abort ();
15958
ed8603738240 vasnprintf: Fix comments.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
916 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
917 latter is an integer. */
15958
ed8603738240 vasnprintf: Fix comments.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
918 /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
919 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
920 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
921 '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
922 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
923 # 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
924 # 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
925 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
926 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
927 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
928 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
929 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
930 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
931 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
932 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
933 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
934 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
935 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
936 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
937 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
938 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
939 # else
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
940 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
941 mp_limb_t d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
942 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
943 d = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
944 y -= d;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
945 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
946 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
947 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
948 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
949 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
950 # endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
951 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
952 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
953 mp_limb_t hi, lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
954 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
955 hi = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
956 y -= hi;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
957 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
958 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
959 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
960 lo = (int) y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
961 y -= lo;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
962 if (!(y >= 0.0L && y < 1.0L))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
963 abort ();
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
964 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
965 }
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14260
diff changeset
966 # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14260
diff changeset
967 precision. */
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
968 if (!(y == 0.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
969 abort ();
14384
588ed4cdea87 Fix misindentation of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 14260
diff changeset
970 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
971 /* Normalise. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
972 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
973 m.nlimbs--;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
974 *mp = m;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
975 *ep = exp - LDBL_MANT_BIT;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
976 return m.limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
977 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
978
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
979 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
980
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
981 # if 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
982
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
983 /* 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
984 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
985 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
986 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
987 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
988 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
989 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
990 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
991 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
992 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
993 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
994
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
995 /* 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
996 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
997 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
998 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
999 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
1000 /* 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
1001 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
1002 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
1003 abort ();
15958
ed8603738240 vasnprintf: Fix comments.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
1004 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1005 latter is an integer. */
15958
ed8603738240 vasnprintf: Fix comments.
Bruno Haible <bruno@clisp.org>
parents: 14384
diff changeset
1006 /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
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 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
1008 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
1009 '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
1010 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
1011 # 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
1012 # 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
1013 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1014 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
1015 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
1016 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
1017 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
1018 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1019 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
1020 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
1021 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
1022 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
1023 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1024 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
1025 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
1026 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1027 # else
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1028 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1029 mp_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
1030 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
1031 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
1032 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
1033 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1034 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
1035 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
1036 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1037 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1038 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1039 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
1040 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1041 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
1042 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
1043 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
1044 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
1045 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1046 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
1047 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
1048 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
1049 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
1050 if (!(y >= 0.0 && y < 1.0))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1051 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
1052 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
1053 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1054 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
1055 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1056 /* Normalise. */
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1057 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
1058 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
1059 *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
1060 *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
1061 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
1062 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1063
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1064 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1065
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1066 /* 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
1067 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
1068 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
1069 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
1070 of memory allocation failure. */
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1071 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
1072 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
1073 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1074 int s;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1075 size_t extra_zeroes;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1076 unsigned int abs_n;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1077 unsigned int abs_s;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1078 mp_limb_t *pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1079 size_t pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1080 unsigned int s_limbs;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1081 unsigned int s_bits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1082 mpn_t pow5;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1083 mpn_t z;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1084 void *z_memory;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1085 char *digits;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1086
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1087 if (memory == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1088 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1089 /* x = 2^e * m, hence
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1090 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
1091 = round (2^s * 5^n * m). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1092 s = e + n;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1093 extra_zeroes = 0;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1094 /* 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
1095 if (s > 0 && n > 0)
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1096 {
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1097 extra_zeroes = (s < n ? s : n);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1098 s -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1099 n -= extra_zeroes;
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1100 }
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1101 /* 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
1102 Before converting to decimal, we need to compute
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1103 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
1104 /* 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
1105 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
1106 abs_n = (n >= 0 ? n : -n);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1107 abs_s = (s >= 0 ? s : -s);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1108 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
1109 + abs_s / GMP_LIMB_BITS + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1110 * sizeof (mp_limb_t));
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1111 if (pow5_ptr == NULL)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1112 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1113 free (memory);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1114 return NULL;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1115 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1116 /* Initialize with 1. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1117 pow5_ptr[0] = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1118 pow5_len = 1;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1119 /* Multiply with 5^|n|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1120 if (abs_n > 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1121 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1122 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
1123 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1124 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
1125 48828125, 244140625, 1220703125
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1126 };
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1127 unsigned int n13;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1128 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
1129 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1130 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
1131 size_t j;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1132 mp_twolimb_t carry = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1133 for (j = 0; j < pow5_len; j++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1134 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1135 mp_limb_t digit2 = pow5_ptr[j];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1136 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
1137 pow5_ptr[j] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1138 carry = carry >> GMP_LIMB_BITS;
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 if (carry > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1141 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1142 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1143 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1144 s_limbs = abs_s / GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1145 s_bits = abs_s % GMP_LIMB_BITS;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1146 if (n >= 0 ? s >= 0 : s <= 0)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1147 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1148 /* Multiply with 2^|s|. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1149 if (s_bits > 0)
12421
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 mp_limb_t *ptr = pow5_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1152 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1153 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1154 for (count = pow5_len; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1155 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1156 accu += (mp_twolimb_t) *ptr << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1157 *ptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1158 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1159 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1160 if (accu > 0)
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 *ptr = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1163 pow5_len++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1164 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1165 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1166 if (s_limbs > 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1167 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1168 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1169 for (count = pow5_len; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1170 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1171 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1172 pow5_ptr[s_limbs + count] = pow5_ptr[count];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1173 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1174 for (count = s_limbs; count > 0;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1175 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1176 count--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1177 pow5_ptr[count] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1178 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1179 pow5_len += s_limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1180 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1181 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1182 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1183 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1184 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1185 /* Multiply m with pow5. No division needed. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1186 z_memory = multiply (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1187 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1188 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1189 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1190 /* Divide m by pow5 and round. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1191 z_memory = divide (m, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1192 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1193 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1194 else
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1195 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1196 pow5.limbs = pow5_ptr;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1197 pow5.nlimbs = pow5_len;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1198 if (n >= 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1199 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1200 /* n >= 0, s < 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1201 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
1202 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1203 mpn_t denominator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1204 void *tmp_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1205 tmp_memory = multiply (m, pow5, &numerator);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1206 if (tmp_memory == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1207 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1208 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1209 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1210 return NULL;
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 /* Construct 2^|s|. */
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 mp_limb_t *ptr = pow5_ptr + pow5_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1215 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1216 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1217 ptr[i] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1218 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
1219 denominator.limbs = ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1220 denominator.nlimbs = s_limbs + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1221 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1222 z_memory = divide (numerator, denominator, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1223 free (tmp_memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1224 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1225 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1226 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1227 /* n < 0, s > 0.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1228 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
1229 mpn_t numerator;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1230 mp_limb_t *num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1231 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
1232 * sizeof (mp_limb_t));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1233 if (num_ptr == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1234 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1235 free (pow5_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1236 free (memory);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1237 return NULL;
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 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1240 mp_limb_t *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 size_t i;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1243 for (i = 0; i < s_limbs; i++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1244 *destptr++ = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1245 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1246 if (s_bits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1247 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1248 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1249 mp_twolimb_t accu = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1250 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1251 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1252 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1253 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1254 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1255 accu = accu >> GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1256 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1257 if (accu > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1258 *destptr++ = (mp_limb_t) accu;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1259 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1260 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1261 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1262 const mp_limb_t *sourceptr = m.limbs;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1263 size_t count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1264 for (count = m.nlimbs; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1265 *destptr++ = *sourceptr++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1266 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1267 numerator.limbs = num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1268 numerator.nlimbs = destptr - num_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1269 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1270 z_memory = divide (numerator, pow5, &z);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1271 free (num_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1272 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1273 }
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1274 free (pow5_ptr);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1275 free (memory);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1276
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1277 /* 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
1278
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1279 if (z_memory == NULL)
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1280 return NULL;
8838
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1281 digits = convert_to_decimal (z, extra_zeroes);
db95bdbeb72c Optimize the case of huge precision.
Bruno Haible <bruno@clisp.org>
parents: 8837
diff changeset
1282 free (z_memory);
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1283 return digits;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1284 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1285
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1286 # 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
1287
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1288 /* 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
1289 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
1290 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
1291 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
1292 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
1293 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
1294 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
1295 {
10100
369c11a9858d avoid a warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10084
diff changeset
1296 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
1297 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
1298 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
1299 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
1300 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1301
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1302 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1303
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1304 # 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
1305
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1306 /* 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
1307 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
1308 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
1309 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
1310 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
1311 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
1312 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
1313 {
10101
daad35fae43c avoid another warning from gcc
Jim Meyering <meyering@redhat.com>
parents: 10100
diff changeset
1314 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
1315 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
1316 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
1317 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
1318 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1319
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1320 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1321
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1322 # 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
1323
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1324 /* Assuming x is finite and > 0:
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1325 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
1326 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
1327 static int
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1328 floorlog10l (long double x)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1329 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1330 int exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1331 long double y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1332 double z;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1333 double l;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1334
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1335 /* Split into exponential part and mantissa. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1336 y = frexpl (x, &exp);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1337 if (!(y >= 0.0L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1338 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1339 if (y == 0.0L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1340 return INT_MIN;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1341 if (y < 0.5L)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1342 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1343 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
1344 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1345 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
1346 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1347 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1348 if (y < (1.0L / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1349 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1350 y *= 1.0L * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1351 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1352 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1353 if (y < (1.0L / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1354 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1355 y *= 1.0L * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1356 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1357 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1358 if (y < (1.0L / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1359 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1360 y *= 1.0L * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1361 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1362 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1363 if (y < (1.0L / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1364 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1365 y *= 1.0L * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1366 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1367 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1368 if (y < (1.0L / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1369 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1370 y *= 1.0L * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1371 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1372 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1373 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1374 if (!(y >= 0.5L && y < 1.0L))
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1375 abort ();
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1376 /* 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
1377 l = exp;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1378 z = y;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1379 if (z < 0.70710678118654752444)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1380 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1381 z *= 1.4142135623730950488;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1382 l -= 0.5;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1383 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1384 if (z < 0.8408964152537145431)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1385 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1386 z *= 1.1892071150027210667;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1387 l -= 0.25;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1388 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1389 if (z < 0.91700404320467123175)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1390 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1391 z *= 1.0905077326652576592;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1392 l -= 0.125;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1393 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1394 if (z < 0.9576032806985736469)
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1395 {
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1396 z *= 1.0442737824274138403;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1397 l -= 0.0625;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1398 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1399 /* Now 0.95 <= z <= 1.01. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1400 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1401 /* 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
1402 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
1403 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
1404 /* 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
1405 log10(x). */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1406 l *= 0.30102999566398119523;
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1407 /* Round down to the next integer. */
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1408 return (int) l + (l < 0 ? -1 : 0);
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1409 }
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
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 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1412
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1413 # 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
1414
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1415 /* 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
1416 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
1417 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
1418 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
1419 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
1420 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1421 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
1422 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
1423 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
1424 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
1425
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1426 /* 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
1427 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
1428 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
1429 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1430 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
1431 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
1432 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
1433 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1434 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
1435 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1436 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
1437 exp -= GMP_LIMB_BITS;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1438 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1439 if (y < (1.0 / (1 << 16)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1440 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1441 y *= 1.0 * (1 << 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1442 exp -= 16;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1443 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1444 if (y < (1.0 / (1 << 8)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1445 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1446 y *= 1.0 * (1 << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1447 exp -= 8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1448 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1449 if (y < (1.0 / (1 << 4)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1450 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1451 y *= 1.0 * (1 << 4);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1452 exp -= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1453 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1454 if (y < (1.0 / (1 << 2)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1455 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1456 y *= 1.0 * (1 << 2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1457 exp -= 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1458 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1459 if (y < (1.0 / (1 << 1)))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1460 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1461 y *= 1.0 * (1 << 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1462 exp -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1463 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1464 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1465 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
1466 abort ();
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1467 /* 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
1468 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
1469 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
1470 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
1471 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1472 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
1473 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
1474 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1475 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
1476 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1477 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
1478 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
1479 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1480 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
1481 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1482 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
1483 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
1484 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1485 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
1486 {
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1487 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
1488 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
1489 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1490 /* 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
1491 z = 1 - z;
9939
11aa2931dfc7 Make floorlog10 function more precise.
Bruno Haible <bruno@clisp.org>
parents: 9938
diff changeset
1492 /* 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
1493 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
1494 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
1495 /* 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
1496 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
1497 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
1498 /* 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
1499 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
1500 }
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1501
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1502 # endif
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
1503
9938
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1504 /* 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
1505 a single '1' digit. */
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1506 static int
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1507 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
1508 {
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1509 for (; precision > 0; precision--, digits++)
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1510 if (*digits != '0')
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1511 return 0;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1512 if (*digits != '1')
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1513 return 0;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1514 digits++;
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1515 return *digits == '\0';
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1516 }
9f8c455639fc Fix rounding when a precision is given.
Bruno Haible <bruno@clisp.org>
parents: 9834
diff changeset
1517
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1518 #endif
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
1519
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1520 #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
1521
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1522 /* 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
1523 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
1524 translation unit. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1525 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1526 # 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
1527 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1528 # 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
1529 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1530
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1531 /* 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
1532 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
1533 static inline size_t
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1534 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
1535 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
1536 size_t precision, int pad_ourselves)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1537 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1538 size_t tmp_length;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1539
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1540 switch (conversion)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1541 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1542 case 'd': case 'i': case 'u':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1543 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1544 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1545 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1546 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1547 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1548 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1549 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1550 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1551 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1552 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1553 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1554 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1555 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1556 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1557 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1558 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1559 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1560 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1561 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1562 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1563 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1564 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1565 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1566 /* 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
1567 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
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 'o':
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.333334 /* binary -> octal */
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.333334 /* binary -> octal */
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.333334 /* binary -> octal */
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 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
1597 tmp_length = xsum (tmp_length, 1);
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 'x': case 'X':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1601 # if HAVE_LONG_LONG_INT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1602 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
1603 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1604 (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
1605 * 0.25 /* binary -> hexadecimal */
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 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1609 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1610 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
1611 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1612 (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
1613 * 0.25 /* binary -> hexadecimal */
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 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1617 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1618 (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
1619 * 0.25 /* binary -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1620 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1621 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1622 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1623 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1624 /* 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
1625 tmp_length = xsum (tmp_length, 2);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1626 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1627
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1628 case 'f': case 'F':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1629 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1630 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1631 (unsigned int) (LDBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1632 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1633 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1634 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1635 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1636 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1637 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1638 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1639 (unsigned int) (DBL_MAX_EXP
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1640 * 0.30103 /* binary -> decimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1641 * 2 /* estimate for FLAG_GROUP */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1642 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1643 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1644 + 10; /* sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1645 tmp_length = xsum (tmp_length, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1646 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1647
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1648 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
1649 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1650 12; /* sign, decimal point, exponent etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1651 tmp_length = xsum (tmp_length, precision);
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 'a': case 'A':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1655 if (type == TYPE_LONGDOUBLE)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1656 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1657 (unsigned int) (LDBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1658 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1659 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1660 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1661 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1662 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1663 (unsigned int) (DBL_DIG
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1664 * 0.831 /* decimal -> hexadecimal */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1665 )
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1666 + 1; /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1667 if (tmp_length < precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1668 tmp_length = precision;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1669 /* Account for sign, decimal point etc. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1670 tmp_length = xsum (tmp_length, 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1671 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1672
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1673 case 'c':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1674 # 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
1675 if (type == TYPE_WIDE_CHAR)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1676 tmp_length = MB_CUR_MAX;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1677 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1678 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1679 tmp_length = 1;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1680 break;
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 case 's':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1683 # if HAVE_WCHAR_T
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1684 if (type == TYPE_WIDE_STRING)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1685 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1686 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1687 /* 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
1688 "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
1689 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
1690 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
1691 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
1692
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1693 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1694 tmp_length = local_wcsnlen (arg, precision);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1695 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1696 tmp_length = local_wcslen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1697 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1698 /* 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
1699 "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
1700 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
1701 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
1702 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
1703 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
1704 array."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1705 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
1706 /* 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
1707 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1708 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1709 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1710 else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1711 # endif
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 # if WIDE_CHAR_VERSION
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1714 /* 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
1715 "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
1716 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
1717 null wide character."
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1718 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
1719 /* 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
1720 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1721 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1722 /* 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
1723 "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
1724 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
1725 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
1726 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
1727
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1728 if (has_precision)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1729 tmp_length = local_strnlen (arg, precision);
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 tmp_length = strlen (arg);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1732 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1733 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1734 break;
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 case 'p':
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1737 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1738 (unsigned int) (sizeof (void *) * CHAR_BIT
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1739 * 0.25 /* binary -> hexadecimal */
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 + 1 /* turn floor into ceil */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1742 + 2; /* account for leading 0x */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1743 break;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1744
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1745 default:
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1746 abort ();
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1747 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1748
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1749 if (!pad_ourselves)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1750 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1751 # if ENABLE_UNISTDIO
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1752 /* 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
1753 elements after padding may be
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1754 > max (tmp_length, width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1755 but is certainly
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1756 <= tmp_length + width. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1757 tmp_length = xsum (tmp_length, width);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1758 # else
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1759 /* 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
1760 if (tmp_length < width)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1761 tmp_length = width;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1762 # endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1763 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1764
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1765 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
1766
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1767 return tmp_length;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1768 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1769
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1770 #endif
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
1771
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1772 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
1773 VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1774 const FCHAR_T *format, va_list args)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1775 {
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1776 DIRECTIVES d;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1777 arguments a;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1778
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1779 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
1780 /* errno is already set. */
290aa46b4526 Fix out-of-memory handling of vasnprintf.
Bruno Haible <bruno@clisp.org>
parents: 9371
diff changeset
1781 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1782
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1783 #define CLEANUP() \
14260
2113f76f7b44 vasnprintf: Reduce use of malloc for small format strings.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
1784 if (d.dir != d.direct_alloc_dir) \
2113f76f7b44 vasnprintf: Reduce use of malloc for small format strings.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
1785 free (d.dir); \
2113f76f7b44 vasnprintf: Reduce use of malloc for small format strings.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
1786 if (a.arg != a.direct_alloc_arg) \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1787 free (a.arg);
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1788
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1789 if (PRINTF_FETCHARGS (args, &a) < 0)
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1790 {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1791 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1792 errno = EINVAL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1793 return NULL;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1794 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1795
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1796 {
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1797 size_t buf_neededlength;
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1798 TCHAR_T *buf;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1799 TCHAR_T *buf_malloced;
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1800 const FCHAR_T *cp;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1801 size_t i;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
1802 DIRECTIVE *dp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1803 /* Output string accumulator. */
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1804 DCHAR_T *result;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1805 size_t allocated;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1806 size_t length;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1807
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1808 /* 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
1809 sprintf or snprintf. */
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1810 buf_neededlength =
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1811 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
1812 #if HAVE_ALLOCA
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
1813 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1814 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1815 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
1816 buf_malloced = NULL;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1817 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1818 else
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1819 #endif
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1820 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1821 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
1822 if (size_overflow_p (buf_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1823 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1824 buf = (TCHAR_T *) malloc (buf_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1825 if (buf == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1826 goto out_of_memory_1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1827 buf_malloced = buf;
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1828 }
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
1829
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1830 if (resultbuf != NULL)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1831 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1832 result = resultbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1833 allocated = *lengthp;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1834 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1835 else
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1836 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1837 result = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1838 allocated = 0;
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 length = 0;
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1841 /* Invariants:
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1842 result is either == resultbuf or == NULL or malloc-allocated.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1843 If length > 0, then result != NULL. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1844
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
1845 /* 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
1846 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
1847 #define ENSURE_ALLOCATION(needed) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1848 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1849 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1850 size_t memory_size; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1851 DCHAR_T *memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1852 \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1853 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1854 if ((needed) > allocated) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1855 allocated = (needed); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1856 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1857 if (size_overflow_p (memory_size)) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1858 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1859 if (result == resultbuf || result == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1860 memory = (DCHAR_T *) malloc (memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1861 else \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1862 memory = (DCHAR_T *) realloc (result, memory_size); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1863 if (memory == NULL) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1864 goto out_of_memory; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1865 if (result == resultbuf && length > 0) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1866 DCHAR_CPY (memory, result, length); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1867 result = memory; \
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1868 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1869
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1870 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
1871 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1872 if (cp != dp->dir_start)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1873 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1874 size_t n = dp->dir_start - cp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1875 size_t augmented_length = xsum (length, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1876
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1877 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1878 /* 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
1879 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
1880 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
1881 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1882 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1883 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
1884 length = augmented_length;
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 else
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 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1889 result[length++] = (unsigned char) *cp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1890 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1891 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1892 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1893 if (i == d.count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1894 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1895
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1896 /* Execute a single directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1897 if (dp->conversion == '%')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1898 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1899 size_t augmented_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1900
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1901 if (!(dp->arg_index == ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1902 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1903 augmented_length = xsum (length, 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1904 ENSURE_ALLOCATION (augmented_length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1905 result[length] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1906 length = augmented_length;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1909 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1910 if (!(dp->arg_index != ARG_NONE))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1911 abort ();
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 if (dp->conversion == 'n')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1914 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1915 switch (a.arg[dp->arg_index].type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1916 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1917 case TYPE_COUNT_SCHAR_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1918 *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
1919 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1920 case TYPE_COUNT_SHORT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1921 *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
1922 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1923 case TYPE_COUNT_INT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1924 *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
1925 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1926 case TYPE_COUNT_LONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1927 *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
1928 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
1929 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1930 case TYPE_COUNT_LONGLONGINT_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1931 *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
1932 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1933 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1934 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1935 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1936 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1937 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
1938 #if ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1939 /* The unistdio extensions. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1940 else if (dp->conversion == 'U')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1941 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1942 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
1943 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1944 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1945 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1946 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1947 size_t precision;
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 = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1950 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1951 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1952 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1953 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1954 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1955 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1956
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1957 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
1958 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1959 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
1960 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1961 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1962 /* "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
1963 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1964 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1965 width = (unsigned int) (-arg);
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1968 width = arg;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1971 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1972 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1973
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1974 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1975 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1976 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1977 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1978 has_width = 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 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1982 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1983 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1984 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1985 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1986 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1987 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1988
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1989 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
1990 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1991 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
1992 /* "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
1993 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1994 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1995 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1996 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1997 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1998 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
1999 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2000 else
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 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2003
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2004 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2005 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2006 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2007 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2008 }
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2011 switch (type)
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 case TYPE_U8_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2014 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2015 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
2016 const uint8_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2017 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2018
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2019 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2020 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2021 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2022 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2023 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2024 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2025 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2026 int count = u8_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2027 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2028 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2029 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2030 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2031 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2032 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2033 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2034 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2035 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2036 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2037 return NULL;
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 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2040 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2041 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2042 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2043 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2044 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2045 /* 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
2046 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2047 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2048 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2049 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2050 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2051 int count = u8_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2052 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2053 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2054 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2055 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2056 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2057 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2058 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2059 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2060 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2061 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2062 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2063 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2064 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2065 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2066 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2067 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2068 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2069 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2070 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2071 arg_end = arg + u8_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2072 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2073 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2074 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2075
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2076 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2077 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2078 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2079 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2080 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2081 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2082 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2083 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2084
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2085 # if DCHAR_IS_UINT8_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2086 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2087 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2088 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2089 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2090 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2091 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2092 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2093 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2094 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2095 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
2096 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2097 /* Convert from UTF-8 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2098 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2099 u8_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2100 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2101 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2102 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
2103 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2104 /* 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
2105 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2106 U8_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2107 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2108 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2109 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2110 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2111 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2112 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2113 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2114 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2115 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2116 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2117 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2118 return NULL;
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 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2121 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2122 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2123 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2124 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2125 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2126 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2127 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2128 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2129
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2130 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2131 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2132 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2133 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2134 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2135 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2136 length += n;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2139 break;
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 case TYPE_U16_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2142 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2143 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
2144 const uint16_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2145 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2146
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2147 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2148 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2149 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2150 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2151 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2152 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2153 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2154 int count = u16_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2155 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2156 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2157 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2158 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2159 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2160 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2161 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2162 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2163 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2164 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2165 return NULL;
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 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2168 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2169 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2170 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2171 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2172 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2173 /* 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
2174 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2175 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2176 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2177 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2178 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2179 int count = u16_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2180 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2181 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2182 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2183 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2184 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2185 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2186 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2187 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2188 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2189 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2190 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2191 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2192 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2193 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2194 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2195 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2196 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2197 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2198 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2199 arg_end = arg + u16_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2200 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2201 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2202 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2203
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2204 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2205 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2206 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2207 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2208 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2209 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2210 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2211 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2212
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2213 # if DCHAR_IS_UINT16_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2214 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2215 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2216 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2217 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2218 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2219 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2220 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2221 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2222 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2223 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
2224 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2225 /* Convert from UTF-16 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2226 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2227 u16_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2228 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2229 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2230 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
2231 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2232 /* 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
2233 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2234 U16_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2235 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2236 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2237 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2238 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2239 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2240 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2241 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2242 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2243 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2244 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2245 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2246 return NULL;
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 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2249 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2250 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2251 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2252 free (converted);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2253 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2254 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2255 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2256 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2257
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2258 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2259 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2260 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2261 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2262 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2263 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2264 length += n;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2267 break;
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 case TYPE_U32_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2270 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2271 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
2272 const uint32_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2273 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2274
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2275 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2276 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2277 /* Use only PRECISION characters, from the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2278 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2279 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2280 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2281 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2282 int count = u32_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2283 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2284 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2285 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2286 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2287 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2288 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2289 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2290 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2291 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2292 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2293 return NULL;
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 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2296 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2297 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2298 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2299 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2300 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2301 /* 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
2302 characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2303 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2304 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2305 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2306 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2307 int count = u32_strmblen (arg_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2308 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2309 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2310 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2311 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2312 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2313 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2314 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2315 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2316 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2317 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2318 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2319 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2320 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2321 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2322 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2323 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2324 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2325 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2326 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2327 arg_end = arg + u32_strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2328 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2329 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2330 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2331
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2332 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2333 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2334 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2335 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2336 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2337 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2338 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2339 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2340
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2341 # if DCHAR_IS_UINT32_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2342 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2343 size_t n = arg_end - arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2344 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2345 DCHAR_CPY (result + length, arg, n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2346 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2347 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2348 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2349 { /* Convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2350 DCHAR_T *converted = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2351 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
2352 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2353 /* Convert from UTF-32 to locale encoding. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2354 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2355 u32_conv_to_encoding (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2356 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2357 arg, arg_end - arg, NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2358 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
2359 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2360 /* 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
2361 converted =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2362 U32_TO_DCHAR (arg, arg_end - arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2363 converted, &converted_len);
11509
ef4e896a3b4f Simplify calling convention of u*_conv_to_encoding.
Bruno Haible <bruno@clisp.org>
parents: 11422
diff changeset
2364 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2365 if (converted == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2366 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2367 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2368 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2369 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2370 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2371 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2372 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2373 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2374 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2375 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2376 if (converted != result + length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2377 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2378 ENSURE_ALLOCATION (xsum (length, converted_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2379 DCHAR_CPY (result + length, converted, converted_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2380 free (converted);
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 length += converted_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2383 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2384 # endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2385
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2386 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2387 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2388 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2389 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2390 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2391 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2392 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2393 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2394 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2395 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2396
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2397 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2398 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2399 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2400 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
2401 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
2402 #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
2403 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
2404 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2405 && 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
2406 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2407 && 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
2408 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2409 )
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 /* 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
2412 allocating a temporary buffer. The determination of its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2413 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
2414 specified, below requires a conversion between a char[]
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2415 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
2416 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
2417 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
2418 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
2419 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
2420 directive ourselves. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2421 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2422 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2423 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2424 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2425 size_t precision;
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 = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2428 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2429 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2430 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2431 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2432 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2433 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2434
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2435 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
2436 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2437 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
2438 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2439 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2440 /* "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
2441 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2442 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2443 width = (unsigned int) (-arg);
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2446 width = arg;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2449 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2450 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2451
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2452 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2453 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2454 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2455 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2456 has_width = 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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2459 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2460 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2461 if (dp->precision_start != dp->precision_end)
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 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2464 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2465 int arg;
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 (!(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
2468 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2469 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
2470 /* "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
2471 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2472 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2473 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2474 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2475 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2476 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2477 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2478 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2479 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2480 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2481
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2482 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2483 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2484 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2485 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2486 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2487 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2488
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2489 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2490 /* %s in vasnwprintf. See the specification of fwprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2491 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2492 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
2493 const char *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2494 size_t characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2495
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2496 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2497 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2498 /* 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
2499 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
2500 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2501 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2502 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
2503 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2504 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2505 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2506 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2507 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2508 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2509 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2510 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
2511 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2512 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
2513 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2514 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2515 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2516 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2517 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2518 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2519 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2520 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2521 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2522 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2523 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2524 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2525 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2526 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2527 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2528 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2529 characters++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2530 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2531 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2532 else if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2533 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2534 /* 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
2535 characters. */
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2536 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2537 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2538 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
2539 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2540 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2541 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2542 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2543 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2544 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2545 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2546 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
2547 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2548 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
2549 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2550 if (count == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2551 /* Found the terminating NUL. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2552 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2553 if (count < 0)
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 /* Invalid or incomplete multibyte character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2556 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2557 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2558 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2559 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2560 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2561 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2562 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2563 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2564 arg_end += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2565 characters++;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2568 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2569 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2570 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2571 arg_end = arg + strlen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2572 /* The number of characters doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2573 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2574 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2575
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2576 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2577 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2578 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2579 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2580 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2581 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2582 length += n;
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2585 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2586 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2587 /* 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
2588 size_t remaining;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2589 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2590 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2591 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
2592 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2593 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2594 for (remaining = characters; remaining > 0; remaining--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2595 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2596 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2597 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2598 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2599 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
2600 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2601 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
2602 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2603 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2604 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2605 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2606 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2607 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2608 arg += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2609 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2610 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2611 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2612 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2613 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2614 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2615 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2616 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2617 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
2618 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2619 while (arg < arg_end)
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 wchar_t wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2622 int count;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2623 # if HAVE_MBRTOWC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2624 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
2625 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2626 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
2627 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2628 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2629 /* mbrtowc not consistent with mbrlen, or mbtowc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2630 not consistent with mblen. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2631 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2632 ENSURE_ALLOCATION (xsum (length, 1));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2633 result[length++] = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2634 arg += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2635 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2636 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2637
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2638 if (has_width && width > characters
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2639 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2640 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2641 size_t n = width - characters;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2642 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2643 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2644 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2645 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2646 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2647 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2648 /* %ls in vasnprintf. See the specification of fprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2649 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2650 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
2651 const wchar_t *arg_end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2652 size_t characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2653 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2654 /* 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
2655 verify (sizeof (TCHAR_T) == 1);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2656 TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2657 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2658 size_t tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2659 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2660 size_t w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2661
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2662 if (has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2663 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2664 /* 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
2665 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
2666 # 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
2667 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2668 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
2669 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2670 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2671 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2672 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2673 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2674 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2675 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2676
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2677 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2678 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2679 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2680 # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2681 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
2682 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2683 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
2684 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2685 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2686 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2687 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2688 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2689 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2690 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2691 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2692 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2693 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2694 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2695 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2696 if (precision < count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2697 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2698 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2699 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2700 precision -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2701 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2702 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2703 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2704 else if (has_width)
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2705 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2706 else
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2707 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2708 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2709 /* 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
2710 bytes. */
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2711 # 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
2712 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2713 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
2714 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2715 arg_end = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2716 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2717 for (;;)
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 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2720 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2721
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2722 if (*arg_end == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2723 /* Found the terminating null wide character. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2724 break;
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2725 # 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
2726 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
2727 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2728 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
2729 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2730 if (count < 0)
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 /* Cannot convert. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2733 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2734 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2735 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2736 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2737 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2738 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2739 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2740 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2741 arg_end++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2742 characters += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2743 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2744 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2745 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2746 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2747 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2748 /* Use the entire string. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2749 arg_end = arg + local_wcslen (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2750 /* The number of bytes doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2751 characters = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2752 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2753 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2754
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2755 # if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2756 /* 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
2757 tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2758 if (tmpsrc == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2759 goto out_of_memory;
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 TCHAR_T *tmpptr = tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2762 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
2763 # 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
2764 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2765 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
2766 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2767 for (remaining = characters; remaining > 0; )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2768 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2769 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2770 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2771
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2772 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2773 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2774 # 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
2775 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
2776 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2777 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2778 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2779 if (count <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2780 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2781 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2782 memcpy (tmpptr, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2783 tmpptr += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2784 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2785 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2786 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2787 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2788 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2789 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2790
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2791 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2792 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2793 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2794 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2795 tmpsrc, characters,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2796 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2797 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2798 if (tmpdst == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2799 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2800 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2801 free (tmpsrc);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2802 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2803 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2804 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2805 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2806 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2807 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2808 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2809 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2810 free (tmpsrc);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2811 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2812
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2813 if (has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2814 {
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2815 # if ENABLE_UNISTDIO
16358
a712776b11ce maint: spelling fixes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16214
diff changeset
2816 /* Outside POSIX, it's preferable to compare the width
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2817 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2818 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2819 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
2820 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2821 /* 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
2822 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2823 w = characters;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2824 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2825 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2826 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2827 /* w doesn't matter. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2828 w = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2829
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2830 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2831 && !(dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2832 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2833 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2834 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2835 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2836 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2837 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2838
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2839 # if DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2840 if (has_precision || has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2841 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2842 /* We know the number of bytes in advance. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2843 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
2844 # 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
2845 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2846 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
2847 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2848 ENSURE_ALLOCATION (xsum (length, characters));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2849 for (remaining = characters; remaining > 0; )
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)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2862 /* Inconsistency. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2863 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2864 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2865 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2866 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2867 remaining -= count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2868 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2869 if (!(arg == arg_end))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2870 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2871 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2872 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2873 {
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2874 # 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
2875 mbstate_t state;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2876 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
2877 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2878 while (arg < arg_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2879 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2880 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2881 int count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2882
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2883 if (*arg == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2884 abort ();
12552
adc711ccbbd5 vasnprintf: Avoid passing an 'rpl_mbstate_t *' to the system's wcrtomb.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
2885 # 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
2886 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
2887 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2888 count = wctomb (cbuf, *arg);
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2889 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2890 if (count <= 0)
13198
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2891 {
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2892 /* Cannot convert. */
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2893 if (!(result == resultbuf || result == NULL))
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2894 free (result);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2895 if (buf_malloced != NULL)
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2896 free (buf_malloced);
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2897 CLEANUP ();
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2898 errno = EILSEQ;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2899 return NULL;
2f3ddf3c58a5 vasnprintf: Fix crash in %ls directive.
Bruno Haible <bruno@clisp.org>
parents: 13197
diff changeset
2900 }
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2901 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2902 memcpy (result + length, cbuf, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2903 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2904 arg++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2905 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2906 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2907 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2908 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2909 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2910 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2911 length += tmpdst_len;
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2912 # endif
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2913
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2914 if (has_width && width > w
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2915 && (dp->flags & FLAG_LEFT))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2916 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2917 size_t n = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2918 ENSURE_ALLOCATION (xsum (length, n));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2919 DCHAR_SET (result + length, ' ', n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2920 length += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2921 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2922 }
13323
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
2923 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2924 }
11207
4daf474e9033 vasnprintf: Fix invalid read past end of memory block.
Bruno Haible <bruno@clisp.org>
parents: 10462
diff changeset
2925 #endif
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2926 #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
2927 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
2928 # 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
2929 && (0
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2930 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2931 || 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
2932 # endif
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2933 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2934 || 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
2935 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2936 )
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
2937 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2938 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2939 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2940 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
2941 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2942 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2943 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2944 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2945 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2946 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2947 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2948 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2949 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2950 DCHAR_T *p;
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 = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2953 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2954 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2955 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2956 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2957 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2958 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2959
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2960 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
2961 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2962 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
2963 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2964 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2965 /* "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
2966 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2967 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2968 width = (unsigned int) (-arg);
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2971 width = arg;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2974 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2975 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2976
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2977 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2978 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2979 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2980 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2981 has_width = 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 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2985 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2986 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2987 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2988 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2989 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2990 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2991
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2992 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
2993 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2994 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
2995 /* "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
2996 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2997 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2998 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
2999 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3000 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3001 }
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3004 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3005 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3006
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3007 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3008 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3009 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3010 has_precision = 1;
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 }
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 /* Allocate a temporary buffer of sufficient size. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3015 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3016 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3017 (unsigned int) ((LDBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3018 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3019 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3020 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3021 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3022 tmp_length =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3023 (unsigned int) ((DBL_DIG + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3024 * 0.831 /* decimal -> hexadecimal */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3025 )
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3026 + 1; /* turn floor into ceil */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3027 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3028 tmp_length = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3029 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3030 tmp_length = xsum (tmp_length, 12);
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 (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3033 tmp_length = width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3034
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3035 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
3036
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3037 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3038 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3039 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3040 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3041 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
3042
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3043 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3044 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3045 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3046 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3047 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3048 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3049 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3050 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3051
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3052 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3053 p = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3054 if (type == TYPE_LONGDOUBLE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3055 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3056 # 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
3057 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
3058
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3059 if (isnanl (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3060 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3061 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3062 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3063 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3064 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3065 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3066 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3067 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3068 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3069 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3070 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3071 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3072 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3073 DECL_LONG_DOUBLE_ROUNDING
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3074
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3075 BEGIN_LONG_DOUBLE_ROUNDING ();
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 (signbit (arg)) /* arg < 0.0L or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3078 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3079 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3080 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3081 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3082
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3083 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3084 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3085 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3086 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3087 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3088 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3089
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3090 if (arg > 0.0L && arg + arg == arg)
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 if (dp->conversion == 'A')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3095 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3096 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3099 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3100 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3101 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3102 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3103 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3104 long double mantissa;
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 (arg > 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3107 mantissa = printf_frexpl (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3108 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3109 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3110 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3111 mantissa = 0.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3112 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3113
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3114 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3115 && 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
3116 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3117 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3118 long double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3119 size_t q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3120
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3121 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3122 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3123 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3124 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3125 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3126 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3127 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
3128 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3129 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3130 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3131 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3132 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3133 tail *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3134 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3135 if (tail != 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3136 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3137 tail *= 0.0625L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3138 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3139 }
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 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3143 pad_ptr = p;
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 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3146
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3147 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3148 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3149 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3150 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3151 || mantissa > 0.0L || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3154 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3155 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3156 while (mantissa > 0.0L)
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 mantissa *= 16.0L;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3159 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3160 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3161 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3162 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3163 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3164 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3165 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3166 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3167 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3168 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3169 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3170 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3171 precision--;
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3174 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3175 *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
3176 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3177 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3178 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3179 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3180 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3181 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3182 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3183 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3184 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3185 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3186 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3187 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3188 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3189 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3190 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3191 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3192 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3193 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3194 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3195 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3196 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3197 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3198 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3199 # endif
12421
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3202 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3203 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3204 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3205 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3206 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3207 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3208 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3209 {
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3210 # 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
3211 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
3212
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3213 if (isnand (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3214 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3215 if (dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3216 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3217 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3218 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3219 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3220 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3221 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3224 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3225 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3226 int sign = 0;
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 (signbit (arg)) /* arg < 0.0 or negative zero */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3229 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3230 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3231 arg = -arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3232 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3233
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3234 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3235 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3236 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3237 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3238 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3239 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3240
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3241 if (arg > 0.0 && arg + arg == arg)
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 if (dp->conversion == 'A')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3246 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3247 else
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 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3250 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3251 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3252 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3253 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3254 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3255 double mantissa;
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 (arg > 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3258 mantissa = printf_frexp (arg, &exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3259 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3260 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3261 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3262 mantissa = 0.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3263 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3264
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3265 if (has_precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3266 && 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
3267 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3268 /* Round the mantissa. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3269 double tail = mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3270 size_t q;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3271
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3272 for (q = precision; ; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3273 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3274 int digit = (int) tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3275 tail -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3276 if (q == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3277 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3278 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
3279 tail = 1 - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3280 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3281 tail = - tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3282 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3283 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3284 tail *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3285 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3286 if (tail != 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3287 for (q = precision; q > 0; q--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3288 tail *= 0.0625;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3289 mantissa += tail;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3290 }
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 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3294 pad_ptr = p;
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 int digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3297
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3298 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3299 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3300 *p++ = '0' + digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3301 if ((flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3302 || mantissa > 0.0 || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3305 /* This loop terminates because we assume
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3306 that FLT_RADIX is a power of 2. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3307 while (mantissa > 0.0)
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 mantissa *= 16.0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3310 digit = (int) mantissa;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3311 mantissa -= digit;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3312 *p++ = digit
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3313 + (digit < 10
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3314 ? '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3315 : dp->conversion - 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3316 if (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3317 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3318 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3319 while (precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3320 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3321 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3322 precision--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3323 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3324 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3325 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3326 *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
3327 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3328 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3329 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3330 { '%', '+', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3331 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3332 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3333 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3334 p++;
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3335 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3336 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3337 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3338 sprintf ((char *) p, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3339 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3340 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3341 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3342 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3343 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3344 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3345 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3346 sprintf (expbuf, "%+d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3347 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3348 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3349 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3350 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3351 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3352 }
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3353 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3354 abort ();
9454
265fad13afeb Fix the processing of finite numbers with the 'a' and 'A' directives.
Bruno Haible <bruno@clisp.org>
parents: 9450
diff changeset
3355 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3356 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3357 /* 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
3358 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3359 if (has_width && p - tmp < width)
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 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3362 DCHAR_T *end = p + pad;
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 if (flags & FLAG_LEFT)
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 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3367 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3368 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3369 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3370 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3371 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3372 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3373 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3374
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3375 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3376 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3377 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3378 *p++ = '0';
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3381 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3382 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3383 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3384
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3385 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3386 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3387 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3388 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3389 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3390
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3391 p = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3392 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3393
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3394 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3395 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3396
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3397 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3398 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3399 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3400 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3401
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3402 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3403 if (count >= allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3404 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3405 size_t n = xsum (length, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3406
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3407 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3408 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3409
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3410 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3411 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3412 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3413 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3414 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3415 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3416 }
8916
353e81af4858 Do the extra handling of NaN and Inf also the %a / %A.
Bruno Haible <bruno@clisp.org>
parents: 8915
diff changeset
3417 #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
3418 #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
3419 else if ((dp->conversion == 'f' || dp->conversion == 'F'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3420 || dp->conversion == 'e' || dp->conversion == 'E'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3421 || dp->conversion == 'g' || dp->conversion == 'G'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3422 || dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3423 && (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
3424 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3425 || 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
3426 # elif NEED_PRINTF_INFINITE_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3427 || (a.arg[dp->arg_index].type == TYPE_DOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3428 /* The systems (mingw) which produce wrong output
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3429 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
3430 Therefore we treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3431 && 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
3432 # endif
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3433 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3434 || 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
3435 # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3436 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3437 /* Some systems produce wrong output for Inf,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3438 -Inf, and NaN. Some systems in this category
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3439 (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
3440 treat this case here as well. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3441 && 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
3442 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3443 ))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3444 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3445 # 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
3446 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
3447 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3448 int flags = dp->flags;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3449 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3450 size_t width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3451 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3452 size_t precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3453 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3454 DCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3455 DCHAR_T *tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3456 DCHAR_T *pad_ptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3457 DCHAR_T *p;
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 = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3460 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3461 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3462 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3463 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3464 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3465 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3466
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3467 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
3468 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3469 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
3470 if (arg < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3471 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3472 /* "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
3473 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3474 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3475 width = (unsigned int) (-arg);
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3478 width = arg;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3481 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3482 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3483
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3484 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3485 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3486 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3487 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3488 has_width = 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 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3492 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3493 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3494 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3495 if (dp->precision_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3496 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3497 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3498
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3499 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
3500 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3501 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
3502 /* "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
3503 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3504 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3505 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3506 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3507 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3508 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3509 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3510 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3511 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3512 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3513
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3514 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3515 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3516 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3517 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3518 }
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3521 /* 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
3522 %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
3523 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
3524 the default precision is 0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3525 if (!has_precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3526 if (!(dp->conversion == 'a' || dp->conversion == 'A'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3527 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3528
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3529 /* 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
3530 # 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
3531 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
3532 # 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
3533 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
3534 # elif NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3535 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
3536 # elif NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3537 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
3538 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3539 tmp_length = 0;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3540 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3541 if (tmp_length < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3542 tmp_length = precision;
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3543 # 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
3544 # 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
3545 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
3546 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3547 if (dp->conversion == 'f' || dp->conversion == 'F')
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 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
3550 if (!(isnanl (arg) || arg + arg == arg))
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 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3553 int exponent = floorlog10l (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3554 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3555 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3556 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3557 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
3558 # 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
3559 # 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
3560 # 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
3561 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
3562 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3563 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3564 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3565 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
3566 if (!(isnand (arg) || arg + arg == arg))
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 /* arg is finite and nonzero. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3569 int exponent = floorlog10 (arg < 0 ? -arg : arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3570 if (exponent >= 0 && tmp_length < exponent + precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3571 tmp_length = exponent + precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3572 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3573 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
3574 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3575 /* Account for sign, decimal point etc. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3576 tmp_length = xsum (tmp_length, 12);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3577
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3578 if (tmp_length < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3579 tmp_length = width;
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 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
3582
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3583 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3584 tmp = tmpbuf;
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 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
3588
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3589 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3590 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3591 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3592 tmp = (DCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3593 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3594 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3595 goto out_of_memory;
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3598 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3599 p = tmp;
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
3600
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3601 # 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
3602 # 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
3603 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
3604 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3605 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3606 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
3607
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3608 if (isnanl (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3609 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3610 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3611 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3612 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3613 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3614 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3615 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3616 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3617 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3618 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3619 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3620 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3621 int sign = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3622 DECL_LONG_DOUBLE_ROUNDING
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3623
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3624 BEGIN_LONG_DOUBLE_ROUNDING ();
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 (signbit (arg)) /* arg < 0.0L or negative zero */
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 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3629 arg = -arg;
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3632 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3633 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3634 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3635 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3636 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3637 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3638
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3639 if (arg > 0.0L && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3640 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3641 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3644 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3645 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3646 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3647 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3648 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3649 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3650 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3651 {
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
3652 # if NEED_PRINTF_LONG_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3653 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3654
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3655 if (dp->conversion == 'f' || dp->conversion == 'F')
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 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3658 size_t 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 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3661 scale10_round_decimal_long_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3662 if (digits == NULL)
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 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3665 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3666 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3667 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3668
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3669 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3670 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3671 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3672 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3673 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3674 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3675 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3676 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3677 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3678 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3679 if ((flags & FLAG_ALT) || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3682 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3683 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3684 while (ndigits > 0)
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 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3687 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3688 }
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3691 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3692 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3693 else if (dp->conversion == 'e' || dp->conversion == 'E')
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 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3696
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3697 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3698 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3699 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3700 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3701 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3702 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3703 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3704 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3705 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3706 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3707 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3708 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3709 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3710 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3711 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3712 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3713 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3714
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3715 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3716 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3717 for (;;)
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 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3720 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3721 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3722 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3723 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3724 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3725 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3726 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3727 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3728
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3729 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3730 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3731 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3732 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3733 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3734 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3735 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3736 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3737 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3738 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3739 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3740 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3741 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3742 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3743 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3744 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3745 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3746 adjusted = 1;
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 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3749 if (is_borderline (digits, precision))
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 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3752 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3753 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3754 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3755 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3756 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3757 if (digits2 == NULL)
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 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3760 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3761 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3762 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3763 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3764 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3765 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3766 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3767 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3768 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3769 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3770 free (digits2);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3771 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3772 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3773
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3774 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3775 if ((flags & FLAG_ALT) || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3778 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3779 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3780 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3781 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3782 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3783 }
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 free (digits);
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3788 *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
3789 # if WIDE_CHAR_VERSION
12421
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 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3792 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3793 SNPRINTF (p, 6 + 1, decimal_format, exponent);
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 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3796 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
3797 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3798 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3799 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3800 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3801 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3802 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3803 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3804 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3805 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3806 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3807 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3808 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3809 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3810 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3811 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3812 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3813 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3814 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3815 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3816 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3817 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3818 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3819
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3820 if (arg == 0.0L)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3821 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3822 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3823 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3824 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3825 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3826 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3827 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3828 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3829
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3830 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3831 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3832 if ((flags & FLAG_ALT) || ndigits > nzeroes)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3835 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3836 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3837 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3838 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3839 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3840 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3841 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3842 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3843 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3844 /* arg > 0.0L. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3845 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3846 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3847 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3848 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3849 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3850
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3851 exponent = floorlog10l (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3852 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3853 for (;;)
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 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3856 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3857 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3858 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3859 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3860 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3861 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3862 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3863 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3864
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3865 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3866 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3867 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3868 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3869 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3870 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3871 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3872 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3873 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3874 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3875 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3876 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3877 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3878 if (ndigits < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3879 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3880 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3881 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3882 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3883 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3884 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3885 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3886 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3887 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3888 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3889 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3890 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3891 scale10_round_decimal_long_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3892 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3893 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3894 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3895 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3896 END_LONG_DOUBLE_ROUNDING ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3897 goto out_of_memory;
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 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3900 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3901 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3902 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3903 exponent -= 1;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3906 free (digits2);
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 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3909
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3910 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3911 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3912 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3913 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3914 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3915 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3916 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3917
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3918 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3919 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3920 && exponent < (long)precision)
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 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3923 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3924 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3925 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3926 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3927 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3928 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3929 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3930 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3931 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3932 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3933 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3934 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3935 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3936 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3937 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3938 *p++ = digits[ndigits];
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3941 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3942 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3943 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3944 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3945 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3946 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3947 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3948 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3949 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3950 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3951 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3952 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3953 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3954 }
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 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3959 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3960 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3961 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3962 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3963 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3964 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3965 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3966 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3967 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3968 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3969 *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
3970 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3971 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3972 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3973 { '%', '+', '.', '2', 'd', '\0' };
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3974 SNPRINTF (p, 6 + 1, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3975 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3976 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3977 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
3978 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3979 if (sizeof (DCHAR_T) == 1)
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 sprintf ((char *) p, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3982 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3983 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3984 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3985 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3986 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3987 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3988 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3989 sprintf (expbuf, "%+.2d", exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3990 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3991 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3992 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
3993 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3994 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3995
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3996 free (digits);
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
3999 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4000 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
4001 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4002 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4003 if (!(arg == 0.0L))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4004 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4005
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4006 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4009 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4010 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4011 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4012 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4013 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4014 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4015 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4016 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4017 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4018 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4019 {
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; /* 'e' or 'E' */
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 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4031 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4032 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4033 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4034 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4035 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4036 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4037 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4038 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4039 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4040 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4041 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4042 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4043 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4044 else if (dp->conversion == 'a' || dp->conversion == 'A')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4045 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4046 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4047 *p++ = dp->conversion - 'A' + 'X';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4048 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4049 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4050 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4051 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4052 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4053 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4054 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4055 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4056 *p++ = dp->conversion - 'A' + 'P';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4057 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4058 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4059 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4060 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4061 abort ();
8860
771a56a41b5b Fix *printf result for NaN, Inf on AIX, Solaris, OSF/1.
Bruno Haible <bruno@clisp.org>
parents: 8846
diff changeset
4062 # endif
12421
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4065 END_LONG_DOUBLE_ROUNDING ();
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 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4068 # 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
4069 else
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4070 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4071 # 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
4072 # 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
4073 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4074 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
4075
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4076 if (isnand (arg))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4077 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4078 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4079 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4080 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4081 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4082 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4083 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4084 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4087 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4088 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4089 int sign = 0;
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 (signbit (arg)) /* arg < 0.0 or negative zero */
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 sign = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4094 arg = -arg;
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4097 if (sign < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4098 *p++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4099 else if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4100 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4101 else if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4102 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4103
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4104 if (arg > 0.0 && arg + arg == arg)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4105 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4106 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
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 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4109 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4110 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4111 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4112 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4115 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4116 {
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4117 # if NEED_PRINTF_DOUBLE
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4118 pad_ptr = p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4119
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4120 if (dp->conversion == 'f' || dp->conversion == 'F')
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 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4123 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4124
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4125 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4126 scale10_round_decimal_double (arg, precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4127 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4128 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4129 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4130
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4131 if (ndigits > precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4132 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4133 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4134 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4135 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4136 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4137 while (ndigits > precision);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4138 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4139 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4140 /* Here ndigits <= precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4141 if ((flags & FLAG_ALT) || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4144 for (; precision > ndigits; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4145 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4146 while (ndigits > 0)
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 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4149 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4150 }
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
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4153 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4154 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4155 else if (dp->conversion == 'e' || dp->conversion == 'E')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4156 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4157 int exponent;
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 (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4160 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4161 exponent = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4162 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4163 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4164 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4165 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4166 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4167 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4168 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4169 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4170 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4171 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4172 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4173 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4174 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4175 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4176
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4177 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4178 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4179 for (;;)
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 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4182 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4183 (int)precision - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4184 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4185 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4186 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4187
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4188 if (ndigits == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4189 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4190 if (ndigits < precision
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4191 || ndigits > precision + 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4192 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4193 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4194 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4195 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4196 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4197 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4198 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4199 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4200 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4201 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4202 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4203 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4204 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4205 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4206 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4207 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4208 if (is_borderline (digits, precision))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4209 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4210 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4211 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4212 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4213 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4214 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4215 (int)precision - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4216 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4217 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4218 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4219 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4220 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4221 if (strlen (digits2) == precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4222 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4223 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4224 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4225 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4226 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4227 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4228 free (digits2);
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 /* Here ndigits = precision+1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4231
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4232 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4233 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4234 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4235 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4236 while (ndigits > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4237 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4238 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4239 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4240 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4241 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4242
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4243 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4244 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4245
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4246 *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
4247 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4248 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4249 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4250 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4251 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
4252 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4253 { '%', '+', '.', '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
4254 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4255 { '%', '+', '.', '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
4256 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4257 SNPRINTF (p, 6 + 1, decimal_format, exponent);
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 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4260 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
4261 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4262 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4263 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4264 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4265 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
4266 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4267 "%+.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
4268 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4269 "%+.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
4270 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4271 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4272 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4273 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4274 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4275 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4276 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4277 else
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 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4280 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4281 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4282 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4283 p++;
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 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4286 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4287 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4288 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4289 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4290 if (precision == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4291 precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4292 /* precision >= 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4293
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4294 if (arg == 0.0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4295 /* The exponent is 0, >= -4, < precision.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4296 Use fixed-point notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4297 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4298 size_t ndigits = precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4299 /* Number of trailing zeroes that have to be
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4300 dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4301 size_t nzeroes =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4302 (flags & FLAG_ALT ? 0 : precision - 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4303
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4304 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4305 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4306 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4307 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4308 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4309 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4310 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4311 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4312 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4313 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4314 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4315 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4316 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4317 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4318 /* arg > 0.0. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4319 int exponent;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4320 int adjusted;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4321 char *digits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4322 size_t ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4323 size_t nzeroes;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4324
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4325 exponent = floorlog10 (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4326 adjusted = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4327 for (;;)
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 digits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4330 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4331 (int)(precision - 1) - exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4332 if (digits == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4333 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4334 ndigits = strlen (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4335
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4336 if (ndigits == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4337 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4338 if (ndigits < precision - 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4339 || ndigits > precision + 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4340 /* The exponent was not guessed
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4341 precisely enough. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4342 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4343 if (adjusted)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4344 /* None of two values of exponent is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4345 the right one. Prevent an endless
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4346 loop. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4347 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4348 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4349 if (ndigits < precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4350 exponent -= 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4351 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4352 exponent += 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4353 adjusted = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4354 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4355 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4356 if (is_borderline (digits, precision - 1))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4357 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4358 /* Maybe the exponent guess was too high
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4359 and a smaller exponent can be reached
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4360 by turning a 10...0 into 9...9x. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4361 char *digits2 =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4362 scale10_round_decimal_double (arg,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4363 (int)(precision - 1) - exponent + 1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4364 if (digits2 == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4365 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4366 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4367 goto out_of_memory;
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 if (strlen (digits2) == precision)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4370 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4371 free (digits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4372 digits = digits2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4373 exponent -= 1;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4376 free (digits2);
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 /* Here ndigits = precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4379
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4380 /* Determine the number of trailing zeroes
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4381 that have to be dropped. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4382 nzeroes = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4383 if ((flags & FLAG_ALT) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4384 while (nzeroes < ndigits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4385 && digits[nzeroes] == '0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4386 nzeroes++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4387
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4388 /* The exponent is now determined. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4389 if (exponent >= -4
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4390 && exponent < (long)precision)
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 /* Fixed-point notation:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4393 max(exponent,0)+1 digits, then the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4394 decimal point, then the remaining
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4395 digits without trailing zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4396 if (exponent >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4397 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4398 size_t count = exponent + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4399 /* Note: count <= precision = ndigits. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4400 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4401 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4402 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4403 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4404 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4405 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4406 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4407 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4408 *p++ = digits[ndigits];
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4411 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4412 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4413 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4414 size_t count = -exponent - 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4415 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4416 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4417 for (; count > 0; count--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4418 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4419 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4420 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4421 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4422 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4423 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4424 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4425 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4426 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4427 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4428 /* Exponential notation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4429 *p++ = digits[--ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4430 if ((flags & FLAG_ALT) || ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4431 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4432 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4433 while (ndigits > nzeroes)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4434 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4435 --ndigits;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4436 *p++ = digits[ndigits];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4437 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4438 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4439 *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
4440 # if WIDE_CHAR_VERSION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4441 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4442 static const wchar_t decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4443 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4444 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
4445 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4446 { '%', '+', '.', '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
4447 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4448 { '%', '+', '.', '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
4449 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4450 SNPRINTF (p, 6 + 1, decimal_format, exponent);
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 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4453 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
4454 # else
12421
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 static const char decimal_format[] =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4457 /* Produce the same number of exponent digits
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4458 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
4459 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4460 "%+.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
4461 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4462 "%+.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
4463 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4464 if (sizeof (DCHAR_T) == 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4465 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4466 sprintf ((char *) p, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4467 while (*p != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4468 p++;
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4471 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4472 char expbuf[6 + 1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4473 const char *ep;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4474 sprintf (expbuf, decimal_format, exponent);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4475 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4476 p++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4477 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4478 }
9442
ad275322f8f1 Fix *printf behaviour in out-of-memory situations on MacOS X and *BSD.
Bruno Haible <bruno@clisp.org>
parents: 9439
diff changeset
4479 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4480 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4481
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4482 free (digits);
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4485 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4486 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
4487 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4488 /* arg is finite. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4489 if (!(arg == 0.0))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4490 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4491
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4492 pad_ptr = p;
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 if (dp->conversion == 'f' || dp->conversion == 'F')
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) || precision > 0)
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 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4500 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4501 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4502 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4503 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4504 else if (dp->conversion == 'e' || dp->conversion == 'E')
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 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4507 if ((flags & FLAG_ALT) || precision > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4508 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4509 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4510 for (; precision > 0; precision--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4511 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4512 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4513 *p++ = dp->conversion; /* 'e' or 'E' */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4514 *p++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4515 /* Produce the same number of exponent digits as
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4516 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
4517 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4518 *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
4519 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4520 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4521 *p++ = '0';
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 else if (dp->conversion == 'g' || dp->conversion == 'G')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4524 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4525 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4526 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4527 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4528 size_t ndigits =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4529 (precision > 0 ? precision - 1 : 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4530 *p++ = decimal_point_char ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4531 for (; ndigits > 0; --ndigits)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4532 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4533 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4534 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4535 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4536 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
4537 # endif
12421
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 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4540 }
8840
792e05ae7ac8 Fix *printf result for NaN, Inf, -0.0 on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8838
diff changeset
4541 # endif
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4542
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4543 /* 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
4544 zero padding insertion point being at pad_ptr. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4545 if (has_width && p - tmp < width)
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 size_t pad = width - (p - tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4548 DCHAR_T *end = p + pad;
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 if (flags & FLAG_LEFT)
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 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4553 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4554 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4555 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4556 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4557 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4558 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4559 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4560
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4561 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4562 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4563 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4564 *p++ = '0';
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 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4567 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4568 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4569 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4570
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4571 while (p > tmp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4572 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4573 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4574 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4575 }
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 p = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4578 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4579
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4580 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4581 size_t count = p - tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4582
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4583 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4584 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4585 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4586 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4587
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4588 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4589 if (count >= allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4590 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4591 size_t n = xsum (length, count);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4592
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4593 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4594 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4595
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4596 /* Append the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4597 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4598 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4599 free (tmp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4600 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4601 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4602 }
8832
1466c973f476 Add support for 'long double' number output.
Bruno Haible <bruno@clisp.org>
parents: 8831
diff changeset
4603 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4604 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4605 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4606 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
4607 int flags = dp->flags;
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4608 #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
4609 int has_width;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4610 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
4611 #endif
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4612 #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
4613 int has_precision;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4614 size_t precision;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4615 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4616 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4617 int prec_ourselves;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4618 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4619 # 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
4620 #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
4621 #if NEED_PRINTF_FLAG_LEFTADJUST
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4622 # 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
4623 #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
4624 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
4625 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4626 # 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
4627 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4628 TCHAR_T *fbp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4629 unsigned int prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4630 int prefixes[2] IF_LINT (= { 0 });
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4631 int orig_errno;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4632 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4633 size_t tmp_length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4634 TCHAR_T tmpbuf[700];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4635 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
4636 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4637
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4638 #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
4639 has_width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4640 width = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4641 if (dp->width_start != dp->width_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4642 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4643 if (dp->width_arg_index != ARG_NONE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4644 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4645 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4646
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4647 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
4648 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4649 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
4650 if (arg < 0)
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 /* "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
4653 followed by a positive field width." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4654 flags |= FLAG_LEFT;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4655 width = (unsigned int) (-arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4656 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4657 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4658 width = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4659 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4660 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4661 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4662 const FCHAR_T *digitp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4663
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4664 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4665 width = xsum (xtimes (width, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4666 while (digitp != dp->width_end);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4667 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4668 has_width = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4669 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4670 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4671
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4672 #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
4673 has_precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4674 precision = 6;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4675 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4676 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4677 if (dp->precision_arg_index != ARG_NONE)
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 int arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4680
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4681 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
4682 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4683 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
4684 /* "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
4685 were omitted." */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4686 if (arg >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4687 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4688 precision = arg;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4689 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4690 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4691 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4692 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4693 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4694 const FCHAR_T *digitp = dp->precision_start + 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4695
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4696 precision = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4697 while (digitp != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4698 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4699 has_precision = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4700 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4701 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4702 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
4703
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4704 /* Decide whether to handle the precision ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4705 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4706 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4707 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4708 case 'd': case 'i': case 'u':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4709 case 'o':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4710 case 'x': case 'X': case 'p':
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4711 prec_ourselves = has_precision && (precision > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4712 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4713 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4714 prec_ourselves = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4715 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4716 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4717 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4718
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4719 /* Decide whether to perform the padding ourselves. */
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4720 #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
4721 switch (dp->conversion)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4722 {
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4723 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4724 /* 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
4725 to perform the padding after this conversion. Functions
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4726 with unistdio extensions perform the padding based on
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4727 character count rather than element count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4728 case 'c': case 's':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4729 # endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4730 # if NEED_PRINTF_FLAG_ZERO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4731 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
4732 case 'a': case 'A':
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4733 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4734 pad_ourselves = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4735 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4736 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4737 pad_ourselves = prec_ourselves;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4738 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4739 }
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4740 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
4741
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
4742 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4743 /* 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
4744 sprintf. */
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4745 tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4746 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
4747 flags, width, has_precision, precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
4748 pad_ourselves);
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4749
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4750 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4751 tmp = tmpbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4752 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4753 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4754 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
4755
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4756 if (size_overflow_p (tmp_memsize))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4757 /* Overflow, would lead to out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4758 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4759 tmp = (TCHAR_T *) malloc (tmp_memsize);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4760 if (tmp == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4761 /* Out of memory. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4762 goto out_of_memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4763 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4764 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4765
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4766 /* Construct the format string for calling snprintf or
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4767 sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4768 fbp = buf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4769 *fbp++ = '%';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4770 #if NEED_PRINTF_FLAG_GROUPING
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4771 /* The underlying implementation doesn't support the ' flag.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4772 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
4773 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
4774 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4775 if (flags & FLAG_GROUP)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4776 *fbp++ = '\'';
8803
6c1d98929550 Work around lack of support of grouping flag.
Bruno Haible <bruno@clisp.org>
parents: 8664
diff changeset
4777 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4778 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4779 *fbp++ = '-';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4780 if (flags & FLAG_SHOWSIGN)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4781 *fbp++ = '+';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4782 if (flags & FLAG_SPACE)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4783 *fbp++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4784 if (flags & FLAG_ALT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4785 *fbp++ = '#';
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
4786 #if __GLIBC__ >= 2 && !defined __UCLIBC__
13841
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4787 if (flags & FLAG_LOCALIZED)
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4788 *fbp++ = 'I';
05717fb89fa4 vasnprintf: Support I flag on glibc systems.
Bruno Haible <bruno@clisp.org>
parents: 13806
diff changeset
4789 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4790 if (!pad_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4791 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4792 if (flags & FLAG_ZERO)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4793 *fbp++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4794 if (dp->width_start != dp->width_end)
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 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
4797 /* The width specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4798 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4799 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4800 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4801 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
4802 fbp += n;
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 else
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 const FCHAR_T *mp = dp->width_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4807 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4808 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4809 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4810 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4811 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4812 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4813 if (!prec_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4814 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4815 if (dp->precision_start != dp->precision_end)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4816 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4817 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
4818 /* The precision specification is known to consist only
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4819 of standard ASCII characters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4820 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4821 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4822 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
4823 fbp += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4824 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4825 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4826 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4827 const FCHAR_T *mp = dp->precision_start;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4828 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4829 *fbp++ = (unsigned char) *mp++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4830 while (--n > 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4831 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4832 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4833 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4834
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4835 switch (type)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4836 {
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4837 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4838 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4839 case TYPE_ULONGLONGINT:
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4840 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4841 *fbp++ = 'I';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4842 *fbp++ = '6';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4843 *fbp++ = '4';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4844 break;
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4845 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4846 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4847 /*FALLTHROUGH*/
8830
abc2bcb16721 Support output of 64-bit numbers on mingw.
Bruno Haible <bruno@clisp.org>
parents: 8804
diff changeset
4848 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4849 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4850 case TYPE_LONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4851 case TYPE_ULONGINT:
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4852 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4853 case TYPE_WIDE_CHAR:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4854 #endif
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
4855 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4856 case TYPE_WIDE_STRING:
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4857 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4858 *fbp++ = 'l';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4859 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4860 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4861 *fbp++ = 'L';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4862 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4863 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4864 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4865 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4866 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4867 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4868 *fbp = 'f';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4869 else
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
4870 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4871 *fbp = dp->conversion;
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4872 #if USE_SNPRINTF
13885
87a95303747f Port to uClibc.
Bruno Haible <bruno@clisp.org>
parents: 13883
diff changeset
4873 # 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
4874 fbp[1] = '%';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4875 fbp[2] = 'n';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4876 fbp[3] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4877 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4878 /* On glibc2 systems from glibc >= 2.3 - probably also older
13883
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4879 ones - we know that snprintf's return value conforms to
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4880 ISO C 99: the tests gl_SNPRINTF_RETVAL_C99 and
92b602471ee0 Fix a comment.
Bruno Haible <bruno@clisp.org>
parents: 13841
diff changeset
4881 gl_SNPRINTF_TRUNCATION_C99 pass.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4882 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
4883 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
4884 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
4885 (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
4886 in this situation. */
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
4887 /* On native Windows systems (such as mingw), we can avoid using
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4888 %n because:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4889 - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4890 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
4891 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
4892 '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
4893 - 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
4894 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
4895 buffer size: it returns -1 in this case.
16214
ec738d6aeef5 Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
4896 On native Windows systems (such as mingw) where the OS is
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4897 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
4898 crashes the program. See
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4899 <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
4900 <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
4901 So we should avoid %n in this situation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4902 fbp[1] = '\0';
9361
40389da21b02 Don't use %n on glibc >= 2.3 systems.
Bruno Haible <bruno@clisp.org>
parents: 9095
diff changeset
4903 # endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4904 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4905 fbp[1] = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4906 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4907
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4908 /* Construct the arguments for calling snprintf or sprintf. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4909 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4910 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
4911 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4912 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
4913 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4914 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
4915 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4916 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
4917 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4918 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
4919 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4920 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
4921 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4922
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4923 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4924 /* 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
4925 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
4926 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
4927 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4928 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
4929 # 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
4930 /* 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
4931 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
4932 ENSURE_ALLOCATION (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4933 (2 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4934 / TCHARS_PER_DCHAR));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4935 /* Prepare checking whether snprintf returns the count
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4936 via %n. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4937 *(TCHAR_T *) (result + length) = '\0';
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4938 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4939
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4940 orig_errno = errno;
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
4941
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4942 for (;;)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4943 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4944 int count = -1;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4945
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
4946 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4947 int retcount = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4948 size_t maxlen = allocated - length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4949 /* SNPRINTF can fail if its second argument is
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4950 > INT_MAX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4951 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4952 maxlen = INT_MAX / TCHARS_PER_DCHAR;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4953 maxlen = maxlen * TCHARS_PER_DCHAR;
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4954 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4955 switch (prefix_count) \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4956 { \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4957 case 0: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4958 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4959 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4960 arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4961 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4962 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4963 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4964 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4965 prefixes[0], arg, &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4966 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4967 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4968 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4969 maxlen, buf, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4970 prefixes[0], prefixes[1], arg, \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4971 &count); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4972 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4973 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4974 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4975 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4976 #else
4704
b7a1b14802c8 Correct cpp indentation.
Jim Meyering <jim@meyering.net>
parents: 4589
diff changeset
4977 # define SNPRINTF_BUF(arg) \
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4978 switch (prefix_count) \
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 case 0: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4981 count = sprintf (tmp, buf, arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4982 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4983 case 1: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4984 count = sprintf (tmp, buf, prefixes[0], arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4985 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4986 case 2: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4987 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4988 arg); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4989 break; \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4990 default: \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4991 abort (); \
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4992 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4993 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4994
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
4995 errno = 0;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4996 switch (type)
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 case TYPE_SCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
4999 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5000 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
5001 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5002 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5003 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5004 case TYPE_UCHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5005 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5006 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
5007 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5008 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5009 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5010 case TYPE_SHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5011 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5012 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
5013 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5014 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5015 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5016 case TYPE_USHORT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5017 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5018 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
5019 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5020 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5021 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5022 case TYPE_INT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5023 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5024 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
5025 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5026 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5027 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5028 case TYPE_UINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5029 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5030 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
5031 SNPRINTF_BUF (arg);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5034 case TYPE_LONGINT:
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 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
5037 SNPRINTF_BUF (arg);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5040 case TYPE_ULONGINT:
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 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
5043 SNPRINTF_BUF (arg);
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 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5046 #if HAVE_LONG_LONG_INT
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5047 case TYPE_LONGLONGINT:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5048 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5049 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
5050 SNPRINTF_BUF (arg);
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 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5053 case TYPE_ULONGLONGINT:
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 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
5056 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5057 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5058 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5059 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5060 case TYPE_DOUBLE:
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 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
5063 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5064 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5065 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5066 case TYPE_LONGDOUBLE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5067 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5068 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
5069 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5070 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5071 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5072 case TYPE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5073 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5074 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
5075 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5076 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5077 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5078 #if HAVE_WINT_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5079 case TYPE_WIDE_CHAR:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5080 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5081 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
5082 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5083 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5084 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5085 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5086 case TYPE_STRING:
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 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
5089 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5090 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5091 break;
8051
9c2b0396b27c Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents: 7475
diff changeset
5092 #if HAVE_WCHAR_T
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5093 case TYPE_WIDE_STRING:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5094 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5095 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
5096 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5097 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5098 break;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5099 #endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5100 case TYPE_POINTER:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5101 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5102 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
5103 SNPRINTF_BUF (arg);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5104 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5105 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5106 default:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5107 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5108 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5109
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5110 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5111 /* Portability: Not all implementations of snprintf()
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5112 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
5113 bytes that snprintf() has produced or would have
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5114 produced. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5115 if (count >= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5116 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5117 /* Verify that snprintf() has NUL-terminated its
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5118 result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5119 if (count < maxlen
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5120 && ((TCHAR_T *) (result + length)) [count] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5121 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5122 /* Portability hack. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5123 if (retcount > count)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5124 count = retcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5125 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5126 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5127 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5128 /* snprintf() doesn't understand the '%n'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5129 directive. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5130 if (fbp[1] != '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5131 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5132 /* Don't use the '%n' directive; instead, look
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5133 at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5134 fbp[1] = '\0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5135 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5136 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5137 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5138 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5139 /* Look at the snprintf() return value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5140 if (retcount < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5141 {
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5142 # if !HAVE_SNPRINTF_RETVAL_C99
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5143 /* HP-UX 10.20 snprintf() is doubly deficient:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5144 It doesn't understand the '%n' directive,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5145 *and* it returns -1 (rather than the length
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5146 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
5147 buffer is too small.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5148 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
5149 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
5150 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
5151 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
5152 floating-point argument. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5153 size_t tmp_length =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5154 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
5155 dp->conversion, type, flags,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5156 width, has_precision,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5157 precision, pad_ourselves);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5158
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5159 if (maxlen < tmp_length)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5160 {
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5161 /* 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
5162 this reallocation only once. */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5163 size_t bigger_need =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5164 xsum (length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5165 xsum (tmp_length,
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5166 TCHARS_PER_DCHAR - 1)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5167 / TCHARS_PER_DCHAR);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5168 /* And always grow proportionally.
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5169 (There may be several arguments, each
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5170 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
5171 previous one.) */
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5172 size_t bigger_need2 =
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5173 xsum (xtimes (allocated, 2), 12);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5174 if (bigger_need < bigger_need2)
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5175 bigger_need = bigger_need2;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5176 ENSURE_ALLOCATION (bigger_need);
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5177 continue;
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5178 }
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5179 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5180 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5181 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5182 count = retcount;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5183 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5184 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5185 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5186
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5187 /* Attempt to handle failure. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5188 if (count < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5189 {
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5190 /* 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
5191 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
5192 int saved_errno = errno;
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5193
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5194 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5195 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5196 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5197 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5198 CLEANUP ();
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5199 errno =
13259
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5200 (saved_errno != 0
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5201 ? saved_errno
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5202 : (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
5203 ? EILSEQ
5aa26fc2aec0 vasnprintf: Correct errno value in case of out-of-memory.
Bruno Haible <bruno@clisp.org>
parents: 13199
diff changeset
5204 : EINVAL));
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5205 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5206 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5207
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5208 #if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5209 /* Handle overflow of the allocated buffer.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5210 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
5211 returns a count >= maxlen. However, a non-compliant
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5212 snprintf() function returns only count = maxlen - 1. To
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5213 cover both cases, test whether count >= maxlen - 1. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5214 if ((unsigned int) count + 1 >= maxlen)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5215 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5216 /* If maxlen already has attained its allowed maximum,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5217 allocating more memory will not increase maxlen.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5218 Instead of looping, bail out. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5219 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5220 goto overflow;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5221 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5222 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5223 /* Need at least (count + 1) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5224 bytes. (The +1 is for the trailing NUL.)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5225 But ask for (count + 2) * sizeof (TCHAR_T)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5226 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
5227 maxlen > (unsigned int) count + 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5228 and so we don't get here again.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5229 And allocate proportionally, to avoid looping
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5230 eternally if snprintf() reports a too small
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5231 count. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5232 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5233 xmax (xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5234 ((unsigned int) count + 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5235 + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5236 / TCHARS_PER_DCHAR),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5237 xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5238
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5239 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5240 continue;
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 }
8837
6d6277df29a9 Fix logic bug introduced on 2007-05-06.
Bruno Haible <bruno@clisp.org>
parents: 8835
diff changeset
5243 #endif
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5244
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5245 #if NEED_PRINTF_UNBOUNDED_PRECISION
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5246 if (prec_ourselves)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5247 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5248 /* Handle the precision. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5249 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
5250 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5251 (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
5252 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5253 tmp;
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5254 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5255 size_t prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5256 size_t move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5257
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5258 prefix_count = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5259 /* Put the additional zeroes after the sign. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5260 if (count >= 1
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5261 && (*prec_ptr == '-' || *prec_ptr == '+'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5262 || *prec_ptr == ' '))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5263 prefix_count = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5264 /* 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
5265 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5266 else if (count >= 2
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5267 && prec_ptr[0] == '0'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5268 && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5269 prefix_count = 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5270
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5271 move = count - prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5272 if (precision > move)
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 /* Insert zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5275 size_t insert = precision - move;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5276 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
5277
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5278 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5279 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5280 xsum (length,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5281 (count + insert + TCHARS_PER_DCHAR - 1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5282 / TCHARS_PER_DCHAR);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5283 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
5284 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5285 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
5286 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
5287 # endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5288
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5289 prec_end = prec_ptr + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5290 prec_ptr += prefix_count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5291
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5292 while (prec_end > prec_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5293 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5294 prec_end--;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5295 prec_end[insert] = prec_end[0];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5296 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5297
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5298 prec_end += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5299 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5300 *--prec_end = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5301 while (prec_end > prec_ptr);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5302
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5303 count += insert;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5304 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5305 }
9446
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5306 #endif
0519fc8ce06a Fix *printf behaviour for large precisions on mingw and BeOS.
Bruno Haible <bruno@clisp.org>
parents: 9445
diff changeset
5307
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5308 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5309 if (count >= tmp_length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5310 /* tmp_length was incorrectly calculated - fix the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5311 code above! */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5312 abort ();
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5313 #endif
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5314
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5315 #if !DCHAR_IS_TCHAR
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5316 /* Convert from TCHAR_T[] to DCHAR_T[]. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5317 if (dp->conversion == 'c' || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5318 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5319 /* 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
5320 TYPE_WIDE_STRING.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5321 The result string is not certainly ASCII. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5322 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5323 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5324 size_t tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5325 /* 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
5326 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
5327 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5328 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
5329 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5330 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
5331 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5332 tmpdst =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5333 DCHAR_CONV_FROM_ENCODING (locale_charset (),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5334 iconveh_question_mark,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5335 tmpsrc, count,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5336 NULL,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5337 NULL, &tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5338 if (tmpdst == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5339 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5340 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5341 if (!(result == resultbuf || result == NULL))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5342 free (result);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5343 if (buf_malloced != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5344 free (buf_malloced);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5345 CLEANUP ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5346 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5347 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5348 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5349 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5350 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5351 free (tmpdst);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5352 count = tmpdst_len;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5353 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5354 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5355 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5356 /* The result string is ASCII.
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5357 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
5358 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5359 /* 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
5360 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
5361 at (result + length). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5362 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
5363 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5364 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5365 const TCHAR_T *tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5366 DCHAR_T *tmpdst;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5367 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
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 USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5370 if (result == resultbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5371 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5372 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5373 /* ENSURE_ALLOCATION will not move tmpsrc
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5374 (because it's part of resultbuf). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5375 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5376 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5377 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5378 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5379 /* ENSURE_ALLOCATION will move the array
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5380 (because it uses realloc(). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5381 ENSURE_ALLOCATION (xsum (length, count));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5382 tmpsrc = (TCHAR_T *) (result + length);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5383 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5384 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5385 tmpsrc = tmp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5386 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
5387 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5388 tmpdst = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5389 /* Copy backwards, because of overlapping. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5390 tmpsrc += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5391 tmpdst += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5392 for (n = count; n > 0; n--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5393 *--tmpdst = (unsigned char) *--tmpsrc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5394 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5395 }
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5396 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5397
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5398 #if DCHAR_IS_TCHAR && !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5399 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5400 if (count > allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5401 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5402 /* Need at least count elements. But allocate
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5403 proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5404 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5405 xmax (xsum (length, count), xtimes (allocated, 2));
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5406
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5407 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5408 }
8963
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5409 #endif
f2b97e5b14f5 Tweak the size computations and reallocations.
Bruno Haible <bruno@clisp.org>
parents: 8962
diff changeset
5410
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5411 /* Here count <= allocated - length. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5412
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5413 /* 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
5414 #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
5415 if (pad_ourselves && has_width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5416 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5417 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
5418 # if ENABLE_UNISTDIO
16358
a712776b11ce maint: spelling fixes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16214
diff changeset
5419 /* Outside POSIX, it's preferable to compare the width
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5420 against the number of _characters_ of the converted
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5421 value. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5422 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
5423 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5424 /* 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
5425 of the converted value, says POSIX. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5426 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
5427 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5428 if (w < width)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5429 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5430 size_t pad = width - w;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5431
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5432 /* Make room for the result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5433 if (xsum (count, pad) > allocated - length)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5434 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5435 /* Need at least count + pad elements. But
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5436 allocate proportionally. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5437 size_t n =
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5438 xmax (xsum3 (length, count, pad),
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5439 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
5440
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5441 # if USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5442 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5443 ENSURE_ALLOCATION (n);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5444 length -= count;
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5445 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5446 ENSURE_ALLOCATION (n);
9834
b4f1e5627265 Fix buffer overrun.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
5447 # endif
12421
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 /* Here count + pad <= allocated - length. */
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 {
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5452 # if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5453 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
5454 # else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5455 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
5456 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5457 DCHAR_T *p = rp + count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5458 DCHAR_T *end = p + pad;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5459 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
5460 # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5461 if (dp->conversion == 'c'
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5462 || dp->conversion == 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5463 /* No zero-padding for string directives. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5464 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5465 else
9672
c0fb9d4d860d Fix bug introduced on 2007-06-10.
Bruno Haible <bruno@clisp.org>
parents: 9651
diff changeset
5466 # endif
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5467 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5468 pad_ptr = (*rp == '-' ? rp + 1 : rp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5469 /* No zero-padding of "inf" and "nan". */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5470 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5471 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5472 pad_ptr = NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5473 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5474 /* 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
5475 with the zero padding insertion point being at
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5476 pad_ptr. */
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 count = count + pad; /* = end - rp */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5479
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5480 if (flags & FLAG_LEFT)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5481 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5482 /* Pad with spaces on the right. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5483 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5484 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5485 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5486 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5487 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5488 /* Pad with zeroes. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5489 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5490
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5491 while (p > pad_ptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5492 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5493 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5494 *p++ = '0';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5495 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5496 else
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 /* Pad with spaces on the left. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5499 DCHAR_T *q = end;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5500
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5501 while (p > rp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5502 *--q = *--p;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5503 for (; pad > 0; pad--)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5504 *p++ = ' ';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5505 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5506 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5507 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5508 }
8804
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5509 #endif
6ce313658b4d Work around an incorrect implementation of the 0 flag on most platforms.
Bruno Haible <bruno@clisp.org>
parents: 8803
diff changeset
5510
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5511 /* Here still count <= allocated - length. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5512
8973
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5513 #if !DCHAR_IS_TCHAR || USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5514 /* The snprintf() result did fit. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5515 #else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5516 /* Append the sprintf() result. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5517 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
5518 #endif
7df86a1985e4 Add support for Unicode strings (both as output format and as arguments to
Bruno Haible <bruno@clisp.org>
parents: 8963
diff changeset
5519 #if !USE_SNPRINTF
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5520 if (tmp != tmpbuf)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5521 free (tmp);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5522 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5523
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5524 #if NEED_PRINTF_DIRECTIVE_F
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5525 if (dp->conversion == 'F')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5526 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5527 /* 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
5528 DCHAR_T *rp = result + length;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5529 size_t rc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5530 for (rc = count; rc > 0; rc--, rp++)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5531 if (*rp >= 'a' && *rp <= 'z')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5532 *rp = *rp - 'a' + 'A';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5533 }
8664
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5534 #endif
40c507f55b0f Implement the %F directive if the system doesn't implement it correctly.
Bruno Haible <bruno@clisp.org>
parents: 8655
diff changeset
5535
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5536 length += count;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5537 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5538 }
13806
3468ff86245b vasnprintf: Don't set errno to 0.
Bruno Haible <bruno@clisp.org>
parents: 13788
diff changeset
5539 errno = orig_errno;
13323
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
5540 #undef pad_ourselves
cbb76cf0c6f1 vasnprintf: Fix syntax errors in libintl build on mingw.
Bruno Haible <bruno@clisp.org>
parents: 13259
diff changeset
5541 #undef prec_ourselves
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5542 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5543 }
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5544 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5545
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5546 /* Add the final NUL. */
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5547 ENSURE_ALLOCATION (xsum (length, 1));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5548 result[length] = '\0';
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5549
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5550 if (result != resultbuf && length + 1 < allocated)
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5551 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5552 /* Shrink the allocated memory if possible. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5553 DCHAR_T *memory;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5554
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5555 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
5556 if (memory != NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12344
diff changeset
5557 result = memory;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5558 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5559
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5560 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5561 free (buf_malloced);
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5562 CLEANUP ();
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5563 *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
5564 /* 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
5565 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
5566 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
5567 not have this limitation. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5568 return result;
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5569
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5570 #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
5571 overflow:
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5572 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
5573 free (result);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5574 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
5575 free (buf_malloced);
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5576 CLEANUP ();
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5577 errno = EOVERFLOW;
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5578 return NULL;
9651
cee38e50f29c vasnprintf.c: Avoid warning about unused label
Jim Meyering <meyering@redhat.com>
parents: 9646
diff changeset
5579 #endif
8468
336e69863755 Fix endless loop when the given allocated size was > INT_MAX.
Bruno Haible <bruno@clisp.org>
parents: 8465
diff changeset
5580
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5581 out_of_memory:
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5582 if (!(result == resultbuf || result == NULL))
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5583 free (result);
4879
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5584 if (buf_malloced != NULL)
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5585 free (buf_malloced);
9b96adbed643 Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents: 4872
diff changeset
5586 out_of_memory_1:
4872
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5587 CLEANUP ();
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5588 errno = ENOMEM;
c5afc99b8ce5 Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents: 4871
diff changeset
5589 return NULL;
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5590 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5591 }
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5592
13199
af2bd6fe2a98 vasnprintf: Correct handling of unconvertible wide string arguments.
Bruno Haible <bruno@clisp.org>
parents: 13198
diff changeset
5593 #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
5594 #undef TCHARS_PER_DCHAR
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5595 #undef SNPRINTF
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5596 #undef USE_SNPRINTF
11804
73f84ac92381 Undefine DCHAR_SET after use.
Bruno Haible <bruno@clisp.org>
parents: 11511
diff changeset
5597 #undef DCHAR_SET
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5598 #undef DCHAR_CPY
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5599 #undef PRINTF_PARSE
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5600 #undef DIRECTIVES
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5601 #undef DIRECTIVE
9450
9928de4bfe8a Undefine two macros after they are not needed any more.
Bruno Haible <bruno@clisp.org>
parents: 9446
diff changeset
5602 #undef DCHAR_IS_TCHAR
8961
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5603 #undef TCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5604 #undef DCHAR_T
f1938b0a1252 More type parameters.
Bruno Haible <bruno@clisp.org>
parents: 8916
diff changeset
5605 #undef FCHAR_T
4871
87df35000dab Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents: 4801
diff changeset
5606 #undef VASNPRINTF