ftoastr: don't assume snprintf
[gnulib.git] / lib / ftoastr.c
1 /* floating point to accurate string
2
3    Copyright (C) 2010 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Paul Eggert.  */
19
20 /* This code can misbehave on some buggy or older platforms, when
21    operating on arguments on floating types other than 'double', or
22    when given unusual combinations of options.  Gnulib's
23    snprintf-posix module works around many of these problems.
24
25    This code relies on sprintf, strtod, etc. operating accurately;
26    otherwise, the resulting strings could be inaccurate or too long.  */
27
28 #include "ftoastr.h"
29
30 #include "intprops.h"
31 #include <float.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #if LENGTH == 3
36 # define FLOAT long double
37 # define FLOAT_DIG LDBL_DIG
38 # define FLOAT_MIN LDBL_MIN
39 # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND
40 # define FTOASTR ldtoastr
41 # define STRTOF strtold
42 #elif LENGTH == 2
43 # define FLOAT double
44 # define FLOAT_DIG DBL_DIG
45 # define FLOAT_MIN DBL_MIN
46 # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND
47 # define FTOASTR dtoastr
48 # define STRTOF strtod
49 #else
50 # define LENGTH 1
51 # define FLOAT float
52 # define FLOAT_DIG FLT_DIG
53 # define FLOAT_MIN FLT_MIN
54 # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND
55 # define FTOASTR ftoastr
56 # define STRTOF strtof
57 #endif
58
59 /* On pre-C99 hosts, approximate strtof and strtold with strtod.  This
60    may generate one or two extra digits, but that's better than not
61    working at all.  Assume that strtof works if strtold does.  */
62 #if LENGTH != 2 && ! HAVE_C99_STRTOLD
63 # undef STRTOF
64 # define STRTOF strtod
65 #endif
66
67 /* On hosts where it's not known that snprintf works, use sprintf to
68    implement the subset needed here.  Typically BUFSIZE is big enough
69    and there's little performance hit.  */
70 #if ! GNULIB_SNPRINTF_POSIX
71 # undef snprintf
72 # define snprintf ftoastr_snprintf
73 static int
74 ftoastr_snprintf (char *buf, size_t bufsize, char const *format,
75                   int width, int prec, FLOAT x)
76 {
77   char width_0_buffer[LENGTH == 1 ? FLT_BUFSIZE_BOUND
78                       : LENGTH == 2 ? DBL_BUFSIZE_BOUND
79                       : LDBL_BUFSIZE_BOUND];
80   int n = width;
81   if (bufsize < sizeof width_0_buffer)
82     {
83       n = sprintf (width_0_buffer, format, 0, prec, x);
84       if (n < 0)
85         return n;
86       if (n < width)
87         n = width;
88     }
89   if (n < bufsize)
90     n = sprintf (buf, format, width, prec, x);
91   return n;
92 }
93 #endif
94
95 int
96 FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x)
97 {
98   /* The following method is simple but slow.
99      For ideas about speeding things up, please see:
100
101      Florian Loitsch, Printing floating-point numbers quickly and accurately
102      with integers.  ACM SIGPLAN notices 46, 6 (June 2010), 233-243
103      <http://dx.doi.org/10.1145/1809028.1806623>; also see the
104      2010-03-21 draft <http://florian.loitsch.com/tmp/article.pdf>.  */
105
106   char format[sizeof "%-+ 0*.*Lg"];
107   FLOAT abs_x = x < 0 ? -x : x;
108   int prec;
109
110   char *p = format;
111   *p++ = '%';
112
113   /* Support flags that generate output parsable by strtof.  */
114   *p = '-'; p += (flags & FTOASTR_LEFT_JUSTIFY  ) != 0;
115   *p = '+'; p += (flags & FTOASTR_ALWAYS_SIGNED ) != 0;
116   *p = ' '; p += (flags & FTOASTR_SPACE_POSITIVE) != 0;
117   *p = '0'; p += (flags & FTOASTR_ZERO_PAD      ) != 0;
118
119   *p++ = '*';
120   *p++ = '.';
121   *p++ = '*';
122   *p = 'L'; p += 2 < LENGTH;
123   *p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g';
124   *p = '\0';
125
126   for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++)
127     {
128       int n = snprintf (buf, bufsize, format, width, prec, x);
129       if (n < 0
130           || FLOAT_PREC_BOUND <= prec
131           || (n < bufsize && STRTOF (buf, NULL) == x))
132         return n;
133     }
134 }