]> Savannah Git Hosting - gnulib.git/commitdiff
c-strtof: New module.
authorBruno Haible <bruno@clisp.org>
Thu, 22 Feb 2024 00:52:05 +0000 (01:52 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 22 Feb 2024 00:52:05 +0000 (01:52 +0100)
* lib/c-strtod.h (c_strtof): New declaration.
* lib/c-strtod.c: Support FLOAT.
* lib/c-strtof.c: New file.
* m4/c-strtod.m4 (gl_C_STRTOF): New macro.
* modules/c-strtof: New file.

ChangeLog
lib/c-strtod.c
lib/c-strtod.h
lib/c-strtof.c [new file with mode: 0644]
m4/c-strtod.m4
modules/c-strtof [new file with mode: 0644]

index 4d03615791e94f212c2c3a8d8226ac91f909d1db..1ab1246616db2d4b690b3bcdd614b1f27a952b5f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-02-21  Bruno Haible  <bruno@clisp.org>
+
+       c-strtof: New module.
+       * lib/c-strtod.h (c_strtof): New declaration.
+       * lib/c-strtod.c: Support FLOAT.
+       * lib/c-strtof.c: New file.
+       * m4/c-strtod.m4 (gl_C_STRTOF): New macro.
+       * modules/c-strtof: New file.
+
 2024-02-21  Bruno Haible  <bruno@clisp.org>
 
        c-strtold: Use strtold_l when available (regr. 2019-01-31).
index f955434eaa94e3315e3619fc1ba282bfa5fa1bf9..308b6d4c8418eb3f267b43697f38a648adeb1581 100644 (file)
@@ -1,4 +1,4 @@
-/* Convert string to double, using the C locale.
+/* Convert string to floating-point number, using the C locale.
 
    Copyright (C) 2003-2004, 2006, 2009-2024 Free Software Foundation, Inc.
 
 #include <stdlib.h>
 #include <string.h>
 
-#if LONG
+#if FLOAT
+# define C_STRTOD c_strtof
+# define DOUBLE float
+# define STRTOD_L strtof_l
+# define HAVE_GOOD_STRTOD_L (HAVE_STRTOF_L && !GNULIB_defined_strtof_function)
+# define STRTOD strtof
+#elif LONG
 # define C_STRTOD c_strtold
 # define DOUBLE long double
 # define STRTOD_L strtold_l
index 7576e5fb05cbb3734b27f9ab2d427cc79766459a..c26908077b6b63b9f3e05976b8cc4d6c4959e09b 100644 (file)
@@ -1,4 +1,4 @@
-/* Convert string to double, using the C locale.  -*- coding: utf-8 -*-
+/* Convert string to floating-point number, using the C locale.
 
    Copyright (C) 2003-2004, 2009-2024 Free Software Foundation, Inc.
 
@@ -27,7 +27,7 @@ extern "C" {
    parsed number or to NPTR if the string does not start with a parsable
    number.
    Return value:
-   - If successful, return the value as a double or 'long double',
+   - If successful, return the value as a float, double, or 'long double',
      respectively, and don't modify errno.
    - In case of overflow, return ±HUGE_VAL or ±HUGE_VALL, respectively, and
      set errno to ERANGE.
@@ -37,9 +37,17 @@ extern "C" {
      that if ENDPTR != NULL, *ENDPTR is set to NPTR), and maybe set errno to
      EINVAL.
    - In case of other error, return 0 and set errno, for example to ENOMEM.  */
+extern float       c_strtof  (char const *nptr, char **endptr);
 extern double      c_strtod  (char const *nptr, char **endptr);
 extern long double c_strtold (char const *nptr, char **endptr);
 
 #ifdef __cplusplus
 }
 #endif
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
diff --git a/lib/c-strtof.c b/lib/c-strtof.c
new file mode 100644 (file)
index 0000000..dcb8173
--- /dev/null
@@ -0,0 +1,19 @@
+/* Convert string to 'float' in C locale.
+
+   Copyright (C) 2004-2024 Free Software Foundation, Inc.
+
+   This file 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 file 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 <https://www.gnu.org/licenses/>.  */
+
+#define FLOAT 1
+#include "c-strtod.c"
index d1c4547948ccf724bed7f7ecea3b5b83cc925bcb..102b3f58c50fab744b1151db0cbb71cebe9beb8e 100644 (file)
@@ -1,4 +1,4 @@
-# c-strtod.m4 serial 19
+# c-strtod.m4 serial 20
 
 # Copyright (C) 2004-2006, 2009-2024 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -42,6 +42,41 @@ AC_DEFUN([gl_C_STRTOD],
     [Define to 1 if the system has the 'strtod_l' function.])
 ])
 
+dnl Prerequisites of lib/c-strtof.c.
+AC_DEFUN([gl_C_STRTOF],
+[
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_REQUIRE([gt_FUNC_USELOCALE])
+
+  AC_CHECK_HEADERS_ONCE([xlocale.h])
+  dnl We can't use AC_CHECK_FUNC here, because strtof_l() is defined as a
+  dnl static inline function when compiling for Android 7.1 or older.
+  AC_CACHE_CHECK([for strtof_l], [gl_cv_func_strtof_l],
+    [AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM(
+          [[#include <stdlib.h>
+            #include <locale.h>
+            #if HAVE_XLOCALE_H
+            # include <xlocale.h>
+            #endif
+            locale_t loc;
+          ]],
+          [[char *end;
+            return strtof_l("0",&end,loc) < 0.0f;
+          ]])
+       ],
+       [gl_cv_func_strtof_l=yes],
+       [gl_cv_func_strtof_l=no])
+    ])
+  if test $gl_cv_func_strtof_l = yes; then
+    HAVE_STRTOF_L=1
+  else
+    HAVE_STRTOF_L=0
+  fi
+  AC_DEFINE_UNQUOTED([HAVE_STRTOF_L], [$HAVE_STRTOF_L],
+    [Define to 1 if the system has the 'strtof_l' function.])
+])
+
 dnl Prerequisites of lib/c-strtold.c.
 AC_DEFUN([gl_C_STRTOLD],
 [
diff --git a/modules/c-strtof b/modules/c-strtof
new file mode 100644 (file)
index 0000000..e058293
--- /dev/null
@@ -0,0 +1,31 @@
+Description:
+Convert string to float in C locale.
+
+Files:
+lib/c-strtod.h
+lib/c-strtof.c
+lib/c-strtod.c
+m4/c-strtod.m4
+m4/intl-thread-locale.m4
+
+Depends-on:
+c99
+extensions
+locale
+strdup-posix
+strtof
+
+configure.ac:
+gl_C_STRTOF
+
+Makefile.am:
+lib_SOURCES += c-strtof.c
+
+Include:
+"c-strtod.h"
+
+License:
+GPL
+
+Maintainer:
+Paul Eggert, Jim Meyering