]> Savannah Git Hosting - gnulib.git/commitdiff
trim: support GNULIB_MCEL_PREFER
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 Sep 2023 00:22:19 +0000 (17:22 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 10 Sep 2023 01:11:44 +0000 (18:11 -0700)
Support mcel API for apps that prefer it.
The following changes are in effect only if GNULIB_MCEL_PREFER.
* lib/trim.c: Include mcel.h, not mbchar.h and mbuiterf.h.
(trim2): Use mcel API.
* modules/trim (Depends-on): Add c32isspace.

ChangeLog
lib/trim.c
modules/trim

index a97a5081b0ebf5c10e4e56d7917b8b60375a2728..2b0569af8bdf1523bb0bff411d3f2b9f0a382673 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2023-09-09  Paul Eggert  <eggert@cs.ucla.edu>
 
+       trim: support GNULIB_MCEL_PREFER
+       Support mcel API for apps that prefer it.
+       The following changes are in effect only if GNULIB_MCEL_PREFER.
+       * lib/trim.c: Include mcel.h, not mbchar.h and mbuiterf.h.
+       (trim2): Use mcel API.
+       * modules/trim (Depends-on): Add c32isspace.
+
        mbsstr: support GNULIB_MCEL_PREFER
        Support mcel API for apps that prefer it.
        * lib/mbsstr.c [GNULIB_MCEL_PREFER]: Include mcel.h not mbuiter.h.
index 8ebbc296580db698f9f32e60b338fa4f8035b957..bad19c13b64f63a8fe0670ced5efc579e373be79 100644 (file)
 #include <stddef.h>
 #include <stdlib.h>
 
-#include "mbchar.h"
-#include "mbuiterf.h"
+#if GNULIB_MCEL_PREFER
+# include "mcel.h"
+#else
+# include "mbchar.h"
+# include "mbuiterf.h"
+#endif
 #include "xalloc.h"
 
 char *
@@ -38,6 +42,26 @@ trim2 (const char *s, int how)
 
   if (MB_CUR_MAX > 1)
     {
+#if GNULIB_MCEL_PREFER
+      /* Skip leading whitespace. */
+      if (how != TRIM_TRAILING)
+        for (mcel_t g; *start; start += g.len)
+          {
+            g = mcel_scanz (start);
+            if (!c32isspace (g.ch))
+              break;
+          }
+
+      /* Find start of any trailing whitespace.  */
+      if (how != TRIM_LEADING)
+        for (const char *p = end = start; *p; )
+          {
+            mcel_t g = mcel_scanz (p);
+            p += g.len;
+            if (!c32isspace (g.ch))
+              end = p;
+          }
+#else
       mbuif_state_t state;
       mbuif_init (state);
 
@@ -60,6 +84,7 @@ trim2 (const char *s, int how)
             if (!mb_isspace (cur))
               end = p;
           }
+#endif
     }
   else
     {
index 9f45dade80edef748e9c23f8bad5c0387be78b3a..f20d8ad84eabbde37f25210dee92a7288d618ad0 100644 (file)
@@ -6,6 +6,7 @@ lib/trim.h
 lib/trim.c
 
 Depends-on:
+c32isspace
 mbchar
 mbuiterf
 mempcpy