1 /* Convert string to floating-point number, using the C locale.
3 Copyright (C) 2003-2004, 2006, 2009-2024 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert and Bruno Haible. */
30 # define C_STRTOD c_strtof
32 # define STRTOD_L strtof_l
33 # define HAVE_GOOD_STRTOD_L (HAVE_STRTOF_L && !GNULIB_defined_strtof_function)
34 # define STRTOD strtof
36 # define C_STRTOD c_strtold
37 # define DOUBLE long double
38 # define STRTOD_L strtold_l
39 # define HAVE_GOOD_STRTOD_L (HAVE_STRTOLD_L && !GNULIB_defined_strtold_function)
40 # define STRTOD strtold
42 # define C_STRTOD c_strtod
43 # define DOUBLE double
44 # define STRTOD_L strtod_l
45 # define HAVE_GOOD_STRTOD_L (HAVE_STRTOD_L && !GNULIB_defined_strtod_function)
46 # define STRTOD strtod
49 #if defined LC_ALL_MASK && (HAVE_GOOD_STRTOD_L || HAVE_WORKING_USELOCALE)
51 /* Cache for the C locale object.
52 Marked volatile so that different threads see the same value
54 static volatile locale_t c_locale_cache
;
56 /* Return the C locale object, or (locale_t) 0 with errno set
57 if it cannot be created. */
62 c_locale_cache
= newlocale (LC_ALL_MASK
, "C", (locale_t
) 0);
63 return c_locale_cache
;
69 # include <langinfo.h>
73 /* Determine the decimal-point character according to the current locale. */
75 decimal_point_char (void)
78 /* Determine it in a multithread-safe way. We know nl_langinfo is
79 multithread-safe on glibc systems and Mac OS X systems, but is not required
80 to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
81 localeconv() is rarely multithread-safe. */
82 # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
83 point
= nl_langinfo (RADIXCHAR
);
86 sprintf (pointbuf
, "%#.0f", 1.0);
89 point
= localeconv () -> decimal_point
;
91 /* The decimal point is always a single byte: either '.' or ','. */
92 return (point
[0] != '\0' ? point
[0] : '.');
98 C_STRTOD (char const *nptr
, char **endptr
)
102 #if defined LC_ALL_MASK && (HAVE_GOOD_STRTOD_L || HAVE_WORKING_USELOCALE)
104 locale_t locale
= c_locale ();
108 *endptr
= (char *) nptr
;
109 return 0; /* errno is set here */
112 # if HAVE_GOOD_STRTOD_L
114 r
= STRTOD_L (nptr
, endptr
, locale
);
116 # else /* HAVE_WORKING_USELOCALE */
118 locale_t old_locale
= uselocale (locale
);
119 if (old_locale
== (locale_t
)0)
122 *endptr
= (char *) nptr
;
123 return 0; /* errno is set here */
126 r
= STRTOD (nptr
, endptr
);
128 int saved_errno
= errno
;
129 if (uselocale (old_locale
) == (locale_t
)0)
130 /* We can't switch back to the old locale. The thread is hosed. */
138 char decimal_point
= decimal_point_char ();
140 if (decimal_point
== '.')
141 /* In this locale, C_STRTOD and STRTOD behave the same way. */
142 r
= STRTOD (nptr
, endptr
);
145 /* Start and end of the floating-point number. */
148 /* Position of the decimal point '.' in the floating-point number.
149 Either decimal_point_p == NULL or start <= decimal_point_p < end. */
150 char const *decimal_point_p
= NULL
;
151 /* Set to true if we encountered decimal_point while parsing. */
152 int seen_decimal_point
= 0;
155 1. a sequence of white-space characters,
156 2. a subject sequence possibly containing a floating-point number,
157 as described in POSIX
158 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtod.html>.
165 /* Parse a sequence of white-space characters. */
166 while (*p
!= '\0' && c_isspace ((unsigned char) *p
))
171 /* Start to parse a subject sequence. */
172 if (*p
== '+' || *p
== '-')
177 if (p
[1] == 'x' || p
[1] == 'X')
179 size_t num_hex_digits
= 0;
181 /* Parse a non-empty sequence of hexadecimal digits optionally
182 containing the decimal point character '.'. */
185 if (c_isxdigit ((unsigned char) *p
))
192 if (decimal_point_p
== NULL
)
203 seen_decimal_point
= (*p
== decimal_point
);
204 if (num_hex_digits
> 0)
207 /* Parse the character 'p' or the character 'P', optionally
208 followed by a '+' or '-' character, and then followed by
209 one or more decimal digits. */
210 if (*p
== 'p' || *p
== 'P')
213 if (*p
== '+' || *p
== '-')
215 if (*p
!= '\0' && c_isdigit ((unsigned char) *p
))
218 while (*p
!= '\0' && c_isdigit ((unsigned char) *p
))
225 decimal_point_p
= NULL
;
230 size_t num_digits
= 0;
231 /* Parse a non-empty sequence of decimal digits optionally containing
232 the decimal point character '.'. */
235 if (c_isdigit ((unsigned char) *p
))
242 if (decimal_point_p
== NULL
)
253 seen_decimal_point
= (*p
== decimal_point
);
257 /* Parse the character 'e' or the character 'E', optionally
258 followed by a '+' or '-' character, and then followed by one or
259 more decimal digits. */
260 if (*p
== 'e' || *p
== 'E')
263 if (*p
== '+' || *p
== '-')
265 if (*p
!= '\0' && c_isdigit ((unsigned char) *p
))
268 while (*p
!= '\0' && c_isdigit ((unsigned char) *p
))
275 decimal_point_p
= NULL
;
279 if (end
== start
|| (decimal_point_p
== NULL
&& !seen_decimal_point
))
280 /* If end == start, we have not parsed anything. The given string
281 might be "INF", "INFINITY", "NAN", "NAN(chars)", or invalid.
282 We can pass it to STRTOD.
283 If end > start and decimal_point_p == NULL, we have parsed a
284 floating-point number, and it does not have a '.' (and not a ','
285 either, of course). If !seen_decimal_point, we did not
286 encounter decimal_point while parsing. In this case, the
287 locale-dependent STRTOD function can handle it. */
288 r
= STRTOD (nptr
, endptr
);
291 /* Create a modified floating-point number, in which the character '.'
292 is replaced with the locale-dependent decimal_point. */
293 size_t len
= end
- start
;
295 char *buf_malloced
= NULL
;
299 if (len
< sizeof (stackbuf
))
303 buf
= malloc (len
+ 1);
307 Callers may not be prepared to see errno == ENOMEM. But
308 they should be prepared to see errno == EINVAL. */
311 *endptr
= (char *) nptr
;
317 memcpy (buf
, start
, len
);
318 if (decimal_point_p
!= NULL
)
319 buf
[decimal_point_p
- start
] = decimal_point
;
322 r
= STRTOD (buf
, &end_in_buf
);
324 *endptr
= (char *) (start
+ (end_in_buf
- buf
));
326 if (buf_malloced
!= NULL
)
328 int saved_errno
= errno
;