This patch adds a module 'c-strtof', similar to 'c-strtod' and 'c-strtold'.
GNU poke needs this.


2024-02-21  Bruno Haible  <br...@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.

From 9f2ae68c82b7177ee83e5c2e8fc81fefcfb92546 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Thu, 22 Feb 2024 01:52:05 +0100
Subject: [PATCH] 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.
---
 ChangeLog        |  9 +++++++++
 lib/c-strtod.c   | 10 ++++++++--
 lib/c-strtod.h   | 12 ++++++++++--
 lib/c-strtof.c   | 19 +++++++++++++++++++
 m4/c-strtod.m4   | 37 ++++++++++++++++++++++++++++++++++++-
 modules/c-strtof | 31 +++++++++++++++++++++++++++++++
 6 files changed, 113 insertions(+), 5 deletions(-)
 create mode 100644 lib/c-strtof.c
 create mode 100644 modules/c-strtof

diff --git a/ChangeLog b/ChangeLog
index 4d03615791..1ab1246616 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-02-21  Bruno Haible  <br...@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  <br...@clisp.org>
 
 	c-strtold: Use strtold_l when available (regr. 2019-01-31).
diff --git a/lib/c-strtod.c b/lib/c-strtod.c
index f955434eaa..308b6d4c84 100644
--- a/lib/c-strtod.c
+++ b/lib/c-strtod.c
@@ -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.
 
@@ -26,7 +26,13 @@
 #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
diff --git a/lib/c-strtod.h b/lib/c-strtod.h
index 7576e5fb05..c26908077b 100644
--- a/lib/c-strtod.h
+++ b/lib/c-strtod.h
@@ -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
index 0000000000..dcb817336d
--- /dev/null
+++ b/lib/c-strtof.c
@@ -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"
diff --git a/m4/c-strtod.m4 b/m4/c-strtod.m4
index d1c4547948..102b3f58c5 100644
--- a/m4/c-strtod.m4
+++ b/m4/c-strtod.m4
@@ -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
index 0000000000..e058293725
--- /dev/null
+++ b/modules/c-strtof
@@ -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
-- 
2.34.1

Reply via email to