mbsstr, mbscasestr, mbspcasecmp: Use const-improved function macros.
authorBruno Haible <bruno@clisp.org>
Sun, 9 Feb 2025 07:18:18 +0000 (08:18 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 9 Feb 2025 07:18:18 +0000 (08:18 +0100)
* lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): Define as macros
that cast the result to 'const char *' when the first argument is a
'const char *'.
* lib/mbsstr.c: Define _GL_NO_CONST_GENERICS.
* lib/mbscasestr.c: Likewise.
* lib/mbspcasecmp.c: Likewise.

ChangeLog
lib/mbscasestr.c
lib/mbspcasecmp.c
lib/mbsstr.c
lib/string.in.h

index 73350f9f99cd87313cf50143d25df1e91fbb5e7d..be4e2425d33f78c3b89a40345e643abbc140c683 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2025-02-09  Bruno Haible  <bruno@clisp.org>
+
+       mbsstr, mbscasestr, mbspcasecmp: Use const-improved function macros.
+       * lib/string.in.h (mbsstr, mbspcasecmp, mbscasestr): Define as macros
+       that cast the result to 'const char *' when the first argument is a
+       'const char *'.
+       * lib/mbsstr.c: Define _GL_NO_CONST_GENERICS.
+       * lib/mbscasestr.c: Likewise.
+       * lib/mbspcasecmp.c: Likewise.
+
 2025-02-09  Bruno Haible  <bruno@clisp.org>
 
        stdckdint-h tests: Fix compilation error (regression 2025-02-07).
index 7a186e0f988e24d528326d7b2cd2edd1d7d3ad3c..843229666fb715ecaf649a2167aef1f7fd4efe29 100644 (file)
@@ -15,6 +15,9 @@
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
+/* Don't use the const-improved function macros in this compilation unit.  */
+#define _GL_NO_CONST_GENERICS
+
 #include <config.h>
 
 /* Specification.  */
index f57337f01e85054f613a338db03b5c84ff4708cc..0717cd893a04d1c87c5fd38bdd6483b85ff008e2 100644 (file)
@@ -15,6 +15,9 @@
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
+/* Don't use the const-improved function macros in this compilation unit.  */
+#define _GL_NO_CONST_GENERICS
+
 #include <config.h>
 
 /* Specification.  */
index 579bcd054de8bdf1558bc24974c826eb82cb873d..8e8eb73d55f67a98ae851c60a55d3fb14a6a744c 100644 (file)
@@ -15,6 +15,9 @@
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
+/* Don't use the const-improved function macros in this compilation unit.  */
+#define _GL_NO_CONST_GENERICS
+
 #include <config.h>
 
 /* Specification.  */
index ce488299006f30b9323601e5d2241c6399344f2c..6705967c59c3af93d8d7ceeeb7043fc018c4aac5 100644 (file)
@@ -1178,6 +1178,19 @@ _GL_CXXALIASWARN (mbsrchr);
 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
      _GL_ATTRIBUTE_PURE
      _GL_ARG_NONNULL ((1, 2));
+/* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
+   compiler warnings for 'const' related mistakes.  */
+# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus)
+#  if __STDC_VERSION__ >= 202311
+#   define mbsstr(h,n) (typeof(h)) mbsstr ((h), (n))
+#  elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
+         || defined __ICC  || defined __TINYC__)
+#   define mbsstr(h,n) \
+      _Generic ((h), \
+                char const *: (char const *) mbsstr ((h), (n)), \
+                default     :                mbsstr ((h), (n)))
+#  endif
+# endif
 #endif
 
 #if @GNULIB_MBSCASECMP@
@@ -1219,6 +1232,19 @@ _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
      _GL_ATTRIBUTE_PURE
      _GL_ARG_NONNULL ((1, 2));
+/* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
+   compiler warnings for 'const' related mistakes.  */
+# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus)
+#  if __STDC_VERSION__ >= 202311
+#   define mbspcasecmp(s,p) (typeof(s)) mbspcasecmp ((s), (p))
+#  elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
+         || defined __ICC  || defined __TINYC__)
+#   define mbspcasecmp(s,p) \
+      _Generic ((s), \
+                char const *: (char const *) mbspcasecmp ((s), (p)), \
+                default     :                mbspcasecmp ((s), (p)))
+#  endif
+# endif
 #endif
 
 #if @GNULIB_MBSCASESTR@
@@ -1230,6 +1256,19 @@ _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
      _GL_ATTRIBUTE_PURE
      _GL_ARG_NONNULL ((1, 2));
+/* Don't silently convert a 'const char *' to a 'char *'.  Programmers want
+   compiler warnings for 'const' related mistakes.  */
+# if !(defined _GL_NO_CONST_GENERICS || defined __cplusplus)
+#  if __STDC_VERSION__ >= 202311
+#   define mbscasestr(h,n) (typeof(h)) mbscasestr ((h), (n))
+#  elif ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
+         || defined __ICC  || defined __TINYC__)
+#   define mbscasestr(h,n) \
+      _Generic ((h), \
+                char const *: (char const *) mbscasestr ((h), (n)), \
+                default     :                mbscasestr ((h), (n)))
+#  endif
+# endif
 #endif
 
 #if @GNULIB_MBSCSPN@