]> Savannah Git Hosting - gnulib.git/commitdiff
mbschr, mbsrchr: support GNULIB_MCEL_PREFER
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 21 Sep 2023 19:46:52 +0000 (12:46 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 21 Sep 2023 22:03:45 +0000 (15:03 -0700)
Support mcel API for apps that prefer it.
* lib/mbschr.c, lib/mbsrchr.c: Include stdlib.h, for MB_CUR_MAX.
[GNULIB_MCEL_PREFER]: Include mcel.h instead of mbuiterf.h.
(mbschr, mbsrchr) [GNULIB_MCEL_PREFER]: Use mcel API.

ChangeLog
lib/mbschr.c
lib/mbsrchr.c

index 160332c1160c72f623a546c87ca3737226c5d0a3..4430828d3b34ecb8ecf63f3ec3ff88325722bc20 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2023-09-21  Paul Eggert  <eggert@cs.ucla.edu>
 
+       mbschr, mbsrchr: support GNULIB_MCEL_PREFER
+       Support mcel API for apps that prefer it.
+       The following changes are in effect only if GNULIB_MCEL_PREFER.
+       * lib/mbschr.c, lib/mbsrchr.c: Include stdlib.h, for MB_CUR_MAX.
+       [GNULIB_MCEL_PREFER]: Include mcel.h instead of mbuiterf.h.
+       (mbschr, mbsrchr) [GNULIB_MCEL_PREFER]: Use mcel API.
+
        gnulib-common: don’t suppress -Wpedantic
        Problem reported by Pádraig Brady in:
        https://lists.gnu.org/r/bug-gnulib/2023-09/msg00130.html
index 330e3f1c75c66f820e1abae78a946983e949a7fd..a78a75a02864628c06e8a99043f8522eb96aafc1 100644 (file)
 /* Specification.  */
 #include <string.h>
 
-#include "mbuiterf.h"
+#include <stdlib.h>
+
+#if GNULIB_MCEL_PREFER
+# include "mcel.h"
+#else
+# include "mbuiterf.h"
+#endif
 
 /* Locate the first single-byte character C in the character string STRING,
    and return a pointer to it.  Return NULL if C is not found in STRING.  */
@@ -33,6 +39,15 @@ mbschr (const char *string, int c)
          the faster unibyte loop can be used.  */
       && (unsigned char) c >= 0x30)
     {
+#if GNULIB_MCEL_PREFER
+      while (*string)
+        {
+          mcel_t g = mcel_scanz (string);
+          if (g.len == 1 && (unsigned char) *string == (unsigned char) c)
+            return (char *) string;
+          string += g.len;
+        }
+#else
       mbuif_state_t state;
       const char *iter;
       for (mbuif_init (state), iter = string;; )
@@ -46,6 +61,7 @@ mbschr (const char *string, int c)
         }
       return (char *) iter;
      notfound:
+#endif
       return NULL;
     }
   else
index 4c4767462c2da73d278880afd08907d2b70e19e3..626d3b7cbff1dc2900ddb6308fc1c35452a5dbc0 100644 (file)
 /* Specification.  */
 #include <string.h>
 
-#include "mbuiterf.h"
+#include <stdlib.h>
+
+#if GNULIB_MCEL_PREFER
+# include "mcel.h"
+#else
+# include "mbuiterf.h"
+#endif
 
 /* Locate the last single-byte character C in the character string STRING,
    and return a pointer to it.  Return NULL if C is not found in STRING.  */
@@ -35,6 +41,15 @@ mbsrchr (const char *string, int c)
     {
       const char *result = NULL;
 
+#if GNULIB_MCEL_PREFER
+      while (*string)
+        {
+          mcel_t g = mcel_scanz (string);
+          if (g.len == 1 && (unsigned char) *string == (unsigned char) c)
+            result = string;
+          string += g.len;
+        }
+#else
       mbuif_state_t state;
       const char *iter;
       for (mbuif_init (state), iter = string; mbuif_avail (state, iter); )
@@ -44,6 +59,7 @@ mbsrchr (const char *string, int c)
             result = iter;
           iter += mb_len (cur);
         }
+#endif
 
       return (char *) result;
     }