From 758f820bcc0f68aeebac1717e537ca13a320b909 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 18:11:47 +0200 Subject: Adding upstream version 9.1. Signed-off-by: Daniel Baumann --- lib/cl-strtod.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lib/cl-strtod.c (limited to 'lib/cl-strtod.c') diff --git a/lib/cl-strtod.c b/lib/cl-strtod.c new file mode 100644 index 0000000..a8c7d41 --- /dev/null +++ b/lib/cl-strtod.c @@ -0,0 +1,76 @@ +/* Convert string to double in the current locale, falling back on the C locale. + + Copyright 2019-2022 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Paul Eggert. */ + +#include + +#include "cl-strtod.h" + +#include + +#include +#include + +#if LONG +# define CL_STRTOD cl_strtold +# define DOUBLE long double +# define C_STRTOD c_strtold +# define STRTOD strtold +#else +# define CL_STRTOD cl_strtod +# define DOUBLE double +# define C_STRTOD c_strtod +# define STRTOD strtod +#endif + +/* This function acts like strtod or strtold, except that it falls + back on the C locale if the initial prefix is not parsable in + the current locale. If the prefix is parsable in both locales, + it uses the longer parse, breaking ties in favor of the current locale. + + Parse the initial prefix of NPTR as a floating-point number in the + current locale or in the C locale (preferring the locale that + yields the longer parse, or the current locale if there is a tie). + If ENDPTR is not NULL, set *ENDPTR to the first unused byte, or to + NPTR if the prefix cannot be parsed. + + If successful, return a number without changing errno. + If the prefix cannot be parsed, return 0 and possibly set errno to EINVAL. + If the number overflows, return an extreme value and set errno to ERANGE. + If the number underflows, return a value close to 0 and set errno to ERANGE. + If there is some other error, return 0 and set errno. */ + +DOUBLE +CL_STRTOD (char const *nptr, char **restrict endptr) +{ + char *end; + DOUBLE d = STRTOD (nptr, &end); + if (*end) + { + int strtod_errno = errno; + char *c_end; + DOUBLE c = C_STRTOD (nptr, &c_end); + if (end < c_end) + d = c, end = c_end; + else + errno = strtod_errno; + } + if (endptr) + *endptr = end; + return d; +} -- cgit v1.2.3