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     |  7 +++++++
 lib/mbschr.c  | 18 +++++++++++++++++-
 lib/mbsrchr.c | 18 +++++++++++++++++-
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 160332c116..4430828d3b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2023-09-21  Paul Eggert  <egg...@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
diff --git a/lib/mbschr.c b/lib/mbschr.c
index 330e3f1c75..a78a75a028 100644
--- a/lib/mbschr.c
+++ b/lib/mbschr.c
@@ -20,7 +20,13 @@
 /* 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
diff --git a/lib/mbsrchr.c b/lib/mbsrchr.c
index 4c4767462c..626d3b7cbf 100644
--- a/lib/mbsrchr.c
+++ b/lib/mbsrchr.c
@@ -20,7 +20,13 @@
 /* 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;
     }
-- 
2.39.2


Reply via email to